TOKIO-SCHEDULER-RS

Yet Another JobScheduler

简体中文

Features

Example

```rust use std::sync::Arc;

use examplehook::ExampleHook; use examplejobs::ExampleJob; use tokioschedulerrs::{DefaultJobExecutor, JobScheduler, MemoryJobStorage};

pub mod examplehook; pub mod examplejobs;

[tokio::main]

async fn main() { // Create a new job_storage, you can impl it by yourself. // !!! PLEASE NOTICE THAT MEMORYJOBSTORAGE SHOULD NOT BE USED IN PRODUCTION !!! let jobstorage = Arc::new(MemoryJobStorage::new(chrono::Utc)); // Create a new job_executor. // You should register your job hook here let jobexecutor = DefaultJobExecutor::new( jobstorage.toowned(), Some(10), Some(Box::new(ExampleHook)), ); let scheduler = JobScheduler::new(jobstorage, jobexecutor);

// Register a job
scheduler.register_job(Box::new(ExampleJob)).await.unwrap();

// Set a schedule with given cron expression.
// !!! PLEASE NOTICE THAT YOU MUST REGISTER THE JOB FIRST !!!
scheduler
    .add_job(ExampleJob::JOB_NAME, "*/5 * * * * * *", &None)
    .await
    .unwrap();

// Don't forget to start it.
scheduler.start().await.unwrap();
// If you don't want to wait it, then:
// scheduler.start().await;

tokio::time::sleep(std::time::Duration::from_secs(60)).await;

// Wait for all jobs are processed and stop the schedule.
// The `JobExecutor` will stop execute NEW job once you execute this.
scheduler.wait_for_stop().await;

} ```

Examples

You can see examples in the examples directory.

Contribute

If you have some ideas, you can create a pull request or open an issue.

Any kinds of contributions are welcome!

Distributed Job Execution

As you can see, in JobExecutor trait, we define two functions: start and stop.

So, you can define your own JobExecutor, polling the JobStorage and transfer job information to remote via anyway you want.

In the remote machine, you should define the Job with given name, then execute it with given job information(new JobContext()) received from origin JobExecutor machine.

DistributedJob

Roadmap

License

MIT