paint-brush
How To Run Recurring Jobs using Velo by Wixby@velo
1,238 reads
1,238 reads

How To Run Recurring Jobs using Velo by Wix

by Velo by WixMay 25th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Velo is a full-stack development platform that empowers you to rapidly build, manage and deploy professional web apps. The Job Scheduler allows you to schedule code to run at specified intervals. The code your job runs can be any backend function in any backend.js or jsw file. Each scheduled job object contains the "functionLocation", "functionName", "description" and "executionConfig" properties that are detailed below. The jobs.config file contains a. JSON object which defines the scheduled jobs.
featured image - How To Run Recurring Jobs using Velo by Wix
Velo by Wix HackerNoon profile picture

The Job Scheduler allows you to schedule code to run at specified intervals. You schedule code to run by creating a job.

Each job defines what code to run and when to run it. The code your job runs can be any backend function. You can schedule that code to run on an hourly, daily, weekly, or monthly basis.

For example, you might create jobs that:

  • Import data from an external source once a day.
  • Delete collection data that is no longer relevant once a week.
  • Send a status report to relevant parties once a month.  

Configuring Jobs

You configure what jobs to run and when to run them in the jobs.config file found in your site's Velo Sidebar, in the Code Files, Backend section.

To create a jobs.config file:

  1. Hover over the Code Files, Backend section in the Velo Sidebar.
  2. Click the add icon.
  3. Select Job Scheduler

The jobs.config file contains a JSON object which defines the scheduled jobs. The object consists of the properties listed below.

Note: You must publish your site to save changes to your jobs configuration.

jobs (Array)

The jobs object contains one top-level key named "jobs". The key maps to an array of objects, each of which represents a scheduled job. Each scheduled job object contains the "functionLocation", "functionName", "description" and "executionConfig" properties that are detailed below.

Note: You can configure up to 20 jobs. The first 2 options below are for 1 job only. The 3rd option describes what a jobs file looks like with multiple jobs.

Option 1 - Using a CRON expression

{
  "jobs": [
    {
      "functionLocation": "/module/filename.js(w)", 
      "functionName": "funcName",
      "description": "describe your job",
      "executionConfig": {
        "cronExpression": "0 8 * * *"
      }
    }
  ]
}

Option 2 - Setting the time, day of week, and day of month

Important: Both lines 9 and 10 provide optional interval controls for your job. If you don't want that level of control you can delete either or both from your code. If you delete line 10 make sure to also delete the comma at the end of line 9.
{
  "jobs": [
    {
      "functionLocation": "/module/filename.js(w)",
      "functionName": "funcName",
      "description": "describe your job",
      "executionConfig": {
        "time": "08:00",
        "dayOfWeek": "Monday",
        "dateInMonth": 1 
      }
    }
  ]
}

jobs.config with more than 1 job

Note: The following is an example of what the jobs.config file looks like with more than 1 job (3, in this case), using a CRON expression. It is meant to display the structure of the array of objects with more than one object. As noted above, you can schedule up to 20 jobs, and to do that you would add more objects following the pattern described here. Note that there is a comma between each object (lines 10 and 18) but that the final object does not have a comma afterwards (line 26).
{
  "jobs": [
    {
      "functionLocation": "/module/filename.js(w)", 
      "functionName": "funcName1",
      "description": "describe your job1",
      "executionConfig": {
        "cronExpression": "0 8 * * *"
      }
    },
    {
      "functionLocation": "/module/filename.js(w)", 
      "functionName": "funcName2",
      "description": "describe your job2",
      "executionConfig": {
        "cronExpression": "0 8 * * *"
      }
    },
    {
      "functionLocation": "/module/filename.js(w)", 
      "functionName": "funcName3",
      "description": "describe your job3",
      "executionConfig": {
        "cronExpression": "0 8 * * *"
      }
    }
  ]
}

functionLocation (String)

The location of the file of the backend function that contains the function you want to run at the scheduled time. The function can be any function in any backend .js or .jsw file. The function location is a relative path within the Backend folder.

For example, if you want to run the deleteExpired() function as shown below:

Set the "

functionLocation
" value to "
/utils/dbUtils.js
".

functionName (String)

Name of the function you want to run at the scheduled time.

Important: Make sure you export the function that you want the job scheduler to call.

For example, if you want to run the deleteExpired() function as shown below:

Set the "

functionName
" value to "
deleteExpired
".

description (String) - optional

A description of the job.

executionConfig (Object)

An object containing information about when the job should run. You can define when the job runs using a cron expression or by specifying a time of day and optionally a day of the week or date in the month.

Important:

  • Jobs occuring more than once a day must be defined using a cron expression.
  • If both specifications exist for a single job, only the cron expression will be used.

Cron Expression

When using a cron expression to specify when a job runs, the "

executionConfig
" object contains a single property, named "
cronExpression
", whose value is a valid cron expression.

cronExpression (String)

A valid cron expression. For example, to run a job every day at 8:00 in the morning, use:

"cronExpression": "0 8 * * *"
Important: When using a cron expression, you can schedule your job to run at intervals as short as one hour apart, but not shorter. If you define your job to run more frequently, the job will be ignored.

Time, Day, and Date

Jobs defined using the "

time
", "
dayOfWeek
", and "
dateInMonth
" properties can run on a daily, weekly, or monthly basis. In all cases using this option, the "
executionConfig
" object must contain a "
time
" property which defines the time of day the job will run.

  • For daily jobs, the "
    executionConfig
    " object only contains the "
    time
    " property.
  • For weekly jobs, the "
    executionConfig
    " object contains the "
    time
    " property and a "
    dayOfWeek
    " property that defines on what day of the week the job runs.
  • For monthly jobs, the "
    executionConfig
    " object contains the "
    time
    " property and a "
    dateInMonth
    " property that defines on what day of the month the job runs.

time (String)

The time of day the job runs. The time is specified as UTC time in 

HH:MM
format.

Note: The job will run within 5 minutes after the specified time.

dayOfWeek (String) - optional

For weekly jobs, add the 

dayOfWeek
 property to the 
executionConfig
object. The 
dayOfWeek
 property defines what day of the week the job runs on.

For daily or monthly jobs, omit the 

dayOfWeek
 property.

The value of the dayOfWeek property is one of: "

Sunday
", "
Monday
", "
Tuesday
", "
Wednesday
", "
Thursday
", "
Friday
", or "
Saturday
".

dateInMonth (Number) - optional

For monthly jobs, add the 

dateInMonth
 property to the
executionConfig
object. The 
dateInMonth
 property defines what day of the month the job runs on.

For daily or weekly jobs, omit the dayOfWeek property.

The value of the 

dateInMonth
 property is a number between 
1
 and 
31
.

Previously published at https://support.wix.com/en/article/velo-scheduling-recurring-jobs