0

I try to build a REST with Rocket, that allows the caller to configure times at which he will be unavailable. Therefore it can either happen, that the unavailability has to be started immediately or I have to store it in a database for later execution.

I can implement all this with a normal set up of Rocket, rocket_sync_db_pools and Diesel:

#[database("postgres_diesel")]
pub struct DbConn(diesel::PgConnection);

#[rocket::main]
async fn main() {
    rocket::build()
        .attach(DbConn::fairing())
        .mount(
            "/v1",
            routes![ post_block ],
        )
        .launch()
        .await
        .expect("could not launch rocket");
}

Within the handler function for the incoming requests I can get the database connection:

#[post("/block", data = "<new_block>")]
pub async fn post_block(
    new_block: Json<NewBlock>,
    conn: DbConn,
) -> Result<Json<Block>, BadRequest<&'static str>> {
    Ok(Json(conn.run(move |c| configure_block(new_block.0, c)).await))
}

Within the implementation of configure_block() I either execute the configuration immediately or write it as a job to the database.

But how can I execute the configuration changes written to the database at a later moment? I need something like a cronjob within Rocket within that I can check the database, if there are pending jobs.

I tried to do it using tokio::time::interval which will run my task repeatedly. But I cannot get access to by DbPool within this tokio timer. I cannot use DbPool::get_one(&Rocket) as I don't have access to my rocket instance.

So I wonder: how can I run a repeated task within rocket?

Matthias Wimmer
  • 3,789
  • 2
  • 22
  • 41
  • 1
    spawn a task and wait for a timer https://tokio.rs/tokio/tutorial/spawning – Stargateur Sep 22 '21 at 08:46
  • @Stargateur you mean spawning tasks for each job instead of storing them in the database? Interesting idea, because activation will have fewer latency than a cronjob checking for what has to be done. But still I need a database connection to document that the task has been done. How could I get the database connection in the spawned task? – Matthias Wimmer Sep 22 '21 at 08:56
  • 2
    clone it I guess, I merely answer your question. I don't understand at all why you want that. – Stargateur Sep 22 '21 at 09:08

0 Answers0