Class: Shikibu::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/shikibu/worker.rb

Overview

Background worker for workflow processing

Constant Summary collapse

WORKFLOW_POLL_INTERVAL =
1
TIMER_CHECK_INTERVAL =
10
STALE_LOCK_INTERVAL =
60
MESSAGE_CHECK_INTERVAL =
5
MESSAGE_CLEANUP_INTERVAL =

1 hour

3600
DEFAULT_MESSAGE_RETENTION_DAYS =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Worker

Returns a new instance of Worker.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/shikibu/worker.rb', line 15

def initialize(app)
  @app = app
  @storage = app.storage
  @worker_id = app.worker_id
  @running = false
  @threads = []
  @is_leader = false
  @leader_tasks = []

  # Wake events for NOTIFY integration
  @resume_wake_event = Notify::WakeEvent.new
  @message_wake_event = Notify::WakeEvent.new
  @outbox_wake_event = Notify::WakeEvent.new
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



13
14
15
# File 'lib/shikibu/worker.rb', line 13

def app
  @app
end

#message_wake_eventObject (readonly)

Returns the value of attribute message_wake_event.



13
14
15
# File 'lib/shikibu/worker.rb', line 13

def message_wake_event
  @message_wake_event
end

#outbox_wake_eventObject (readonly)

Returns the value of attribute outbox_wake_event.



13
14
15
# File 'lib/shikibu/worker.rb', line 13

def outbox_wake_event
  @outbox_wake_event
end

#resume_wake_eventObject (readonly)

Returns the value of attribute resume_wake_event.



13
14
15
# File 'lib/shikibu/worker.rb', line 13

def resume_wake_event
  @resume_wake_event
end

#storageObject (readonly)

Returns the value of attribute storage.



13
14
15
# File 'lib/shikibu/worker.rb', line 13

def storage
  @storage
end

#worker_idObject (readonly)

Returns the value of attribute worker_id.



13
14
15
# File 'lib/shikibu/worker.rb', line 13

def worker_id
  @worker_id
end

Instance Method Details

#leader?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/shikibu/worker.rb', line 54

def leader?
  @is_leader
end

#running?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/shikibu/worker.rb', line 50

def running?
  @running
end

#startObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shikibu/worker.rb', line 30

def start
  return if @running

  @running = true

  # All workers run these
  @threads << Thread.new { run_workflow_resumption }
  @threads << Thread.new { run_message_delivery }

  # Leader election and leader-only tasks
  @threads << Thread.new { run_leader_election }
end

#stopObject



43
44
45
46
47
48
# File 'lib/shikibu/worker.rb', line 43

def stop
  @running = false
  stop_leader_tasks
  @threads.each { |t| t.join(5) }
  @threads.clear
end