11import { initialize } from "./cluster.js"
22import { getMongoConnection , getPostgresConnection } from './db.js'
33import cliProgress from 'cli-progress'
4- import { setTimeout } from 'node:timers/promises'
54const mongoDB = await getMongoConnection ( )
65const postgresDB = await getPostgresConnection ( )
7- const ITEMS_PER_PAGE = 4000
8- const CLUSTER_SIZE = 99
6+ // const ITEMS_PER_PAGE = 4000
7+ const CLUSTER_SIZE = 8
98const TASK_FILE = new URL ( './background-task.js' , import . meta. url ) . pathname
9+ const DATA_STREAMING_FILE = new URL ( './data-streaming.js' , import . meta. url ) . pathname
1010
1111// console.log(`there was ${await postgresDB.students.count()} items on Postgres, deleting all...`)
1212await postgresDB . students . deleteAll ( )
1313
14- async function * getAllPagedData ( itemsPerPage , page = 0 ) {
15-
16- const data = mongoDB . students . find ( ) . skip ( page ) . limit ( itemsPerPage )
17- const items = await data . toArray ( )
18- if ( ! items . length ) return
19-
20- yield items
21-
22- yield * getAllPagedData ( itemsPerPage , page += itemsPerPage )
23- }
24-
2514const total = await mongoDB . students . countDocuments ( )
2615// console.log(`total items on DB: ${total}`)
2716
@@ -37,25 +26,29 @@ const cp = initialize(
3726 backgroundTaskFile : TASK_FILE ,
3827 clusterSize : CLUSTER_SIZE ,
3928 amountToBeProcessed : total ,
40- async onMessage ( message ) {
41- progress . increment ( )
29+ async onMessage ( cumulativeProcessed ) {
30+ totalProcessed += cumulativeProcessed ;
31+ progress . update ( totalProcessed ) ;
4232
43- if ( ++ totalProcessed !== total ) return
33+ if ( totalProcessed !== total ) return
4434 // console.log(`all ${amountToBeProcessed} processed! Exiting...`)
4535 progress . stop ( )
4636 cp . killAll ( )
4737
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' } ` )
38+ const insertedOnSQLPostGres = await postgresDB . students . count ( )
39+ console . log ( `total on MongoDB ${ total } and total on PostGres ${ insertedOnSQLPostGres } ` )
40+ console . log ( `are the same? ${ total === insertedOnSQLPostGres ? 'yes' : 'no' } ` )
5141 process . exit ( )
52-
5342 }
5443 }
5544)
56- await setTimeout ( 1000 )
57-
58- for await ( const data of getAllPagedData ( ITEMS_PER_PAGE ) ) {
59- cp . sendToChild ( data )
60- }
6145
46+ initialize (
47+ {
48+ backgroundTaskFile : DATA_STREAMING_FILE ,
49+ clusterSize : 1 ,
50+ async onMessage ( message ) {
51+ cp . sendToChild ( message )
52+ }
53+ }
54+ )
0 commit comments