@@ -53,13 +53,9 @@ use std::{
53
53
collections:: HashMap ,
54
54
fmt:: { Debug , Display , Formatter } ,
55
55
future:: Future ,
56
- sync:: {
57
- atomic:: { AtomicUsize , Ordering } ,
58
- Arc ,
59
- } ,
60
- time:: Duration ,
56
+ sync:: Arc ,
61
57
} ;
62
- use temporal_client:: { ServerGatewayApis , ServerGatewayOptionsBuilder , WorkflowOptions } ;
58
+ use temporal_client:: { ServerGatewayApis , ServerGatewayOptionsBuilder } ;
63
59
use temporal_sdk_core:: Url ;
64
60
use temporal_sdk_core_api:: {
65
61
errors:: { PollActivityError , PollWfError } ,
@@ -112,7 +108,6 @@ pub fn sdk_client_options(url: impl Into<Url>) -> ServerGatewayOptionsBuilder {
112
108
pub struct Worker {
113
109
worker : Arc < dyn CoreWorker > ,
114
110
task_queue : String ,
115
- task_timeout : Option < Duration > ,
116
111
workflow_half : WorkflowHalf ,
117
112
activity_half : ActivityHalf ,
118
113
}
@@ -122,8 +117,6 @@ struct WorkflowHalf {
122
117
workflows : HashMap < String , UnboundedSender < WorkflowActivation > > ,
123
118
/// Maps workflow type to the function for executing workflow runs with that ID
124
119
workflow_fns : HashMap < String , WorkflowFunction > ,
125
- /// Number of live workflows
126
- incomplete_workflows : Arc < AtomicUsize > ,
127
120
/// Handles for each spawned workflow run are inserted here to be cleaned up when all runs
128
121
/// are finished
129
122
join_handles : FuturesUnordered < BoxFuture < ' static , Result < WorkflowResult < ( ) > , JoinError > > > ,
@@ -137,19 +130,13 @@ struct ActivityHalf {
137
130
138
131
impl Worker {
139
132
/// Create a new rust worker
140
- pub fn new (
141
- worker : Arc < dyn CoreWorker > ,
142
- task_queue : impl Into < String > ,
143
- task_timeout : Option < Duration > ,
144
- ) -> Self {
133
+ pub fn new ( worker : Arc < dyn CoreWorker > , task_queue : impl Into < String > ) -> Self {
145
134
Self {
146
135
worker,
147
136
task_queue : task_queue. into ( ) ,
148
- task_timeout,
149
137
workflow_half : WorkflowHalf {
150
138
workflows : Default :: default ( ) ,
151
139
workflow_fns : Default :: default ( ) ,
152
- incomplete_workflows : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
153
140
join_handles : FuturesUnordered :: new ( ) ,
154
141
} ,
155
142
activity_half : ActivityHalf {
@@ -169,36 +156,6 @@ impl Worker {
169
156
& self . task_queue
170
157
}
171
158
172
- /// Create a workflow, asking the server to start it with the provided workflow ID and using the
173
- /// provided workflow function.
174
- ///
175
- /// Increments the expected Workflow run count.
176
- ///
177
- /// Returns the run id of the started workflow
178
- pub async fn submit_wf (
179
- & self ,
180
- workflow_id : impl Into < String > ,
181
- workflow_type : impl Into < String > ,
182
- input : Vec < Payload > ,
183
- mut options : WorkflowOptions ,
184
- ) -> Result < String , tonic:: Status > {
185
- options. task_timeout = options. task_timeout . or ( self . task_timeout ) ;
186
- let res = self
187
- . worker
188
- . server_gateway ( )
189
- . start_workflow (
190
- input,
191
- self . task_queue . clone ( ) ,
192
- workflow_id. into ( ) ,
193
- workflow_type. into ( ) ,
194
- options,
195
- )
196
- . await ?;
197
-
198
- self . incr_expected_run_count ( 1 ) ;
199
- Ok ( res. run_id )
200
- }
201
-
202
159
/// Register a Workflow function to invoke when the Worker is asked to run a workflow of
203
160
/// `workflow_type`
204
161
pub fn register_wf < F : Into < WorkflowFunction > > (
@@ -226,25 +183,9 @@ impl Worker {
226
183
) ;
227
184
}
228
185
229
- // TODO: Should be removed before making this worker prod ready. There can be a test worker
230
- // which wraps this one and implements the workflow counting / run_until_done concepts.
231
- // This worker can expose an interceptor for completions that could be used to assist with
232
- // workflow tracking
233
- /// Increment the expected Workflow run count on this Worker. The Worker tracks the run count
234
- /// and will resolve `run_until_done` when it goes down to 0.
235
- /// You do not have to increment if scheduled a Workflow with `submit_wf`.
236
- pub fn incr_expected_run_count ( & self , count : usize ) {
237
- self . workflow_half
238
- . incomplete_workflows
239
- . fetch_add ( count, Ordering :: SeqCst ) ;
240
- }
241
-
242
- /// See [Self::run_until_done], except calls the provided callback just before performing core
243
- /// shutdown.
244
- pub async fn run_until_done_shutdown_hook (
245
- & mut self ,
246
- before_shutdown : impl FnOnce ( ) ,
247
- ) -> Result < ( ) , anyhow:: Error > {
186
+ /// Runs the worker. Eventually resolves after the worker has been explicitly shut down,
187
+ /// or may return early with an error in the event of some unresolvable problem.
188
+ pub async fn run ( & mut self ) -> Result < ( ) , anyhow:: Error > {
248
189
let ( shutdown_tx, shutdown_rx) = watch:: channel ( false ) ;
249
190
let pollers = async move {
250
191
let ( worker, task_q, wf_half, act_half) = self . split_apart ( ) ;
@@ -269,13 +210,6 @@ impl Worker {
269
210
activation,
270
211
)
271
212
. await ?;
272
- if wf_half. incomplete_workflows. load( Ordering :: SeqCst ) == 0 {
273
- info!( "All expected workflows complete" ) ;
274
- // Die rebel scum - evict all workflows (which are complete now),
275
- // and turn off activity polling.
276
- let _ = shutdown_tx. send( true ) ;
277
- break Result :: <_, anyhow:: Error >:: Ok ( ( ) ) ;
278
- }
279
213
}
280
214
} ,
281
215
// Only poll on the activity queue if activity functions have been registered. This
@@ -309,18 +243,11 @@ impl Worker {
309
243
while let Some ( h) = myself. workflow_half . join_handles . next ( ) . await {
310
244
h??;
311
245
}
312
- before_shutdown ( ) ;
313
246
myself. worker . shutdown ( ) . await ;
314
247
myself. workflow_half . workflows . clear ( ) ;
315
248
Ok ( ( ) )
316
249
}
317
250
318
- /// Drives all workflows & activities until they have all finished, repeatedly polls server to
319
- /// fetch work for them.
320
- pub async fn run_until_done ( & mut self ) -> Result < ( ) , anyhow:: Error > {
321
- self . run_until_done_shutdown_hook ( || { } ) . await
322
- }
323
-
324
251
/// Turns this rust worker into a new worker with all the same workflows and activities
325
252
/// registered, but with a new underlying core worker. Can be used to swap the worker for
326
253
/// a replay worker, change task queues, etc.
@@ -398,7 +325,6 @@ impl WorkflowHalf {
398
325
let completion = completions_rx. recv ( ) . await . expect ( "No workflows left?" ) ;
399
326
if completion. has_execution_ending ( ) {
400
327
debug ! ( "Workflow {} says it's finishing" , & completion. run_id) ;
401
- self . incomplete_workflows . fetch_sub ( 1 , Ordering :: SeqCst ) ;
402
328
}
403
329
worker. complete_workflow_activation ( completion) . await ?;
404
330
Ok ( ( ) )
0 commit comments