Class: Dumper::Agent
Constant Summary
collapse
- API_VERSION =
1
- MAX_FILESIZE =
2147483648
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#log, #log_last_error, #logger, #stdout_logger
Constructor Details
#initialize(options = {}) ⇒ Agent
Returns a new instance of Agent.
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/dumper/agent.rb', line 31
def initialize(options = {})
log 'app_key is missing' if options[:app_key].blank?
@stack = Dumper::Stack.new(options)
@api_base = options[:api_base] || 'https://dumper.io'
@app_key = options[:app_key]
@app_env = @stack.rails_env
@app_name = ObjectSpace.each_object(Rails::Application).first.class.name.split("::").first
logger.level = stdout_logger.level = options[:loglevel] if options[:loglevel]
end
|
Instance Attribute Details
#max_filesize ⇒ Object
Returns the value of attribute max_filesize.
11
12
13
|
# File 'lib/dumper/agent.rb', line 11
def max_filesize
@max_filesize
end
|
#stack ⇒ Object
Returns the value of attribute stack.
11
12
13
|
# File 'lib/dumper/agent.rb', line 11
def stack
@stack
end
|
Class Method Details
.start(options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/dumper/agent.rb', line 14
def start(options = {})
if defined?(Rails::Railtie)
ActiveSupport.on_load :after_initialize do
Dumper::Agent.new(options).start
end
else
new(options).start
end
end
|
.start_if(options = {}) ⇒ Object
26
27
28
|
# File 'lib/dumper/agent.rb', line 26
def start_if(options = {})
start(options) if yield
end
|
Instance Method Details
#api_request(method_name, options) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/dumper/agent.rb', line 107
def api_request(method_name, options)
uri = URI.parse("#{@api_base}/api/#{method_name}")
http = Net::HTTP.new(uri.host, uri.port)
if uri.is_a? URI::HTTPS
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Post.new(uri.request_uri)
request['x-app-key'] = @app_key
request['x-app-env'] = @app_env
request['x-api-version'] = API_VERSION.to_s
request['user-agent'] = "Dumper-RailsAgent/#{Dumper::VERSION} (ruby #{::RUBY_VERSION} #{::RUBY_PLATFORM} / rails #{Rails::VERSION::STRING})"
if options[:params]
request.set_form_data(options[:params])
else
request.body = options[:json] || ''
request['Content-Type'] = 'application/octet-stream'
end
response = http.request(request)
if response.code == '200'
log response.body, :debug
MultiJson.load(response.body).with_indifferent_access
else
log "******** ERROR on api: #{method_name}, resp code: #{response.code} ********", :error
{} end
rescue
log_last_error
{} end
|
#register_hash ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/dumper/agent.rb', line 98
def register_hash
{
:hostname => Socket.gethostname,
:agent_version => Dumper::VERSION,
:app_name => @app_name,
:stack => @stack.to_hash,
}
end
|
#start ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/dumper/agent.rb', line 42
def start
log "stack: #{@stack.to_hash}", :debug
return unless @stack.supported?
@loop_thread = Thread.new { start_loop }
@loop_thread[:name] = 'Loop Thread'
end
|
#start_loop ⇒ Object
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/dumper/agent.rb', line 50
def start_loop
sec = 1
register_body = MultiJson.dump(register_hash)
log "message body for agent/register: #{register_body}", :debug
begin
sec *= 2
log "sleeping #{sec} seconds for agent/register", :debug
sleep sec
json = api_request('agent/register', :json => register_body)
end until json[:status]
return log("agent stopped: #{json.to_s}") if json[:status] == 'error'
@token = json[:token]
@max_filesize = (json[:max_filesize] || MAX_FILESIZE).to_i
log "agent started as #{@token ? 'primary' : 'secondary'}, max_filesize = #{@max_filesize}"
sleep 1.hour + rand(10) unless @token
loop do
json = api_request('agent/poll', :params => { :token => @token })
if json[:status] == 'ok'
if json[:token]
log 'promoted to primary' if @token.nil?
@token = json[:token]
else
log 'demoted to secondary' if @token
@token = nil
end
if json[:job]
if pid = fork
srand Process.detach(pid)
else
Dumper::Job.new(self, json[:job]).run_and_exit
end
end
end
sleep [ json[:interval].to_i, 60 ].max
end
end
|