Class: JobDispatch::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/job_dispatch/status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connect_address) ⇒ Status

Returns a new instance of Status.



9
10
11
# File 'lib/job_dispatch/status.rb', line 9

def initialize(connect_address)
  @connect_address = connect_address
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



6
7
8
# File 'lib/job_dispatch/status.rb', line 6

def socket
  @socket
end

Instance Method Details

#connectObject



13
14
15
16
17
18
# File 'lib/job_dispatch/status.rb', line 13

def connect
  if @socket.nil?
    @socket = JobDispatch.context.socket(ZMQ::REQ)
    @socket.connect(@connect_address)
  end
end

#disconnectObject



20
21
22
23
# File 'lib/job_dispatch/status.rb', line 20

def disconnect
  @socket.close
  @socket = nil
end

#fetchObject



25
26
27
28
29
30
# File 'lib/job_dispatch/status.rb', line 25

def fetch
  @socket.send(JSON.dump({command:'status'}))
  json = @socket.recv
  @status = JSON.parse(json).with_indifferent_access
  @time = Time.now
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/job_dispatch/status.rb', line 32

def print
  puts "Job Dispatcher status: #{@status[:status]} at #{@time}"
  puts ""

  table = Text::Table.new
  table.head = ['Queue', 'Worker ID', 'Worker Name', 'Status', 'Job ID', 'Job Details']
  table.rows = []

  @status[:queues].each do |queue, workers|
    if workers.empty?
      table.rows << [
          queue,
          '- no workers -',
          '',
          '',
          '',
          '',
      ]
    else
      workers.each_pair do |worker_id, worker_status|

        job = worker_status[:job]
        if job
          params_str = if job[:parameters]
                         job[:parameters].map(&:inspect).join(',')[0..20]
                       else
                         ''
                       end
          job_details = "#{job[:target]}.#{job[:method]}(#{params_str})"
        end

        table.rows << [
            queue,
            worker_id,
            worker_status[:name],
            worker_status[:status],
            worker_status[:job_id],
            job_details,
        ]
      end
    end
  end

  puts table.to_s
end