Class: RocketJob::Worker

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: 0, server_name: 'inline:0', inline: false, re_check_seconds: Config.instance.re_check_seconds, filter: nil) ⇒ Worker

Returns a new instance of Worker.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rocket_job/worker.rb', line 36

def initialize(id: 0,
               server_name: 'inline:0',
               inline: false,
               re_check_seconds: Config.instance.re_check_seconds,
               filter: nil)
  @id               = id
  @server_name      = server_name
  @shutdown         = Concurrent::Event.new
  @name             = "#{server_name}:#{id}"
  @re_check_seconds = (re_check_seconds || 60).to_f
  @re_check_start   = Time.now
  @filter           = filter.nil? ? {} : filter.dup
  @current_filter   = @filter.dup
  @thread           = Thread.new { run } unless inline
  @inline           = inline
end

Instance Attribute Details

#current_filterObject

Returns the value of attribute current_filter.



14
15
16
# File 'lib/rocket_job/worker.rb', line 14

def current_filter
  @current_filter
end

#filterObject

Returns the value of attribute filter.



14
15
16
# File 'lib/rocket_job/worker.rb', line 14

def filter
  @filter
end

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/rocket_job/worker.rb', line 14

def id
  @id
end

#inlineObject (readonly)

Returns the value of attribute inline.



15
16
17
# File 'lib/rocket_job/worker.rb', line 15

def inline
  @inline
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/rocket_job/worker.rb', line 15

def name
  @name
end

#re_check_secondsObject

Returns the value of attribute re_check_seconds.



14
15
16
# File 'lib/rocket_job/worker.rb', line 14

def re_check_seconds
  @re_check_seconds
end

#threadObject (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

Returns:

  • (Boolean)


53
54
55
# File 'lib/rocket_job/worker.rb', line 53

def alive?
  inline ? true : @thread.alive?
end

#backtraceObject



57
58
59
# File 'lib/rocket_job/worker.rb', line 57

def backtrace
  inline ? Thread.current.backtrace : @thread.backtrace
end

#join(*args) ⇒ Object



61
62
63
# File 'lib/rocket_job/worker.rb', line 61

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

#killObject

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



66
67
68
69
70
# File 'lib/rocket_job/worker.rb', line 66

def kill
  return true if inline

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

#shutdown!Object



76
77
78
# File 'lib/rocket_job/worker.rb', line 76

def shutdown!
  @shutdown.set
end

#shutdown?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/rocket_job/worker.rb', line 72

def shutdown?
  @shutdown.set?
end

#wait_for_shutdown?(timeout = nil) ⇒ Boolean

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

Returns:

  • (Boolean)


81
82
83
# File 'lib/rocket_job/worker.rb', line 81

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