-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
62 lines (49 loc) · 1.54 KB
/
index.js
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
;(async () => {
const __ = require('./utils/utils');
require(__.path('utils/prototypes'))
const _ = require('lodash');
const cron = require('node-cron');
const moment = require('moment');
moment.locale('ru');
const fs = require('fs').promises;
/* Config from env */
const config = {
DEBUG: process.env.DEBUG || false,
MONGO: process.env.MONGO || 'mongodb://localhost/js_cdn'
};
/* Logger */
const log = require(__.path('utils/log'))({
prefix: '#CRON |',
level: config.DEBUG ? 'debug' : 'info',
});
log.info('START CONST', config);
/* Initial modules pool */
const initModules = { __, _, log, moment, config, cron };
/* Minio server */
const Minio = require('minio');
function getMinio({endPoint, accessKey, secretKey, port = false, useSSL = true}) {
return new Minio.Client({endPoint, accessKey, secretKey, port, useSSL})
}
/* Mongo */
const mongo = await require(__.path('granny-server-backend/src/mongo/_load'))(initModules);
let dataFile = __.path('data/stats.json')
let initStats = await getState(dataFile)
_.assign(initModules, { mongo, getMinio, initStats });
/* Script modules */
let stats = await require(__.path('scripts/_load'))(initModules);
setInterval(async () => {
await saveState(dataFile, stats)
}, 10000)
async function getState(dataFile) {
try {
let data = await fs.readFile(dataFile)
return JSON.parse(data)
} catch(ex) {
console.log('getState.ex', ex.message)
return {}
}
}
async function saveState(dataFile, state) {
await fs.writeFile(dataFile, JSON.stringify(state))
}
})()