Files
2026-04-17 15:44:42 +02:00

62 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use App\Jobs\SendToImmichJob;
#[Signature("immich:watch-folder")]
#[Description("Dispatch photos folder")]
class WatchFolder extends Command
{
private bool $continue = true;
private $instance;
private $resourceHandle;
/**
* Execute the console command.
*/
public function handle()
{
$watchDir = config("immich.folder_path");
// Gracefully exit when TERM signal received
pcntl_async_signals(true);
pcntl_signal(SIGTERM, function () {
$this->continue = false;
});
$this->instance = inotify_init();
stream_set_blocking($this->instance, 0);
$this->resourceHandle = inotify_add_watch(
$this->instance,
$watchDir,
IN_CLOSE_WRITE | IN_ATTRIB | IN_MOVED_TO | IN_MOVE_SELF,
);
while ($this->continue) {
$events = inotify_read($this->instance);
if (!empty($events)) {
// Get filenames only and remove duplicates
$fileNames = array_unique(
array_map(function ($event) {
return $event["name"];
}, $events),
);
foreach ($fileNames as $fileName) {
echo $fileName . PHP_EOL;
SendToImmichJob::dispatch($fileName);
}
}
sleep(1);
}
fclose($this->instance);
}
}