File tree Expand file tree Collapse file tree 5 files changed +45
-3
lines changed
Expand file tree Collapse file tree 5 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -47,4 +47,10 @@ interface DaemonInterface
4747 * @throws \Exception On fatal error
4848 */
4949 public function run ();
50+
51+ /**
52+ * Flag the daemon for shutting down. This will stop it from accepting
53+ * requests.
54+ */
55+ public function flagShutdown ();
5056}
Original file line number Diff line number Diff line change 99
1010trait DaemonTrait
1111{
12+ /**
13+ * @var bool
14+ */
15+ private $ isShutdown = false ;
16+
1217 /**
1318 * @var int
1419 */
@@ -24,6 +29,14 @@ trait DaemonTrait
2429 */
2530 private $ memoryLimit ;
2631
32+ /**
33+ * Flags the daemon for shutting down.
34+ */
35+ public function flagShutdown ()
36+ {
37+ $ this ->isShutdown = true ;
38+ }
39+
2740 /**
2841 * Loads to configuration from the daemon options and installs signal
2942 * handlers.
@@ -76,12 +89,17 @@ private function installSignalHandlers()
7689
7790 /**
7891 * Checks the current PHP process against the limits specified in a daemon
79- * options object.
92+ * options object. This function will also throw an exception if the daemon
93+ * has been flagged for shutdown.
8094 *
8195 * @throws ShutdownException When limits in the daemon options are exceeded
8296 */
8397 private function checkDaemonLimits ()
8498 {
99+ if ($ this ->isShutdown ) {
100+ throw new ShutdownException ('Daemon flagged for shutdown ' );
101+ }
102+
85103 pcntl_signal_dispatch ();
86104
87105 if (DaemonOptions::NO_LIMIT !== $ this ->requestLimit ) {
Original file line number Diff line number Diff line change 88 * They may be triggered by a SIGINT or by exceeding limits specified in the
99 * daemon configuration (such as memory and request limits).
1010 */
11- class ShutdownException extends \RuntimeException
11+ class ShutdownException extends DaemonException
1212{
1313}
Original file line number Diff line number Diff line change 1919class UserlandDaemonTest extends \PHPUnit_Framework_TestCase
2020{
2121 /**
22- * Tests that the daemon shuts down after reaching its request .
22+ * Tests that the daemon shuts down after being flagged for shutdown.
23+ */
24+ public function testFlagShutdown ()
25+ {
26+ // Set a memory limit to make sure that it isn't breached (added 10MB on top of peak usage)
27+ $ context = $ this ->createTestingContext (1 , memory_get_peak_usage () + (10 * 1024 * 1024 ));
28+
29+ $ context ['daemon ' ]->flagShutdown ();
30+ $ context ['daemon ' ]->run ();
31+
32+ $ this ->assertEquals ('Daemon flagged for shutdown ' , $ context ['logger ' ]->getMessages ()[0 ]['message ' ]);
33+ }
34+
35+ /**
36+ * Tests that the daemon shuts down after reaching its request.
2337 */
2438 public function testRequestLimit ()
2539 {
Original file line number Diff line number Diff line change @@ -12,4 +12,8 @@ public function run()
1212 {
1313 return $ this ->delegateCall ('run ' , func_get_args ());
1414 }
15+
16+ public function flagShutdown ()
17+ {
18+ }
1519}
You can’t perform that action at this time.
0 commit comments