Skip to content

Commit

Permalink
Add error handling in sync command
Browse files Browse the repository at this point in the history
  • Loading branch information
richan-fongdasen committed Apr 16, 2024
1 parent 6743b68 commit e439f32
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/turso-laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// config for RichanFongdasen/TursoLaravel
return [
'sync_command' => [
'node_path' => env('NODE_PATH'), // Full path to the node executable. E.g: /usr/bin/node
'script_filename' => 'turso-sync.mjs',
'script_path' => realpath(__DIR__ . '/..'),
'timeout' => 60,
Expand Down
19 changes: 15 additions & 4 deletions src/Commands/TursoSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;
use RuntimeException;

class TursoSyncCommand extends Command
{
Expand All @@ -16,14 +17,26 @@ class TursoSyncCommand extends Command
protected function compileRunProcess(): string
{
return sprintf(
'node %s "%s" "%s" "%s"',
'%s %s "%s" "%s" "%s"',
$this->getNodePath(),
config('turso-laravel.sync_command.script_filename'),
config('database.connections.turso.db_url'),
config('database.connections.turso.access_token'),
config('database.connections.turso.db_replica'),
);
}

protected function getNodePath(): string
{
$nodePath = config('turso-laravel.sync_command.node_path') ?? trim((string) Process::run('which node')->output());

if (($nodePath === '') || ! file_exists($nodePath)) {
throw new RuntimeException('Node executable not found.');
}

return $nodePath;
}

public function handle(): int
{
$timeout = (int) config('turso-laravel.sync_command.timeout');
Expand All @@ -33,9 +46,7 @@ public function handle(): int
->run($this->compileRunProcess());

if ($result->failed()) {
$this->error($result->errorOutput());

return self::FAILURE;
throw new RuntimeException('Turso sync command failed: ' . $result->errorOutput());
}

$this->info($result->output());
Expand Down
14 changes: 10 additions & 4 deletions tests/Unit/TursoManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@
test('it can trigger the sync command immediately', function () {
Process::fake();

config(['database.connections.turso.db_replica' => '/tmp/turso.sqlite']);
config([
'database.connections.turso.db_replica' => '/tmp/turso.sqlite',
'turso-laravel.sync_command.node_path' => '/dev/null',
]);

Turso::sync();

Process::assertRan(function (PendingProcess $process) {
$expectedPath = realpath(__DIR__ . '/../..');

expect($process->command)->toBe('node turso-sync.mjs "http://127.0.0.1:8080" "your-access-token" "/tmp/turso.sqlite"')
expect($process->command)->toBe('/dev/null turso-sync.mjs "http://127.0.0.1:8080" "your-access-token" "/tmp/turso.sqlite"')
->and($process->timeout)->toBe(60)
->and($process->path)->toBe($expectedPath);

Expand All @@ -79,14 +82,17 @@
test('it can run the sync background job and call the sync artisan command', function () {
Process::fake();

config(['database.connections.turso.db_replica' => '/tmp/turso.sqlite']);
config([
'database.connections.turso.db_replica' => '/tmp/turso.sqlite',
'turso-laravel.sync_command.node_path' => '/dev/null',
]);

Turso::backgroundSync();

Process::assertRan(function (PendingProcess $process) {
$expectedPath = realpath(__DIR__ . '/../..');

expect($process->command)->toBe('node turso-sync.mjs "http://127.0.0.1:8080" "your-access-token" "/tmp/turso.sqlite"')
expect($process->command)->toBe('/dev/null turso-sync.mjs "http://127.0.0.1:8080" "your-access-token" "/tmp/turso.sqlite"')
->and($process->timeout)->toBe(60)
->and($process->path)->toBe($expectedPath);

Expand Down
23 changes: 18 additions & 5 deletions tests/Unit/TursoSyncCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
test('it can run the cli script to sync the database', function () {
Process::fake();

config(['database.connections.turso.db_replica' => '/tmp/turso.sqlite']);
config([
'database.connections.turso.db_replica' => '/tmp/turso.sqlite',
'turso-laravel.sync_command.node_path' => '/dev/null',
]);

Artisan::call('turso:sync');

Process::assertRan(function (PendingProcess $process) {
$expectedPath = realpath(__DIR__ . '/../..');

expect($process->command)->toBe('node turso-sync.mjs "http://127.0.0.1:8080" "your-access-token" "/tmp/turso.sqlite"')
expect($process->command)->toBe('/dev/null turso-sync.mjs "http://127.0.0.1:8080" "your-access-token" "/tmp/turso.sqlite"')
->and($process->timeout)->toBe(60)
->and($process->path)->toBe($expectedPath);

Expand All @@ -31,9 +34,19 @@
),
]);

config(['database.connections.turso.db_replica' => '/tmp/turso.sqlite']);
config([
'database.connections.turso.db_replica' => '/tmp/turso.sqlite',
'turso-laravel.sync_command.node_path' => '/dev/null',
]);

$result = Artisan::call('turso:sync');
})->throws(RuntimeException::class)->group('TursoSyncCommandTest', 'UnitTest');

expect($result)->toBe(1);
})->group('TursoSyncCommandTest', 'UnitTest');
test('it raises exception on failing to find node executable file', function () {
config([
'database.connections.turso.db_replica' => '/tmp/turso.sqlite',
'turso-laravel.sync_command.node_path' => '/usr/invalid/bin/node',
]);

$result = Artisan::call('turso:sync');
})->throws(RuntimeException::class)->group('TursoSyncCommandTest', 'UnitTest');

0 comments on commit e439f32

Please sign in to comment.