Skip to content

Commit

Permalink
Merge pull request #5 from gwleuverink/update/native-child-processes
Browse files Browse the repository at this point in the history
Update/native child processes
  • Loading branch information
gwleuverink authored Nov 20, 2024
2 parents c53acc8 + df3b220 commit 10fde58
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 617 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')
? $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
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"laravel/framework": "^11.0",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.4",
"nativephp/electron": "^0.7.2",
"nativephp/electron": "^0.8.7",
"blade-ui-kit/blade-heroicons": "^2.3",
"react/socket": "^1.15",
"spatie/laravel-settings": "^3.3",
Expand Down Expand Up @@ -74,7 +74,11 @@
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix",
"analyze": "vendor/bin/phpstan analyse",
"baseline": "vendor/bin/phpstan analyse --generate-baseline"
"baseline": "vendor/bin/phpstan analyse --generate-baseline",
"native:dev": [
"Composer\\Config::disableProcessTimeout",
"npx concurrently -k -c \"#93c5fd,#c4b5fd\" \"php artisan native:serve --no-interaction\" \"npm run dev\" --names=app,vite"
]
},
"extra": {
"laravel": {
Expand Down
Loading

0 comments on commit 10fde58

Please sign in to comment.