Skip to content

Commit

Permalink
Document new lifecycle hooks for dispatcher and scheduler
Browse files Browse the repository at this point in the history
And reduce a bit the implementation.
  • Loading branch information
rosa committed Jan 28, 2025
1 parent 2bc05ed commit 378107c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ In Solid queue, you can hook into two different points in the supervisor's life:
- `start`: after the supervisor has finished booting and right before it forks workers and dispatchers.
- `stop`: after receiving a signal (`TERM`, `INT` or `QUIT`) and right before starting graceful or immediate shutdown.

And into two different points in a worker's life:
- `worker_start`: after the worker has finished booting and right before it starts the polling loop.
- `worker_stop`: after receiving a signal (`TERM`, `INT` or `QUIT`) and right before starting graceful or immediate shutdown (which is just `exit!`).
And into two different points in the worker's, dispatcher's and scheduler's life:
- `(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.
- `(worker|dispatcher|scheduler)_stop`: after receiving a signal (`TERM`, `INT` or `QUIT`) and right before starting graceful or immediate shutdown (which is just `exit!`).

You can use the following methods with a block to do this:
```ruby
Expand All @@ -386,6 +386,12 @@ SolidQueue.on_stop
SolidQueue.on_worker_start
SolidQueue.on_worker_stop
SolidQueue.on_dispatcher_start
SolidQueue.on_dispatcher_stop
SolidQueue.on_scheduler_start
SolidQueue.on_scheduler_stop
```

For example:
Expand Down
30 changes: 8 additions & 22 deletions lib/solid_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,14 @@ module SolidQueue

delegate :on_start, :on_stop, to: Supervisor

def on_worker_start(...)
Worker.on_start(...)
end

def on_worker_stop(...)
Worker.on_stop(...)
end

def on_dispatcher_start(...)
Dispatcher.on_start(...)
end

def on_dispatcher_stop(...)
Dispatcher.on_stop(...)
end

def on_scheduler_start(...)
Scheduler.on_start(...)
end

def on_scheduler_stop(...)
Scheduler.on_stop(...)
# on_worker_start, on_worker_stop, on_dispatcher_start ...
%w[ worker dispatcher scheduler ].each do |process|
%w[ start stop ].each do |action|
define_method("on_#{process}_#{action}") do |&block|
# Worker.on_start(&block), Dispatcher.on_stop(&block)
const_get(process.capitalize).public_send("on_#{action}", &block)
end
end
end

def supervisor?
Expand Down

0 comments on commit 378107c

Please sign in to comment.