diff --git a/app/Livewire/Inbox.php b/app/Livewire/Inbox.php index 68a6dc7..ceb7041 100644 --- a/app/Livewire/Inbox.php +++ b/app/Livewire/Inbox.php @@ -6,14 +6,16 @@ use Livewire\Component; use Livewire\Attributes\On; use Livewire\Attributes\Url; -use App\Services\Smtp\Server; use Livewire\Attributes\Title; use App\Events\MessageReceived; use Native\Laravel\Facades\App; use App\Livewire\Concerns\Config; use Livewire\Attributes\Computed; use Illuminate\Support\Collection; +use Native\Laravel\Facades\ChildProcess; use App\Livewire\Concerns\MessageControls; +use Native\Laravel\Events\ChildProcess\ProcessExited; +use Native\Laravel\Events\ChildProcess\ProcessSpawned; /** * @property ?Message $message @@ -53,18 +55,6 @@ public function selectMessage(int $id) $this->updateBadgeCount(); } - public function heartbeat() - { - $online = Server::new($this->config->port)->ping(); - - // Skip rerender whe the online status didn't change - if ($this->online === $online) { - $this->skipRender(); - } - - $this->online = $online; - } - //--------------------------------------------------------------- // Computed properties //--------------------------------------------------------------- @@ -99,9 +89,51 @@ public function messageReceived() $this->updateBadgeCount(); } + #[On('native:' . ProcessSpawned::class)] + public function serverStarted($alias) + { + if ($alias !== 'smtp-server') { + return $this->skipRender(); + } + + $this->online = true; + } + + #[On('native:' . ProcessExited::class)] + public function serverStopped($alias) + { + if ($alias !== 'smtp-server') { + return $this->skipRender(); + } + + $this->online = false; + } + //--------------------------------------------------------------- // System //--------------------------------------------------------------- + public function heartbeat() + { + (bool) ChildProcess::get('smtp-server') + ? $this->online = true + : $this->startServer(); + } + + protected function startServer() + { + // Better to mock this method instead, but since this should be handled + // by NativePHP (maybe in future) just keep it simple. + if (app()->runningUnitTests()) { + return; + } + + ChildProcess::artisan( + cmd: 'smtp:serve', + alias: 'smtp-server', + persistent: false // Let livewire handle restarts + ); + } + protected function updateBadgeCount() { // Better to mock this method instead, but since this should be handled diff --git a/app/Livewire/Settings.php b/app/Livewire/Settings.php index 58bc311..65af94e 100644 --- a/app/Livewire/Settings.php +++ b/app/Livewire/Settings.php @@ -9,6 +9,7 @@ use App\Livewire\Concerns\Config; use Livewire\Attributes\Computed; use Livewire\Attributes\Validate; +use Native\Laravel\Facades\ChildProcess; /** * Note that the property casing is non-standard, because we're mapping directly to the config object @@ -46,6 +47,7 @@ public function save() // Stop the server & restart picked up by the scheduler if ($this->port !== $oldPort) { + ChildProcess::stop('smtp-server'); Server::new($oldPort)->kill(); } } diff --git a/app/Services/Smtp/Server.php b/app/Services/Smtp/Server.php index 3fcb890..1357cd1 100644 --- a/app/Services/Smtp/Server.php +++ b/app/Services/Smtp/Server.php @@ -188,6 +188,7 @@ public function onMessageReceived(callable $callback): self /** * Stops the currently running server + * NOTE: Not used since introduction Native ChildProcesses */ public function stop(): void { @@ -201,6 +202,7 @@ public function stop(): void /** * Tries to kill the process on the configured Port nr. + * NOTE: Not used since introduction Native ChildProcesses */ public function kill(): void { @@ -228,6 +230,7 @@ public function kill(): void /** * Check if a process is alive on the configured port + * NOTE: Not used since introduction Native ChildProcesses */ public function ping(): bool { diff --git a/bootstrap/app.php b/bootstrap/app.php index c1b8c11..5171e58 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -13,15 +13,7 @@ ) ->withSchedule(function (Schedule $schedule) { - - // Make sure the SMTP server is always up - // This is here as a fallback for whenever the - // process spawned by the Inbox component ever crashes. - // Or is restarted by updating the port in your preferences. - $schedule->command('smtp:serve') - // ->withoutOverlapping() // Won't work when enabled - // ->runInBackground() // Server won't stop when app closes when enabled (note: in final build the process stays alive regardless?) - ->everyFiveSeconds(); + // }) ->withMiddleware(function (Middleware $middleware) { diff --git a/resources/views/livewire/inbox.blade.php b/resources/views/livewire/inbox.blade.php index d3b936e..83ef260 100644 --- a/resources/views/livewire/inbox.blade.php +++ b/resources/views/livewire/inbox.blade.php @@ -1,6 +1,9 @@