lholden / job_scheduler

A simple cron-like job scheduling library for Rust.
Apache License 2.0
196 stars 34 forks source link

Meet a issue when I add the notification handler to Job #32

Open longzou opened 6 months ago

longzou commented 6 months ago

When I create a new Job for shared Scheduler, the job could not add notication handler.

let mut jobsch = get_scheduler().clone(); jobsch.shutdown().await?; let job = Job::new_repeated_async(Duration::from_millis(100u64), run)?; // job.on_done_notificationadd(&jobsch, async move |, , |{}); log::info!("Do add a job notification {}", jobsch.inited.read().await); // below code will locked this thread match job.on_notifications_add(&jobsch, Box::new(|job_id, notification_id, type_of_notification| { Box::pin(async move { println!("Job {:?} was completed, notification {:?} ran ({:?})", job_id, notification_id, type_of_notification); }) }), vec![tokio_cronscheduler::JobNotification::Done]).await { Ok() => {}, Err(err) => { log::info!("add notic failed {}", err); }, } log::info!("Do add a job into scheduler"); // this code will not be reached. get_scheduler().add(job).await

longzou commented 6 months ago

I found the code in NotificationCreator::add,

        tokio::spawn(async move {
            tokio::spawn(async move {
                // TODO can maybe not use RwLock
                if let Err(_e) = create_tx.send((data, Arc::new(RwLock::new(run)))) {
                    error!("Error sending notification data");
                }
            });
           ....
         });

Should the two tokio::spawn calling be using .await?