Platform
Cron Job
13 min
how to create your parse cron job introduction this section explains how you can schedule a cron job using parse server core features through back4app for this tutorial, as an example, you will build a cron job that removes users of your parse dashboard parse dashboard that haven’t verified their emails some time after they have signed up at any time, you can access the complete project built for this tutorial at our github repository prerequisites to complete this tutorial, you need an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app knowledge about cloud code note follow the cloud code for android tutorial or the cloud code for ios tutorial for more information a device (or virtual device ) running android 4 0 (ice cream sandwich)or newer 1 create your cron job code create a js js file to put your cron job code into in this example, a main js main js file is created in a cloud code cloud code directory define a job function using parse cloud job parse cloud job in this example, the following code verifies every user in your parse dashboard parse dashboard , then query the ones that still have their email unverified after some time and destroy them parse server 3 x main js 1 parse cloud job("removeinvalidlogin", async (request) => { 2 let date = new date(); 3 let timenow = date gettime(); 4 let intervaloftime = 3 60 1000; // the time set is 3 minutes in milliseconds 5 let timethen = timenow intervaloftime; 6 7 // limit date 8 let querydate = new date(); 9 querydate settime(timethen); 10 11 // the query object 12 let query = new parse query(parse user); 13 14 // query the users that still unverified after 3 minutes 15 query equalto("emailverified", false); 16 query lessthanorequalto("createdat", querydate); 17 18 const results = await query find({usemasterkey\ true}); 19 20 results foreach(object => { 21 object destroy({usemasterkey true}) then(destroyed => { 22 console log("successfully destroyed object" + json stringify(destroyed)); 23 }) catch(error => { 24 console log("error " + error code + " " + error message); 25 }) 26 }); 27 28 return ("successfully retrieved " + results length + " invalid logins "); 29 }); parse server 2 x main js 1 parse cloud job("removeinvalidlogin", function (request, response) { 2 var date = new date(); 3 var timenow = date gettime(); 4 var intervaloftime = 3 60 1000; // the time set is 3 minutes in milliseconds 5 var timethen = timenow intervaloftime; 6 7 // limit date 8 var querydate = new date(); 9 querydate settime(timethen); 10 11 // the query object 12 var query = new parse query(parse user); 13 14 // query the users that still unverified after 3 minutes 15 query equalto("emailverified", false); 16 query lessthanorequalto("createdat", querydate); 17 18 query find({ 19 success function (results) { 20 console log("successfully retrieved " + results length + " invalid logins "); 21 22 // destroying the invalid users 23 query each(function (object, err) { 24 object destroy({ 25 success function (object) { 26 response success("successfully destroyed object " + object objectid); 27 }, 28 error function (error) { 29 response error("error " + error code + " " + error message); 30 }, 31 usemasterkey true // very important!! 32 }) 33 }) 34 }, 35 error function (error) { 36 response error("error " + error code + " " + error message); 37 } 38 }); 39 }); it is required to use the master key in this operation you can modify the intervaloftime intervaloftime content with the amount of time you think an unverified user can still have his account active without verifying it just don’t forget that to test your application, small time intervals are better so, it’s suggested that you set the intervaloftime intervaloftime content to three minutes to test if the cron job is working and then change the javascript code with the amount of time you actually want intervaloftime intervaloftime to be don’t forget that changes to the javascript file are only computed in your application if you upload the file again on back4app cloud code block to do this, delete the js js file with the unwanted intervaloftime intervaloftime content and follow step 2 to upload the file with the correct intervaloftime intervaloftime content 2 upload cron job to cloud code to know more about how to get started with cloud code look at cloud code for android tutorial or cloud code for ios tutorial go to your app at the back4app website and click on dashboard dashboard find the cloud code cloud code and click on functions & web hosting functions & web hosting it looks like this 3\ upload or create a new file (you can also edit the current main js main js file directly on the browser) then, click on deploy deploy as shown here 3 schedule cron job on back4app go to your app at the back4app website and click on server settings server settings find the “background jobs” block and click on settings settings the “background jobs” block looks like this 3\ a background jobs page will appear and two options will be displayed browser a job or schedule a job click on schedule a job, as shown below if you want to edit edit , run now run now , or delete delete an existing cron job, click on the browser a job browser a job button 4\ a schedule a job page will appear and you have to fill in the description description field of your job with its description and also the field cloud job cloud job with the name you set to your cron job in the first line of its javascript code in this example, the name of the cron job created is removeinvalidlogin removeinvalidlogin 5\ you can also set other options for your cron job as what time it should start running, if it should repeat, and how often after filling in these options with your preferences, click on the save save button 4 test your app create some users with emailverified emailverified column set as false false at your parse dashboard parse dashboard , as shown below 2\ run your application and refresh your parse dashboard parse dashboard it should have destroyed the unverified users for the parse dashboard parse dashboard shown above, this is the result you can also see if the cron job is working by accessing back4app logs to do that, follow these steps go to your app at the back4app website and click on server settings server settings find the “logs” block and click on settings settings the “logs” block looks like this 3 scroll the page until you see the server system log server system log there you should find information about the cron job that is running, as shown below it’s done! at this stage, you can schedule cron jobs in your application using parse server core features through back4app!