# Task Scheduling
ExpressWebJs task scheduler helps you manage scheduled tasks on your server. It allows you to define your command schedule within your application. When using the scheduler, only a single cron entry is needed on your server. Your task schedule is defined in the App/Console/Kernel.ts file’s schedule method.
# Define Schedules
The schedule method in the App/Console/Kernel.ts class. Let’s take a look at the closure scheduler example which makes use of scheduler call method. Within the closure, we will just console.log a string.
”use strict” import scheduler from "expressweb-scheduler-ts"; class Kernel{ /** * The Maker commands provided by your application. * * @var array */ static commands () { return[ // ] }; /** * Define the application's command schedule. * * @return void */ static schedule() { scheduler.call(()=> { console.log(‘running a closure scheduler’) }).daily().run(); } }
Copied!
# Scheduling Maker Commands
In addition to scheduling closures, you may also schedule Maker commands. You can do this by using the scheduler command method to schedule a Maker command using the command’s name(signature) while adding the namespace in the commands method
”use strict” import scheduler from "expressweb-scheduler-ts"; class Kernel{ /** * The Maker commands provided by your application. * * @var array */ static commands () { return ["App/Console/Commands/SendMail_command.js"]; }; /** * Define the application's command schedule. * * @return void */ static schedule() { scheduler.command(‘sendMail’).daily().run(); } }
Copied!
# Schedule Frequency Options
We've taken time to review few ways on how you may configure a task to run at specified intervals. However, there are many more task schedule frequencies that you may assign to a task: Check below for fields and values for cron methods and other available methods.
Allowed fields in cron method
# ┌────────────── second (optional) # │ ┌──────────── minute # │ │ ┌────────── hour # │ │ │ ┌──────── day of month # │ │ │ │ ┌────── month # │ │ │ │ │ ┌──── day of week # │ │ │ │ │ │ # │ │ │ │ │ │ # * * * * * *
Copied!
Field | Value |
---|---|
->second | 0-59 |
->minute | 0-59 |
->hour | 0-23 |
->day of month | 1-12 (or names) |
->month | 1-12 (or names) |
->day of week | 0-7 (or names, 0 or 7 are sunday) |
Method | Description |
---|---|
->cron('* * * * *'); | Run the task on a custom cron schedule |
->everyMinute(); | Run the task every minute |
->everyTwoMinutes(); | Run the task every two minutes |
->everyThreeMinutes(); | Run the task every three minutes |
->everyFourMinutes(); | Run the task every four minutes |
->everyFiveMinutes(); | Run the task every five minutes |
->everyTenMinutes(); | Run the task every ten minutes |
->everyFifteenMinutes(); | Run the task every fifteen minutes |
->everyThirtyMinutes(); | Run the task every thirty minutes |
->hourly(); | Run the task every hour |
->hourlyAt(17); | Run the task every hour at 17 minutes past the hour |
->everyTwoHours(); | Run the task every two hours |
->everyThreeHours(); | Run the task every three hours |
->everyFourHours(); | Run the task every four hours |
->everySixHours(); | Run the task every six hours |
->daily(); | Run the task every day at midnight |
->dailyAt('13:00'); | Run the task every day at 13:00 |
->twiceDaily(1, 13); | Run the task daily at 1:00 & 13:00 |
->weekly(); | Run the task every Sunday at 00:00 |
->weeklyOn(1, '8:00'); | Run the task every week on Monday at 8:00 |
->monthly(); | Run the task on the first day of every month at 00:00 |
->monthlyOn(4, '15:00'); | Run the task every month on the 4th at 15:00 |
->twiceMonthly(1, 16, '13:00'); | Run the task monthly on the 1st and 16th at 13:00 |
->lastDayOfMonth('15:00'); | Run the task on the last day of the month at 15:00 |
->quarterly(); | Run the task on the first day of every quarter at 00:00 |
->yearly(); | Run the task on the first day of every year at 00:00 |
->yearlyOn(6, 1, '17:00'); | Run the task every year on June 1st at 17:00 |
->timezone('America/New_York'); | Set the timezone for the task |
These methods may be combined with additional constraints to create even more finely tuned schedules that only run on certain days of the week. For example, you may schedule a command to run weekly on Monday:
// Run once per week on Monday ... scheduler .call(() => { // }).weekly().mondays().run(); // Run hourly on weekdays... scheduler.command("foo").weekdays().hourly().run();
Copied!
# Day Constraints
The days method may be used to limit the execution of a task to specific days of the week. For example, you may schedule a command to run hourly on Sundays and Wednesdays:
scheduler.command('SendEmail').hourly().days([0, 3]).run();
Copied!
# Truth Test Constraints
The when method may be used to limit the execution of a task based on the result of a given truth test. In other words, if the given closure returns true, the task will execute as long as no other constraining conditions prevent the task from running:
scheduler.command('SendEmail').daily().when(()=> { return true; }).run();
Copied!
# Running The Scheduler
Now that we have learned how to define scheduled tasks, let's discuss how to actually run them on our server. The run-schedule Maker command will evaluate all of your scheduled tasks and determine if they need to run based on the server's current time.
ts-node maker run-schedule
Copied!
← Queues Web Socket →