Skip to content

Commit 4d44dfb

Browse files
committed
refactoring
Signed-off-by: Erick Wendel <erick.workspace@gmail.com>
1 parent 910315a commit 4d44dfb

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

src/background-task.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ process.on('message', (items) => {
88
for (const item of items) {
99
db.students.insert(item)
1010
.then(() => {
11-
process.send({ status: 'done' });
11+
process.send('item-done');
1212
})
1313
.catch((error) => {
1414
console.error(error);

src/cluster.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function initializeCluster({ backgroundTaskFile, clusterSize, onMessage }) {
2525
process.exit(1)
2626
})
2727

28-
child.on('message', onMessage)
28+
child.on('message', (message) => {
29+
if (message !== 'item-done') return
30+
onMessage(message)
31+
})
2932

3033
processes.set(child.pid, child)
3134
}
@@ -39,21 +42,9 @@ function initializeCluster({ backgroundTaskFile, clusterSize, onMessage }) {
3942

4043
}
4144

42-
export function initialize({ backgroundTaskFile, clusterSize, amountToBeProcessed, onDone, onMessage }) {
43-
let totalProcessed = 0
44-
const _onMessage = (message) => {
45-
onMessage(message)
46-
47-
// ++totalProcessed
48-
// if (totalProcessed === amountToBeProcessed) {
49-
// // console.log(`all ${amountToBeProcessed} processed! Exiting...`)
50-
// onDone(totalProcessed)
51-
// // killAll()
52-
// }
53-
}
54-
45+
export function initialize({ backgroundTaskFile, clusterSize, onMessage }) {
5546

56-
const { getProcess, killAll } = initializeCluster({ backgroundTaskFile, clusterSize, onMessage: _onMessage })
47+
const { getProcess, killAll } = initializeCluster({ backgroundTaskFile, clusterSize, onMessage })
5748
// console.log(`starting with ${clusterSize} processes`)
5849

5950
function sendToChild(message) {

src/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ const cp = initialize(
4040
async onMessage(message) {
4141
progress.increment()
4242

43-
++totalProcessed
44-
45-
if (totalProcessed === total) {
46-
// console.log(`all ${amountToBeProcessed} processed! Exiting...`)
47-
progress.stop()
48-
cp.killAll()
49-
50-
const insertedOnSQLite = await postgresDB.students.count()
51-
console.log(`total on MongoDB ${total} and total on PostGres ${insertedOnSQLite}`)
52-
console.log(`are the same? ${total === insertedOnSQLite ? 'yes' : 'no'}`)
53-
process.exit()
54-
}
43+
if (++totalProcessed !== total) return
44+
// console.log(`all ${amountToBeProcessed} processed! Exiting...`)
45+
progress.stop()
46+
cp.killAll()
47+
48+
const insertedOnSQLite = await postgresDB.students.count()
49+
console.log(`total on MongoDB ${total} and total on PostGres ${insertedOnSQLite}`)
50+
console.log(`are the same? ${total === insertedOnSQLite ? 'yes' : 'no'}`)
51+
process.exit()
52+
5553
}
5654
}
5755
)

src/seed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
22
import { getMongoConnection, getPostgresConnection } from './db.js'
33

44
async function seedMongoDB(amount) {
5-
const { students, client } = getMongoConnection()
5+
const { students, client } = await getMongoConnection()
66
console.log('deleting all students')
77
await students.deleteMany({})
88
let person = []
@@ -31,5 +31,5 @@ async function seedPostegres() {
3131
console.log('table students created with success')
3232
await db.client.end()
3333
}
34-
// await seedMongoDB(1_000_000)
34+
await seedMongoDB(1_000_000)
3535
await seedPostegres()

0 commit comments

Comments
 (0)