forked from niuhuan/rust_proc_qq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(marco): scheduler proc macro support
- Loading branch information
Showing
11 changed files
with
222 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::Arc; | ||
use tokio_cron_scheduler::Job; | ||
use crate::{Client, SchedulerJob}; | ||
|
||
|
||
pub struct SchedulerHandler { | ||
pub(crate) client: Arc<Client>, | ||
pub(crate) scheduler_job: Vec<SchedulerJob>, | ||
} | ||
|
||
impl SchedulerHandler { | ||
|
||
/// 启动定时任务执行器 | ||
pub async fn start(&self) -> anyhow::Result<()>{ | ||
let scheduler = tokio_cron_scheduler::JobScheduler::new().await?; | ||
let client = self.client.clone(); | ||
let scheduler_job = self.scheduler_job.clone(); | ||
for job in scheduler_job { | ||
tracing::info!("Add {} Job",job.name); | ||
for job in job.handles { | ||
let _teak = Arc::clone(&job); | ||
let client = Arc::clone(&client); | ||
let job = Job::new_async(_teak.cron().as_str(), move |_, _| _teak.call(client.clone()))?; | ||
scheduler.add(job).await?; | ||
} | ||
} | ||
scheduler.start().await?; | ||
Ok(()) | ||
} | ||
|
||
} | ||
|
||
pub trait ScheduledJobHandler: Sync + Send { | ||
fn cron(&self) -> String; | ||
fn call(&self, bot: Arc<Client>) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::sync::Arc; | ||
use proc_qq::{Client, ClientTrait, scheduler, scheduler_job, SchedulerJob}; | ||
|
||
/// 每1分钟获取一次 bot uin | ||
#[scheduler_job(cron = "0 0/1 * * * ?")] | ||
async fn handle_scheduler(c:Arc<Client>) { | ||
let bot_uin = c.bot_uin().await; | ||
println!("{}", bot_uin); | ||
} | ||
|
||
/// scheduler | ||
pub fn scheduler() -> SchedulerJob { | ||
scheduler!( | ||
"hello_jobs", | ||
handle_scheduler | ||
) | ||
} |