Skip to content
Snippets Groups Projects
Commit 7095770b authored by Dillenn Terumalai's avatar Dillenn Terumalai :speech_balloon:
Browse files

// WIP

parent ff6f9a02
No related branches found
No related tags found
No related merge requests found
Pipeline #2279 passed
...@@ -5,7 +5,7 @@ test:package: ...@@ -5,7 +5,7 @@ test:package:
- apt-get install -qq git curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libzip-dev zip - apt-get install -qq git curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libzip-dev zip
- apt-get clean - apt-get clean
- pecl install mcrypt inotify - pecl install mcrypt inotify
- docker-php-ext-enable mcrypt - docker-php-ext-enable mcrypt inotify
- docker-php-ext-install zip - docker-php-ext-install zip
- curl --silent --show-error "https://getcomposer.org/installer" | php -- --install-dir=/usr/local/bin --filename=composer - curl --silent --show-error "https://getcomposer.org/installer" | php -- --install-dir=/usr/local/bin --filename=composer
script: script:
......
...@@ -18,6 +18,7 @@ class WatchCommand extends Command ...@@ -18,6 +18,7 @@ class WatchCommand extends Command
*/ */
protected $signature = 'watch:run protected $signature = 'watch:run
{--force : Force the worker to run even in maintenance mode} {--force : Force the worker to run even in maintenance mode}
{--once : Specify if the watcher should stop after one file has been detected}
{--sleep=3 : Number of seconds to sleep when no job is available} {--sleep=3 : Number of seconds to sleep when no job is available}
{--rest=0 : Number of seconds to rest between jobs} {--rest=0 : Number of seconds to rest between jobs}
{--timeout=0 : Only process the event on the queue}'; {--timeout=0 : Only process the event on the queue}';
...@@ -99,7 +100,8 @@ class WatchCommand extends Command ...@@ -99,7 +100,8 @@ class WatchCommand extends Command
$this->option('sleep'), $this->option('sleep'),
$this->option('force'), $this->option('force'),
$this->option('rest'), $this->option('rest'),
$this->option('timeout') $this->option('timeout'),
$this->option('once')
); );
} }
...@@ -107,6 +109,7 @@ class WatchCommand extends Command ...@@ -107,6 +109,7 @@ class WatchCommand extends Command
* Listen for the queue events in order to update the console output. * Listen for the queue events in order to update the console output.
* *
* @return void * @return void
* @psalm-suppress UndefinedInterfaceMethod
*/ */
protected function listenForEvents(): void protected function listenForEvents(): void
{ {
......
...@@ -48,7 +48,7 @@ class Watcher ...@@ -48,7 +48,7 @@ class Watcher
/** /**
* The inotify instance * The inotify instance
* *
* @var resource * @var resource|closed-resource
*/ */
protected $inotify; protected $inotify;
...@@ -94,9 +94,9 @@ class Watcher ...@@ -94,9 +94,9 @@ class Watcher
* Execute the console command. * Execute the console command.
* *
* @param WatcherOptions $options * @param WatcherOptions $options
* @return int * @return int|null
*/ */
public function daemon(WatcherOptions $options): int public function daemon(WatcherOptions $options)
{ {
if ($this->supportsAsyncSignals()) { if ($this->supportsAsyncSignals()) {
$this->listenForSignals(); $this->listenForSignals();
...@@ -138,6 +138,10 @@ class Watcher ...@@ -138,6 +138,10 @@ class Watcher
$cookie $cookie
); );
if ($options->once) {
$this->shouldQuit = true;
}
if ($options->rest > 0) { if ($options->rest > 0) {
$this->sleep($options->rest); $this->sleep($options->rest);
} }
...@@ -270,10 +274,10 @@ class Watcher ...@@ -270,10 +274,10 @@ class Watcher
protected function stopWatchers(): void protected function stopWatchers(): void
{ {
foreach (array_keys($this->watchers) as $watcher) { foreach (array_keys($this->watchers) as $watcher) {
inotify_rm_watch($this->inotify, $watcher); if(inotify_rm_watch($this->inotify, $watcher)) {
unset($this->watchers[$watcher]);
}
} }
$this->watchers = [];
$this->unregisterWatchers(); $this->unregisterWatchers();
} }
......
...@@ -26,12 +26,19 @@ class WatcherOptions ...@@ -26,12 +26,19 @@ class WatcherOptions
public $force; public $force;
/** /**
* Indicates if the watcher should stop after one event. * Indicates if the watcher should stop after a specific elapsed time (in seconds).
* *
* @var mixed * @var mixed
*/ */
public $timeout; public $timeout;
/**
* Indicates if the watcher should stop after one event.
*
* @var mixed
*/
public $once;
/** /**
* Create a new watcher options instance. * Create a new watcher options instance.
* *
...@@ -40,11 +47,12 @@ class WatcherOptions ...@@ -40,11 +47,12 @@ class WatcherOptions
* @param mixed $rest * @param mixed $rest
* @return void * @return void
*/ */
public function __construct($sleep = 5, $force = false, $rest = 0, $timeout = 0) public function __construct($sleep = 5, $force = false, $rest = 0, $timeout = 0, $once = false)
{ {
$this->sleep = $sleep; $this->sleep = $sleep;
$this->rest = $rest; $this->rest = $rest;
$this->force = $force; $this->force = $force;
$this->timeout = $timeout; $this->timeout = $timeout;
$this->once = $once;
} }
} }
...@@ -4,7 +4,6 @@ namespace Dterumal\Watcher\Tests\Feature; ...@@ -4,7 +4,6 @@ namespace Dterumal\Watcher\Tests\Feature;
use Dterumal\Watcher\Events\FileEvent; use Dterumal\Watcher\Events\FileEvent;
use Dterumal\Watcher\Events\WatcherCreated; use Dterumal\Watcher\Events\WatcherCreated;
use Dterumal\Watcher\Events\WatcherRestarted;
use Dterumal\Watcher\Events\WatcherStopped; use Dterumal\Watcher\Events\WatcherStopped;
use Dterumal\Watcher\Tests\TestCase; use Dterumal\Watcher\Tests\TestCase;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
...@@ -73,11 +72,53 @@ class WatcherCommandTest extends TestCase ...@@ -73,11 +72,53 @@ class WatcherCommandTest extends TestCase
// Check that the watcher was created // Check that the watcher was created
Event::assertDispatched(FileEvent::class, function ($event) { Event::assertDispatched(FileEvent::class, function ($event) {
ray($event);
return $event->directory === 'default' && return $event->directory === 'default' &&
$event->mask === 256 && $event->mask === 256 &&
$event->cookie === 0 && $event->cookie === 0 &&
$event->name === 'test.txt'; $event->name === 'test.txt';
}); });
} }
/** @test */
function it_detects_a_file_and_exits_with_option_once()
{
Event::fake();
// File created
$fooFile = storage_path('app/test.txt');
// make sure we're starting from a clean state
if (File::exists($fooFile)) {
unlink($fooFile);
}
// Start background process
$process = new Process(['/bin/bash', 'sleep-and-create-a-file.sh', $fooFile], $this->getStubDirectory());
$process->start();
// Run the artisan command
$exitCode = Artisan::call('watch:run', [
'--once' => true
], new NullOutput);
// Assert a new file is created
$this->assertTrue(File::exists($fooFile));
unlink($fooFile);
// Check that exit code
$this->assertSame(0, $exitCode);
// Check that the watcher was created
Event::assertDispatched(FileEvent::class, function ($event) {
return $event->directory === 'default' &&
$event->mask === 256 &&
$event->cookie === 0 &&
$event->name === 'test.txt';
});
// Check that the watcher was stopped
Event::assertDispatched(WatcherStopped::class, function ($event) {
return $event->status === 0;
});
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment