Recurring scheduled tasks?
-
I have a scheduled job that I set up in the Job Queue and it runs as expected, but is there a way to schedule it so that it runs weekly? Or tie it to a tournament so that it runs when the tournament starts?
-
Here is an example of scheduling a weekly cloud code execution,
"use strict"; function main() { var response = {} bridge.logDebugJson("Script Inputs", data) const scriptName = data.scriptName const interval = data.args.interval const searchSpan = interval < 60 ? 60 : 60 * 24 // schedule checking and re-schedule itself for the next week at 23:59 on the same day var scriptProxy = bridge.getScriptServiceProxy() var dateTimeSpanMinsFromNowInMillis = new Date().getTime() + (searchSpan * 60 * 1000) var result = scriptProxy.getScheduledCloudScripts(dateTimeSpanMinsFromNowInMillis) var nowTime = new Date().getTime() if ((result.status == 200) && (result.data !== null)) { for (var i = 0; i < result.data.scheduledJobs.length; i++) { if (result.data.scheduledJobs[i].scriptName === scriptName && result.data.scheduledJobs[i].scheduledStartTime > nowTime) { scriptProxy.cancelScheduledScript(result.data.scheduledJobs[i].jobId) } } } const targetHour = 23 const targetMinute = 59 const minutesDifference = getMinutesDifference(targetHour, targetMinute) let minutesFromNow = minutesDifference + interval bridge.logInfo(`minutesFromNow between the script calling time and the time of 23:59 on a same day${minutesFromNow}`) response.scheduleJob = scriptProxy.scheduleRunScriptMinutes(scriptName, data, minutesFromNow) // the code for your script return response } function getMinutesDifference(targetHour, targetMinute) { const now = new Date() const targetTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), targetHour, targetMinute) const differenceInMilliseconds = targetTime - now bridge.logInfo(`differenceInMilliseconds between the script calling time and the target time: ${differenceInMilliseconds}`) const differenceInMinutes = Math.floor(differenceInMilliseconds / (1000 * 60)) return differenceInMinutes } main()
The parameters for this script will be like below, the
interval
will be10080
minutes (a week),{ "scriptName": "thisScriptNameItself", "args": { "interval": 10080 } }
-
Thanks. So I create the script and it just runs? Do I need to do any other configuration or set up?
-
Thanks for the example, this might prove helpful for me too. But I do feel the API is lacking the ability to hook a script to the start and end of tournaments. It'd be nice to be able to extend the functionality of what happens when a tournament ends and rewards are paid out for example.