@@ -145,11 +145,6 @@ func NewLauncherFromBus(newWkr NewWorker, c bus.Consumer, p bus.Producer, opt *L
145
145
opt .DoneTopic = defaultDoneTopic
146
146
}
147
147
148
- lgr := log .New (os .Stderr , "" , log .LstdFlags )
149
- if opt .Logger != nil {
150
- lgr = opt .Logger
151
- }
152
-
153
148
// make sure maxInProgress is at least 1
154
149
maxInProgress := uint (1 )
155
150
if opt .MaxInProgress > 1 {
@@ -171,20 +166,11 @@ func NewLauncherFromBus(newWkr NewWorker, c bus.Consumer, p bus.Producer, opt *L
171
166
// lifetime max remaining (0; no lifetime max)
172
167
remaining := opt .LifetimeWorkers
173
168
174
- // doneCncl (done cancel function)
175
- // - is called internally by the Launcher to signal that the Launcher
176
- // has COMPLETED shutting down.
177
- //
178
- // doneCtx (done context)
179
- // - is for communicating externally that the Launcher is DONE and
180
- // has shutdown gracefully.
181
- doneCtx , doneCncl := context .WithCancel (context .Background ())
182
-
183
169
// stop context and cancel func: shutdown Launcher/workers
184
170
//
185
171
// stopCtx - Launcher will listen on stopCtx.Done() for external forced shutdown.
186
172
// stopCncl - used externally of Launcher to initiate forced Launcher shutdown.
187
- stopCtx , stopCncl := context .WithCancel (context .Background ())
173
+ // stopCtx, stopCncl := context.WithCancel(context.Background())
188
174
189
175
// last context and cancel func - for indicating the last task
190
176
// is in progress.
@@ -194,7 +180,7 @@ func NewLauncherFromBus(newWkr NewWorker, c bus.Consumer, p bus.Producer, opt *L
194
180
//
195
181
// lastCncl - for sending a signal indicating the last task has
196
182
// been received and is currently being processed.
197
- lastCtx , lastCncl := context .WithCancel (context .Background ())
183
+ // lastCtx, lastCncl := context.WithCancel(context.Background())
198
184
199
185
// make sure logger is not nil
200
186
if opt .Logger == nil {
@@ -213,10 +199,10 @@ func NewLauncherFromBus(newWkr NewWorker, c bus.Consumer, p bus.Producer, opt *L
213
199
// warn if typeHandling is set and
214
200
// a task type is not provided.
215
201
if typeHandling != "" && opt .TaskType == "" {
216
- lgr .Printf ("NO WORKERS WILL BE LAUNCHED! task type handling is set to '%v' but no task type is provided" , typeHandling )
202
+ opt . Logger .Printf ("NO WORKERS WILL BE LAUNCHED! task type handling is set to '%v' but no task type is provided" , typeHandling )
217
203
}
218
204
219
- l := & Launcher {
205
+ return & Launcher {
220
206
initTime : time .Now (),
221
207
isInitialized : true ,
222
208
consumer : c ,
@@ -226,19 +212,11 @@ func NewLauncherFromBus(newWkr NewWorker, c bus.Consumer, p bus.Producer, opt *L
226
212
lgr : opt .Logger ,
227
213
taskType : opt .TaskType ,
228
214
typeHandling : typeHandling ,
229
- doneCtx : doneCtx ,
230
- doneCncl : doneCncl ,
231
- stopCtx : stopCtx ,
232
- stopCncl : stopCncl ,
233
- lastCtx : lastCtx ,
234
- lastCncl : lastCncl ,
235
215
maxInProgress : maxInProgress ,
236
216
remaining : remaining ,
237
217
slots : slots ,
238
218
closeTimeout : workerTimeout ,
239
219
}
240
-
241
- return l
242
220
}
243
221
244
222
// Launcher handles the heavy lifting of worker lifecycle, general
@@ -267,19 +245,6 @@ type Launcher struct {
267
245
taskType string // registered task type; used for identifying the worker and handling task types that do not match.
268
246
typeHandling string // how to handle unmatching task types: one of "reject", "ignore"
269
247
270
- // communicating Launcher has finished shutting down
271
- doneCtx context.Context // Launcher context (highest level context)
272
- doneCncl context.CancelFunc // Launcher cancel func (calling it indicates the Launcher has cleanly closed up)
273
-
274
- // forcing workers/Launcher to shut down
275
- // all worker contexts inherit from stopCtx.
276
- stopCtx context.Context // for listening to Launcher shutdown signal. Initiates shutdown process.
277
- stopCncl context.CancelFunc // for telling the Launcher to shutdown. Initiates shutdown process. Shutdown is complete when doneCtx.Done() is closed
278
-
279
- // indicate the last task is in progress
280
- lastCtx context.Context // main loop will listen on lastCtx.Done() to know if the last task is in progress
281
- lastCncl context.CancelFunc // called to indicate the last task is in progress
282
-
283
248
// closeTimeout tells the Launcher how long to wait
284
249
// when forcing a task to close.
285
250
closeTimeout time.Duration
@@ -350,8 +315,8 @@ func (l *Launcher) Stats() LauncherStats {
350
315
finishedTasks := createdTasks - activeTasks
351
316
resp := LauncherStats {
352
317
RunTime : time .Now ().Sub (l .initTime ).String (),
353
- TasksConsumed : l .tasksConsumed ,
354
- TasksRunning : l .tasksRunning ,
318
+ TasksConsumed : atomic . LoadInt64 ( & l .tasksConsumed ) ,
319
+ TasksRunning : atomic . LoadInt64 ( & l .tasksRunning ) ,
355
320
Producer : l .producer .Info (),
356
321
Consumer : l .consumer .Info (),
357
322
}
@@ -362,8 +327,14 @@ func (l *Launcher) Stats() LauncherStats {
362
327
return resp
363
328
}
364
329
365
- // DoTasks will start the task loop and immediately
366
- // begin working on tasks if any are available.
330
+ // Deprecated: Use Start() instead.
331
+ func (l * Launcher ) DoTasks () (doneCtx context.Context , stopCncl context.CancelFunc ) {
332
+ ctx , cancel := context .WithCancel (context .Background ())
333
+ go l .Start (ctx )
334
+ return ctx , cancel
335
+ }
336
+
337
+ // Start is a BLOCKING task loop and immediately begin working on tasks if any are available.
367
338
//
368
339
// The Launcher assumes the producer and consumer
369
340
// are fully initialized when the Launcher is created.
@@ -375,28 +346,19 @@ func (l *Launcher) Stats() LauncherStats {
375
346
// will not do anything. If called more than once will
376
347
// return a copy of the same context and cancel function
377
348
// received the first time.
378
- func (l * Launcher ) DoTasks () ( doneCtx context.Context , stopCncl context. CancelFunc ) {
349
+ func (l * Launcher ) Start ( ctx context.Context ) {
379
350
if ! l .isInitialized {
380
351
panic ("launcher not initialized" )
381
352
}
382
-
383
353
if l .isDoing {
384
- return l . doneCtx , l . stopCncl
354
+ return
385
355
}
386
- go l .do ()
387
-
388
356
l .isDoing = true
389
- return l .doneCtx , l .stopCncl
390
- }
391
-
392
- // do is the main task loop.
393
- func (l * Launcher ) do () {
394
- defer l .doneCncl ()
357
+ stopCtx , stopCancel := context .WithCancel (ctx )
358
+ defer stopCancel ()
395
359
for {
396
360
select {
397
- case <- l .stopCtx .Done ():
398
- goto Shutdown
399
- case <- l .lastCtx .Done ():
361
+ case <- stopCtx .Done ():
400
362
goto Shutdown
401
363
case <- l .slots :
402
364
l .wg .Add (1 )
@@ -408,14 +370,14 @@ func (l *Launcher) do() {
408
370
if l .remaining == 0 {
409
371
// the message about to be requested will
410
372
// be the last one.
411
- l . lastCncl ()
373
+ stopCancel ()
412
374
}
413
375
}
414
376
l .mu .Unlock ()
415
377
416
378
// next() needs to be non-blocking so
417
- // the application can shut down when asked to.
418
- go l .next ()
379
+ // the application can shut down when asked to
380
+ go l .next (stopCtx , stopCancel )
419
381
}
420
382
}
421
383
@@ -440,29 +402,29 @@ Shutdown:
440
402
}
441
403
442
404
// next handles getting and processing the next task.
443
- func (l * Launcher ) next () {
405
+ func (l * Launcher ) next (ctx context. Context , cancel context. CancelFunc ) {
444
406
tskB , done , err := l .consumer .Msg ()
445
407
if done {
446
- l . lastCncl ()
408
+ cancel ()
447
409
}
448
410
if err != nil {
449
411
l .log (err .Error ())
450
- l .giveBackSlot ()
412
+ l .giveBackSlot (ctx )
451
413
452
414
return
453
415
}
454
416
455
417
// handle a zero byte message
456
418
if len (tskB ) == 0 {
457
- l .giveBackSlot ()
419
+ l .giveBackSlot (ctx )
458
420
459
421
return
460
422
}
461
423
462
424
tsk , err := NewFromBytes (tskB )
463
- if err != nil {
464
- l .log (err . Error () )
465
- l .giveBackSlot ()
425
+ if err != nil || tsk == nil {
426
+ l .log (err )
427
+ l .giveBackSlot (ctx )
466
428
467
429
return
468
430
}
@@ -472,7 +434,7 @@ func (l *Launcher) next() {
472
434
go func () {
473
435
atomic .AddInt64 (& l .tasksConsumed , 1 )
474
436
atomic .AddInt64 (& l .tasksRunning , 1 )
475
- l .doLaunch (tsk )
437
+ l .doLaunch (tsk , ctx )
476
438
atomic .AddInt64 (& l .tasksRunning , - 1 )
477
439
}()
478
440
}
@@ -481,15 +443,15 @@ func (l *Launcher) next() {
481
443
// doLaunch will safely handle the wait
482
444
// group and cleanly close down a worker
483
445
// and report back on the task result.
484
- func (l * Launcher ) doLaunch (tsk * Task ) {
485
- defer l .giveBackSlot ()
446
+ func (l * Launcher ) doLaunch (tsk * Task , ctx context. Context ) {
447
+ defer l .giveBackSlot (ctx )
486
448
487
449
var wCtx context.Context
488
450
var cncl context.CancelFunc
489
451
if l .completeTimeout > time .Duration (0 ) {
490
- wCtx , cncl = context .WithTimeout (l . stopCtx , l .completeTimeout )
452
+ wCtx , cncl = context .WithTimeout (ctx , l .completeTimeout )
491
453
} else {
492
- wCtx , cncl = context .WithCancel (l . stopCtx )
454
+ wCtx , cncl = context .WithCancel (ctx )
493
455
}
494
456
defer cncl () // clean up worker context
495
457
@@ -565,16 +527,16 @@ func (l *Launcher) sendTsk(tsk *Task) {
565
527
//
566
528
// will not give back the slot if the application is
567
529
// shutting down or processing the last task.
568
- func (l * Launcher ) giveBackSlot () {
569
- if l . stopCtx . Err () == nil && l . lastCtx .Err () == nil {
530
+ func (l * Launcher ) giveBackSlot (ctx context. Context ) {
531
+ if ctx .Err () == nil {
570
532
//atomic.AddInt64(&l.tasksRunning, -1)
571
533
l .slots <- 1
572
534
}
573
535
l .wg .Done ()
574
536
}
575
537
576
538
// log is the central point of operational logging.
577
- func (l * Launcher ) log (msg string ) {
539
+ func (l * Launcher ) log (msg interface {} ) {
578
540
l .lgr .Println (msg )
579
541
}
580
542
@@ -587,9 +549,5 @@ func (l *Launcher) log(msg string) {
587
549
func (l * Launcher ) Err () error {
588
550
l .mu .Lock ()
589
551
defer l .mu .Unlock ()
590
- if l .doneCtx .Err () != nil {
591
- return l .closeErr
592
- }
593
-
594
- return nil
552
+ return l .closeErr
595
553
}
0 commit comments