Class: ProcessBalancer::Private::Cancellation
- Inherits:
-
Object
- Object
- ProcessBalancer::Private::Cancellation
- Defined in:
- lib/process_balancer/private/cancellation.rb
Overview
The Cancellation abstraction provides cooperative cancellation.
The standard methods ‘Thread#raise` of `Thread#kill` available in Ruby are very dangerous (see linked the blog posts bellow). Therefore concurrent-ruby provides an alternative.
-
<jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/>
-
<www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/>
-
<blog.headius.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html>
It provides an object which represents a task which can be executed, the task has to get the reference to the object and periodically cooperatively check that it is not cancelled. Good practices to make tasks cancellable:
-
check cancellation every cycle of a loop which does significant work,
-
do all blocking actions in a loop with a timeout then on timeout check cancellation and if ok block again with the timeout
The idea was inspired by <msdn.microsoft.com/en-us/library/dd537607(v=vs.110).aspx>
Class Method Summary collapse
-
.timeout(intended_time) ⇒ Cancellation
Create Cancellation which will cancel itself in given time.
Instance Method Summary collapse
-
#canceled? ⇒ true, false
Is the cancellation cancelled? Respective, was the origin of the cancellation resolved.
-
#check!(error = CancelledOperationError) ⇒ self
Raise error when cancelled.
-
#initialize(origin = Promises.resolvable_event) ⇒ Cancellation
constructor
Creates the cancellation object.
-
#join(*cancellations) ⇒ Cancellation
Creates a new Cancellation which is cancelled when first of the supplied cancellations or self is cancelled.
-
#origin ⇒ Promises::Future, Promises::Event
The event or future which is the origin of the cancellation.
-
#to_ary ⇒ Array(Cancellation, Promises::Future), Array(Cancellation, Promises::Event)
Allow to multi-assign the Cancellation object.
-
#to_s ⇒ String
(also: #inspect)
Short string representation.
Constructor Details
#initialize(origin = Promises.resolvable_event) ⇒ Cancellation
Creates the cancellation object.
62 63 64 65 |
# File 'lib/process_balancer/private/cancellation.rb', line 62 def initialize(origin = Promises.resolvable_event) super() @Origin = origin end |
Class Method Details
.timeout(intended_time) ⇒ Cancellation
Create Cancellation which will cancel itself in given time
51 52 53 |
# File 'lib/process_balancer/private/cancellation.rb', line 51 def self.timeout(intended_time) new Concurrent::Promises.schedule(intended_time) end |
Instance Method Details
#canceled? ⇒ true, false
Is the cancellation cancelled? Respective, was the origin of the cancellation resolved.
85 86 87 |
# File 'lib/process_balancer/private/cancellation.rb', line 85 def canceled? @Origin.resolved? end |
#check!(error = CancelledOperationError) ⇒ self
Raise error when cancelled
93 94 95 96 |
# File 'lib/process_balancer/private/cancellation.rb', line 93 def check!(error = CancelledOperationError) raise error if canceled? self end |
#join(*cancellations) ⇒ Cancellation
Creates a new Cancellation which is cancelled when first of the supplied cancellations or self is cancelled.
103 104 105 |
# File 'lib/process_balancer/private/cancellation.rb', line 103 def join(*cancellations) Cancellation.new Promises.any_event(*[@Origin, *cancellations.map(&:origin)]) end |
#origin ⇒ Promises::Future, Promises::Event
The event or future which is the origin of the cancellation
78 79 80 |
# File 'lib/process_balancer/private/cancellation.rb', line 78 def origin @Origin end |
#to_ary ⇒ Array(Cancellation, Promises::Future), Array(Cancellation, Promises::Event)
Allow to multi-assign the Cancellation object
72 73 74 |
# File 'lib/process_balancer/private/cancellation.rb', line 72 def to_ary [self, @Origin] end |
#to_s ⇒ String Also known as: inspect
Short string representation.
109 110 111 |
# File 'lib/process_balancer/private/cancellation.rb', line 109 def to_s format '%s %s>', super[0..-2], canceled? ? 'canceled' : 'pending' end |