Skip to content

Commit

Permalink
refactor to use process events
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Nov 5, 2024
1 parent cc7a64e commit 732e7ae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
58 changes: 45 additions & 13 deletions app/Livewire/Inbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
//---------------------------------------------------------------
Expand Down Expand Up @@ -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')

Check failure on line 117 in app/Livewire/Inbox.php

View workflow job for this annotation

GitHub Actions / duster-lint

Ternary operator condition is always true.
? $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
Expand Down
2 changes: 2 additions & 0 deletions app/Livewire/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/Services/Smtp/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
10 changes: 1 addition & 9 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions resources/views/livewire/inbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<main
{{-- Poll for heartbeat --}}
wire:poll.5s="heartbeat"
{{-- When the initial server status is offline, we ping a heartbeat, which checks the status & restarts the server process if needed --}}
@unless ($online)
wire:init="heartbeat"
wire:poll.3s="heartbeat"
@endunless
{{-- Make sure we stay on the same page when refreshing (workaround for snappier UI) --}}
x-on:keydown.meta.r.prevent="
window.location.href = '/{{ $this->message?->id }}';
Expand Down

0 comments on commit 732e7ae

Please sign in to comment.