Class: Kward::PluginTask
- Inherits:
-
Object
- Object
- Kward::PluginTask
- Defined in:
- lib/kward/plugins/resources.rb
Overview
Cooperative background task owned by a plugin runtime.
Instance Attribute Summary collapse
-
#cancellation ⇒ Object
readonly
Returns the value of attribute cancellation.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #cancel! ⇒ Object
- #cancelled? ⇒ Boolean
- #complete? ⇒ Boolean
- #error ⇒ Object
-
#initialize(name:, cancellation:, &work) ⇒ PluginTask
constructor
A new instance of PluginTask.
- #join(timeout = nil) ⇒ Object
- #result ⇒ Object
- #start(&finished) ⇒ Object
Constructor Details
#initialize(name:, cancellation:, &work) ⇒ PluginTask
Returns a new instance of PluginTask.
42 43 44 45 46 47 48 49 50 |
# File 'lib/kward/plugins/resources.rb', line 42 def initialize(name:, cancellation:, &work) @name = name.to_s.freeze @cancellation = cancellation @work = work @mutex = Mutex.new @thread = nil @result = nil @error = nil end |
Instance Attribute Details
#cancellation ⇒ Object (readonly)
Returns the value of attribute cancellation.
40 41 42 |
# File 'lib/kward/plugins/resources.rb', line 40 def cancellation @cancellation end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
40 41 42 |
# File 'lib/kward/plugins/resources.rb', line 40 def name @name end |
Instance Method Details
#alive? ⇒ Boolean
83 84 85 86 |
# File 'lib/kward/plugins/resources.rb', line 83 def alive? thread = @mutex.synchronize { @thread } thread&.alive? == true end |
#cancel! ⇒ Object
74 75 76 77 |
# File 'lib/kward/plugins/resources.rb', line 74 def cancel! cancellation.cancel! self end |
#cancelled? ⇒ Boolean
79 80 81 |
# File 'lib/kward/plugins/resources.rb', line 79 def cancelled? cancellation.cancelled? end |
#complete? ⇒ Boolean
88 89 90 |
# File 'lib/kward/plugins/resources.rb', line 88 def complete? !alive? end |
#error ⇒ Object
103 104 105 |
# File 'lib/kward/plugins/resources.rb', line 103 def error @mutex.synchronize { @error } end |
#join(timeout = nil) ⇒ Object
92 93 94 95 96 |
# File 'lib/kward/plugins/resources.rb', line 92 def join(timeout = nil) thread = @mutex.synchronize { @thread } thread&.join(timeout) self end |
#result ⇒ Object
98 99 100 101 |
# File 'lib/kward/plugins/resources.rb', line 98 def result join @mutex.synchronize { @result } end |
#start(&finished) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kward/plugins/resources.rb', line 52 def start(&finished) @mutex.synchronize do raise "Plugin task #{name.inspect} has already started" if @thread @thread = Thread.new do begin cancellation.raise_if_cancelled! result = @work.arity.zero? ? @work.call : @work.call(cancellation) @mutex.synchronize { @result = result } rescue Cancellation::CancelledError nil rescue StandardError => e @mutex.synchronize { @error = e } ensure finished&.call(self) end end @thread.report_on_exception = false end self end |