Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Watchdogagent #376

Open
wants to merge 1 commit into from

6 participants

@peter-x

Agent that emits events if it did not receive events for a given amount of time.

@coveralls

Coverage Status

Coverage decreased (-0.56%) when pulling bcc295f on peter-x:watchdogagent into 1512d80 on cantino:master.

@coveralls

Coverage Status

Coverage decreased (-0.56%) when pulling bcc295f on peter-x:watchdogagent into 1512d80 on cantino:master.

@cantino
Owner

Another very useful concept, thanks! (Some systems call this a NoDataAlert.)

Could you add some specs to this Agent as well? If you need any help with them, please let me know!

@peter-x

Yes, actually I am a bit lost. Writing rspec does not seem difficult, but how do I run the tests?

@alias1
Collaborator
@dsander
Collaborator

@peter-x bundle exec rake spec to run everything, but if you are just working on one spec you can use bundle exec rspec spec/models/agents/your_agent_spec.rb

@peter-x

It seems that the test database is out of date. How can I db:migrate in the test environment?

@CloCkWeRX
@peter-x

Great, thanks, that worked! Sorry - I am really new to ruby.

@CloCkWeRX
@alias1 alias1 added the agent label
@cantino
Owner

How's this going? Can I help you with anything?

@peter-x

I did not really have time to further look into it. Perhaps I will this week, but feel free to add anything if you want.

@cantino
Owner

Hey @peter-x, I wanted to check in and see if you're still interested in working on this. If not, I'll add some specs and try to get it merged.

@peter-x

I would be pleased if you could pick it up, as you might have noticed, I currently can't find time to work on it - sorry for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 8, 2014
  1. @peter-x

    Watchdog agent.

    peter-x authored
This page is out of date. Refresh to see the latest.
Showing with 67 additions and 0 deletions.
  1. +67 −0 app/models/agents/watchdog_agent.rb
View
67 app/models/agents/watchdog_agent.rb
@@ -0,0 +1,67 @@
+module Agents
+ class WatchdogAgent < Agent
+
+ description <<-MD
+ A WatchdogAgent emits an event if it did not receive an event for a given amount of time.
+ `timespan` can be something like `10m` (10 minutes), `9.7h` (9.7 hours) or `18d` (18 days).
+ If `emit_once` is set to `false`, it will emit one event for each scheduled run, until it
+ receives an event again. If it is set to `true`, it will only emit one event.
+ In `message` you can give a message that will be part of the emitted event.
+ MD
+
+ event_description <<-MD
+ Events look like this:
+
+ { "message": "Your message" }
+ MD
+
+ default_schedule "8pm"
+
+ def validate_options
+ log options['timespan']
+ unless options['timespan'].present? && ('mhd'.include? options['timespan'][-1]) && options['timespan'][0..-2].to_f > 0
+ errors.add(:base, "timespan is required and has to be a positive number followed by 'm', 'h' or 'd'")
+ end
+ errors.add(:base, "message is required") unless options['message'].present?
+ end
+
+ def default_options
+ {
+ 'timespan' => "5.2d",
+ 'message' => "Nothing happened in the last 5.2 days!",
+ 'emit_once' => "true"
+ }
+ end
+
+ def working?
+ !recent_error_logs?
+ end
+
+ def check
+ unless options['emit_once'].to_s == 'true' && !received_after_last_emit?
+ timeout = (options['timespan'][0..-2].to_f * (case options['timespan'][-1]
+ when 'm' then 60
+ when 'h' then (60 * 60)
+ when 'd' then (60 * 60 * 25)
+ else 1
+ end)).seconds.ago
+ if last_receive_at < timeout
+ received_after_last_emit false
+ create_event :payload => {'message' => options['message'] }
+ end
+ end
+ end
+
+ def receive(events)
+ received_after_last_emit true
+ end
+
+ def received_after_last_emit?
+ memory['received after last emit'].to_s == 'true'
+ end
+
+ def received_after_last_emit(value)
+ memory['received after last emit'] = (value.to_s == 'true')
+ end
+ end
+end
Something went wrong with that request. Please try again.