Skip to content

Commit

Permalink
Cancel the task instead of changing @running
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Mar 1, 2024
1 parent f3d097c commit 1908a6e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/concurrent-ruby/concurrent/timer_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ def timeout_interval=(value)
warn 'TimerTask timeouts are now ignored as these were not able to be implemented correctly'
end

def shutdown
@task.cancel if @task # must happen outside of synchronization
super
end

def kill
@task.cancel if @task # must happen outside of synchronization
super
end

private :post, :<<

private
Expand All @@ -270,6 +280,7 @@ def ns_initialize(opts, &task)
@run_now = opts[:now] || opts[:run_now]
@executor = Concurrent::SafeTaskExecutor.new(task)
@running = Concurrent::AtomicBoolean.new(false)
@task = nil
@value = nil

self.observers = Collection::CopyOnNotifyObserverSet.new
Expand All @@ -278,26 +289,24 @@ def ns_initialize(opts, &task)
# @!visibility private
def ns_shutdown_execution
@running.make_false
@running = Concurrent::AtomicBoolean.new(false)
super
end

# @!visibility private
def ns_kill_execution
@running.make_false
@running = Concurrent::AtomicBoolean.new(false)
super
end

# @!visibility private
def schedule_next_task(interval = execution_interval)
ScheduledTask.execute(interval, args: [Concurrent::Event.new, @running], &method(:execute_task))
@task = ScheduledTask.execute(interval, args: [Concurrent::Event.new], &method(:execute_task))
nil
end

# @!visibility private
def execute_task(completion, continue_running)
return nil unless continue_running.true?
def execute_task(completion)
return nil unless @running.true?
_success, value, reason = @executor.execute(self)
if completion.try?
self.value = value
Expand Down

0 comments on commit 1908a6e

Please sign in to comment.