Skip to content

Commit 18cf59f

Browse files
authored
Merge pull request #47 from seregazhuk/detecting-script-exit
Support for watching script exit
2 parents 7b0d77b + 07fb4b6 commit 18cf59f

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ function terminationHandler()
209209

210210
By default PHP-watcher sends `SIGINT` signal.
211211

212+
## Automatic restart
213+
214+
PHP-watcher was originally written to restart long-running processes such as web servers, but
215+
it also supports apps that cleanly exit. If your script exits cleanly, the watcher will continue
216+
to monitor the directory (or directories) and restart the script if there are any changes. If the
217+
script crashes PHP-watcher will notify you about that.
218+
219+
![app exit](images/exit.svg)
220+
212221
# License
213222

214223
MIT [http://rem.mit-license.org](http://rem.mit-license.org)

images/exit.svg

Lines changed: 1 addition & 0 deletions
Loading

src/Screen.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ private function info(string $text): void
5353
$this->output->writeln($text);
5454
}
5555

56+
private function warning(string $text): void
57+
{
58+
$text = sprintf('<fg=red>%s</>', $this->message($text));
59+
$this->output->writeln($text);
60+
}
61+
5662
public function start(string $command): void
5763
{
5864
$command = str_replace('exec', '', $command);
@@ -78,6 +84,14 @@ public function subscribeToProcessOutput(Process $process): void
7884
$process->stderr->on('data', static function ($data) {
7985
echo $data;
8086
});
87+
88+
$process->on('exit', function($exitCode) {
89+
if ($exitCode === 0) {
90+
$this->info('clean exit - waiting for changes before restart');
91+
} else {
92+
$this->warning('app crashed - waiting for file changes before starting...');
93+
}
94+
});
8195
}
8296

8397
public function showSpinner(LoopInterface $loop): void

src/Watcher/Watcher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private function startProcess(Process $process): void
4545
private function restartProcess(Process $process, int $signal, float $delayToRestart): void
4646
{
4747
$process->terminate($signal);
48+
$process->removeAllListeners();
4849
$this->screen->restarting($process->getCommand());
4950

5051
$this->loop->addTimer($delayToRestart, function () use ($process) {

tests/Helper/Filesystem.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public static function createHelloWorldPHPFile(): string
2424
return $name;
2525
}
2626

27+
public static function createPHPFileThatCrashes(): string
28+
{
29+
$name = self::FIXTURES_DIR . 'test.php';
30+
self::createFile($name, '<?php exit(1);');
31+
32+
return $name;
33+
}
34+
2735
public static function createStdErrorPHPFile(): string
2836
{
2937
$name = self::FIXTURES_DIR . 'test.php';

tests/ScriptExitTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace tests;
4+
5+
use tests\Helper\Filesystem;
6+
use tests\Helper\WatcherRunner;
7+
use tests\Helper\WatcherTestCase;
8+
9+
final class ScriptExitTest extends WatcherTestCase
10+
{
11+
/** @test */
12+
public function it_detects_when_script_exits(): void
13+
{
14+
$fileToWatch = Filesystem::createHelloWorldPHPFile();
15+
$watcher = (new WatcherRunner)->run($fileToWatch, ['--watch', $fileToWatch]);
16+
$this->wait();
17+
18+
$this->assertStringContainsString('clean exit - waiting for changes before restart', $watcher->getOutput());
19+
}
20+
21+
/** @test */
22+
public function it_detects_when_script_crashes(): void
23+
{
24+
$fileToWatch = Filesystem::createPHPFileThatCrashes();
25+
$watcher = (new WatcherRunner)->run($fileToWatch, ['--watch', $fileToWatch]);
26+
$this->wait();
27+
28+
$this->assertStringContainsString('app crashed - waiting for file changes before starting', $watcher->getOutput());
29+
}
30+
}

0 commit comments

Comments
 (0)