Struct rustygear::client::WorkerJob

source ·
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

source

pub fn handle(&self) -> &[u8]

source

pub fn function(&self) -> &[u8]

source

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(())
}
source

pub fn unique(&self) -> &[u8]

source

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);
source

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.

source

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§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more