Class: Postjob::WorkerSession

Inherits:
Record
  • Object
show all
Defined in:
lib/postjob/worker_session.rb

Overview

A worker worker_session

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#initialize

Constructor Details

This class inherits a constructor from Postjob::Record

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



53
54
55
# File 'lib/postjob/worker_session.rb', line 53

def attributes
  @attributes
end

#client_socketObject (readonly)

Returns the value of attribute client_socket.



51
52
53
# File 'lib/postjob/worker_session.rb', line 51

def client_socket
  @client_socket
end

#created_atObject (readonly)

Returns the value of attribute created_at.



54
55
56
# File 'lib/postjob/worker_session.rb', line 54

def created_at
  @created_at
end

#host_idObject (readonly)

Returns the value of attribute host_id.



50
51
52
# File 'lib/postjob/worker_session.rb', line 50

def host_id
  @host_id
end

#idObject (readonly)

Returns the value of attribute id.



49
50
51
# File 'lib/postjob/worker_session.rb', line 49

def id
  @id
end

#queuesObject (readonly)

Returns the value of attribute queues.



55
56
57
# File 'lib/postjob/worker_session.rb', line 55

def queues
  @queues
end

#workflowsObject (readonly)

Returns the value of attribute workflows.



52
53
54
# File 'lib/postjob/worker_session.rb', line 52

def workflows
  @workflows
end

Class Method Details

.start!(workflows_with_versions, heartbeat: true, queues:) ⇒ Object

Starts a worker session.



11
12
13
14
15
16
17
18
19
# File 'lib/postjob/worker_session.rb', line 11

def start!(workflows_with_versions, heartbeat: true, queues:)
  host_id = ::Postjob.host_id
  worker_session = ::Postjob::Queue.worker_session_start(workflows_with_versions, host_id: host_id, queues: queues)

  Postjob.logger.info "Starting worker_session #{worker_session.inspect}, on pid #{$$}"

  start_heartbeat_monitor(host_id) if heartbeat
  worker_session
end

.start_heartbeat_monitor(host_id) ⇒ Object

Starts a heartbeat monitor in the background (i.e. in a new thread).



26
27
28
29
30
31
32
33
34
35
# File 'lib/postjob/worker_session.rb', line 26

def start_heartbeat_monitor(host_id)
  Thread.new do
    begin
      Simple::SQL.connect!
      run_heartbeat_monitor(host_id)
    rescue Exception => e
      STDERR.puts "#{e}, from \n\t#{e.backtrace[0, 5].join("\n\t")}"
    end
  end
end

.stop!(worker_session) ⇒ Object



21
22
23
# File 'lib/postjob/worker_session.rb', line 21

def stop!(worker_session)
  ::Postjob::Queue.worker_session_stop(worker_session.id)
end

Instance Method Details

#inspectObject



61
62
63
64
65
66
67
# File 'lib/postjob/worker_session.rb', line 61

def inspect
  # [TODO] - grepping workflows by /\d$/ to only count workflows with a version number is a bit hackish.
  workflow_count = self.workflows.grep(/\d$/)
  queues         = self.queues.map(&:inspect).join(", ")

  "<Session##{id} w/host_id: #{host_id}, queues: #{queues}, client_socket: #{client_socket}, #{workflow_count} workflows>"
end

#to_sObject



57
58
59
# File 'lib/postjob/worker_session.rb', line 57

def to_s
  "Session##{id}"
end