Add settings-driven Artisan scheduler registry with Etsy sync and token refresh jobs
Introduces App\Support\DashboardSchedule (modelled on LHITS_Dashboard's pattern) as a single settings-driven registry for recurring Artisan jobs, replacing the two hard-coded Schedule::command() lines in routes/console.php. Migrated unchanged: - integrations_refresh (integrations:refresh), every_30_seconds, withoutOverlapping(5) - alerts_scan (alerts:scan), every_30_seconds, withoutOverlapping(5) New: - etsy_sync (etsy:sync), every_15_minutes - forces a fresh pull of Etsy receipts/listings into the existing IntegrationCache entries, decoupled from the bundled integrations:refresh job so it can be tuned/disabled independently of Parcel2Go/BamBuddy/dryer refreshes. - etsy_token_refresh (etsy:refresh-token), every_45_minutes - proactively refreshes the Etsy OAuth token (tokens last ~1hr) via a new public Etsy::refreshAccessToken() wrapper around the existing on-demand accessToken() refresh logic, so the connection survives quiet periods. Frequency is persisted to storage/app/dashboard-schedules.json (no app_settings table exists in this app, unlike LHITS's AppSetting-backed store) and surfaced in a new 'Scheduler' section on the existing Settings page (ScheduleSettingsController + routes), with per-job frequency dropdowns and manual 'Run now' buttons, matching the page's existing Tailwind conventions. Tests: SchedulerTest (registry/frequency behavior), ScheduleSettingsTest (settings UI + authorization), EtsySyncScheduledJobsTest (new commands, Http::fake only, no live Etsy calls).
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\EtsyAppCredential;
|
||||
use App\Models\EtsyConnection;
|
||||
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EtsySyncScheduledJobsTest extends TestCase
|
||||
{
|
||||
use LazilyRefreshDatabase;
|
||||
|
||||
public function test_etsy_sync_skips_cleanly_when_shop_is_not_connected(): void
|
||||
{
|
||||
Http::fake(fn () => Http::response([], 500));
|
||||
|
||||
$this->artisan('etsy:sync')->assertExitCode(0);
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_etsy_sync_refreshes_receipts_and_listings(): void
|
||||
{
|
||||
$this->connectShop();
|
||||
|
||||
Http::fake([
|
||||
'*/receipts*' => Http::response(['count' => 1, 'results' => [[
|
||||
'receipt_id' => 1, 'status' => 'open', 'was_paid' => true, 'was_shipped' => false,
|
||||
]]]),
|
||||
'*/listings*' => Http::response(['count' => 1, 'results' => [['listing_id' => 11]]]),
|
||||
]);
|
||||
|
||||
$this->artisan('etsy:sync')->assertExitCode(0);
|
||||
|
||||
Http::assertSentCount(2);
|
||||
}
|
||||
|
||||
public function test_etsy_refresh_token_skips_cleanly_when_shop_is_not_connected(): void
|
||||
{
|
||||
Http::fake(fn () => Http::response([], 500));
|
||||
|
||||
$this->artisan('etsy:refresh-token')->assertExitCode(0);
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_etsy_refresh_token_proactively_refreshes_an_expired_token(): void
|
||||
{
|
||||
$connection = $this->connectShop(expiresAt: now()->subMinute());
|
||||
|
||||
Http::fake([
|
||||
'*/oauth/token' => Http::response([
|
||||
'access_token' => 'new-access-token',
|
||||
'refresh_token' => 'new-refresh-token',
|
||||
'expires_in' => 3600,
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->artisan('etsy:refresh-token')->assertExitCode(0);
|
||||
|
||||
Http::assertSentCount(1);
|
||||
$connection->refresh();
|
||||
$this->assertSame('new-access-token', $connection->access_token);
|
||||
$this->assertTrue($connection->expires_at->isFuture());
|
||||
}
|
||||
|
||||
public function test_etsy_refresh_token_does_not_call_etsy_when_token_is_still_fresh(): void
|
||||
{
|
||||
$this->connectShop(expiresAt: now()->addMinutes(30));
|
||||
|
||||
Http::fake();
|
||||
|
||||
$this->artisan('etsy:refresh-token')->assertExitCode(0);
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
|
||||
private function connectShop(?Carbon $expiresAt = null): EtsyConnection
|
||||
{
|
||||
EtsyAppCredential::create([
|
||||
'id' => 1,
|
||||
'keystring' => 'key',
|
||||
'shared_secret' => 'secret',
|
||||
'shop_name' => 'HickLab3D',
|
||||
]);
|
||||
|
||||
return EtsyConnection::create([
|
||||
'shop_id' => 123,
|
||||
'shop_name' => 'HickLab3D',
|
||||
'access_token' => 'token',
|
||||
'refresh_token' => 'refresh',
|
||||
'expires_at' => $expiresAt ?? now()->addHour(),
|
||||
'scopes' => 'listings_r listings_w transactions_r transactions_w shops_r',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Support\DashboardSchedule;
|
||||
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ScheduleSettingsTest extends TestCase
|
||||
{
|
||||
use LazilyRefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
File::delete(DashboardSchedule::runtimeSettingsPath());
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
File::delete(DashboardSchedule::runtimeSettingsPath());
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_non_admin_cannot_view_or_change_scheduler_settings(): void
|
||||
{
|
||||
$this->get(route('settings.index'))->assertRedirect(route('login'));
|
||||
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user)->get(route('settings.index'))->assertForbidden();
|
||||
$this->actingAs($user)->put(route('settings.schedules.update'), [
|
||||
'schedules' => ['etsy_sync' => 'hourly'],
|
||||
])->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_admin_can_view_scheduler_section(): void
|
||||
{
|
||||
$response = $this
|
||||
->actingAs(User::factory()->create(['role' => 'admin']))
|
||||
->get(route('settings.index'));
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertSee('Scheduler');
|
||||
$response->assertSee('etsy:sync');
|
||||
$response->assertSee('etsy:refresh-token');
|
||||
$response->assertSee('integrations:refresh');
|
||||
$response->assertSee('Run now');
|
||||
}
|
||||
|
||||
public function test_admin_can_change_job_frequencies(): void
|
||||
{
|
||||
$payload = collect(DashboardSchedule::jobs())
|
||||
->mapWithKeys(fn (array $job, string $key): array => [$key => $job['default_frequency']])
|
||||
->all();
|
||||
|
||||
$payload['etsy_sync'] = 'hourly';
|
||||
$payload['etsy_token_refresh'] = 'disabled';
|
||||
|
||||
$response = $this
|
||||
->actingAs(User::factory()->create(['role' => 'admin']))
|
||||
->put(route('settings.schedules.update'), ['schedules' => $payload]);
|
||||
|
||||
$response->assertRedirect(route('settings.index'));
|
||||
$this->assertSame('hourly', DashboardSchedule::frequencyFor('etsy_sync'));
|
||||
$this->assertSame('disabled', DashboardSchedule::frequencyFor('etsy_token_refresh'));
|
||||
$this->assertFileExists(DashboardSchedule::runtimeSettingsPath());
|
||||
}
|
||||
|
||||
public function test_admin_can_manually_run_a_job(): void
|
||||
{
|
||||
$response = $this
|
||||
->actingAs(User::factory()->create(['role' => 'admin']))
|
||||
->post(route('settings.schedules.run', 'alerts_scan'));
|
||||
|
||||
$response->assertRedirect(route('settings.index'));
|
||||
$response->assertSessionHas('status');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Support\DashboardSchedule;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SchedulerTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
File::delete(DashboardSchedule::runtimeSettingsPath());
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
// Boot (parent::setUp()) registers the schedule by reading this file
|
||||
// before the next test's setUp() gets a chance to delete it, so clean
|
||||
// up here too or a dirty write leaks into the next test's boot.
|
||||
File::delete(DashboardSchedule::runtimeSettingsPath());
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_registry_schedules_all_default_jobs(): void
|
||||
{
|
||||
$summaries = collect(app(Schedule::class)->events())
|
||||
->map(fn ($event): string => $event->getSummaryForDisplay());
|
||||
|
||||
$this->assertCount(4, $summaries);
|
||||
$this->assertScheduleContains($summaries, 'integrations:refresh');
|
||||
$this->assertScheduleContains($summaries, 'alerts:scan');
|
||||
$this->assertScheduleContains($summaries, 'etsy:sync');
|
||||
$this->assertScheduleContains($summaries, 'etsy:refresh-token');
|
||||
}
|
||||
|
||||
public function test_existing_jobs_keep_their_thirty_second_cadence_and_overlap_guard(): void
|
||||
{
|
||||
$events = collect(app(Schedule::class)->events());
|
||||
|
||||
$refresh = $events->first(fn ($event): bool => str_contains((string) $event->command, 'integrations:refresh'));
|
||||
$alerts = $events->first(fn ($event): bool => str_contains((string) $event->command, 'alerts:scan'));
|
||||
|
||||
$this->assertNotNull($refresh);
|
||||
$this->assertNotNull($alerts);
|
||||
$this->assertSame('* * * * *', $refresh->expression);
|
||||
$this->assertSame(30, $refresh->repeatSeconds);
|
||||
$this->assertSame('* * * * *', $alerts->expression);
|
||||
$this->assertSame(30, $alerts->repeatSeconds);
|
||||
$this->assertTrue($refresh->withoutOverlapping);
|
||||
$this->assertSame(5, $refresh->expiresAt);
|
||||
$this->assertTrue($alerts->withoutOverlapping);
|
||||
$this->assertSame(5, $alerts->expiresAt);
|
||||
}
|
||||
|
||||
public function test_etsy_jobs_use_their_default_frequencies(): void
|
||||
{
|
||||
$events = collect(app(Schedule::class)->events());
|
||||
|
||||
$sync = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:sync'));
|
||||
$tokenRefresh = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:refresh-token'));
|
||||
|
||||
$this->assertNotNull($sync);
|
||||
$this->assertNotNull($tokenRefresh);
|
||||
$this->assertSame('*/15 * * * *', $sync->expression);
|
||||
$this->assertSame('*/45 * * * *', $tokenRefresh->expression);
|
||||
}
|
||||
|
||||
public function test_changing_a_stored_frequency_changes_what_gets_scheduled(): void
|
||||
{
|
||||
DashboardSchedule::persistRuntimeFrequencies([
|
||||
'etsy_sync' => 'hourly',
|
||||
'etsy_token_refresh' => 'disabled',
|
||||
]);
|
||||
|
||||
// The schedule singleton is built once at boot (before this test writes
|
||||
// the settings file), so force a fresh Schedule and re-run the registry
|
||||
// against it to see the just-persisted frequencies take effect, the way
|
||||
// the next real `schedule:run` boot cycle would.
|
||||
app()->forgetInstance(Schedule::class);
|
||||
\Illuminate\Support\Facades\Schedule::clearResolvedInstance(Schedule::class);
|
||||
DashboardSchedule::register();
|
||||
|
||||
$events = collect(app(Schedule::class)->events());
|
||||
|
||||
$sync = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:sync'));
|
||||
$tokenRefresh = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:refresh-token'));
|
||||
|
||||
$this->assertNotNull($sync);
|
||||
$this->assertSame('0 * * * *', $sync->expression);
|
||||
$this->assertNull($tokenRefresh);
|
||||
}
|
||||
|
||||
private function assertScheduleContains($summaries, string $expected): void
|
||||
{
|
||||
$this->assertTrue(
|
||||
$summaries->contains(fn (string $summary): bool => str_contains($summary, $expected)),
|
||||
"Failed asserting that the scheduler contains [{$expected}]."
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user