pub struct WorkerJob {
pub handle: Bytes,
pub function: Bytes,
pub payload: Bytes,
pub unique: Bytes,
/* private fields */
}
Expand description
This structure is passed to worker functions after a JOB_ASSIGN_UNIQ packet is received.
Fields§
§handle: Bytes
§function: Bytes
§payload: Bytes
§unique: Bytes
Implementations§
source§impl WorkerJob
impl WorkerJob
pub fn handle(&self) -> &[u8] ⓘ
pub fn function(&self) -> &[u8] ⓘ
sourcepub fn payload(&self) -> &[u8] ⓘ
pub fn payload(&self) -> &[u8] ⓘ
Examples found in repository?
examples/worker.rs (line 17)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let worker = Client::new();
worker
.add_server("127.0.0.1:4731")
.add_server("127.0.0.1:4730")
//.add_server("127.0.0.1:4731") Add all of your servers here
.set_client_id("example")
.connect()
.await
.expect("CONNECT failed")
.can_do("reverse", |job| {
let payload = String::from_utf8(job.payload().to_vec()).unwrap();
println!("reversing {}", payload);
let reversed: String = payload.chars().rev().collect();
let reversed: Vec<u8> = reversed.into_bytes();
Ok(reversed)
})
.await
.expect("CAN_DO reverse failed")
.can_do("alwaysfail", |_job| {
Err(io::Error::new(io::ErrorKind::Other, "Always fails"))
})
.await
.expect("CAN_DO alwaysfail failed")
//.can_do_async("status", status_user)
.work()
.await
.expect("WORK FAILED");
Ok(())
}
pub fn unique(&self) -> &[u8] ⓘ
sourcepub async fn work_status(
&mut self,
numerator: u32,
denominator: u32
) -> Result<(), Error>
pub async fn work_status( &mut self, numerator: u32, denominator: u32 ) -> Result<(), Error>
Sends a WORK_STATUS
This will send a WORK_STATUS packet to the server, and can be called from a worker, although that worker may need to manage its own async runtime to execute this function.
use rustygear::client::{Client, WorkerJob};
let worker = Client::new();
fn sends_status(work: &mut WorkerJob) -> Result<Vec<u8>, std::io::Error> {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
rt.block_on(work.work_status(50, 100))?;
Ok("Done".into())
}
let mut worker = worker
.can_do("statusfunc", sends_status);
sourcepub async fn work_fail(&mut self) -> Result<(), Error>
pub async fn work_fail(&mut self) -> Result<(), Error>
Sends a WORK_FAIL
This method is typically called by the Client::work method upon return of an error from the assigned closure.
sourcepub async fn work_complete(&mut self, response: Vec<u8>) -> Result<(), Error>
pub async fn work_complete(&mut self, response: Vec<u8>) -> Result<(), Error>
Sends a WORK_COMPLETE
This method is typically called by the Client::work method upon return of the assigned closure.
Auto Trait Implementations§
impl !Freeze for WorkerJob
impl !RefUnwindSafe for WorkerJob
impl Send for WorkerJob
impl Sync for WorkerJob
impl Unpin for WorkerJob
impl !UnwindSafe for WorkerJob
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more