Module: Concurrent::Runnable
Defined Under Namespace
Classes: Context
Constant Summary collapse
- LifecycleError =
Class.new(StandardError)
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
Instance Method Details
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/concurrent/runnable.rb', line 44 def run mutex.synchronize do raise LifecycleError.new('already running') if @running raise LifecycleError.new('#on_task not implemented') unless self.respond_to?(:on_task, true) on_run if respond_to?(:on_run, true) @running = true end loop do break unless @running on_task break unless @running Thread.pass end after_run if respond_to?(:after_run, true) return true rescue LifecycleError => ex @running = false raise ex rescue => ex @running = false return false end |
#run!(abort_on_exception = false) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/concurrent/runnable.rb', line 34 def run!(abort_on_exception = false) raise LifecycleError.new('already running') if @running thread = Thread.new do Thread.current.abort_on_exception = abort_on_exception self.run end thread.join(0.1) # let the thread start return thread end |
#running? ⇒ Boolean
80 81 82 |
# File 'lib/concurrent/runnable.rb', line 80 def running? return @running == true end |
#stop ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/concurrent/runnable.rb', line 69 def stop return true unless @running mutex.synchronize do @running = false on_stop if respond_to?(:on_stop, true) end return true rescue => ex return false end |