@@ -81,11 +81,11 @@ impl Display for JobStatus {
81
81
/// Client for interacting with Gearman service
82
82
///
83
83
/// Both workers and clients will use this as the top-level object to communicate
84
- /// with a gearman server. [Client. new] should produce a functioning structure which
84
+ /// with a gearman server. [Client:: new] should produce a functioning structure which
85
85
/// should then be configured as needed. It will not do anything useful until after
86
- /// [Client. connect] has been called.
86
+ /// [Client:: connect] has been called.
87
87
///
88
- /// See examples/client.rs and examples/worker.rs for information on how to use it.
88
+ /// See examples for more information on how to use it.
89
89
pub struct Client {
90
90
servers : Vec < Hostname > ,
91
91
conns : Arc < Mutex < Connections > > ,
@@ -111,11 +111,8 @@ impl fmt::Display for ClientJob {
111
111
}
112
112
}
113
113
114
- /// Passed to workers
114
+ /// This structure is passed to worker functions after a JOB_ASSIGN_UNIQ packet is received.
115
115
///
116
- /// The sink_tx property of this structure can be used to send raw packets
117
- /// to the gearman server from workers, although this is not known to work
118
- /// generically as of this writing.
119
116
pub struct WorkerJob {
120
117
pub handle : Bytes ,
121
118
pub function : Bytes ,
@@ -164,10 +161,9 @@ impl ClientJob {
164
161
& self . handle
165
162
}
166
163
167
- /// Should only return when the worker has sent data or completed the job. Errors if used on background jobs.
164
+ /// Should only return when the worker has sent data or completed the job.
168
165
///
169
- /// Use this in clients to wait for a response on a job that was submitted. This will block
170
- /// forever or error if used on a background job.
166
+ /// Use this in clients to wait for a response on a job that was submitted. This will return an error if used on a background job.
171
167
pub async fn response ( & mut self ) -> Result < WorkUpdate , io:: Error > {
172
168
if let Some ( workupdate) = self . response_rx . recv ( ) . await {
173
169
Ok ( workupdate)
@@ -194,10 +190,21 @@ impl WorkerJob {
194
190
/// Sends a WORK_STATUS
195
191
///
196
192
/// This will send a WORK_STATUS packet to the server, and can be called from a worker,
197
- /// although that worker may need to manage its own runtime, and as of this writing, this
198
- /// method may not be functional.
193
+ /// although that worker may need to manage its own async runtime to execute this function.
199
194
///
200
- /// See examples/worker.rs for an idea of how it may work.
195
+ /// ```
196
+ /// fn sends_status(work: &mut WorkerJob) -> Result<Vec<u8>, std::io::Error> {
197
+ /// let rt = tokio::runtime::Builder::new_current_thread()
198
+ /// .build()
199
+ /// .unwrap();
200
+ /// rt.block_on(work.work_status(50, 100))?;
201
+ /// Ok("Done".into())
202
+ /// }
203
+ /// let mut worker = worker
204
+ /// .can_do("statusfunc", sends_status)
205
+ /// .await
206
+ /// .expect("CAN_DO should succeed");
207
+ /// ```
201
208
pub async fn work_status ( & mut self , numerator : u32 , denominator : u32 ) -> Result < ( ) , io:: Error > {
202
209
let numerator = format ! ( "{}" , numerator) ;
203
210
let denominator = format ! ( "{}" , denominator) ;
@@ -222,7 +229,7 @@ impl WorkerJob {
222
229
223
230
/// Sends a WORK_FAIL
224
231
///
225
- /// This method is typically called by the [Client. work] method upon return
232
+ /// This method is typically called by the [Client:: work] method upon return
226
233
/// of an error from the assigned closure.
227
234
pub async fn work_fail ( & mut self ) -> Result < ( ) , io:: Error > {
228
235
let packet = new_res ( WORK_FAIL , self . handle . clone ( ) ) ;
@@ -231,7 +238,7 @@ impl WorkerJob {
231
238
232
239
/// Sends a WORK_COMPLETE
233
240
///
234
- /// This method is typically called by the [Client. work] method upon return of
241
+ /// This method is typically called by the [Client:: work] method upon return of
235
242
/// the assigned closure.
236
243
pub async fn work_complete ( & mut self , response : Vec < u8 > ) -> Result < ( ) , io:: Error > {
237
244
let mut payload = BytesMut :: with_capacity ( self . handle . len ( ) + 1 + self . payload . len ( ) ) ;
@@ -261,8 +268,8 @@ impl Client {
261
268
self
262
269
}
263
270
264
- /// Call this to enable TLS/SSL connections to servers.
265
- /// This takes a rustls:: ClientConfig object which allows a lot of flexiblity in how TLS operates .
271
+ /// Call this to enable TLS/SSL connections to servers. If it is never called, the connection will remain plain.
272
+ /// This takes a [ ClientConfig] object which allows a lot of flexibility in how TLS will operate .
266
273
pub fn set_tls_config ( mut self , config : ClientConfig ) -> Self {
267
274
self . tls = Some ( config) ;
268
275
self
@@ -497,7 +504,7 @@ impl Client {
497
504
Ok ( self )
498
505
}
499
506
500
- /// Sends an ECHO_REQ to the server, a good way to confirm the connection is alive
507
+ /// Sends an ECHO_REQ to the first server, a good way to confirm the connection is alive
501
508
///
502
509
/// Returns an error if there aren't any connected servers, or no ECHO_RES comes back
503
510
pub async fn echo ( & mut self , payload : & [ u8 ] ) -> Result < ( ) , io:: Error > {
@@ -523,13 +530,14 @@ impl Client {
523
530
Ok ( ( ) )
524
531
}
525
532
526
- /// Submits a foreground job. The see [ClientJob. response] for how to see the response from the
527
- /// worker.
533
+ /// Submits a foreground job. The see [ClientJob:: response] for how to see the response from the
534
+ /// worker. The unique ID will be generated using [Uuid::new_v4]
528
535
pub async fn submit ( & mut self , function : & str , payload : & [ u8 ] ) -> Result < ClientJob , io:: Error > {
529
536
self . direct_submit ( SUBMIT_JOB , function, payload, None )
530
537
. await
531
538
}
532
539
540
+ /// Submits a job with an explicit unique ID.
533
541
pub async fn submit_unique (
534
542
& mut self ,
535
543
function : & str ,
@@ -541,7 +549,7 @@ impl Client {
541
549
}
542
550
543
551
/// Submits a background job. The [ClientJob] returned won't be able to use the
544
- /// [ClientJob. response] method because the server will never send packets for it.
552
+ /// [ClientJob:: response] method because the server will never send packets for it.
545
553
pub async fn submit_background (
546
554
& mut self ,
547
555
function : & str ,
@@ -552,7 +560,7 @@ impl Client {
552
560
}
553
561
554
562
/// Submits a background job. The [ClientJob] returned won't be able to use the
555
- /// [ClientJob. response] method because the server will never send packets for it.
563
+ /// [ClientJob:: response] method because the server will never send packets for it.
556
564
pub async fn submit_background_unique (
557
565
& mut self ,
558
566
function : & str ,
@@ -712,7 +720,7 @@ impl Client {
712
720
}
713
721
714
722
/// Receive and do just one job. Will not return until a job is done or there
715
- /// is an error. See [Client. work] for more information .
723
+ /// is an error. This is called in a loop by [Client:: work].
716
724
pub async fn do_one_job ( & mut self ) -> Result < ( ) , io:: Error > {
717
725
let job = self . client_data . receivers ( ) . worker_job_rx . try_recv ( ) ;
718
726
let job = match job {
@@ -769,7 +777,7 @@ impl Client {
769
777
/// Run the assigned jobs through can_do functions until an error happens
770
778
///
771
779
/// After you have set up all functions your worker can do via the
772
- /// [Client. can_do] method, call this function to begin working. It will
780
+ /// [Client:: can_do] method, call this function to begin working. It will
773
781
/// not return unless there is an unexpected error.
774
782
///
775
783
/// See examples/worker.rs for more information on how to use it.
0 commit comments