Skip to content

shutdown outside of the handler #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions example-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ To run:
bundle
bundle exec clock
```

To test invocation of existing signal handlers, put this code at the very top of exe/clock:

```ruby
Signal.trap('INT') do
puts "This is a well-behaving INT handler from outside of ruby-clock"
exit
end
```
1 change: 1 addition & 0 deletions exe/clock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'ruby-clock'
require "ruby-clock/dsl"
RubyClock.detect_and_load_rails_app
require 'rufus_monkeypatch'
RubyClock.instance.listen_for_shutdown
RubyClock.instance.listen_to_signals
RubyClock.instance.prepare_rake
RubyClock.instance.schedule.pause
Expand Down
23 changes: 19 additions & 4 deletions lib/ruby-clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RubyClock
include RubyClock::AroundActions

attr_accessor :on_error, :around_trigger_code_location
attr_accessor :should_shutdown, :old_shutdown_handler

def initialize
set_up_around_actions
Expand All @@ -24,6 +25,22 @@ def wait_seconds
ENV['RUBY_CLOCK_SHUTDOWN_WAIT_SECONDS']&.to_i || 29
end

def listen_for_shutdown
Thread.new do
loop do
sleep 1
if should_shutdown
shutdown
if old_shutdown_handler
old_shutdown_handler.call
else
exit
end
end
end
end
end

def shutdown
puts "Shutting down ruby-clock. Waiting #{wait_seconds} seconds for jobs to finish..."
schedule.shutdown(wait: wait_seconds)
Expand All @@ -34,12 +51,10 @@ def listen_to_signals
signals = %w[INT TERM]
signals.each do |signal|
old_handler = Signal.trap(signal) do
shutdown
if old_handler.respond_to?(:call)
old_handler.call
else
exit
self.old_shutdown_handler = old_handler
end
self.should_shutdown = true
end
end
puts "RUBY_CLOCK_SHUTDOWN_WAIT_SECONDS is set to #{wait_seconds}"
Expand Down