Velo: Scheduling Recurring Jobs

Visit the Velo by Wix website to onboard and continue learning.

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.  

Notes

  • Scheduled Jobs run with Admin permissions.
  • Scheduled Jobs will run at least one time.
  • Free sites can add up to 3 scheduled jobs that run at a minimum of 1 hour intervals. If you need to add more jobs or run them more frequently, you can upgrade your site to a premium plan. Learn more about premium plans.

Configuring Jobs

You configure what jobs to run and when to run them in the jobs.config file in the backend. 

To create a jobs.config file:

  1. Go to the Public & Backend section of the Code sidebar (Wix Studio), or the Velo sidebar (Wix Editor).
  2. Hover over Backend, click the plus icon , and then click Add scheduled jobs.

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.

To simplify the process of building your jobs.config file, you can use this third-party Jobs Config tool. It allows you to enter the details of your jobs and builds the jobs object for you. The tool also includes a validator that can check your existing jobs.config file for errors.

Note: 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

Copy
1
{
2
"jobs": [
3
{
4
"functionLocation": "/module/filename.js",
5
"functionName": "funcName",
6
"description": "describe your job",
7
"executionConfig": {
8
"cronExpression": "0 8 * * *"
9
}
10
}
11
]
12
}

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.

Copy
1
{
2
"jobs": [
3
{
4
"functionLocation": "/module/filename.js",
5
"functionName": "funcName",
6
"description": "describe your job",
7
"executionConfig": {
8
"time": "08:00",
9
"dayOfWeek": "Monday",
10
"dateInMonth": 1
11
}
12
}
13
]
14
}

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

Copy
1
{
2
"jobs": [
3
{
4
"functionLocation": "/module/filename.js",
5
"functionName": "funcName1",
6
"description": "describe your job1",
7
"executionConfig": {
8
"cronExpression": "0 8 * * *"
9
}
10
},
11
{
12
"functionLocation": "/module/filename.js",
13
"functionName": "funcName2",
14
"description": "describe your job2",
15
"executionConfig": {
16
"cronExpression": "0 8 * * *"
17
}
18
},
19
{
20
"functionLocation": "/module/filename.js",
21
"functionName": "funcName3",
22
"description": "describe your job3",
23
"executionConfig": {
24
"cronExpression": "0 8 * * *"
25
}
26
}
27
]
28
}

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

Copy
1
"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.

To calculate the UTC equivalent of your local time, find your location's UTC time offset. Then either add or subtract to your local time based on the offset.

For example, the Eastern Time Zone has an offset of -5, meaning it's 5 hours behind UTC time. To convert Eastern Time to UTC, add 5 hours.

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.

Was this helpful?
Yes
No