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 https //www back4app com/product/parse server through back4app for this tutorial, as an example, you will build a cron job that removes users of your \<font color="#2166ae">parse dashboard\</font> 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 https //github com/back4app/android background jobs prerequisites to complete this tutorial, you need an app created on back4app note follow the new parse app tutorial https //www back4app com/docs/get started/new parse app to learn how to create a parse app on back4app knowledge about cloud code note follow the cloud code for android tutorial https //www back4app com/docs/android/parse cloud code or the cloud code for ios tutorial https //www back4app com/docs for more information a device (or virtual device https //developer android com/studio/run/managing avds?hl=pt br ) running android 4 0 (ice cream sandwich)or newer 1 create your cron job code create a \<font color="#2166ae"> js\</font> file to put your cron job code into in this example, a \<font color="#2166ae">main js\</font> file is created in a \<font color="#2166ae">cloud code\</font> directory define a job function using \<font color="#2166ae">parse cloud job\</font> in this example, the following code verifies every user in your \<font color="#2166ae">parse dashboard\</font> , 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 \<font color="#2166ae"> intervaloftime\</font> 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 \<font color="#2166ae"> intervaloftime\</font> 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 \<font color="#2166ae">intervaloftime\</font> 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 \<font color="#2166ae"> js\</font> file with the unwanted \<font color="#2166ae">intervaloftime\</font> content and follow step 2 to upload the file with the correct \<font color="#2166ae">intervaloftime\</font> 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 https //www back4app com/docs/android/parse cloud code or cloud code for ios tutorial https //www back4app com/docs go to your app at the back4app https //www back4app com/ website and click on \<font color="#2166ae">dashboard\</font> find the \<font color="#2166ae">cloud code\</font> and click on \<font color="#2166ae">functions \& web hosting\</font> it looks like this 3\ upload or create a new file (you can also edit the current \<font color="#2166ae">main js\</font> file directly on the browser) then, click on \<font color="#2166ae">deploy\</font> as shown here 3 schedule cron job on back4app go to your app at the back4app website https //www back4app com/ and click on \<font color="#2166ae">server settings\</font> find the “background jobs” block and click on \<font color="#2166ae">settings\</font> 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 \<font color="#2166ae">edit\</font> , \<font color="#2166ae">run now\</font> , or \<font color="#2166ae">delete\</font> an existing cron job, click on the \<font color="#2166ae">browser a job\</font> button 4\ a schedule a job page will appear and you have to fill in the \<font color="#2166ae">description\</font> field of your job with its description and also the field \<font color="#2166ae">cloud job\</font> 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 \<font color="#2166ae">removeinvalidlogin\</font> 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 \<font color="#2166ae">save\</font> button 4 test your app create some users with \<font color="#2166ae">emailverified\</font> column set as \<font color="#2166ae">false\</font> at your \<font color="#2166ae">parse dashboard\</font> , as shown below 2\ run your application and refresh your \<font color="#2166ae">parse dashboard\</font> it should have destroyed the unverified users for the \<font color="#2166ae">parse dashboard\</font> 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 https //www back4app com/ and click on \<font color="#2166ae">server settings\</font> find the “logs” block and click on \<font color="#2166ae">settings\</font> the “logs” block looks like this 3 scroll the page until you see the \<font color="#2166ae">server system log\</font> 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!