Skip to content

Commit 378107c

Browse files
committed
Document new lifecycle hooks for dispatcher and scheduler
And reduce a bit the implementation.
1 parent 2bc05ed commit 378107c

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ In Solid queue, you can hook into two different points in the supervisor's life:
375375
- `start`: after the supervisor has finished booting and right before it forks workers and dispatchers.
376376
- `stop`: after receiving a signal (`TERM`, `INT` or `QUIT`) and right before starting graceful or immediate shutdown.
377377

378-
And into two different points in a worker's life:
379-
- `worker_start`: after the worker has finished booting and right before it starts the polling loop.
380-
- `worker_stop`: after receiving a signal (`TERM`, `INT` or `QUIT`) and right before starting graceful or immediate shutdown (which is just `exit!`).
378+
And into two different points in the worker's, dispatcher's and scheduler's life:
379+
- `(worker|dispatcher|scheduler)_start`: after the worker/dispatcher/scheduler has finished booting and right before it starts the polling loop or loading the recurring schedule.
380+
- `(worker|dispatcher|scheduler)_stop`: after receiving a signal (`TERM`, `INT` or `QUIT`) and right before starting graceful or immediate shutdown (which is just `exit!`).
381381

382382
You can use the following methods with a block to do this:
383383
```ruby
@@ -386,6 +386,12 @@ SolidQueue.on_stop
386386
387387
SolidQueue.on_worker_start
388388
SolidQueue.on_worker_stop
389+
390+
SolidQueue.on_dispatcher_start
391+
SolidQueue.on_dispatcher_stop
392+
393+
SolidQueue.on_scheduler_start
394+
SolidQueue.on_scheduler_stop
389395
```
390396

391397
For example:

lib/solid_queue.rb

+8-22
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,14 @@ module SolidQueue
4343

4444
delegate :on_start, :on_stop, to: Supervisor
4545

46-
def on_worker_start(...)
47-
Worker.on_start(...)
48-
end
49-
50-
def on_worker_stop(...)
51-
Worker.on_stop(...)
52-
end
53-
54-
def on_dispatcher_start(...)
55-
Dispatcher.on_start(...)
56-
end
57-
58-
def on_dispatcher_stop(...)
59-
Dispatcher.on_stop(...)
60-
end
61-
62-
def on_scheduler_start(...)
63-
Scheduler.on_start(...)
64-
end
65-
66-
def on_scheduler_stop(...)
67-
Scheduler.on_stop(...)
46+
# on_worker_start, on_worker_stop, on_dispatcher_start ...
47+
%w[ worker dispatcher scheduler ].each do |process|
48+
%w[ start stop ].each do |action|
49+
define_method("on_#{process}_#{action}") do |&block|
50+
# Worker.on_start(&block), Dispatcher.on_stop(&block)
51+
const_get(process.capitalize).public_send("on_#{action}", &block)
52+
end
53+
end
6854
end
6955

7056
def supervisor?

0 commit comments

Comments
 (0)