Cron
Create a CRON Job
To create a scheduled task in your bot, also called a CRON job, use the CLI to set up a basic structure for the job. This simplifies scheduling and running periodic tasks within your bot environment.
CLI Pattern
Scheduling the Task
Once the CRON file is created, define the schedule
parameter to set the execution frequency of the job. This can be configured in three ways:
1. Interval Key
These keys represent common intervals and simplify the scheduling process. Choose from:
minutely: Executes every minute
hourly: Executes every hour
daily: Executes once every day
weekly: Executes once every week
monthly: Executes once every month
yearly: Executes once every year
Example:
2. Simple Interval
Define a custom interval for any unit of time with the following properties:
type: Set to "minute", "hour", "day", "week", "month", or "year".
duration: Specifies the interval length in units of the
type
.
Example:
3. Advanced Interval
For more precise control over the timing, use the following properties:
minute: (0-59 or "*")
hour: (0-23 or "*")
dayOfMonth: (1-31 or "*")
month: (1-12 or CronMonth enum or "*")
dayOfWeek: (0-6 or CronDayOfWeek enum or "*")
Example:
Running on Startup
If you want your CRON job to run immediately when the bot starts, set the runOnStart
property to true
.
Example:
CRON Class Overview
The CRON class is designed to facilitate the scheduling and management of tasks. Below is an overview of its key components:
Properties
task
: Holds the scheduled task instance.native
: Indicates whether the CRON job is a native task.filepath
: Optional, stores the path to the CRON file.ranCount
: Counts how many times the task has run.
Methods
start()
: Starts the scheduled task based on the definedschedule
.stop()
: Stops the scheduled task.
With these options, you can create and manage periodic tasks for your bot, tailoring them to your needs using the various schedule options.
Last updated