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:
2026-11-07 17:38:11 +00:00
parent 9b13640571
commit 960189d14b
12 changed files with 681 additions and 5 deletions
@@ -8,6 +8,7 @@ use App\Models\IntegrationSetting;
use App\Services\Etsy;
use App\Services\IntegrationSettings;
use App\Services\Parcel2Go;
use App\Support\DashboardSchedule;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
@@ -24,8 +25,13 @@ class EtsySettingsController extends Controller
$parcel2go = $settings->for('parcel2go');
$bambuddy = $settings->for('bambuddy');
$dryer = $settings->for('dryer');
$scheduleJobs = collect(DashboardSchedule::jobs())
->map(fn (array $job, string $key): array => [...$job, 'key' => $key, 'frequency' => DashboardSchedule::frequencyFor($key)])
->values()->all();
return view('settings.index', [
'scheduleJobs' => $scheduleJobs,
'scheduleFrequencyOptions' => DashboardSchedule::frequencyOptions(),
'storedCredentials' => $credentials !== null,
'configured' => $etsy->configured(),
'shopConnected' => EtsyConnection::query()->exists(),
@@ -0,0 +1,76 @@
<?php
namespace App\Http\Controllers;
use App\Support\DashboardSchedule;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Throwable;
class ScheduleSettingsController extends Controller
{
public function update(Request $request): RedirectResponse
{
abort_unless($request->user()->role === 'admin', 403);
$jobKeys = array_keys(DashboardSchedule::jobs());
$frequencyKeys = array_keys(DashboardSchedule::frequencyOptions());
$validated = $request->validate([
'schedules' => ['required', 'array'],
'schedules.*' => ['required', 'string', Rule::in($frequencyKeys)],
]);
$frequencies = collect($validated['schedules'])
->only($jobKeys)
->all();
DashboardSchedule::persistRuntimeFrequencies($frequencies);
return redirect()
->route('settings.index')
->with('status', 'Schedule settings saved. Laravel will apply them on the next scheduler tick.');
}
public function run(Request $request, string $job): RedirectResponse
{
abort_unless($request->user()->role === 'admin', 403);
$definition = DashboardSchedule::jobs()[$job] ?? null;
abort_unless($definition !== null && ($definition['manual'] ?? false) === true, 404);
$command = $definition['command'] ?? null;
abort_unless(is_string($command) && $command !== '', 404);
try {
$exitCode = Artisan::call($command);
$output = Str::limit(trim(preg_replace('/\s+/', ' ', Artisan::output()) ?? ''), 220);
} catch (Throwable $exception) {
Log::warning('Manual scheduled job failed.', [
'job' => $job,
'command' => $command,
'error' => $exception->getMessage(),
]);
return redirect()
->route('settings.index')
->withErrors(['schedule' => $definition['name'].' failed: '.$exception->getMessage()]);
}
if ($exitCode !== 0) {
return redirect()
->route('settings.index')
->withErrors(['schedule' => $definition['name'].' finished with exit code '.$exitCode.'. '.$output]);
}
return redirect()
->route('settings.index')
->with('status', $definition['name'].' completed.'.($output !== '' ? ' '.$output : ''));
}
}