1
1
import { initialize } from "./cluster.js"
2
2
import { getMongoConnection , getPostgresConnection } from './db.js'
3
3
import cliProgress from 'cli-progress'
4
- import { setTimeout } from 'node:timers/promises'
5
4
const mongoDB = await getMongoConnection ( )
6
5
const 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
9
8
const 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
10
10
11
11
// console.log(`there was ${await postgresDB.students.count()} items on Postgres, deleting all...`)
12
12
await postgresDB . students . deleteAll ( )
13
13
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
-
25
14
const total = await mongoDB . students . countDocuments ( )
26
15
// console.log(`total items on DB: ${total}`)
27
16
@@ -37,25 +26,29 @@ const cp = initialize(
37
26
backgroundTaskFile : TASK_FILE ,
38
27
clusterSize : CLUSTER_SIZE ,
39
28
amountToBeProcessed : total ,
40
- async onMessage ( message ) {
41
- progress . increment ( )
29
+ async onMessage ( cumulativeProcessed ) {
30
+ totalProcessed += cumulativeProcessed ;
31
+ progress . update ( totalProcessed ) ;
42
32
43
- if ( ++ totalProcessed !== total ) return
33
+ if ( totalProcessed !== total ) return
44
34
// console.log(`all ${amountToBeProcessed} processed! Exiting...`)
45
35
progress . stop ( )
46
36
cp . killAll ( )
47
37
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' } ` )
51
41
process . exit ( )
52
-
53
42
}
54
43
}
55
44
)
56
- await setTimeout ( 1000 )
57
-
58
- for await ( const data of getAllPagedData ( ITEMS_PER_PAGE ) ) {
59
- cp . sendToChild ( data )
60
- }
61
45
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