-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (41 loc) · 1.3 KB
/
Copy pathapp.js
File metadata and controls
50 lines (41 loc) · 1.3 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
'use strict'
const cluster = require('cluster'),
numCPUs = require('os').cpus().length;
let express = require('express'),
config = require('./config'),
cache = require('memory-cache'),
bankScraper = require('./bank-scraper/scraper');
if(cluster.isMaster){
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (var i = numCPUs - 1; i >= 0; i--) {
cluster.fork();
}
cluster.on('exit', (code, signal) => {
console.log(`worker ${process.pid} died with signal/code ${signal || code}, forking...`);
cluster.fork();
});
// Run only once, no need to scrap in parallel clusters
bankScraper(config).then(() => {
console.log('currency rates updated.');
});
} else {
// update cache per worker whenever master fetches new currency rates
process.on('message', (msg) => {
console.log('Updating worker with currency results from master...');
if(msg.byCurrency && msg.byBank){
cache.put('by-currency', msg.byCurrency);
cache.put('by-bank', msg.byBank);
} else{
console.log(`Error updating worker, msg: ${msg}`);
}
});
let app = express();
let port = process.env.PORT || 3000;
let currencyRouter = require('./routes/currencyRouter')();
app.use('/api', currencyRouter);
// run server
app.listen(port, function(){
console.log(`Worker ${process.pid} started on port: ${port}`);
});
}