Child pages
  • Schedule jobs
Skip to end of metadata
Go to start of metadata

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.

 

How to schedule the daily execution of a script
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;
}
 Click here to see the full code of both scripts (Scheduling and draw)
Code to execute a script on a daily basis
<script>
	<scriptACL>
		<execute>authenticated</execute>
		<read>nobody</read>
		<write>nobody</write>
	</scriptACL>
	<code>
		<![CDATA[
			
			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 false, 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;
			}
			
		]]>
	</code>
</script>
The "dailyDraw" script that implements the actual draw
<script>
	<scriptACL>
		<execute>nobody</execute>
		<read>nobody</read>
		<write>nobody</write>
	</scriptACL>
	<code>
		<![CDATA[
			
			// Invoke the Apstrata ListUsers API that returns all the users of your Apstrata application
			// In the parameters we pass to the API, we specify that we need to obtain the user's 
			// login and email
			var params = {
			    "apsdb.attributes": "login, email"
			};
			
			var response = apsdb.callApi("ListUsers", params, null);
			
			// Randomly pick one of the users form the list
			var users = response.result.users;
			var winner = Math.floor(Math.random() * users.length);
			var email = users[winner].email;
			
			// Send an email to the winner
		    var email = {
			        
                "apsma.to": email,
                "apsma.subject": "Winner of the day!",
                "apsma.body": "Congratulations! You've just won a prize!"
			}
			
			return apsdb.callApi("SendEmail", email, null);
		]]>
	</code>
</script>

Dig deeper

Related tutorials