Class: RocketJob::ThreadWorker

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

Overview

ThreadWorker

A worker runs on a single operating system thread. Is usually started under a Rocket Job server process.

Instance Attribute Summary collapse

Attributes inherited from Worker

#current_filter, #id, #name, #server_name

Instance Method Summary collapse

Methods inherited from Worker

#add_to_current_filter, #find_and_assign_job, #next_available_job, #random_wait_interval, #reset_filter_if_expired, #run, #throttled_job?

Constructor Details

#initialize(id:, server_name:) ⇒ ThreadWorker

Returns a new instance of ThreadWorker.



10
11
12
13
14
# File 'lib/rocket_job/thread_worker.rb', line 10

def initialize(id:, server_name:)
  super(id: id, server_name: server_name)
  @shutdown = Concurrent::Event.new
  @thread   = Thread.new { run }
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



8
9
10
# File 'lib/rocket_job/thread_worker.rb', line 8

def thread
  @thread
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rocket_job/thread_worker.rb', line 16

def alive?
  @thread.alive?
end

#backtraceObject



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

def backtrace
  @thread.backtrace
end

#join(*args) ⇒ Object



24
25
26
# File 'lib/rocket_job/thread_worker.rb', line 24

def join(*args)
  @thread.join(*args)
end

#killObject

Send each active worker the RocketJob::ShutdownException so that stops processing immediately.



29
30
31
# File 'lib/rocket_job/thread_worker.rb', line 29

def kill
  @thread.raise(Shutdown, "Shutdown due to kill request for worker: #{name}") if @thread.alive?
end

#shutdown!Object



37
38
39
# File 'lib/rocket_job/thread_worker.rb', line 37

def shutdown!
  @shutdown.set
end

#shutdown?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rocket_job/thread_worker.rb', line 33

def shutdown?
  @shutdown.set?
end

#wait_for_shutdown?(timeout = nil) ⇒ Boolean

Returns [true|false] whether the shutdown indicator was set

Returns:

  • (Boolean)


42
43
44
# File 'lib/rocket_job/thread_worker.rb', line 42

def wait_for_shutdown?(timeout = nil)
  @shutdown.wait(timeout)
end