-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (48 loc) · 1.89 KB
/
index.js
File metadata and controls
56 lines (48 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { initialize } from "./cluster.js"
import { getMongoConnection, getPostgresConnection } from './db.js'
import cliProgress from 'cli-progress'
import os from 'os';
const mongoDB = await getMongoConnection()
const postgresDB = await getPostgresConnection()
// const ITEMS_PER_PAGE = 4000
const CLUSTER_SIZE = os.cpus().length
const TASK_FILE = new URL('./background-task.js', import.meta.url).pathname
const DATA_STREAMING_FILE = new URL('./data-streaming.js', import.meta.url).pathname
// console.log(`there was ${await postgresDB.students.count()} items on Postgres, deleting all...`)
await postgresDB.students.deleteAll()
const total = await mongoDB.students.countDocuments()
// console.log(`total items on DB: ${total}`)
const progress = new cliProgress.SingleBar({
format: 'progress [{bar}] {percentage}% | {value}/{total} | {duration}s',
clearOnComplete: false,
}, cliProgress.Presets.shades_classic);
progress.start(total, 0);
let totalProcessed = 0
const cp = initialize(
{
backgroundTaskFile: TASK_FILE,
clusterSize: CLUSTER_SIZE,
amountToBeProcessed: total,
async onMessage(cumulativeProcessed) {
totalProcessed += cumulativeProcessed;
progress.update(totalProcessed);
if (totalProcessed !== total) return
// console.log(`all ${amountToBeProcessed} processed! Exiting...`)
progress.stop()
cp.killAll()
const insertedOnSQLPostGres = await postgresDB.students.count()
console.log(`total on MongoDB ${total} and total on PostGres ${insertedOnSQLPostGres}`)
console.log(`are the same? ${total === insertedOnSQLPostGres ? 'yes' : 'no'}`)
process.exit()
}
}
)
initialize(
{
backgroundTaskFile: DATA_STREAMING_FILE,
clusterSize: 1,
async onMessage(message) {
cp.sendToChild(message)
}
}
)