From c14c0ab2c72d9c0d82ae11fae55e2523783985e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20Kalu=C5=BEa?= Date: Mon, 18 Aug 2014 10:48:41 +0000 Subject: [PATCH] mod_systemd: Add IdleShutdown - number of seconds in idle-state after which httpd is shutdown. This is useful in a combination with socket activation. Add mod_systemd documentation. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1618579 13f79535-47bb-0310-9956-ffa450edef68 --- docs/log-message-tags/next-number | 2 +- docs/manual/mod/mod_systemd.xml | 70 +++++++++++++++++++++++++++++++ modules/arch/unix/mod_systemd.c | 49 +++++++++++++++++++++- 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 docs/manual/mod/mod_systemd.xml diff --git a/docs/log-message-tags/next-number b/docs/log-message-tags/next-number index 9fe34ed6cf..2e4e684632 100644 --- a/docs/log-message-tags/next-number +++ b/docs/log-message-tags/next-number @@ -1 +1 @@ -2804 +2805 diff --git a/docs/manual/mod/mod_systemd.xml b/docs/manual/mod/mod_systemd.xml new file mode 100644 index 0000000000..e50d3ce185 --- /dev/null +++ b/docs/manual/mod/mod_systemd.xml @@ -0,0 +1,70 @@ + + + + + + + + + +mod_systemd +Provides better support for systemd integration +Extension +mod_systemd.c +systemd_module + + +

This module provides support for systemd integration. It allows + starting httpd as a service with systemd Type=notify + (see systemd.service(5) manual page for more information). It also + provides statistics in systemctl status output and adds + various directives useful for systemd integration. +

+
+ + +IdleShutdown +Enable shutting down the httpd when it is idle for some time. +IdleShutdown seconds +IdleShutdown 0 +server config + + +

The IdleShutdown directive enables shutting + down the httpd when it is idle for some time. The idleness is based on + bytes served, so if there are no bytes sent for some time defined by this + directive, httpd will shutdown. By default, IdleShutdown is set to 0 + meaning this feature is disabled. +

+ +

This feature is useful in a combination with systemd socket activation + (see systemd.socket(5) manual page). When httpd is started by systemd on + some request, using this directive you can stop the httpd automatically + when all the requests are served. +

+ + Implementation warning

+ Because of implementation details, idleness is checked only every 10 + seconds. That means that if you specify IdleShutdown 14, + httpd will stop itself after 20 seconds of idleness. +

+
+
+ + +
diff --git a/modules/arch/unix/mod_systemd.c b/modules/arch/unix/mod_systemd.c index fa56151949..6f34fe44d3 100644 --- a/modules/arch/unix/mod_systemd.c +++ b/modules/arch/unix/mod_systemd.c @@ -34,15 +34,21 @@ #include #endif +static int shutdown_timer = 0; +static int shutdown_counter = 0; +static unsigned long bytes_served; +static pid_t mainpid; + static int systemd_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type) { int rv; ap_extended_status = 1; + mainpid = getpid(); rv = sd_notifyf(0, "READY=1\n" "STATUS=Processing requests...\n" - "MAINPID=%" APR_PID_T_FMT, getpid()); + "MAINPID=%" APR_PID_T_FMT, mainpid); if (rv < 0) { ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p, APLOGNO(02395) "sd_notifyf returned an error %d", rv); @@ -77,6 +83,26 @@ static int systemd_monitor(apr_pool_t *p, server_rec *s) "sd_notifyf returned an error %d", rv); } + /* Shutdown httpd when nothing is sent for shutdown_timer seconds. */ + if (sload.bytes_served == bytes_served) { + shutdown_counter += 10; + if (shutdown_timer > 0 && shutdown_counter >= shutdown_timer) { + rv = sd_notifyf(0, "READY=1\n" + "STATUS=Stopped as result of IdleShutdown " + "timeout."); + if (rv < 0) { + ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02804) + "sd_notifyf returned an error %d", rv); + } + kill(mainpid, SIGWINCH); + } + } + else { + shutdown_counter = 0; + } + + bytes_served = sload.bytes_served; + return DECLINED; } @@ -88,12 +114,31 @@ static void systemd_register_hooks(apr_pool_t *p) ap_hook_monitor(systemd_monitor, NULL, NULL, APR_HOOK_MIDDLE); } +static const char *set_shutdown_timer(cmd_parms *cmd, void *dummy, + const char *arg) +{ + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); + if (err != NULL) { + return err; + } + + shutdown_timer = atoi(arg); + return NULL; +} + +static const command_rec systemd_cmds[] = +{ +AP_INIT_TAKE1("IdleShutdown", set_shutdown_timer, NULL, RSRC_CONF, + "Number of seconds in idle-state after which httpd is shutdown"), + {NULL} +}; + AP_DECLARE_MODULE(systemd) = { STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, - NULL, + systemd_cmds, systemd_register_hooks, }; -- 2.40.0