God - custom code on process exits

116 views Asked by At

Is it possible to execute custom code when God restarts a process. Please see sample below of what I mean. When watch transitions from :up to :start (process restarted), we want to send a log to syslog at :error level. applog is a method defined in God module.

w.transition(:up, :start) do |on|
  applog(w, :error, "critical server process exited")
  on.condition(:process_exits) do |c|
    c.notify = 'alerts'
  end
end

Currently, I see message in syslog when the watch config is loaded. How else can I log an error in syslog on a certain transition?

1

There are 1 answers

0
sumit On

I tried adding a new Contact in God by defining a Contact and loading via God.contact(). Ran into errors around missing name, etc. Not sure how else to register. Better way would be to fork the gem and add a new Contact, however that would mean changing a lot in our infra. Approach below is what I ended up using for now....involves monkey patching.

module God
  class Task
    alias gods_notify notify
    def notify(condition, message)
      applog(nil, :error, "@cee: {\"alert\": \"true\", \"category\": \"alert.#{condition.to_s}\", \"content\": \"#{message}\"}")
      gods_notify(condition, message)
    end
  end
end