feat(actions): add schedule:sync command — persists #[Scheduled] to database

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Snider 2026-03-12 12:41:17 +00:00
parent 9ffb756969
commit d1598882bb
3 changed files with 202 additions and 0 deletions

View file

@ -30,5 +30,6 @@ class Boot
$event->command(Commands\MakePlugCommand::class); $event->command(Commands\MakePlugCommand::class);
$event->command(Commands\MakeWebsiteCommand::class); $event->command(Commands\MakeWebsiteCommand::class);
$event->command(Commands\PruneEmailShieldStatsCommand::class); $event->command(Commands\PruneEmailShieldStatsCommand::class);
$event->command(Commands\ScheduleSyncCommand::class);
} }
} }

View file

@ -0,0 +1,90 @@
<?php
/*
* Core PHP Framework
*
* Licensed under the European Union Public Licence (EUPL) v1.2.
* See LICENSE file for details.
*/
declare(strict_types=1);
namespace Core\Console\Commands;
use Core\Actions\ScheduledAction;
use Core\Actions\ScheduledActionScanner;
use Illuminate\Console\Command;
/**
* Sync #[Scheduled] attribute declarations to the database.
*
* Scans configured paths for Action classes with the #[Scheduled] attribute
* and upserts them into the scheduled_actions table. Run during deploy/migration.
*/
class ScheduleSyncCommand extends Command
{
protected $signature = 'schedule:sync';
protected $description = 'Sync #[Scheduled] action attributes to the database';
public function handle(ScheduledActionScanner $scanner): int
{
$paths = config('core.scheduled_action_paths');
if ($paths === null) {
$paths = [
app_path('Core'),
app_path('Mod'),
app_path('Website'),
];
// Also scan framework paths
$frameworkSrc = dirname(__DIR__, 3);
$paths[] = $frameworkSrc.'/Core';
$paths[] = $frameworkSrc.'/Mod';
}
$discovered = $scanner->scan($paths);
$added = 0;
$disabled = 0;
$unchanged = 0;
// Upsert discovered actions
foreach ($discovered as $class => $attribute) {
$existing = ScheduledAction::where('action_class', $class)->first();
if ($existing) {
$unchanged++;
continue;
}
ScheduledAction::create([
'action_class' => $class,
'frequency' => $attribute->frequency,
'timezone' => $attribute->timezone,
'without_overlapping' => $attribute->withoutOverlapping,
'run_in_background' => $attribute->runInBackground,
'is_enabled' => true,
]);
$added++;
}
// Disable actions no longer in codebase
$discoveredClasses = array_keys($discovered);
$stale = ScheduledAction::where('is_enabled', true)
->whereNotIn('action_class', $discoveredClasses)
->get();
foreach ($stale as $action) {
$action->update(['is_enabled' => false]);
$disabled++;
}
$this->info("Schedule sync complete: {$added} added, {$disabled} disabled, {$unchanged} unchanged.");
return Command::SUCCESS;
}
}

View file

@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Core\Tests\Feature;
use Core\Actions\ScheduledAction;
use Core\Console\Commands\ScheduleSyncCommand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase;
class ScheduleSyncCommandTest extends TestCase
{
use RefreshDatabase;
protected function defineDatabaseMigrations(): void
{
$this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
}
protected function defineEnvironment($app): void
{
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
]);
// Point scanner at test fixtures only
$app['config']->set('core.scheduled_action_paths', [
__DIR__.'/../Fixtures/Mod/Scheduled',
]);
}
protected function setUp(): void
{
parent::setUp();
$this->app->make(\Illuminate\Contracts\Console\Kernel::class)->registerCommand(
$this->app->make(ScheduleSyncCommand::class)
);
}
public function test_sync_inserts_new_scheduled_actions(): void
{
$this->artisan('schedule:sync')
->assertSuccessful();
$this->assertDatabaseHas('scheduled_actions', [
'action_class' => 'Core\\Tests\\Fixtures\\Mod\\Scheduled\\Actions\\EveryMinuteAction',
'frequency' => 'everyMinute',
'is_enabled' => true,
]);
$this->assertDatabaseHas('scheduled_actions', [
'action_class' => 'Core\\Tests\\Fixtures\\Mod\\Scheduled\\Actions\\DailyAction',
'frequency' => 'dailyAt:09:00',
'timezone' => 'Europe/London',
]);
}
public function test_sync_disables_removed_actions(): void
{
// Pre-populate with an action that no longer exists
ScheduledAction::create([
'action_class' => 'App\\Actions\\RemovedAction',
'frequency' => 'hourly',
'is_enabled' => true,
]);
$this->artisan('schedule:sync')
->assertSuccessful();
$this->assertDatabaseHas('scheduled_actions', [
'action_class' => 'App\\Actions\\RemovedAction',
'is_enabled' => false,
]);
}
public function test_sync_preserves_manually_edited_frequency(): void
{
// Pre-populate with a manually edited action
ScheduledAction::create([
'action_class' => 'Core\\Tests\\Fixtures\\Mod\\Scheduled\\Actions\\EveryMinuteAction',
'frequency' => 'hourly', // Manually changed from everyMinute
'is_enabled' => true,
]);
$this->artisan('schedule:sync')
->assertSuccessful();
// Should preserve the manual edit
$this->assertDatabaseHas('scheduled_actions', [
'action_class' => 'Core\\Tests\\Fixtures\\Mod\\Scheduled\\Actions\\EveryMinuteAction',
'frequency' => 'hourly',
]);
}
public function test_sync_is_idempotent(): void
{
$this->artisan('schedule:sync')->assertSuccessful();
$this->artisan('schedule:sync')->assertSuccessful();
$count = ScheduledAction::where(
'action_class',
'Core\\Tests\\Fixtures\\Mod\\Scheduled\\Actions\\EveryMinuteAction'
)->count();
$this->assertSame(1, $count);
}
}