22
33namespace PHPFastCGI \FastCGIDaemon ;
44
5- use PHPFastCGI \FastCGIDaemon \Exception \MemoryLimitException ;
6- use PHPFastCGI \FastCGIDaemon \Exception \RequestLimitException ;
75use PHPFastCGI \FastCGIDaemon \Exception \ShutdownException ;
8- use PHPFastCGI \FastCGIDaemon \Exception \TimeLimitException ;
96
107trait DaemonTrait
118{
@@ -14,6 +11,11 @@ trait DaemonTrait
1411 */
1512 private $ isShutdown = false ;
1613
14+ /**
15+ * @var string
16+ */
17+ private $ shutdownMessage = '' ;
18+
1719 /**
1820 * @var int
1921 */
@@ -29,12 +31,20 @@ trait DaemonTrait
2931 */
3032 private $ memoryLimit ;
3133
34+ /**
35+ * @var bool
36+ */
37+ private $ autoShutdown ;
38+
3239 /**
3340 * Flags the daemon for shutting down.
41+ *
42+ * @param string $message Optional shutdown message
3443 */
35- public function flagShutdown ()
44+ public function flagShutdown ($ message = null )
3645 {
3746 $ this ->isShutdown = true ;
47+ $ this ->shutdownMessage = (null === $ message ? 'Daemon flagged for shutdown ' : $ message );
3848 }
3949
4050 /**
@@ -48,6 +58,7 @@ private function setupDaemon(DaemonOptions $daemonOptions)
4858 $ this ->requestCount = 0 ;
4959 $ this ->requestLimit = $ daemonOptions ->getOption (DaemonOptions::REQUEST_LIMIT );
5060 $ this ->memoryLimit = $ daemonOptions ->getOption (DaemonOptions::MEMORY_LIMIT );
61+ $ this ->autoShutdown = $ daemonOptions ->getOption (DaemonOptions::AUTO_SHUTDOWN );
5162
5263 $ timeLimit = $ daemonOptions ->getOption (DaemonOptions::TIME_LIMIT );
5364
@@ -59,13 +70,22 @@ private function setupDaemon(DaemonOptions $daemonOptions)
5970 }
6071
6172 /**
62- * Increments the request count.
73+ * Increments the request count and looks for application errors .
6374 *
64- * @param int $number The number of requests to increment the count by
75+ * @param int[] $statusCodes The status codes of sent responses
6576 */
66- private function incrementRequestCount ( $ number )
77+ private function considerStatusCodes ( $ statusCodes )
6778 {
68- $ this ->requestCount += $ number ;
79+ $ this ->requestCount += count ($ statusCodes );
80+
81+ if ($ this ->autoShutdown ) {
82+ foreach ($ statusCodes as $ statusCode ) {
83+ if ($ statusCode >= 500 && $ statusCode < 600 ) {
84+ $ this ->flagShutdown ('Automatic shutdown following status code: ' . $ statusCode );
85+ break ;
86+ }
87+ }
88+ }
6989 }
7090
7191 /**
@@ -83,7 +103,7 @@ private function installSignalHandlers()
83103 });
84104
85105 pcntl_signal (SIGALRM , function () {
86- throw new TimeLimitException ('Daemon time limit reached (received SIGALRM) ' );
106+ throw new ShutdownException ('Daemon time limit reached (received SIGALRM) ' );
87107 });
88108 }
89109
@@ -97,22 +117,22 @@ private function installSignalHandlers()
97117 private function checkDaemonLimits ()
98118 {
99119 if ($ this ->isShutdown ) {
100- throw new ShutdownException (' Daemon flagged for shutdown ' );
120+ throw new ShutdownException ($ this -> shutdownMessage );
101121 }
102122
103123 pcntl_signal_dispatch ();
104124
105125 if (DaemonOptions::NO_LIMIT !== $ this ->requestLimit ) {
106126 if ($ this ->requestLimit <= $ this ->requestCount ) {
107- throw new RequestLimitException ('Daemon request limit reached ( ' .$ this ->requestCount .' of ' .$ this ->requestLimit .') ' );
127+ throw new ShutdownException ('Daemon request limit reached ( ' .$ this ->requestCount .' of ' .$ this ->requestLimit .') ' );
108128 }
109129 }
110130
111131 if (DaemonOptions::NO_LIMIT !== $ this ->memoryLimit ) {
112132 $ memoryUsage = memory_get_usage (true );
113133
114134 if ($ this ->memoryLimit <= $ memoryUsage ) {
115- throw new MemoryLimitException ('Daemon memory limit reached ( ' .$ memoryUsage .' of ' .$ this ->memoryLimit .' bytes) ' );
135+ throw new ShutdownException ('Daemon memory limit reached ( ' .$ memoryUsage .' of ' .$ this ->memoryLimit .' bytes) ' );
116136 }
117137 }
118138 }
0 commit comments