Class: Scales::Worker::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/scales-worker/status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port = nil) ⇒ Status

Returns a new instance of Status.



7
8
9
10
11
12
13
14
# File 'lib/scales-worker/status.rb', line 7

def initialize address, port = nil
  @id             = SecureRandom.hex(8)
  @key            = "scales_worker_#{@id}"
  @address, @port = address.to_s, port.to_s
  @log_path       = "log/scales_worker.#{@id}.log"
  @logger         = Logger.new(Scales.env == "test" ? STDOUT : @log_path)
  @redis          = Scales::Storage::Sync.new_connection!
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



5
6
7
# File 'lib/scales-worker/status.rb', line 5

def address
  @address
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/scales-worker/status.rb', line 5

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/scales-worker/status.rb', line 5

def key
  @key
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



5
6
7
# File 'lib/scales-worker/status.rb', line 5

def log_path
  @log_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/scales-worker/status.rb', line 5

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/scales-worker/status.rb', line 5

def port
  @port
end

Instance Method Details

#put_response_in_queue!(response) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/scales-worker/status.rb', line 59

def put_response_in_queue!(response)
  data  = {
    :id         => response[1]['scales.id'],
    :worker_id  => @id,
    :type       => "worker_put_response_in_queue",
    :path       => response[1]['PATH_INFO'],
    :method     => response[1]['REQUEST_METHOD'],
    :status     => response[0]
  }
  json = JSON.generate(data)
  @redis.publish("scales_monitor_events", json)
end

#start!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scales-worker/status.rb', line 16

def start!
  data  = {
    :id         => @id,
    :key        => @key,
    :type       => "worker_started",
    :spawned_at => Time.now.to_i,
    :env        => Scales.env,
    :ip         => @address,
    :port       => @port
  }
  json = JSON.generate(data)
  
  @redis.set(@key, json)
  @redis.publish("scales_monitor_events", json)
  @already_stopped = false
end

#stop!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scales-worker/status.rb', line 33

def stop!
  return if @already_stopped
  
  data  = {
    :id         => @id,
    :key        => @key,
    :type       => "worker_stopped"
  }
  json = JSON.generate(data)
  @redis.del(@key)
  @redis.publish("scales_monitor_events", json)
  @already_stopped = true
end

#took_request_from_queue!(job) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/scales-worker/status.rb', line 47

def took_request_from_queue!(job)
  data  = {
    :id         => job['scales.id'],
    :worker_id  => @id,
    :type       => "worker_took_request_from_queue",
    :path       => job['PATH_INFO'],
    :method     => job['REQUEST_METHOD']
  }
  json = JSON.generate(data)
  @redis.publish("scales_monitor_events", json)
end