Back
Scheduled Execution in Express Using Cron Jobs
Cron is a Linux command line utility that schedules jobs for the computer to execute. Let's discuss how to implement it in your next project.
What is Cron?
Cron
is a Linux command line utility that schedules jobs for the computer to execute. It allows it to schedule repetitive tasks, or tasks that the computer should run at a specific time or date.
Use Cases for Cron
There are several cases where a cron job is useful. A simple instance is a reminder or alarm. In this case, developers can facilitate setting up reminders or alarms by using cron to specify when the computer should send a reminder or trigger an alarm. In an express server, a cron job can be implemented in the following cases:
Triggering an email notification for a schedule.
Checking on the database periodically.
Sending out monthly newsletters.
Node-Cron
The node-cron
is a Node.js library that allows you to set up cron jobs in a Node.js server. In this article, we’ll use node-cron to demonstrate how to schedule jobs on an express server.
First, we’ll examine how node-cron
works. To do this, you’ll need to add the package to your application by running the command below:
To get started with using node-cron, use the code below:
In the code block above, we have the cron package assigned to a variable cron. Then, we call the schedule
method as seen on line 3. The schedule
method accepts two required parameters and an optional configuration parameter:
The first parameter is the expression for the schedule in a string format (”* * * * * *”
), where each asterisk represents a field. The first asterisk is the placeholder for seconds; this is also not required. The second placeholder is for minutes, and the following are for hours, day of the month, month, and day of the week. You can indicate the day of the week using names or 0-7. Multiple values can be separated by a comma in a placeholder, e.g., “2,3,4 * * * *”
. This means the server will execute the job scheduled at every second, third, and fourth minute. You can also use steps by having a forward slash after the asterisk and the value, e.g., “*/2 * * * * *”
. This will execute the job every 2 minutes. The second parameter passed to the schedule
method is the function to be executed.
Lastly, we have the third optional parameter. This is a configuration object with keys:
schedule
: This is a boolean type. The library will schedule immediately if the value is truthy. Otherwise, the library will schedule when thestart
method on the schedule executes. We can also halt the schedule by calling the stop method.timezone
: This is a string value for the timezone in which you are scheduling the expression.name
: This is the name of the schedule. The package will autogenerate a name if you do not provide a name.recoverMissedExecutions
: This is a boolean value. It allows you to specify if the package should catch missed executions instead of skipping them.
Setting up an Express Application
Now, we’ll set up an express server and see how we can use the node-cron package; you can find the project repository here. The first step is to create an express server. To do this, we’ll create an "express-cron" folder and initialize a node application with this command:
Next, we’ll add all required packages with the command:
The nodemon package from the above command will automatically restart the server when file changes are detected. Once we’ve added the required packages, we’ll add scripts to package.json
.
The next step will be to set up our express server:
We import the express package in the code block above and assign it to the variable app
. On line 4, we have the home route for the server; this will send a response with the "welcome to cron server" message to the browser when we hit that route. Then, we’ll set the server to listen to the port set up in the environment variable or 4400. Finally, to start the application in development mode, we’ll run the following command:
Using Node-cron in an Express App
To use the node-cron in an express app, we’ll assign the package to the variable cron
. Then, as discussed in the section above, we must call the schedule method and pass in the parameters.
From the code block above, we have cron jobs scheduled in the server on lines 10, 14 and 18. The job created on line 18 starts on line 32.
The node-cron package has other methods:
validate
: The validate method allows you to check if an expression for a schedule is a valid node-cron expression.getTasks
: This returns a map with the schedule name as key and the schedule details as value.
Conclusion and Resources
This article covered implementing a cron job in an express server using the node-cron
package. We reviewed the methods on the package, their functionalities, and how we can use them. You can check the official documentation for more extensive information on how to use the node-cron
package.