[Fix] Internal cron scheduler blocking view responses
Internal cron is now triggered by an axios request from the frontend so it can truly run in a separate request. Fixes #167
This commit is contained in:
@@ -18,6 +18,7 @@ public function handle(CaldavService $service)
|
||||
// only run every 5 minutes although the task is called every minute
|
||||
$cacheKey = 'caldav_sync_last_run';
|
||||
if (\Illuminate\Support\Facades\Cache::has($cacheKey)) {
|
||||
Log::info('CalDAV sync Throttled');
|
||||
return 0;
|
||||
}
|
||||
Cache::put($cacheKey, true, 300);
|
||||
@@ -33,6 +34,7 @@ public function handle(CaldavService $service)
|
||||
$count++;
|
||||
}
|
||||
|
||||
Log::info("Synced " . count($todos) . " todos.");
|
||||
$this->info("Synced " . count($todos) . " todos.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Inspiring;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Middleware;
|
||||
|
||||
@@ -39,10 +39,9 @@ public function share(Request $request): array
|
||||
return [
|
||||
...parent::share($request),
|
||||
'name' => config('app.name'),
|
||||
'auth' => [
|
||||
'user' => $request->user(),
|
||||
],
|
||||
'auth' => ['user' => $request->user(),],
|
||||
'sidebarOpen' => ! $request->hasCookie('sidebar_state') || $request->cookie('sidebar_state') === 'true',
|
||||
'cron' => Setting::get('app.cron_method') === 'request'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Models\Setting;
|
||||
|
||||
|
||||
class ScheduleListener
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* This listener is attached to RouteMatched when app.schedule_method == 'internal'.
|
||||
* It will trigger the scheduler at most once per minute (throttle via cache).
|
||||
*/
|
||||
public function handle($event)
|
||||
{
|
||||
// TODO: this check is also done, when registering the listener EventServiceProvider.php
|
||||
// it can probably be removed here safely which would spare a database call on each request
|
||||
|
||||
// only run when internal scheduling is enabled (defensive)
|
||||
$method = Setting::where('key', 'app.schedule_method')->value('value') ?? 'internal';
|
||||
if ($method !== 'internal') {
|
||||
return;
|
||||
}
|
||||
|
||||
// throttle key: run at most once every 55 seconds
|
||||
$cacheKey = 'caramel_scheduler_last_run';
|
||||
$ttlSeconds = 55;
|
||||
|
||||
if (Cache::has($cacheKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// mark as run
|
||||
Cache::put($cacheKey, true, $ttlSeconds);
|
||||
|
||||
try {
|
||||
Log::info('Triggering scheduler via ScheduleListener');
|
||||
Artisan::call('schedule:run');
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Error running scheduler: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,6 @@
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Schedule;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Routing\Events\RouteMatched;
|
||||
use App\Models\Setting;
|
||||
use App\Listeners\ScheduleListener;
|
||||
use App\Jobs\CheckInvoiceDueDatesJob;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
@@ -22,16 +17,9 @@ public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
if (Schema::hasTable('settings')) {
|
||||
$method = Setting::where('key', 'app.schedule_method')->value('value') ?? 'internal';
|
||||
if ($method === 'internal') {
|
||||
Event::listen(RouteMatched::class, [ScheduleListener::class, 'handle']);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: read where to put these or ask in the forums
|
||||
// it seems to work here, but where is the apropriate place?
|
||||
// Kernel::schedule did not work
|
||||
// // TODO: read where to put these or ask in the forums
|
||||
// // it seems to work here, but where is the apropriate place?
|
||||
// // Kernel::schedule did not work
|
||||
Schedule::command('caldav:sync')
|
||||
->everyMinute()
|
||||
->withoutOverlapping();
|
||||
|
||||
Reference in New Issue
Block a user