-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Wait for running jobs to finish #40
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7a4dcd9
Not working `signal-shutdown` example
imbolc 153e353
Wait for running jobs to finish
imbolc eedd8b6
Update example
imbolc 34bfe62
Remove signal based example
imbolc 0afcb05
Fix tests
imbolc 1b75f78
Wait longer
imbolc e182165
Identify github actions
imbolc e5a9cf4
Refactoring
imbolc 3caf97c
Merge branch 'ci-tests' into signal-shutdown
imbolc 2197826
CI env var
imbolc 79288e8
Merge branch 'ci-tests' into signal-shutdown
imbolc 1b01f1f
Merge branch 'Diggsey:master' into signal-shutdown
imbolc 3e8c7b1
Increace waiter step
imbolc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The step doesn't need to be this small, 10ms will give other tasks more time to run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than this, the changes look great 👍