Skip to content

How to exit supervisord with error when PROCESS_STATE_EXITED return a not expected exit status #1685

@equeinnec

Description

@equeinnec

I would like to implement such feature to start a process using supervisord (in a docker , k8s context) and this process is a job so I implemented an eventlistener to manage PROCESS_STATE_EXITED event

  • I implemented an eventlistener like shutdown supervisord once one of the programs is killed #712 to shutdown supervisord when I received such PROCESS_STATE_EXITED event using the kill command
  • I get the exit status using expected body result: exit_code = int(body["expected"]) ^ 1
  • Now If I have exit_code=1 I would like to find a solution to change supervisord exit status value

Such code will always exit 0 even if my process exit with status 1
thanks for your help
Should be good to have an option to avoid to implement such event listener

#!/usr/bin/env python3
import sys
import os
import logging
import subprocess
import time

from supervisor.childutils import listener

def main(args):
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG, format='%(asctime)s %(levelname)s %(filename)s: %(message)s')
logger = logging.getLogger("stop_supervisord.py")
debug_mode = True if 'DEBUG' in os.environ else False

while True:
    logger.info("Listening for events...")
    headers, body = listener.wait(sys.stdin, sys.stdout)
    body = dict([pair.split(":") for pair in body.split(" ")])

    exit_code = int(body["expected"]) ^ 1 #expected will be 0 if the exit code was unexpected, or 1 if the exit code was expected
    logger.debug("Headers: %r", repr(headers))
    logger.debug("Body: %r", repr(body))
    logger.debug("Args: %r", repr(args))
    logger.debug("exit_code: %d", exit_code) # Log the exit code

    if debug_mode: continue

    try:
        if headers["eventname"] in ["PROCESS_STATE_EXITED","PROCESS_STATE_FATAL"]:
            logger.info("Process exited or fatal")
            if not args or body["processname"] in args:
                logger.error("Killing off supervisord instance ...")
                res = subprocess.call(["/bin/kill", "-15", "1"], stdout=sys.stderr)
                logger.info("Sent TERM signal to init process")
                if exit_code:
                    logger.critical("Sys exit code: %d",exit_code)
                    listener.fail(sys.stdout)
                    sys.exit(1)
    except Exception as e:
        logger.critical("Unexpected Exception: %s", str(e))
        listener.fail(sys.stdout)
        sys.exit(1)
    else:
        listener.ok(sys.stdout)

if name == 'main':
main(sys.argv[1:])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions