Module: Concurrent::Runnable

Included in:
Actor
Defined in:
lib/concurrent/runnable.rb

Defined Under Namespace

Classes: Context

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/concurrent/runnable.rb', line 21

def self.included(base)

  class << base

    def run!(*args, &block)
      runner = self.new(*args, &block)
      return Context.new(runner)
    rescue => ex
      return nil
    end
  end
end

Instance Method Details

#runObject



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

Raises:



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

Returns:

  • (Boolean)


80
81
82
# File 'lib/concurrent/runnable.rb', line 80

def running?
  return @running == true
end

#stopObject



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