Expand description
bevy_cronjob is a simple helper to run cronjobs (at repeated schedule) in Bevy.
§Usage
use bevy::log::LogPlugin;
use bevy::prelude::*;
use bevy_app::ScheduleRunnerPlugin;
use bevy_cronjob::prelude::*;
use std::time::Duration;
fn main() {
App::new()
.add_plugins(
MinimalPlugins.set(ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(
1.0 / 60.0,
))),
)
.add_plugins(LogPlugin::default())
.add_plugins(CronJobPlugin)
.add_systems(Update, print_per_5_sec.run_if(schedule_passed("0/5 * * * ? *")))
.add_systems(Update, print_per_min.run_if(schedule_passed("0 * * * ? *")))
.add_systems(Update, print_per_hour.run_if(schedule_passed("0 0 * * ? *")))
.run();
}
fn print_per_5_sec() {
info!("print every 5 sec")
}
fn print_per_min() {
info!("print every minute")
}
fn print_per_hour() {
info!("print every hour")
}
fn setup(mut commands: Commands) {
commands
.spawn(ScheduleTimer::new("every 3 seconds"))
.observe(|_: On<ScheduleArrived>| {
info!("3 seconds passed");
});
}§Expression Format
The scheduling expression is based on the cron crate.
| sec | min | hour | day of month | month | day of week | year |
|---|---|---|---|---|---|---|
| * | * | * | * | * | * | * |
| 0-59 | 0-59 | 0-23 | 1-31 | 1-12 | 1-7 | 1970-2100 |
Time is specified in Local Time. Note that the year may be omitted.
Comma separated values such as 1,2,3 are allowed. For example, a schedule of `0,15,30,45 * * *
- *` would execute every 15 seconds.
Ranges can be specified with a dash. For example, 1-5 * * * * * would execute every second
for the first 5 seconds of a minute.
Modules§
- prelude
- Convenient re-exports for common functionality.
Structs§
- Cron
JobPlugin - A Bevy plugin that enables cron job functionality.
- Schedule
Arrived - Event sent when a scheduled task should execute.
- Schedule
Timer - A component that represents a scheduled task using cron expressions.
Constants§
- EVERY_
1_ AM - Execute every day at 1 AM: “0 0 1 */1 * ? *”
- EVERY_
1_ PM - Execute every day at 1 PM: “0 0 13 */1 * ? *”
- EVERY_
2_ AM - Execute every day at 2 AM: “0 0 2 */1 * ? *”
- EVERY_
2_ PM - Execute every day at 2 PM: “0 0 14 */1 * ? *”
- EVERY_
3_ AM - Execute every day at 3 AM: “0 0 3 */1 * ? *”
- EVERY_
3_ PM - Execute every day at 3 PM: “0 0 15 */1 * ? *”
- EVERY_
4_ AM - Execute every day at 4 AM: “0 0 4 */1 * ? *”
- EVERY_
4_ PM - Execute every day at 4 PM: “0 0 16 */1 * ? *”
- EVERY_
5_ AM - Execute every day at 5 AM: “0 0 5 */1 * ? *”
- EVERY_
5_ MIN - Execute every 5 minutes: “0 0/5 * * * ? *”
- EVERY_
5_ PM - Execute every day at 5 PM: “0 0 17 */1 * ? *”
- EVERY_
5_ SEC - Execute every 5 seconds: “0/5 * * * * ? *”
- EVERY_
6_ AM - Execute every day at 6 AM: “0 0 6 */1 * ? *”
- EVERY_
6_ PM - Execute every day at 6 PM: “0 0 18 */1 * ? *”
- EVERY_
7_ AM - Execute every day at 7 AM: “0 0 7 */1 * ? *”
- EVERY_
7_ PM - Execute every day at 7 PM: “0 0 19 */1 * ? *”
- EVERY_
8_ AM - Execute every day at 8 AM: “0 0 8 */1 * ? *”
- EVERY_
8_ PM - Execute every day at 8 PM: “0 0 20 */1 * ? *”
- EVERY_
9_ AM - Execute every day at 9 AM: “0 0 9 */1 * ? *”
- EVERY_
9_ PM - Execute every day at 9 PM: “0 0 21 */1 * ? *”
- EVERY_
10_ AM - Execute every day at 10 AM: “0 0 10 */1 * ? *”
- EVERY_
10_ MIN - Execute every 10 minutes: “0 0/10 * * * ? *”
- EVERY_
10_ PM - Execute every day at 10 PM: “0 0 22 */1 * ? *”
- EVERY_
10_ SEC - Execute every 10 seconds: “0/10 * * * * ? *”
- EVERY_
11_ AM - Execute every day at 11 AM: “0 0 11 */1 * ? *”
- EVERY_
11_ PM - Execute every day at 11 PM: “0 0 23 */1 * ? *”
- EVERY_
12_ AM - Execute every day at 12 AM (midnight): “0 0 0 */1 * ? *”
- EVERY_
12_ PM - Execute every day at 12 PM (noon): “0 0 12 */1 * ? *”
- EVERY_
30_ MIN - Execute every 30 minutes: “0 0/30 * * * ? *”
- EVERY_
30_ SEC - Execute every 30 seconds: “0/30 * * * * ? *”
- EVERY_
DAY - Execute every day at midnight: “0 0 0 */1 * ? *”
- EVERY_
HOUR - Execute every hour: “0 0 * * * ? *”
- EVERY_
MIN - Execute every minute: “0 * * * * ? *”
Functions§
- parse_
expression - Parses a cron expression, handling both standard cron syntax and English expressions.
- schedule_
passed - Creates a run condition that checks if the cron expression has triggered.
- str_
cron_ syntax - Converts an English description of a schedule into cronjob syntax.