Class: Ductwork::Processes::JobWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/ductwork/processes/job_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline, id) ⇒ JobWorker

Returns a new instance of JobWorker.



8
9
10
11
12
13
14
# File 'lib/ductwork/processes/job_worker.rb', line 8

def initialize(pipeline, id)
  @pipeline = pipeline
  @id = id
  @running_context = nil
  @thread = nil
  @last_heartbeat_at = Time.current
end

Instance Attribute Details

#executionObject (readonly)

Returns the value of attribute execution.



6
7
8
# File 'lib/ductwork/processes/job_worker.rb', line 6

def execution
  @execution
end

#last_heartbeat_atObject (readonly)

Returns the value of attribute last_heartbeat_at.



6
7
8
# File 'lib/ductwork/processes/job_worker.rb', line 6

def last_heartbeat_at
  @last_heartbeat_at
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



6
7
8
# File 'lib/ductwork/processes/job_worker.rb', line 6

def pipeline
  @pipeline
end

#threadObject (readonly)

Returns the value of attribute thread.



6
7
8
# File 'lib/ductwork/processes/job_worker.rb', line 6

def thread
  @thread
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ductwork/processes/job_worker.rb', line 34

def alive?
  thread&.alive? || false
end

#join(limit) ⇒ Object



51
52
53
# File 'lib/ductwork/processes/job_worker.rb', line 51

def join(limit)
  thread&.join(limit)
end

#killObject



46
47
48
49
# File 'lib/ductwork/processes/job_worker.rb', line 46

def kill
  stop
  thread&.kill
end

#nameObject



55
56
57
# File 'lib/ductwork/processes/job_worker.rb', line 55

def name
  "ductwork.job_worker.#{pipeline}.#{id}"
end

#restartObject



27
28
29
30
31
32
# File 'lib/ductwork/processes/job_worker.rb', line 27

def restart
  return false if alive?

  cleanup_dead_thread!
  start
end

#startObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/ductwork/processes/job_worker.rb', line 16

def start
  return false if alive?

  @running_context = Ductwork::RunningContext.new
  @last_heartbeat_at = Time.current
  @thread = Thread.new { work_loop }
  @thread.name = name

  true
end

#stopObject



42
43
44
# File 'lib/ductwork/processes/job_worker.rb', line 42

def stop
  running_context&.shutdown!
end

#stuck?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ductwork/processes/job_worker.rb', line 38

def stuck?
  execution.nil? && last_heartbeat_too_long_ago?
end