From 97247b66c787e848d1ad9c87715f5c59820b695b Mon Sep 17 00:00:00 2001 From: Matteo Gheza Date: Thu, 23 Feb 2023 22:19:42 +0100 Subject: [PATCH] Add first scheduled job --- backend/.env.example | 3 ++ backend/app/Console/Kernel.php | 3 +- .../Jobs/IncrementAvailabilityMinutesJob.php | 34 +++++++++++++++++++ backend/config/cron.php | 31 +++++++++++++++++ backend/routes/api.php | 11 ++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 backend/app/Jobs/IncrementAvailabilityMinutesJob.php create mode 100644 backend/config/cron.php diff --git a/backend/.env.example b/backend/.env.example index def93c9..c22b74b 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -6,6 +6,9 @@ APP_URL=http://localhost SANCTUM_STATEFUL_DOMAINS=localhost:4200,allertavvf.test +CRON_EXTERNAL_ENABLE=false +CRON_EXEC_CODE=REPLACE_THIS_IF_USING_EXTERNAL_CRON_SERVICE + LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug diff --git a/backend/app/Console/Kernel.php b/backend/app/Console/Kernel.php index e6b9960..7586ecd 100644 --- a/backend/app/Console/Kernel.php +++ b/backend/app/Console/Kernel.php @@ -4,6 +4,7 @@ namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; +use App\Jobs\IncrementAvailabilityMinutesJob; class Kernel extends ConsoleKernel { @@ -12,7 +13,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule): void { - // $schedule->command('inspire')->hourly(); + $schedule->job(new IncrementAvailabilityMinutesJob)->everyMinute(); } /** diff --git a/backend/app/Jobs/IncrementAvailabilityMinutesJob.php b/backend/app/Jobs/IncrementAvailabilityMinutesJob.php new file mode 100644 index 0000000..356c772 --- /dev/null +++ b/backend/app/Jobs/IncrementAvailabilityMinutesJob.php @@ -0,0 +1,34 @@ +where('available', 1) + ->where('banned', 0) + ->increment('availability_minutes'); + } +} diff --git a/backend/config/cron.php b/backend/config/cron.php new file mode 100644 index 0000000..2c95887 --- /dev/null +++ b/backend/config/cron.php @@ -0,0 +1,31 @@ + env('CRON_EXTERNAL_ENABLE', false), + + /* + |-------------------------------------------------------------------------- + | Cron execution code + |-------------------------------------------------------------------------- + | + | Using this code you can prevent random users from running the scheduler + | and ruining your scheduled jobs execution. + | Remember to set this in your external Cron software, using the + | "Cron" header. + | + */ + + 'execution_code' => env('CRON_EXEC_CODE', env('APP_KEY')), +]; diff --git a/backend/routes/api.php b/backend/routes/api.php index 596913f..87fbbf0 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -4,6 +4,8 @@ use Illuminate\Support\Facades\Route; use App\Http\Controllers\AuthController; use App\Http\Controllers\UserController; use App\Http\Controllers\AvailabilityController; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Artisan; /* |-------------------------------------------------------------------------- @@ -31,3 +33,12 @@ Route::middleware('auth:sanctum')->group( function () { Route::post('/logout', [AuthController::class, 'logout']); }); + +Route::post('/cron/execute', function(Request $request) { + //Go to app/Console/Kernel.php to view schedules + if(config('cron.external_cron_enabled') && $request->header('Cron') == config('cron.execution_code')) { + Artisan::call('schedule:run'); + } else { + return response('Access Denied', 403); + } +});