From 3a1f54eb8ea0e88c026fbd5b6dbafc93198af847 Mon Sep 17 00:00:00 2001 From: ramil Date: Fri, 4 Dec 2020 23:16:35 +0300 Subject: [PATCH] cronjob every 10 seconds --- package.json | 1 + src/index.js | 37 +++++++++++++++++++++++++++++++------ yarn.lock | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 4c4006d..87a0e2c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 6bf829a..58b9363 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); @@ -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; @@ -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; @@ -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', () => { diff --git a/yarn.lock b/yarn.lock index a19aa38..ca8f68f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -75,6 +75,13 @@ cookie@0.4.0: 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" + debug@2.6.9: version "2.6.9" 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" 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: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"