Crate cronframe

Source
Expand description

§CronFrame 0.1.3

This framework allows for the definition and scheduling of cron jobs with macros both on functions in the “global scope” and inside struct types.

Job creation without macros is possible, refer to the example in no_macros.rs on the repo.

§General Information

The cron expression parser used is cron.

Scheduling time is in UTC.

There are three types of jobs that can be defined using the framework:

  • global jobs
  • function jobs
  • method jobs

Each of these is defined with a macro, a standalone macro for global jobs while function a method jobs require a little bit of setup.

A struct that can host jobs is known as a cron object in the context of cronframe and is defined with the cron_obj macro.

Jobs of a cron object must be defined inside a standalone implementation block annotated with the macro cron_impl.

NOTICE: a cron object derives the Clone trait so its fields must too.

The framework supports a daily timeout (timed-out state resets every 24hrs) in ms which is decativated if the value is 0.

During the first run of the framework a templates folder will be created in the current directory with 7 files inside it:

  • base.html.tera
  • index.htm.tera
  • job.html.tera
  • tingle.js
  • cronframe.js
  • styles.css
  • tingle.css

By default the web server runs at localhost:8098, the port can be changed in the cronframe.toml file.

A rolling logger, also configurable via cronframe.toml provides an archive of 3 files in addition to the latest log.

The default size of a log file is 1MB.

§Tutorial

For the tutorial refer to the website.

§Defining A Global Job

#[cron(expr="* * * * * * *", timeout="0")]    
fn hello_job(){
    println!("hello world!");
}
 
fn main(){
    // init and gather global cron jobs
    let cronframe = CronFrame::default();
     
    // start the scheduler
    cronframe.start_scheduler();
 
    // to keep the main thread alive 
    // cronframe.keep_alive();
 
    // alternatively, start the scheduler and keep main alive
    // cronframe.run();
}

§Defining A Function Job

#[cron_obj]
struct User {
    name: String,
}
 
#[cron_impl]
impl User {
    #[fn_job(expr="* * * * * * *", timeout="0")]    
    fn hello_function_job(){
        println!("hello world!");
    }
}
 
fn main(){
    let cronframe = CronFrame::default();
     
    // this function collects all function jobs defined on a cron object
    User::cf_gather_fn(cronframe.clone());
 
    cronframe.start_scheduler();
 
    // alternatively, start the scheduler and keep main alive
    // cronframe.run();
}

§Defining A Method Job

#[cron_obj]
struct User {
    name: String,
    expr1: CronFrameExpr,
}
 
#[cron_impl]
impl User {
    #[fn_job(expr="* * * * * * *", timeout="0")]    
    fn hello_function_job(){
        println!("hello world!");
    }
 
    #[mt_job(expr="expr1")]    
    fn hello_method_job(self){
        println!("hello world!");
    }
}
 
fn main(){
    let cronframe = CronFrame::default();
 
    let mut user1 = User::new_cron_obj(
        "John Smith".to_string(),
        CronFrameExpr::new("0/5", "*", "*", "*", "*", "*", "*", 0)
    );
 
    // this method collects all jobs defined on a cron object
    user1.cf_gather(cronframe.clone());
 
    // in alternative if we only wanted to collect method jobs
    // user1.cf_gather_mt(cronframe.clone());
 
    cronframe.start_scheduler();
 
    // alternatively, start the scheduler and keep main alive
    // cronframe.run();
}

Re-exports§

pub use cronframe::CronFrame;
pub use job_builder::JobBuilder;
pub use cronjob::CronJob;
pub use cronframe_expr::CronFrameExpr;

Modules§

config
Configuration avaliable in cronframe.toml
cronframe
The Core Type of the Framework
cronframe_expr
This type is used in cron objects to define the cron expression and timeout for a method job.
cronjob
CronJob type, built by JobBuilder
job_builder
Builder Type for CronJob
logger
Default logger setup for the cronframe framework and the testing suite
utils
Utilities
web_server
Custom setup of rocket.rs for the Cronframe web server

Enums§

CronFilter
Used in the init function of the CronFrame type to filter in a single type of job for execution.
CronJobType
Used in the init function of the CronJob type to account for the type of job

Attribute Macros§

cron
Global Job definition Macro
cron_impl
Cron Implementation Block Macro
cron_obj
Cron Object definition Macro
fn_job
Function Job definition Macro for a Cron Object
mt_job
Method Job definition Macro for a Cron Object