Skip to content

Instantly share code, notes, and snippets.

@naokirin
Last active March 13, 2017 11:25
Show Gist options
  • Save naokirin/3815ff4237ef916099e2de3f592b4cf7 to your computer and use it in GitHub Desktop.
Save naokirin/3815ff4237ef916099e2de3f592b4cf7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python -u
import sys
import os
import getopt
from supervisor import childutils
from datetime import datetime
class ProcessStateNotifier:
def __init__(self, command):
self.command = command
self.stdin = sys.stdin
self.stdout = sys.stdout
self.stderr = sys.stderr
def exec_command(processname, groupname, pid, time):
with os.popen(self.command, 'w') as m:
m.write(processname)
m.write(groupname)
m.write(pid)
m.write(time)
self.stderr.write('Notified:\n\n%s(PID:%s) %s' % (processname, pid, time))
self.stderr.flush()
def runforever():
headers, payload = childutils.listener.wait(sys.stdin, sys.stdout)
eventname = headers['eventname']
pheader, data = childutils.eventdata(payload+'\n')
childutils.listener.ok(sys.stdout)
if not eventname in ['PROCESS_STATE_BACKOFF', 'PROCESS_STATE_EXITED', 'PROCESS_STATE_FATAL', 'PROCESS_STATE_UNKNOWN']:
return
processname = pheaders['processname']
groupname = pheaders['groupname']
pid = pheaders['pid']
time = childutils.get_asctime()
exec_command(processname, groupname, pid, time)
def main(argv = sys.argv):
short_args = "c:"
long_args = [
"command="
]
arguments = argv[1:]
opts, args = getopt.getopt(arguments, short_args, long_args)
for option, value in opts:
if option in ('-c', '--command'):
command = value
notifier = ProcessStateNotifier(command)
while True:
notifier.runforever
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment