A cronjob to send a mail every day (Actix-web project)


I have a Actix-web project. Let's for the sake of ease say it's just a simple website where people can register. I save the timestamp of when someone registers.

What I want to have now is a code which automatically e-mails people who are exactly one year registered. To do this I'd need a code which gets executed once a day.

A cronjob seems perfect for this job. First I was thinking about making a cronjob in Bash and to let it call a function in my Actix project by sending a HTTP-request to trigger it.

But it seems Rust also has some kind of solution for this: the cronjob Crate.

This is my code:

extern crate cronjob;
use cronjob::CronJob;

async fn main() -> std::io::Result<()> {

    // Cronjob
    let mut cron = CronJob::new("Test", send_mails);
    cron.seconds("20"); // Executed every minute in this example
    CronJob::start_job_threaded(cron); // Threaded

    // start http server
    HttpServer::new(move || {
        // Code and services for Actix-web

pub fn send_mails(name: &str) {
    println!("Sending mails!");

This code seems to work. But I have a few questions regarding the combination of such a cronjob and Actix together.

  • Actix-web creates a new instance for each visitor (otherwise only one visitor at a time would be possible). Does this mean that if 5 people visit my site, the function send_mails would be executed 5 times as well? I don't think this is the case because the HttpServer is 'shielded' from the other code. But I just want to double-check this.
  • My send_mails function is not async like the most part of my code. But I guess this isn't a problem because it's not a problem if the separate cronjob-thread needs to wait for the response.
  • How would I use a async function in this Cronjob-crate?
  • Is this a good approach?
  • Other things I should consider?


  • Yes, you are right. The HttpServer has nothing to do with cron, they are totally unaffected.
  • It doesn't really matter since it's not blocking async runtime anyway.
  • Get a tokio runtime Handle and use Handle::spawn or Handle::block_on Handle in tokio::runtime - Rust
  • Yes, I would say it's good.
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.