Class: Papermill::Agent
- Inherits:
-
Object
- Object
- Papermill::Agent
- Includes:
- Singleton
- Defined in:
- lib/papermill-agent/agent.rb
Constant Summary collapse
- UPDATE_INTERVAL =
send new request data every 10 seconds
10- @@request_timeout =
request timeout threshold for sending data to papermill
5
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#disabled ⇒ Object
Returns the value of attribute disabled.
-
#last_sent ⇒ Object
readonly
Returns the value of attribute last_sent.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Class Method Summary collapse
Instance Method Summary collapse
- #disabled? ⇒ Boolean
- #do_request ⇒ Object
-
#jsonify_payload ⇒ Object
Return a json version of the storage array.
- #seconds_until_next_run ⇒ Object
- #send_data_to_papermill ⇒ Object
- #start ⇒ Object
- #time_since_last_sent ⇒ Object
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/papermill-agent/agent.rb', line 14 def config @config end |
#disabled ⇒ Object
Returns the value of attribute disabled.
15 16 17 |
# File 'lib/papermill-agent/agent.rb', line 15 def disabled @disabled end |
#last_sent ⇒ Object (readonly)
Returns the value of attribute last_sent.
14 15 16 |
# File 'lib/papermill-agent/agent.rb', line 14 def last_sent @last_sent end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/papermill-agent/agent.rb', line 14 def logger @logger end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
14 15 16 |
# File 'lib/papermill-agent/agent.rb', line 14 def mutex @mutex end |
Class Method Details
.request_timeout ⇒ Object
19 20 21 |
# File 'lib/papermill-agent/agent.rb', line 19 def self.request_timeout @@request_timeout end |
Instance Method Details
#disabled? ⇒ Boolean
23 24 25 |
# File 'lib/papermill-agent/agent.rb', line 23 def disabled? !!disabled end |
#do_request ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/papermill-agent/agent.rb', line 59 def do_request begin logger.info("#{Time.now}: Sending #{Storage.size} requests to papermill") RestClient.post config.endpoint, { :token => config.token, :payload => jsonify_payload } Storage.clear rescue RestClient::Exception, Errno::ECONNREFUSED, SocketError => e logger.log_exception e end end |
#jsonify_payload ⇒ Object
Return a json version of the storage array. Also, we'll clear out the storage here
TODO: move this to the Storage class.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/papermill-agent/agent.rb', line 81 def jsonify_payload mutex.synchronize do begin json_data = Storage.store.flatten.to_json rescue => e logger.log_exception(e) end return json_data end end |
#seconds_until_next_run ⇒ Object
73 74 75 |
# File 'lib/papermill-agent/agent.rb', line 73 def seconds_until_next_run UPDATE_INTERVAL - time_since_last_sent end |
#send_data_to_papermill ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/papermill-agent/agent.rb', line 49 def send_data_to_papermill begin Timeout.timeout(config.setting('request_timout') || self.class.request_timeout) do do_request if !Storage.empty? && config.live_mode end rescue Timeout::Error => e logger.log_exception(e) end end |
#start ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/papermill-agent/agent.rb', line 27 def start @last_sent = Time.now @mutex = Mutex.new @logger = Logger.new @config = Configurator.new logger.info "\n\n=============== Starting papermill-agent at #{Time.now} in process #{Process.pid}" Thread.abort_on_exception = true unless disabled? @worker_thread = Thread.new do loop do if time_since_last_sent > UPDATE_INTERVAL send_data_to_papermill @last_sent = Time.now end sleep_time = seconds_until_next_run sleep sleep_time if sleep_time > 0 end end end end |
#time_since_last_sent ⇒ Object
69 70 71 |
# File 'lib/papermill-agent/agent.rb', line 69 def time_since_last_sent Time.now - last_sent end |