-
-
Notifications
You must be signed in to change notification settings - Fork 80
Closed
Description
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct MyJob {
pub params: serde_json::Value,
}
async fn test_handle(job: MyJob) {
println!("✅ Executing job: {:?}", job.params);
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "debug,sqlx::query=error");
tracing_subscriber::fmt::init();
let pool = PgPool::connect("postgres://postgres:[email protected]:5432/test").await?;
PostgresStorage::setup(&pool).await.expect("unable to run migrations for postgres");
let mut pg_storage = PostgresStorage::new_with_config(pool.clone(), Config::new("saas"));
let mut listener = PgListen::new(pool).await?;
listener.subscribe_with(&mut pg_storage);
tokio::spawn(async move {
listener.listen().await.unwrap();
});
let schedule = Schedule::from_str("0/3 * * * * *").unwrap();
let cron_stream = CronStream::new_with_timezone(schedule, Tz::Asia__Tokyo);
let backend = cron_stream.pipe_to_storage(pg_storage);
let worker = WorkerBuilder::new("main")
.backend(backend)
.build_fn(test_handle);
Monitor::new()
.register(worker)
.on_event(|e| debug!("{e}"))
.run_with_signal(async {
tokio::signal::ctrl_c().await?;
info!("Shutting down the system");
Ok(())
})
.await?;
Ok(())
}
As shown in this code, the params printed by the test_handle function is always empty, because when CronStream builds this task, it uses the Default of MyJob, and cannot set the initial value for MyJob. If it can only be Default, then this scheduled task is meaningless.
In some business scenarios, when a scheduled task is executed, some specific parameters will be processed. For example, if an email is sent to a certain user at a certain time, the basic information of the user should be included, etc.
Metadata
Metadata
Assignees
Labels
No labels