Class: SoarThreadWorker::ThreadWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/soar_thread_worker/thread_worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_handler: nil) ⇒ ThreadWorker

Returns a new instance of ThreadWorker.



14
15
16
17
18
# File 'lib/soar_thread_worker/thread_worker.rb', line 14

def initialize(event_handler: nil)
  @api_mutex = Mutex.new
  @stopping = false
  @event_handler = event_handler
end

Instance Attribute Details

#event_handlerObject

Returns the value of attribute event_handler.



3
4
5
# File 'lib/soar_thread_worker/thread_worker.rb', line 3

def event_handler
  @event_handler
end

#threadObject

Returns the value of attribute thread.



4
5
6
# File 'lib/soar_thread_worker/thread_worker.rb', line 4

def thread
  @thread
end

Class Method Details

.create_worker(event_handler) ⇒ Object



6
7
8
# File 'lib/soar_thread_worker/thread_worker.rb', line 6

def self.create_worker(event_handler)
  ThreadWorker.new(event_handler)
end

.error(message) ⇒ Object



10
11
12
# File 'lib/soar_thread_worker/thread_worker.rb', line 10

def self.error(message)
  $stderr.puts "ERROR [ThreadWorker] #{message}"
end

Instance Method Details

#executeObject



44
45
46
47
48
49
# File 'lib/soar_thread_worker/thread_worker.rb', line 44

def execute
  #Inversion of control: override this method to do the work you need.
  #Return true if after execution the thread should stop, false if it
  #should continue running
  false
end

#running?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/soar_thread_worker/thread_worker.rb', line 20

def running?
  (not @thread.nil?) and @thread.alive?
end

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/soar_thread_worker/thread_worker.rb', line 24

def start
  @api_mutex.synchronize {
    begin
      return if (running?)
      @stopping = false
      create_thread()
    rescue Exception => e
      ThreadWorker::error("Exception #{e} in start")
      raise
    end
  }
end

#stop(immediate: false) ⇒ Object



37
38
39
40
41
42
# File 'lib/soar_thread_worker/thread_worker.rb', line 37

def stop(immediate: false)
  @api_mutex.synchronize {
    @stopping = true
    @thread.kill if (not @thread.nil?) and immediate
  }
end