Back4App

How to create your free cron job online

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:

Step 1 - Create your cron job code

  1. Create a .js file to put your cron job code into. In this example, a main.js file is created in a cloud_code directory.
  2. Define a job function using Parse.Cloud.job. In this example, the following code verifies every user in your Parse Dashboard, then query the ones that still have their email unverified after some time and destroy them:
    main.js
    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, status) {
      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 the 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 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 unwanted intervalOfTime content and follow Step 2 to upload the file with the correct intervalOfTime 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.

  1. Go to your app at Back4App website and click on Server Settings.
  2. Find the “Cloud Code” block and click on SETTINGS. The “Could Code” block looks like this:

    Cloud Code block

  3. Click on Choose Files and select the .js file you created at Step 1 then click SAVE, as shown here:

    Cloud Code Settings

Step 3 - Schedule cron job on Back4App

  1. Go to your app at Back4App website and click on Server Settings.
  2. Find the “Background Jobs” block and click on SETTINGS. The “Background Jobs” block looks like this:

    Background Jobs block

  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:

    Schedule a Job

    If you want to Edit, Run now or Delete an existing cron job, click on the Browser a job button.

  4. 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 field 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.

    Schedule Background Job settings

  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 button.

    More Schedule Background Job settings

Step 4 - Test your app

  1. Create some users with emailVerified column set as False at your Parse Dashboard, as shown below:

    Users Dashboard

  2. Run your application and refresh your Parse Dashboard. It should have destroyed the unverified users. For the Parse Dashboard showed above, this is the result:

    Users Dashboard Cleaned

    You can also see if the cron job is working by accessing Back4App Logs. To do that, follow these steps:

    1. Go to your app at Back4App website and click on Server Settings.
    2. Find the “Logs” block and click on SETTINGS. The “Logs” block looks like this:

      Logs block

    3. 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:

      Logs messages

It’s done!

At this stage, you are able to schedule cron jobs in your application using Parse Server Core features through Back4App!