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

JobScheduler::new() - CantInit Error #27

Closed
ev94 opened this issue Jun 29, 2022 · 7 comments
Closed

JobScheduler::new() - CantInit Error #27

ev94 opened this issue Jun 29, 2022 · 7 comments

Comments

@ev94
Copy link

ev94 commented Jun 29, 2022

Hi,

Thx for the great work.
We just update to 0.7.2. The call to JobScheduler::new() in the code below fails with a CantInit error.
Are we missing something?

Thx for your help
Emmanuel

tokio::spawn(async move {
	info!("spawning cron job");
	let sched = JobScheduler::new().expect("failed to create job scheduler");
	let _ = sched.add(
		Job::new_async(
			move |_uuid, _l| {
    			Box::pin(async move {
   					info!("job started);
				})
			},
		).expect("failed to create async job"),
	);
	if let Err(e) = sched.start() {
   		error!("failed to start scheduler: {:?}", e);
	}
});
@mvniekerk
Copy link
Owner

Hi @ev94
Thanks for the bug report. Did this code compile at all? You're missing the cron text for the Job::new_async(, ) code.

@ev94
Copy link
Author

ev94 commented Jul 1, 2022

Hi @mvniekerk,
Thanks for your reply.
Sorry my code sample was incorrect. The cron text is in it.

tokio::spawn(async move {
	info!("spawning cron job");
	let sched = JobScheduler::new().expect("failed to create job scheduler");
	let _ = sched.add(
		Job::new_async(
		       app_config.load_data_cron.as_str(),
			move |_uuid, _l| {
    			Box::pin(async move {
   					info!("job started);
				})
			},
		).expect("failed to create async job"),
	);
	if let Err(e) = sched.start() {
   		error!("failed to start scheduler: {:?}", e);
	}
});

@mvniekerk
Copy link
Owner

Hi @ev94
I could replicate the problem.
For some reason it seems like init-ing the scheduler inside a tokio::spawn causes a block on the async-sync code.

@mvniekerk
Copy link
Owner

mvniekerk commented Jul 10, 2022

I've reached out to the Rust Reddit community on this. I don't know how to resolve this issue apart from marking all the functions as async fn's and then breaking the API of this library.

@mvniekerk
Copy link
Owner

@ev94
Copy link
Author

ev94 commented Jul 12, 2022

Hi @mvniekerk
Thanks for your feedback. So a possible solution is to init the scheduler outside of the tokio::spawn and then to move it to the spawn and start it?

let sched = JobScheduler::new().expect("failed to create job scheduler");
let _ = sched.add(
               Job::new_async(
		app_config.load_data_cron.as_str(),
			move |_uuid, _l| {
    			Box::pin(async move {
   			   info!("job started);
			})
		},
	).expect("failed to create async job"),
);
tokio::spawn(async move {
	info!("starting  cron jobs");

	if let Err(e) = sched.start() {
   		error!("failed to start scheduler: {:?}", e);
	}
});

@mvniekerk
Copy link
Owner

Hi @ev94
Please check 0.8.0. I've made an example that covers your use case. Note that the API did change accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants