We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Hi, there is a way to work with actix-web?
The text was updated successfully, but these errors were encountered:
When using Actix web, I had to make use of Actix Actors as this crate interferes with the Actix run time
use actix::prelude::*; use chrono::Local; use cron::Schedule; use std::{str::FromStr, time::Duration}; // Define actor pub struct Scheduler; // Provide Actor implementation for our actor impl Actor for Scheduler { type Context = Context<Self>; fn started(&mut self, ctx: &mut Context<Self>) { println!("Actor is alive"); ctx.run_later(duration_until_next(), move |this, ctx| { this.schedule_task(ctx) }); } fn stopped(&mut self, _ctx: &mut Context<Self>) { println!("Actor is stopped"); } } impl Scheduler { fn schedule_task(&self, ctx: &mut Context<Self>) { // executes every 1 minute based on cron schedule println!("schedule_task event - {:?}", Local::now()); ctx.run_later(duration_until_next(), move |this, ctx| { this.schedule_task(ctx) }); } } pub fn duration_until_next() -> Duration { let cron_expression = "0 * * * * * *"; //every minute let cron_schedule = Schedule::from_str(cron_expression).unwrap(); let now = Local::now(); let next = cron_schedule.upcoming(Local).next().unwrap(); let duration_until = next.signed_duration_since(now); Duration::from_millis(duration_until.num_milliseconds() as u64) }
Then in your main.rs fn :
Scheduler.start(); Hope this helps
Scheduler.start();
Sorry, something went wrong.
No branches or pull requests
Hi, there is a way to work with actix-web?
The text was updated successfully, but these errors were encountered: