Skip to content

Commit

Permalink
Impl Display for StopReason. Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustworthy committed Aug 3, 2024
1 parent 20377a8 commit 0371e63
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
54 changes: 45 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,53 @@ c.enqueue(Job::new("foobar", vec!["z"])).await.unwrap();
If you want to **accept** jobs from Faktory, use `Worker`.

```rust
use faktory::WorkerBuilder;
use async_trait::async_trait;
use faktory::{JobRunner, Worker};
use std::io;
let mut w = WorkerBuilder::default();
w.register("foobar", |job| async move {
println!("{:?}", job);
Ok::<(), io::Error>(())
});
let mut w = w.connect(None).await.unwrap();
if let Err(e) = w.run(&["default"]).await {
println!("worker failed: {}", e);

struct DomainEntity(i32);

impl DomainEntity {
fn new(buzz: i32) -> Self {
DomainEntity(buzz)
}
}

#[async_trait]
impl JobRunner for DomainEntity {
type Error = io::Error;

async fn run(&self, job: Job) -> Result<(), Self::Error> {
println!("{:?}, buzz={}", job, self.0);
Ok(())
}
}

let mut w = Worker::builder()
.register("fizz", DomainEntity::new(1))
.register("jobtype", DomainEntity::new(100))
.register_fn("foobar", |job| async move {
println!("{:?}", job);
Ok::<(), io::Error>(())
})
.register_blocking_fn("fibo", |job|
std::thread::sleep(Duration::from_millis(1000));
println!("{:?}", job);
Ok::<(), io::Error>(())
})
.connect(None)
.await
.unwrap();

match w.run(&["default"]).await {
Err(e) => println!("worker failed: {}", e),
Ok(stop_details) => {
println!(
"Stop reason: {}, number of workers that were running: {}",
stop_details.reason,
stop_details.workers_still_running
);
}
```

Also see some usage examples in `examples` directory in the project's root. You can run an example with:
Expand Down
11 changes: 9 additions & 2 deletions src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,15 @@ type CallbacksRegistry<E> = FnvHashMap<String, Callback<E>>;
/// .await
/// .unwrap();
///
/// if let Err(e) = w.run(&["default"]).await {
/// println!("worker failed: {}", e);
/// match w.run(&["default"]).await {
/// Err(e) => println!("worker failed: {}", e),
/// Ok(stop_details) => {
/// println!(
/// "Stop reason: {}, number of workers that were running: {}",
/// stop_details.reason,
/// stop_details.workers_still_running
/// );
/// }
/// }
/// # });
/// ```
Expand Down
8 changes: 8 additions & 0 deletions src/worker/stop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{Debug, Display};

#[cfg(doc)]
use super::{Worker, WorkerBuilder};

Expand All @@ -21,6 +23,12 @@ pub enum StopReason {
ServerInstruction,
}

impl Display for StopReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self, f)
}

Check warning on line 29 in src/worker/stop.rs

View check run for this annotation

Codecov / codecov/patch

src/worker/stop.rs#L27-L29

Added lines #L27 - L29 were not covered by tests
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// Holds some details aroung a worker's run stoppage, such as the reason why this worker discontinued
/// and the number of workers that might still be processing jobs at that instant.
Expand Down

0 comments on commit 0371e63

Please sign in to comment.