Custom Notifications

165 views
Skip to first unread message

Dan Cook

unread,
Feb 18, 2012, 2:41:58 PM2/18/12
to god.rb
I need to execute a some ruby code on notification.
We have a central health monitoring service on the box and process
health is just one part of the overall reports.
I would like to put the events into a queue on the disk and have our
health monitoring service slurp up any events and send them to another
centralized server.

I assume I can just do this:

w.transition(:up, :start) do |on|
on.condition(:process_exits) do |c|
# custom notification method here....
end
end

Is this correct?

Cheers,
Dan

Dan Cook

unread,
Feb 19, 2012, 9:26:01 PM2/19/12
to god.rb

A little more context on this:
I set up a very simple test where god was using the above condition.


w.transition(:up, :start) do |on|
 on.condition(:process_exits) do |c|
   LOG.info("Hello world.")
 end
end

and the resulting output is:

I [2012-02-19 18:22:08]  INFO: Loading foo.god
I [2012-02-19 18:22:08]  INFO: Syslog enabled.
I [2012-02-19 18:22:08]  INFO: Using pid file directory: /var/run/god
I [2012-02-19 18:22:08]  INFO: Hello world.
I [2012-02-19 18:22:08]  INFO: Started on drbunix:///tmp/god.17165.sock

So the above code only runs once at start time so my original question stands.

How do I register a custom notification hook in god?

Cheers,
Dan

Shadab Ahmed

unread,
Nov 9, 2013, 9:29:26 AM11/9/13
to god...@googlegroups.com, one...@gmail.com
Hi, 

I searched up of google and found this thread. This is how I got my custom code executed on process_exit:

Create a god/contact subclass like this:

require 'god/contact'

class DelayedJobUnlockingNotifier < God::Contact

  def notify(message, time, priority, category, host)
    if pid = (message.match(/process (?<pid>.*) exited/) || {})['pid']
      `bundle exec rake RAILS_ENV=#{RAILS_ENV} PID=#{pid} delayed_job:recover_locked_jobs`
      applog(nil, :info, "delayed_job_unlocking_notifier: ran rake task to unlock locked jobs for PID: #{pid}")
    end
  rescue Object => e
    applog(nil, :info, "delayed_job_unlocking_notifier: failed to unlock locked jobs for PID: #{pid}")
    applog(nil, :debug, e.backtrace.join("\n"))
  end
end


If your godfile on top:

require APPLICATION_ROOT + '/lib/god/contacts/delayed_job_unlocking_notifier'

God.contact(:delayed_job_unlocking_notifier) do |c|
  c.name = 'stuck_jobs_unlocker'
end

and then in the watch block

w.transition(:up, :start) do |on|
    on.condition(:process_exits) do |c|
     c.notify = 'stuck_jobs_unlocker'
    end
  end

Only problem is not that you cannot customize the parameters sent to the notifier so I had to parse out PID from the message sent.

Reply all
Reply to author
Forward
0 new messages