Skip to content

Instantly share code, notes, and snippets.

@dnephin
Last active May 4, 2018 05:03
Show Gist options
  • Save dnephin/f61646745bd004afba8b to your computer and use it in GitHub Desktop.
Save dnephin/f61646745bd004afba8b to your computer and use it in GitHub Desktop.
"""
A supervisorctl plugin which returns the status of a process and exits with a
proper exit code.
This is a workaround for `https://github.com/Supervisor/supervisor/issues/24`_
Configuration
-------------
Add this section to your supervisord.conf. You may need to adjust the path.
[ctlplugin:status_with_exit]
supervisor.ctl_factory = supervisor_status_with_exit:StatusWithExitCodePlugin
"""
from supervisor.supervisorctl import ControllerPluginBase
class StatusWithExitCodePlugin(ControllerPluginBase):
def do_status_with_exit(self, arg):
action_name = 'status_with_exit'
names = arg.strip().split()
if not names:
raise ValueError("A process name is required for " + action_name)
if len(names) > 1:
raise ValueError("Only a single process name is supported for " +
action_name)
name = names[0]
supervisor = self.ctl.get_supervisor()
info = supervisor.getProcessInfo(name)
if info['statename'] not in ('RUNNING', 'RESTARTING', 'STARTING'):
raise SystemExit('{name} {statename} {description}'.format(**info))
self.ctl.output(info['statename'])
def help_status_with_exit(self):
self.ctl.output('status_with_exit <name> '
'Get process status and exit.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment