mysql - Nested Cron jobs in Nodejs -
i'm having 'tournament' sql table contains start_time , end_time tournaments. have table has playerid , tournamentids can tell players playes in tournament. i'm trying run cron task check tournament table , see if tournament has ended can check players results external api. problem external api has rate limit , have send requestes every 1.5 sec.
what tried write cron job every 10 seconds check tournament table (i couldn't come anyother solution rather keep checking db):
cron.job("*/10 * * * * *", function(){ result = query tournament table endtime=<now && endtime+10second>=now if(result not empty) { cron.job("*/1.5 * * * * *",function(){ send api requests userid parse & store result in db }); } }); i don't feel right , seems buggy me. because inner cron job might take longer 10 seconds. there solution this. i'm using expressjs & mysql.
the problem facing can solved event emitters. there useful module node-schedule in npm can in scenario telling. have is schedule job fire @ deadline of project, job hit 3rd party api , check results.you schedule job this
var schedule = require('node-schedule'); schedule.schedulejob("jobname", "timetofire", function () { //put api hit here. //finally remove schedule if ("jobname" in schedule.scheduledjobs) { schedule.scheduledjobs["jobname"].cancel(); delete schedule.scheduledjobs["jobname"]; } }) make sure store jobs scheduled in database server crash invalidate schedules have scheduled , have reschedule them again.
Comments
Post a Comment