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
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.
Step 1 - Create your cron job code
- Create a
.js
file to put your cron job code into. In this example, amain.js
file is created in acloud_code
directory. - Define a job function using
Parse.Cloud.job
. In this example, the following code verifies every user in yourParse Dashboard
, then query the ones that still have their email unverified after some time and destroy them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Parse.Cloud.job("removeInvalidLogin", async (request) => {
let date = new Date();
let timeNow = date.getTime();
let intervalOfTime = 3*60*1000; // the time set is 3 minutes in milliseconds
let timeThen = timeNow - intervalOfTime;
// Limit date
let queryDate = new Date();
queryDate.setTime(timeThen);
// The query object
let query = new Parse.Query(Parse.User);
// Query the users that still unverified after 3 minutes
query.equalTo("emailVerified", false);
query.lessThanOrEqualTo("createdAt", queryDate);
const results = await query.find({useMasterKey:true});
results.forEach(object => {
object.destroy({useMasterKey: true}).then(destroyed => {
console.log("Successfully destroyed object" + JSON.stringify(destroyed));
}).catch(error => {
console.log("Error: " + error.code + " - " + error.message);
})
});
return ("Successfully retrieved " + results.length + " invalid logins.");
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Parse.Cloud.job("removeInvalidLogin", function (request, response) {
var date = new Date();
var timeNow = date.getTime();
var intervalOfTime = 3*60*1000; // the time set is 3 minutes in milliseconds
var timeThen = timeNow - intervalOfTime;
// Limit date
var queryDate = new Date();
queryDate.setTime(timeThen);
// The query object
var query = new Parse.Query(Parse.User);
// Query the users that still unverified after 3 minutes
query.equalTo("emailVerified", false);
query.lessThanOrEqualTo("createdAt", queryDate);
query.find({
success: function (results) {
console.log("Successfully retrieved " + results.length + " invalid logins.");
// Destroying the invalid users
query.each(function (object, err) {
object.destroy({
success: function (object) {
response.success("Successfully destroyed object " + object.objectId);
},
error: function (error) {
response.error("Error: " + error.code + " - " + error.message);
},
useMasterKey: true // VERY IMPORTANT!!
})
})
},
error: function (error) {
response.error("Error: " + error.code + " - " + error.message);
}
});
});
It is required to use the master key in this operation.
You can modify the
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 theintervalOfTime
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 wantintervalOfTime
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
file with the unwantedintervalOfTime
content and follow Step 2 to upload the file with the correctintervalOfTime
content.
Step 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 Back4App website and click on
Dashboard
. - Find the
Cloud Code
and click onFunctions & Web Hosting
. It looks like this: - Upload or create a new file (you can also edit the current
main.js
file directly on the browser). Then, click atDeploy
as shown here:
Step 3 - Schedule cron job on Back4App
- Go to your app at Back4App website and click on
Server Settings
. - Find the “Background Jobs” block and click on
SETTINGS
. The “Background Jobs” block looks like this: - A Background Jobs page will appear and two options will be displayed:
Browser a job
orSchedule a job
. Click onSchedule a job
, as shown below:If you want to
Edit
,Run now
orDelete
an existing cron job, click on theBrowser a job
button. - A Schedule a job page will appear and you have to fill in the
Description
field of your job with its description and also the fieldCloud 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 isremoveInvalidLogin
. - 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
button.
Step 4 - Test your app
-
Create some users with
emailVerified
column set asFalse
at yourParse Dashboard
, as shown below: -
Run your application and refresh your
Parse Dashboard
. It should have destroyed the unverified users. For theParse Dashboard
showed 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 Back4App website and click on
Server Settings
. - Find the “Logs” block and click on
SETTINGS
. The “Logs” block looks like this:x
- Scrool the page until you see the
Server System Log
. There you should find information about the cron job that is running, as shown below:
- Go to your app at Back4App website and click on
It’s done!
At this stage, you are able to schedule cron jobs in your application using Parse Server Core features through Back4App!