pub async fn lock(db: &Db, key: &str, owner: &str, ttl: i64) -> Result<bool>Expand description
Takes the lock key for owner until ttl seconds from now; false
when another owner holds it and it has not expired. The owner holding it
takes it again (extending it), so a job that continues in several queue
messages keeps its lock from step to step. One D1 write.
The job_locks table comes from the migration of ocre g job --lock
(LOCKS_TABLE_SQL).
§Errors
A D1 error (the table is missing: run the migration).
§Examples
use ocre::{Ctx, Error, Result, jobs};
async fn import(ctx: &Ctx, account_id: i64, run: &str) -> Result<()> {
let db = ctx.db()?;
let key = format!("import:{account_id}");
if !jobs::lock(&db, &key, run, 600).await? {
return Err(Error::internal("another import of this account is running")); // retried later
}
// ... the work
jobs::unlock(&db, &key, run).await
}