-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wait for running jobs to finish (#40)
Wait for running jobs to finish
- Loading branch information
Showing
4 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use sqlxmq::{job, CurrentJob, JobRegistry}; | ||
use std::time::Duration; | ||
use tokio::time; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
dotenv::dotenv().ok(); | ||
let db = sqlx::PgPool::connect(&std::env::var("DATABASE_URL").unwrap()).await?; | ||
|
||
sleep.builder().set_json(&5u64)?.spawn(&db).await?; | ||
|
||
let mut handle = JobRegistry::new(&[sleep]).runner(&db).run().await?; | ||
|
||
// Let's emulate a stop signal in a couple of seconts after running the job | ||
time::sleep(Duration::from_secs(2)).await; | ||
println!("A stop signal received"); | ||
|
||
// Stop listening for new jobs | ||
handle.stop().await; | ||
|
||
// Wait for the running jobs to stop for maximum 10 seconds | ||
handle.wait_jobs_finish(Duration::from_secs(10)).await; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[job] | ||
pub async fn sleep(mut job: CurrentJob) -> sqlx::Result<()> { | ||
let second = Duration::from_secs(1); | ||
let mut to_sleep: u64 = job.json().unwrap().unwrap(); | ||
while to_sleep > 0 { | ||
println!("job#{} {to_sleep} more seconds to sleep ...", job.id()); | ||
time::sleep(second).await; | ||
to_sleep -= 1; | ||
} | ||
job.complete().await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters