Crate bevy_cronjob

Crate bevy_cronjob 

Source
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.

secminhourday of monthmonthday of weekyear
*******
0-590-590-231-311-121-71970-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§

CronJobPlugin
A Bevy plugin that enables cron job functionality.
ScheduleArrived
Event sent when a scheduled task should execute.
ScheduleTimer
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.