Skip to content

Commit

Permalink
Merge pull request #6 from whl2606555/dev
Browse files Browse the repository at this point in the history
fix: doc error
  • Loading branch information
RecordingTheSmile authored Oct 25, 2023
2 parents ac21757 + 8f7c533 commit 24b0ede
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 51 deletions.
102 changes: 93 additions & 9 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div style="text-align: center">Yet Another JobScheduler</div>

<a href="README_CN.MD">简体中文</a>
<div style="text-align: center"><a href="README_CN.MD">简体中文</a></div>

# Features
* Async Completely
Expand All @@ -16,9 +16,93 @@
```rust
use std::sync::Arc;

use example_hook::ExampleHook;
use example_jobs::ExampleJob;
use tokio_scheduler_rs::{DefaultJobExecutor, JobScheduler, MemoryJobStorage};
use tokio_scheduler_rs::{job_hook::JobHook,job_hook::JobHookReturn,async_trait,DefaultJobExecutor, JobScheduler, MemoryJobStorage, JobContext, JobFuture,Value,ScheduleJob};

struct ExampleJob;

impl ScheduleJob for ExampleJob{
fn get_job_name(&self) -> String {
String::from("ExampleJob")
}

fn execute(&self, ctx: JobContext) -> JobFuture {
Box::pin(async move{
println!("Hello, World! My JobId is {}",ctx.get_id());
Ok(Value::default())
})
}
}

struct ExampleHook;

#[async_trait]
impl JobHook for ExampleHook {
async fn on_execute(&self, name: &str, id: &str, args: &Option<Value>) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is going to execute!",
name, id, args
);
JobHookReturn::NoAction
// If you want to Cancel this running ONLY THIS TIME:
// JobHookReturn::CancelRunning
// or you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
}
async fn on_complete(
&self,
name: &str,
id: &str,
args: &Option<Value>,
result: &anyhow::Result<Value>,
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! Result is: {:#?}, retry time is: {}",
name, id, args, result, retry_times
);
JobHookReturn::NoAction
// If you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
// Or if you want to retry this job:
// JobHookReturn::RetryJob
}
async fn on_success(
&self,
name: &str,
id: &str,
args: &Option<Value>,
return_vaule: &Value,
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! ReturnValue is: {:#?}, retry time is: {}",
name, id, args, return_vaule, retry_times
);
JobHookReturn::NoAction
// If you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
// Or if you want to retry this job:
// JobHookReturn::RetryJob
}
async fn on_fail(
&self,
name: &str,
id: &str,
args: &Option<Value>,
error: &anyhow::Error,
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! Error is: {:#?}, retry time is: {}",
name, id, args, error, retry_times
);
JobHookReturn::NoAction
// If you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
// Or if you want to retry this job:
// JobHookReturn::RetryJob
}
}

#[tokio::main]
async fn main() {
Expand All @@ -41,19 +125,19 @@ async fn main() {
// Set a schedule with given cron expression.
// !!! PLEASE NOTICE THAT YOU MUST REGISTER THE JOB FIRST !!!
scheduler
.add_job(ExampleJob::JOB_NAME, "*/5 * * * * * *", &None)
.add_job(&ExampleJob.get_job_name(), "* * * * * * *", &None)
.await
.unwrap();

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

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

// Wait for all jobs are processed and stop the schedule.
// The `JobExecutor` will stop execute NEW job once you execute this.
println!("Stop scheduler");
scheduler.wait_for_stop().await;
}
```
Expand Down
102 changes: 92 additions & 10 deletions README_CN.MD
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,93 @@
```rust
use std::sync::Arc;

use example_hook::ExampleHook;
use example_jobs::ExampleJob;
use tokio_scheduler_rs::{DefaultJobExecutor, JobScheduler, MemoryJobStorage};
use tokio_scheduler_rs::{job_hook::JobHook,job_hook::JobHookReturn,async_trait,DefaultJobExecutor, JobScheduler, MemoryJobStorage, JobContext, JobFuture,Value,ScheduleJob};

pub mod example_hook;
pub mod example_jobs;
struct ExampleJob;

impl ScheduleJob for ExampleJob{
fn get_job_name(&self) -> String {
String::from("ExampleJob")
}

fn execute(&self, ctx: JobContext) -> JobFuture {
Box::pin(async move{
println!("Hello, World! My JobId is {}",ctx.get_id());
Ok(Value::default())
})
}
}

struct ExampleHook;

#[async_trait]
impl JobHook for ExampleHook {
async fn on_execute(&self, name: &str, id: &str, args: &Option<Value>) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is going to execute!",
name, id, args
);
JobHookReturn::NoAction
// If you want to Cancel this running ONLY THIS TIME:
// JobHookReturn::CancelRunning
// or you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
}
async fn on_complete(
&self,
name: &str,
id: &str,
args: &Option<Value>,
result: &anyhow::Result<Value>,
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! Result is: {:#?}, retry time is: {}",
name, id, args, result, retry_times
);
JobHookReturn::NoAction
// If you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
// Or if you want to retry this job:
// JobHookReturn::RetryJob
}
async fn on_success(
&self,
name: &str,
id: &str,
args: &Option<Value>,
return_vaule: &Value,
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! ReturnValue is: {:#?}, retry time is: {}",
name, id, args, return_vaule, retry_times
);
JobHookReturn::NoAction
// If you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
// Or if you want to retry this job:
// JobHookReturn::RetryJob
}
async fn on_fail(
&self,
name: &str,
id: &str,
args: &Option<Value>,
error: &anyhow::Error,
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! Error is: {:#?}, retry time is: {}",
name, id, args, error, retry_times
);
JobHookReturn::NoAction
// If you want to Cancel this running and remove this schedule forever:
// JobHookReturn::RemoveJob
// Or if you want to retry this job:
// JobHookReturn::RetryJob
}
}

#[tokio::main]
async fn main() {
Expand All @@ -33,6 +114,7 @@ async fn main() {
job_storage.to_owned(),
Some(10),
Some(Box::new(ExampleHook)),
30
);
let scheduler = JobScheduler::new(job_storage, job_executor);

Expand All @@ -42,19 +124,19 @@ async fn main() {
// Set a schedule with given cron expression.
// !!! PLEASE NOTICE THAT YOU MUST REGISTER THE JOB FIRST !!!
scheduler
.add_job(ExampleJob::JOB_NAME, "*/5 * * * * * *", &None)
.add_job(&ExampleJob.get_job_name(), "* * * * * * *", &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;
println!("Start scheduler");
scheduler.start();

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

// Wait for all jobs are processed and stop the schedule.
// The `JobExecutor` will stop execute NEW job once you execute this.
println!("Stop scheduler");
scheduler.wait_for_stop().await;
}
```
Expand Down
10 changes: 5 additions & 5 deletions examples/simple_job/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn main() {
// Create a new `job_executor`.
// The second `u64` args means how long does `DefaultJobExecutor` query the `job_storage`.
// If you set it to `None`, Some(5) is used as default.
let job_executor = DefaultJobExecutor::new(job_storage.to_owned(), Some(10), None, 30);
let job_executor = DefaultJobExecutor::new(job_storage.to_owned(), Some(1), None, 30);
let scheduler = JobScheduler::new(job_storage, job_executor);

// Register a job
Expand All @@ -27,13 +27,13 @@ async fn main() {
.unwrap();

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

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

// Wait for all jobs are processed and stop the schedule.
// The `JobExecutor` will stop execute NEW job once you execute this.
println!("Stop scheduler!");
scheduler.wait_for_stop().await;
}
4 changes: 2 additions & 2 deletions examples/simple_job_with_hook/example_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl JobHook for ExampleHook {
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! ReturnValue is: {:#?}, retry time is: {}",
"Task: {} with id: {} and args: {:#?} is success! ReturnValue is: {:#?}, retry time is: {}",
name, id, args, return_vaule, retry_times
);
JobHookReturn::NoAction
Expand All @@ -64,7 +64,7 @@ impl JobHook for ExampleHook {
retry_times: u64,
) -> JobHookReturn {
println!(
"Task: {} with id: {} and args: {:#?} is complete! Error is: {:#?}, retry time is: {}",
"Task: {} with id: {} and args: {:#?} is failed! Error is: {:#?}, retry time is: {}",
name, id, args, error, retry_times
);
JobHookReturn::NoAction
Expand Down
10 changes: 5 additions & 5 deletions examples/simple_job_with_hook/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() {
// You should register your job hook here
let job_executor = DefaultJobExecutor::new(
job_storage.to_owned(),
Some(10),
Some(1),
Some(Box::new(ExampleHook)),
60,
);
Expand All @@ -33,13 +33,13 @@ async fn main() {
.unwrap();

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

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

// Wait for all jobs are processed and stop the schedule.
// The `JobExecutor` will stop execute NEW job once you execute this.
println!("Stop Scheduler!");
scheduler.wait_for_stop().await;
}
4 changes: 3 additions & 1 deletion src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ use std::pin::Pin;
/// # use std::pin::Pin;
/// # use tokio_scheduler_rs::job::ScheduleJob;
/// use tokio_scheduler_rs::JobContext;
/// use tokio_scheduler_rs::JobFuture;
/// pub struct TestJob;
///
/// impl ScheduleJob for TestJob{
/// fn get_job_name(&self) -> String {
/// String::from("TestJob")
/// }
///
/// fn execute(&self,ctx: JobContext) -> Pin<Box<dyn Future<Output=()>>> {
/// fn execute(&self,ctx: JobContext) -> JobFuture {
/// Box::pin(async move{
/// println!("Hello,World! My Task Uuid is: {}",ctx.get_id());
/// Ok(tokio_scheduler_rs::Value::default())
/// })
/// }
/// }
Expand Down
Loading

0 comments on commit 24b0ede

Please sign in to comment.