From 69f9b3bfa4ecee87cf95a6a8dc18c4a62ec126f9 Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Thu, 1 Jun 2017 17:38:54 +0200 Subject: [PATCH] MEDIUM: mworker: exit-on-failure option This option exits every workers when one of the current workers die. It allows you to monitor the master process in order to relaunch everything on a failure. For example it can be used with systemd and Restart=on-failure in a spec file. --- include/types/global.h | 1 + src/cfgparse.c | 11 ++++++++++- src/haproxy.c | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/types/global.h b/include/types/global.h index ee8e95e0e..cce3de77f 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -63,6 +63,7 @@ #define GTUNE_RESOLVE_DONTFAIL (1<<7) #define GTUNE_SOCKET_TRANSFER (1<<8) +#define GTUNE_EXIT_ONFAILURE (1<<9) /* Access level for a stats socket */ #define ACCESS_LVL_NONE 0 diff --git a/src/cfgparse.c b/src/cfgparse.c index bdf55a8da..76a4f318c 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -625,8 +625,17 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm) global.mode |= MODE_DAEMON; } else if (!strcmp(args[0], "master-worker")) { - if (alertif_too_many_args(0, file, linenum, args, &err_code)) + if (alertif_too_many_args(1, file, linenum, args, &err_code)) goto out; + if (*args[1]) { + if (!strcmp(args[1], "exit-on-failure")) { + global.tune.options |= GTUNE_EXIT_ONFAILURE; + } else { + Alert("parsing [%s:%d] : '%s' only supports 'exit-on-failure' option.\n", file, linenum, args[0]); + err_code |= ERR_ALERT | ERR_FATAL; + goto out; + } + } global.mode |= MODE_MWORKER; } else if (!strcmp(args[0], "debug")) { diff --git a/src/haproxy.c b/src/haproxy.c index 4e892c099..10ddf1602 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -671,6 +671,11 @@ static void mworker_wait() /* check if exited child was in the current children list */ if (current_child(exitpid)) { Alert("Current worker %d left with exit code %d\n", exitpid, status); + if (status != 0 && status != 130 && status != 143 + && global.tune.options & GTUNE_EXIT_ONFAILURE) { + Alert("exit-on-failure: killing every workers with SIGTERM\n"); + mworker_kill(SIGTERM); + } } else { Warning("Former worker %d left with exit code %d\n", exitpid, status); delete_oldpid(exitpid);