Skip to content

Commit

Permalink
cronjob every 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
ramilexe committed Dec 4, 2020
1 parent 5627046 commit 3a1f54e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"private": true,
"dependencies": {
"axios": "^0.21.0",
"cron": "^1.8.2",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pg": "^8.5.1",
Expand Down
37 changes: 31 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config()
var process = require('process')
const express = require('express')
var CronJob = require('cron').CronJob;
const etherscan = require('./etherscan');
const AppError = require('./error');
const prom = require('./prometheus');
Expand All @@ -23,9 +24,7 @@ const startServer = () => {
server.listen(serverPort, serverHost, () => console.log(`Http server running on port ${serverHost}:${serverPort}`));
}

const main = async () => {
startServer();

const run = async () => {
const statediffUser = process.env.STATEDIFF_PG_USER;
const statediffPassword = process.env.STATEDIFF_PG_PASSWORD;
const statediffDB = process.env.STATEDIFF_PG_DATABASE;
Expand All @@ -43,11 +42,8 @@ const main = async () => {
dbStateDiffBlockNumber,

])
// console.log(results);

for (const result of results) {
// console.log(result);

if (result.status === 'rejected') {
if (result.reason instanceof AppError) {
const errorData = result.reason.data;
Expand All @@ -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));

process.on('SIGINT', () => {
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
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"

[email protected]:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -257,6 +264,18 @@ [email protected]:
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
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==

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit 3a1f54e

Please sign in to comment.