Background scripts
Apstrata allows you to create your back-end logic using JavaScript scripts running on Apstrata's servers and having native access to all Apstrata's APIs. You can use these scripts to create your own APIs or you can use them to schedule the execution of a script or to execute recurrent jobs, by saving scheduling or cron information into an Apstrata document, notably passing the name of the server-side script to execute and the date/time at which this script has to be triggered. This information is used by Apstrata to create scheduled scripts or cron jobs.
Back to the map Next station: define transactions
Example: daily draw
To generate more traction to your game, let us assume that you decide to run a daily draw: one of your registered players is randomly selected and the winner receives a gift. We will create a script - called "scheduleDraw" in this example - which you use to schedule a daily draw or to cancel it (in order to cancel the draw, you will have to pass the "cancel" parameter set to "true"). We also create another script that implements the actual draw. This latter is regularly executed on a daily basis.
var schedulingDocumentKey = "dailyDrawDocument"; // to cancel the scheduled draw, invoke the current script by passing the "cancel" parameter set to "true" if (request.parameters["cancel"] && request.parameters["cancel"] == "true") { return cancelDraw(); }else { return startDraw(); } function startDraw() { var params = { "apsdb.store": "DefaultStore", "apsST_scriptName": "dailyDraw", // this is the name of the script to trigger "apsST_cronSpec": "0 0 12 * * ?", // The dailyDraw script is triggered every day at noon "apsST_saveResult": "true", // if true, save the result of the execution in the cron document "apsdb.documentKey": schedulingDocumentKey // this scheduling document we are creating will have this key }; // To create a cron job, you need to save a document into your store // passing the required scheduling information (above) var response = this.apsdb.callApi("SaveDocument", params, null); return response.result.document; } function cancelDraw() { // To cancel a scheduled or cron script, you just need to set the // apsST_scriptName field of the scheduling document to an empty value var params = { "apsdb.store": "DefaultStore", "apsST_scriptName": "", "apsdb.documentKey": schedulingDocumentKey } var response = this.apsdb.callApi("SaveDocument", params, null); return response.result.document; }
Dig deeper
Related tutorials