cronjob every 10 seconds
This commit is contained in:
parent
562704641a
commit
3a1f54eb8e
@ -7,6 +7,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.21.0",
|
"axios": "^0.21.0",
|
||||||
|
"cron": "^1.8.2",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"pg": "^8.5.1",
|
"pg": "^8.5.1",
|
||||||
|
37
src/index.js
37
src/index.js
@ -1,6 +1,7 @@
|
|||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
var process = require('process')
|
var process = require('process')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
var CronJob = require('cron').CronJob;
|
||||||
const etherscan = require('./etherscan');
|
const etherscan = require('./etherscan');
|
||||||
const AppError = require('./error');
|
const AppError = require('./error');
|
||||||
const prom = require('./prometheus');
|
const prom = require('./prometheus');
|
||||||
@ -23,9 +24,7 @@ const startServer = () => {
|
|||||||
server.listen(serverPort, serverHost, () => console.log(`Http server running on port ${serverHost}:${serverPort}`));
|
server.listen(serverPort, serverHost, () => console.log(`Http server running on port ${serverHost}:${serverPort}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = async () => {
|
const run = async () => {
|
||||||
startServer();
|
|
||||||
|
|
||||||
const statediffUser = process.env.STATEDIFF_PG_USER;
|
const statediffUser = process.env.STATEDIFF_PG_USER;
|
||||||
const statediffPassword = process.env.STATEDIFF_PG_PASSWORD;
|
const statediffPassword = process.env.STATEDIFF_PG_PASSWORD;
|
||||||
const statediffDB = process.env.STATEDIFF_PG_DATABASE;
|
const statediffDB = process.env.STATEDIFF_PG_DATABASE;
|
||||||
@ -43,11 +42,8 @@ const main = async () => {
|
|||||||
dbStateDiffBlockNumber,
|
dbStateDiffBlockNumber,
|
||||||
|
|
||||||
])
|
])
|
||||||
// console.log(results);
|
|
||||||
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
// console.log(result);
|
|
||||||
|
|
||||||
if (result.status === 'rejected') {
|
if (result.status === 'rejected') {
|
||||||
if (result.reason instanceof AppError) {
|
if (result.reason instanceof AppError) {
|
||||||
const errorData = result.reason.data;
|
const errorData = result.reason.data;
|
||||||
@ -71,6 +67,35 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
startServer();
|
||||||
|
|
||||||
|
let lock = false;
|
||||||
|
|
||||||
|
var job = new CronJob(
|
||||||
|
'*/10 * * * * *',
|
||||||
|
async () => {
|
||||||
|
if (lock) {
|
||||||
|
console.log('Parallel process is executing. Skipping');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lock = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await run();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock = false;
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
main().catch((e) => console.error(e));
|
main().catch((e) => console.error(e));
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
process.on('SIGINT', () => {
|
||||||
|
19
yarn.lock
19
yarn.lock
@ -75,6 +75,13 @@ cookie@0.4.0:
|
|||||||
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
|
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
|
||||||
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
|
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
|
||||||
|
|
||||||
|
cron@^1.8.2:
|
||||||
|
version "1.8.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/cron/-/cron-1.8.2.tgz#4ac5e3c55ba8c163d84f3407bde94632da8370ce"
|
||||||
|
integrity sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg==
|
||||||
|
dependencies:
|
||||||
|
moment-timezone "^0.5.x"
|
||||||
|
|
||||||
debug@2.6.9:
|
debug@2.6.9:
|
||||||
version "2.6.9"
|
version "2.6.9"
|
||||||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
||||||
@ -257,6 +264,18 @@ mime@1.6.0:
|
|||||||
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
|
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
|
||||||
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
|
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
|
||||||
|
|
||||||
|
moment-timezone@^0.5.x:
|
||||||
|
version "0.5.32"
|
||||||
|
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.32.tgz#db7677cc3cc680fd30303ebd90b0da1ca0dfecc2"
|
||||||
|
integrity sha512-Z8QNyuQHQAmWucp8Knmgei8YNo28aLjJq6Ma+jy1ZSpSk5nyfRT8xgUbSQvD2+2UajISfenndwvFuH3NGS+nvA==
|
||||||
|
dependencies:
|
||||||
|
moment ">= 2.9.0"
|
||||||
|
|
||||||
|
"moment@>= 2.9.0":
|
||||||
|
version "2.29.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
|
||||||
|
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
|
||||||
|
|
||||||
ms@2.0.0:
|
ms@2.0.0:
|
||||||
version "2.0.0"
|
version "2.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
|
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
|
||||||
|
Loading…
Reference in New Issue
Block a user