forked from satishjoshi95/DDKOIN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.js
146 lines (133 loc) · 3.93 KB
/
jobs.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
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
let utils = require('./utils');
let sql = require('./sql/accounts.js');
let path = require('path');
let library = {};
exports.attachScope = function (scope) {
library = scope;
};
/**
* @desc update elasticsearch server data every one minute.
* @desc get data from DDK_test database.
* @param {Table} blocks
* @param {Table} dapps
* @param {Table} delegates
* @param {Table} mem_accounts
* @param {Table} migrations
* @param {Table} rounds_fees
* @param {Table} trs
* @param {Table} signatures
* @param {Table} stake_orders
* @param {Table} peers
* @param {Table} peers_dapp
* @param {Table} intransfer
* @param {Table} outtransfer
* @param {Table} multisignatures
*/
exports.updateDataOnElasticSearch = {
on: '* * * * *',
job: function () {
let dbTables = [
'blocks_list',
'dapps',
'delegates',
'mem_accounts',
'migrations',
'rounds_fees',
'trs',
'votes',
'signatures',
'stake_orders',
'peers',
'peers_dapp',
'intransfer',
'outtransfer',
'multisignatures'
];
dbTables.forEach(function (tableName) {
library.db.query('SELECT * FROM ' + tableName)
.then(function (rows) {
if (rows.length > 0) {
let bulk = utils.makeBulk(rows, tableName);
utils.indexall(bulk, tableName)
.then(function (result) {
//FIXME: Do further processing on successful indexing on elasticsearch server
})
.catch(function (err) {
library.logger.error('elasticsearch error :'+ err);
});
}
})
.catch(function (err) {
library.logger.error('database error : '+ err);
});
});
},
spawn: false
};
/**
* @desc archive log files every first day of a month
*/
exports.archiveLogFiles = {
on: '0 0 1 * *',
job: function () {
const today = new Date();
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
if (today.getMonth() !== yesterday.getMonth()) {
library.logger.archive('start executing archiving files');
let createZip = require('./create-zip');
let year = today.getFullYear();
let month = today.toLocaleString('en-us', { month: 'long' });
let dir = path.join(__dirname + '/archive/' + year + '/' + month);
createZip.createDir(dir, function (err) {
if (!err) {
createZip.archiveLogFiles(dir, function (err) {
if (!err) {
library.logger.archive('files are archived');
} else {
library.logger.archive('archive error : ' + err);
}
});
} else {
library.logger.archive('directory creation error : ' + err);
}
});
}
},
spawn: false
};
/**
* @desc checks for pending users and unlocks them everyday at midnight
*/
exports.unlockLockedUsers = {
on: '00 00 00 * * *',
job: function () {
library.logger.info('Checking any pending users(contributors, founders etc...) which needs to be unlocked and unlock them at midnight every day');
library.cache.client.keys('*userTimeHash_*', function (err, userKeys) {
userKeys.forEach(function (key) {
library.modules.cache.hgetall(key, function (err, data) {
let lastBlock = library.modules.blocks.lastBlock.get();
if (data.endTime < lastBlock.timestamp) {
library.modules.cache.getJsonForKey('minedContributorsBalance', function (err, contributorsBalance) {
let totalContributorsBal = parseInt(data.transferedAmount) + parseInt(contributorsBalance);
library.modules.cache.setJsonForKey('minedContributorsBalance', totalContributorsBal);
});
library.db.none(sql.enableAccount, {
senderId: data.address
})
.then(function () {
library.logger.info(data.address + ' account is unlocked');
library.cache.client.del('userInfo_' + data.address);
library.cache.client.del('userTimeHash_' + data.endTime);
})
.catch(function (err) {
library.logger.error(err.stack);
});
}
});
});
});
},
spawn: false
};
/*************************************** END OF FILE *************************************/