Add first scheduled job

This commit is contained in:
Matteo Gheza 2023-02-23 22:19:42 +01:00
parent be4701cce0
commit 97247b66c7
5 changed files with 81 additions and 1 deletions

View File

@ -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

View File

@ -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();
}
/**

View File

@ -0,0 +1,34 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
class IncrementAvailabilityMinutesJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
DB::table('users')
->where('available', 1)
->where('banned', 0)
->increment('availability_minutes');
}
}

31
backend/config/cron.php Normal file
View File

@ -0,0 +1,31 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Enable external Cron
|--------------------------------------------------------------------------
|
| If you enable this, you can use external Cron software like cron-job.org
| to trigger the scheduler and run scheduled jobs.
| For more information about running scheduled tasks, visit
| https://laravel.com/docs/10.x/scheduling#running-the-scheduler
|
*/
'external_cron_enabled' => 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')),
];

View File

@ -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);
}
});