-
Notifications
You must be signed in to change notification settings - Fork 419
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
Shutting down TimerTask is stopping already executing tasks #691
Comments
It seems like a bug if shutting down a TimerTask kills a timed execution that has already started. Seems like it should wait for completion, or there should be another way to say "shutdown_after_current_task_has_completed". |
Thank you, I'll have a look. |
The TimerTask seems to complete if you wait around long enough (not getting killed): require 'concurrent'
t = Concurrent::TimerTask.new(execution_interval: 1) do
puts "#{Time.now}: start task"
sleep 5
puts "#{Time.now}: finish task"
end
begin
t.execute
sleep
rescue Interrupt => e
puts "#{Time.now}: interrupt rescued, shutting down"
t.shutdown
sleep 5 # Waiting 'long enough' here
end
#=> 2019-01-24 15:54:16 -0800: start task
#=> 2019-01-24 15:54:21 -0800: finish task
#=> 2019-01-24 15:54:22 -0800: start task
#=> ^C2019-01-24 15:54:24 -0800: interrupt rescued, shutting down
#=> 2019-01-24 15:54:27 -0800: finish task
There is a method Unfortunately, it doesn't seem to be working for TimerTask (Task is still running even after wait_for_termination returns.) |
I would have expected |
This is my current workaround: module ShutdownConcurrentTasks
def self.all(tasks, timeout)
observers = tasks.map { |t| t.add_observer(ShutdownConcurrentTasks::Observer.new) }
stop_at = Time.now
Concurrent.global_timer_set.shutdown
tasks.each(&:shutdown)
timeout.times do
break if observers.all? { |o| o.task_run_since?(stop_at) }
sleep 1
end
end
class Observer
def initialize
@last_run = Time.now
end
def update(time, *args)
@last_run = time
end
def task_run_since?(time)
@last_run > time
end
end
end
tasks = build_some_tasks
begin
tasks.each(&:execute)
sleep
rescue Interrupt
ShutdownConcurrentTasks.all(tasks, 20)
ensure
puts "Shut down all tasks"
end That will give the tasks up to 20 seconds to finish their current run. By calling Concurrent::TimerTask do |task|
10.times do
break if !task.running?
puts 'still alive'
sleep 1
end
end |
TimerTask executes the task as a
I'm not sure if that's a mistake when it was introduced and instead it should have the argument So as it is today, to safely shutdown, you'll want to shutdown and wait on I think this could also imply:
|
I'm not sure who is maintaining this currently but we should probably try to come to some resolution here. |
I'm planning to make a PR. There are maintainers, but fairly quiet. |
I'll be watching and will help get maintenance going on this project again. |
Not to get too offtopic, but I think this project is incredibly important and I'm personally invested in contributing because of GoodJob (and I'm interested in becoming a formal maintainer too). My colleague |
It's documented in the README: https://github.com/ruby-concurrency/concurrent-ruby/blob/master/README.md#maintainers |
Setting |
@headius Would you like to help maintain this project and e.g. reviews PRs? I would happily add you to the list of active maintainers at https://github.com/ruby-concurrency/concurrent-ruby/blob/master/README.md#maintainers (or you can do it yourself if you like). You already have the rights anyway. I should have asked you earlier when we were searching new maintainers, I missed it somehow, sorry about that. |
Given your many PRs and interest I would be delighted to add you as maintainer, I'll send the GitHub invite now. |
Shutting down a timer task is not waiting for already executing tasks to finish. I talked to @headius and he encouraged me to open an issue since it is not clear if this is a feature or bug.
Example code:
The text was updated successfully, but these errors were encountered: