Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: clippy fix #3

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub async fn run_example(sched: &mut JobScheduler) -> Result<Vec<Uuid>, JobSched
#[cfg(feature = "signal")]
sched.shutdown_on_ctrl_c();

sched.set_shutdown_handler(Box::new(|| {
Box::pin(async move {
sched
.set_shutdown_handler(async move {
info!("Shut down done");
})
}));
.await;

let mut five_s_job = Job::new("1/5 * * * * *", |uuid, _l| {
info!(
Expand All @@ -27,7 +27,7 @@ pub async fn run_example(sched: &mut JobScheduler) -> Result<Vec<Uuid>, JobSched
// the job store, but with stopped marking
five_s_job
.on_removed_notification_add(
&sched,
sched,
Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
info!(
Expand Down Expand Up @@ -55,7 +55,7 @@ pub async fn run_example(sched: &mut JobScheduler) -> Result<Vec<Uuid>, JobSched
let four_s_job_async_clone = four_s_job_async.clone();
let js = sched.clone();
info!("4s job id {:?}", four_s_job_async.guid());
four_s_job_async.on_start_notification_add(&sched, Box::new(move |job_id, notification_id, type_of_notification| {
four_s_job_async.on_start_notification_add(sched, Box::new(move |job_id, notification_id, type_of_notification| {
let four_s_job_async_clone = four_s_job_async_clone.clone();
let js = js.clone();
Box::pin(async move {
Expand All @@ -67,7 +67,7 @@ pub async fn run_example(sched: &mut JobScheduler) -> Result<Vec<Uuid>, JobSched

four_s_job_async
.on_done_notification_add(
&sched,
sched,
Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
info!(
Expand Down
10 changes: 5 additions & 5 deletions examples/repeated_with_start_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio_cron_scheduler::{JobBuilder, JobScheduler};

#[tokio::main]
async fn main() {
println!("start at: {}", Local::now().to_rfc3339().to_string());
println!("start at: {:?}", Local::now().to_rfc3339());

// 创建 scheduler
let mut sched = JobScheduler::new().await.unwrap();
Expand All @@ -26,11 +26,11 @@ async fn main() {
.start_at(datetime)
// TODO:设置 job 的任务重叠策略
// 设置 job 的异步执行函数
.with_run_async(Box::new(|uuid, mut l| {
.with_run_async(Box::new(|_uuid, mut _l| {
Box::pin(async move {
println!(">>> {}", Local::now().to_rfc3339().to_string());
println!(">>> {:?}", Local::now().to_rfc3339());
tokio::time::sleep(Duration::from_secs(INTERVAL + 2)).await;
println!("<<< {}", Local::now().to_rfc3339().to_string());
println!("<<< {:?}", Local::now().to_rfc3339());
})
}))
.build()
Expand All @@ -49,5 +49,5 @@ async fn main() {

// 停止 scheduler
sched.shutdown().await.unwrap();
println!("stop at: {}", Local::now().to_rfc3339().to_string());
println!("stop at: {:?}", Local::now().to_rfc3339());
}
6 changes: 6 additions & 0 deletions src/job/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl JobBuilder<Utc> {
}
}

impl Default for JobBuilder<Utc> {
fn default() -> Self {
Self::new()
}
}

impl<T: TimeZone> JobBuilder<T> {
pub fn with_timezone<U: TimeZone>(self, timezone: U) -> JobBuilder<U> {
JobBuilder {
Expand Down
4 changes: 4 additions & 0 deletions src/job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn nop_async(_uuid: Uuid, _jobs: JobsSchedulerLocked) -> Pin<Box<dyn Future<Outp
pub struct JobLocked(pub(crate) Arc<RwLock<Box<dyn Job + Send + Sync>>>);

pub trait Job {
#[allow(dead_code)]
fn is_cron_job(&self) -> bool;
fn schedule(&self) -> Option<Cron>;
fn repeated_every(&self) -> Option<u64>;
Expand All @@ -69,17 +70,20 @@ pub trait Job {
fn set_next_tick(&mut self, tick: Option<DateTime<Utc>>);
fn set_count(&mut self, count: u32);
fn count(&self) -> u32;
#[allow(dead_code)]
fn increment_count(&mut self);
fn job_id(&self) -> Uuid;
fn job_type(&self) -> JobType;
fn ran(&self) -> bool;
fn set_ran(&mut self, ran: bool);
#[allow(dead_code)]
fn stop(&self) -> bool;
fn set_stopped(&mut self);
fn set_started(&mut self);
fn job_data_from_job(&mut self) -> Result<Option<JobStoredData>, JobSchedulerError>;
fn set_job_data(&mut self, job_data: JobStoredData) -> Result<(), JobSchedulerError>;
fn run(&mut self, jobs: JobScheduler) -> Receiver<bool>;
#[allow(dead_code)]
fn fixed_offset_west(&self) -> i32;
}

Expand Down
22 changes: 8 additions & 14 deletions src/job_scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::simple::{
SimpleJobCode, SimpleMetadataStore, SimpleNotificationCode, SimpleNotificationStore,
};
use crate::store::{MetaDataStorage, NotificationStore};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -331,15 +331,12 @@ impl JobsSchedulerLocked {

let mut metadata = self.context.metadata_storage.write().await;
let jm = metadata.get(job_id).await?;
match jm {
Some(mut job_metadata) => {
job_metadata
.last_updated
.replace(Utc::now().timestamp() as u64);
job_metadata.ran = ran;
metadata.add_or_update(job_metadata).await?;
}
_ => {}
if let Some(mut job_metadata) = jm {
job_metadata
.last_updated
.replace(Utc::now().timestamp() as u64);
job_metadata.ran = ran;
metadata.add_or_update(job_metadata).await?;
}
Ok(())
}
Expand Down Expand Up @@ -410,10 +407,7 @@ impl JobsSchedulerLocked {
if vv.next_tick == 0 {
return None;
}
match NaiveDateTime::from_timestamp_opt(vv.next_tick as i64, 0) {
None => None,
Some(ts) => Some(DateTime::from_naive_utc_and_offset(ts, Utc)),
}
DateTime::from_timestamp(vv.next_tick as i64, 0)
} else {
None
}
Expand Down
5 changes: 1 addition & 4 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ impl Scheduler {
}
}
'next_tick: loop {
let shutdown = {
let r = shutdown.load(Ordering::Relaxed);
r
};
let shutdown = { shutdown.load(Ordering::Relaxed) };
if shutdown {
break 'next_tick;
}
Expand Down
1 change: 1 addition & 0 deletions src/store/metadata_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pub trait MetaDataStorage: DataStore<JobStoredData> + InitStore {
) -> Pin<Box<dyn Future<Output = Result<Option<std::time::Duration>, JobSchedulerError>> + Send>>;
}

#[allow(dead_code)]
pub trait JobCodeGet: CodeGet<Box<JobToRunAsync>> {}
1 change: 1 addition & 0 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ where
) -> Pin<Box<dyn Future<Output = Result<(), JobSchedulerError>> + Send>>;
}

#[allow(dead_code)]
pub trait CodeGet<CODE>
where
CODE: Sized,
Expand Down
1 change: 1 addition & 0 deletions src/store/notification_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ pub trait NotificationStore: DataStore<NotificationData> + InitStore {
) -> Pin<Box<dyn Future<Output = Result<(), JobSchedulerError>> + Send>>;
}

#[allow(dead_code)]
pub trait NotificationRunnableCodeGet: CodeGet<Box<OnJobNotification>> {}