Class: RocketJob::Worker
- Inherits:
-
Object
- Object
- RocketJob::Worker
- Includes:
- ActiveSupport::Callbacks, SemanticLogger::Loggable
- Defined in:
- lib/rocket_job/worker.rb
Overview
Worker
A worker runs on a single operating system thread Is usually started under a Rocket Job server process.
Defined Under Namespace
Classes: Shutdown
Instance Attribute Summary collapse
-
#current_filter ⇒ Object
Returns the value of attribute current_filter.
-
#id ⇒ Object
Returns the value of attribute id.
-
#inline ⇒ Object
readonly
Returns the value of attribute inline.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
- .after_running(*filters, &blk) ⇒ Object
- .around_running(*filters, &blk) ⇒ Object
- .before_running(*filters, &blk) ⇒ Object
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #backtrace ⇒ Object
-
#initialize(id: 0, server_name: 'inline:0', inline: false) ⇒ Worker
constructor
A new instance of Worker.
- #join(*args) ⇒ Object
-
#kill ⇒ Object
Send each active worker the RocketJob::ShutdownException so that stops processing immediately.
- #shutdown! ⇒ Object
- #shutdown? ⇒ Boolean
-
#wait_for_shutdown?(timeout = nil) ⇒ Boolean
Returns [true|false] whether the shutdown indicator was set.
Constructor Details
#initialize(id: 0, server_name: 'inline:0', inline: false) ⇒ Worker
Returns a new instance of Worker.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rocket_job/worker.rb', line 36 def initialize(id: 0, server_name: 'inline:0', inline: false) @id = id @server_name = server_name @shutdown = Concurrent::Event.new @name = "#{server_name}:#{id}" @re_check_start = Time.now @current_filter = Config.filter || {} @thread = Thread.new { run } unless inline @inline = inline end |
Instance Attribute Details
#current_filter ⇒ Object
Returns the value of attribute current_filter.
14 15 16 |
# File 'lib/rocket_job/worker.rb', line 14 def current_filter @current_filter end |
#id ⇒ Object
Returns the value of attribute id.
14 15 16 |
# File 'lib/rocket_job/worker.rb', line 14 def id @id end |
#inline ⇒ Object (readonly)
Returns the value of attribute inline.
15 16 17 |
# File 'lib/rocket_job/worker.rb', line 15 def inline @inline end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/rocket_job/worker.rb', line 15 def name @name end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
15 16 17 |
# File 'lib/rocket_job/worker.rb', line 15 def thread @thread end |
Class Method Details
.after_running(*filters, &blk) ⇒ Object
28 29 30 |
# File 'lib/rocket_job/worker.rb', line 28 def self.after_running(*filters, &blk) set_callback(:running, :after, *filters, &blk) end |
.around_running(*filters, &blk) ⇒ Object
32 33 34 |
# File 'lib/rocket_job/worker.rb', line 32 def self.around_running(*filters, &blk) set_callback(:running, :around, *filters, &blk) end |
.before_running(*filters, &blk) ⇒ Object
24 25 26 |
# File 'lib/rocket_job/worker.rb', line 24 def self.before_running(*filters, &blk) set_callback(:running, :before, *filters, &blk) end |
Instance Method Details
#alive? ⇒ Boolean
47 48 49 |
# File 'lib/rocket_job/worker.rb', line 47 def alive? inline ? true : @thread.alive? end |
#backtrace ⇒ Object
51 52 53 |
# File 'lib/rocket_job/worker.rb', line 51 def backtrace inline ? Thread.current.backtrace : @thread.backtrace end |
#join(*args) ⇒ Object
55 56 57 |
# File 'lib/rocket_job/worker.rb', line 55 def join(*args) @thread.join(*args) unless inline end |
#kill ⇒ Object
Send each active worker the RocketJob::ShutdownException so that stops processing immediately.
60 61 62 63 64 |
# File 'lib/rocket_job/worker.rb', line 60 def kill return true if inline @thread.raise(Shutdown, "Shutdown due to kill request for worker: #{name}") if @thread.alive? end |
#shutdown! ⇒ Object
70 71 72 |
# File 'lib/rocket_job/worker.rb', line 70 def shutdown! @shutdown.set end |
#shutdown? ⇒ Boolean
66 67 68 |
# File 'lib/rocket_job/worker.rb', line 66 def shutdown? @shutdown.set? end |
#wait_for_shutdown?(timeout = nil) ⇒ Boolean
Returns [true|false] whether the shutdown indicator was set
75 76 77 |
# File 'lib/rocket_job/worker.rb', line 75 def wait_for_shutdown?(timeout = nil) @shutdown.wait(timeout) end |