Class: RocketJob::RactorWorker

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

Overview

Run each worker in its own “Ractor”.

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:) ⇒ RactorWorker

Returns a new instance of RactorWorker.



6
7
8
9
10
# File 'lib/rocket_job/ractor_worker.rb', line 6

def initialize(id:, server_name:)
  super(id: id, server_name: server_name)
  @shutdown = Concurrent::Event.new
  @thread   = Ractor.new(name: "rocketjob-#{id}") { run }
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/rocket_job/ractor_worker.rb', line 4

def thread
  @thread
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/rocket_job/ractor_worker.rb', line 12

def alive?
  @thread.alive?
end

#backtraceObject



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

def backtrace
  @thread.backtrace
end

#join(*args) ⇒ Object



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

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

#killObject

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



25
26
27
# File 'lib/rocket_job/ractor_worker.rb', line 25

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

#shutdown!Object



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

def shutdown!
  @shutdown.set
end

#shutdown?Boolean

Returns:

  • (Boolean)


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

def shutdown?
  @shutdown.set?
end

#wait_for_shutdown?(timeout = nil) ⇒ Boolean

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

Returns:

  • (Boolean)


38
39
40
# File 'lib/rocket_job/ractor_worker.rb', line 38

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