-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmain.js
More file actions
147 lines (120 loc) · 3.06 KB
/
main.js
File metadata and controls
147 lines (120 loc) · 3.06 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import Promise from 'bluebird';
import express from 'express';
import morgan from 'morgan';
import db from './db';
import config from './config';
import log from './lib/log';
let taskUpdating = {};
export default function () {
handleExit();
return serve().then(() => {
return runTasks();
}).catch((error) => {
if (error) {
log.err(`Error starting application!`);
log.err(error);
process.exit();
}
});
}
let intervalSet = false;
function runTasks () {
return new Promise((resolve) => {
let tasks = Object.keys(config.tasks);
log.info(`Running tasks: ${tasks.join(',')}`);
return Promise.mapSeries(tasks, (name) => {
taskUpdating[name] = false;
return runTask(name);
}).catch((error) => {
log.err(`Error initializing tasks`);
log.err(error);
}).then(() => {
if (!intervalSet) {
intervalSet = true;
let interval = 6 * (10 * 1e4);
// we want to space out the start of each sync cycle by 10 minutes each
setInterval(() => {
runTasks().then(() => {
log.info(`Libraries synced!`);
});
}, interval);
}
resolve();
});
});
}
function runTask (name) {
return new Promise((resolve) => {
if (!taskUpdating[name]) {
taskUpdating[name] = true;
log.info(`Running task ${name}...`);
try {
require(`./tasks/${name}`)(db).then(() => {
log.info(`Task ${name} complete`);
taskUpdating[name] = false;
resolve();
log.info(`Purging cache`);
require('./lib/purge')(config.maxcdn)().then(() => {
log.info(`Cache purged`);
}).catch((error) => {
log.err(`Error purging cache`);
log.err(error);
});
}).catch((error) => {
log.err(`Error running task ${name}`);
log.err(error);
resolve();
});
} catch (error) {
log.err(`Couldn't run task ${name}`);
log.err(error);
resolve();
}
} else {
log.info(`Task ${name} is already running`);
resolve();
}
});
}
function serve () {
return new Promise((resolve) => {
let app = express();
let port = config.port;
app.use(morgan('dev'));
app.set('json spaces', 2);
// setup CORS
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
next();
});
// v1 routes
app.use('/v1', require('./routes/v1'));
// catch all
app.all('*', function (req, res) {
res.status(404).jsonp({ status: 404, message: `Requested url ${req.url} not found.` });
});
app.listen(port, function () {
log.info(`Node (version: ${process.version}) ${process.argv[ 1 ]} started on ${port} ...`);
resolve(app);
});
});
}
function handleExit () {
process.on('exit', terminator);
[ 'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS',
'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGTERM'
].forEach((element) => {
process.on(element, () => {
terminator(element);
});
});
}
function terminator (sig) {
// close loki db
db.db.close();
if (typeof sig === 'string') {
log.info(`${sig}: Received ${new Date()} - terminating Node server`);
process.exit(1);
}
log.info(`${new Date()}: Node server stopped`);
}