Cron Jobs are game events that run on a set schedule, such as daily healing or weekly cookie awards.
Table of Contents
The Engine’s cron system sends out a CronEvent
once a minute. To implement a cron job, the plugin needs to handle this event as explained in Event Handling.
Unless you want your cron job to run every minute, you’ll need some code in the cron handler to make it run when desired. Typically this is done with the is_cron_match?
utility . For example:
config = Global.read_config("your_plugin", "your_cron_config")
return if !Cron.is_cron_match?(config, event.time)
If the cron event time matches your cron configuration settings, the cron job will run. Otherwise it will return without doing anything.
The is_cron_match?
utility lets you specify when a cron job is supposed to run. There are four components of the cron configuration:
Each item is a list, so you can have a job that runs on Tuesdays and Thursdays, or every half-hour, or on the 1st and 15th of the month, etc.
If you don’t care about a component, just leave it out. Some common examples are shown below.
Runs on the 1st of every month at midnight (doesn’t care what day of the week it is):
day:
- 1
hour:
- 00
minute:
- 00
Runs every Saturday at 9pm (2100 hours) (doesn’t care what the date is):
day_of_week:
- Sat
hour:
- 21
minute:
- 00
Runs every Saturday and Tuesday at 9pm (2100 hours) (doesn’t care what the date is):
day_of_week:
- Sat
- Tue
hour:
- 21
minute:
- 00
Runs every day at 3am (0300 hours) (doesn’t care what the date or day of week is):
hour:
- 03
minute:
- 00
Runs at the top of every hour (doesn’t care what the date, day of week, or hour is):
minute:
- 00
Runs every fifteen minutes (doesn’t care what the date/day/hour is):
minute:
- 00
- 15
- 30
- 45
If you want the task to never run, you can make it an empty hash:
{}