2021-03-03 04:43:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Cron\Hourly;
|
|
|
|
|
|
|
|
use App\Contracts\Listener;
|
|
|
|
use App\Events\CronHourly;
|
|
|
|
use App\Models\Enums\PirepState;
|
|
|
|
use App\Models\Pirep;
|
|
|
|
use App\Services\PirepService;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove cancelled/deleted PIREPs. Look for PIREPs that were created before the setting time
|
|
|
|
* (e.g, 12 hours ago) and are marked with the
|
|
|
|
*/
|
|
|
|
class DeletePireps extends Listener
|
|
|
|
{
|
|
|
|
/**
|
2021-04-14 20:37:48 +08:00
|
|
|
* Delete old rejected PIREPs
|
2021-03-03 04:43:34 +08:00
|
|
|
*
|
|
|
|
* @param CronHourly $event
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function handle(CronHourly $event): void
|
|
|
|
{
|
|
|
|
$this->deletePireps(setting('pireps.delete_rejected_hours'), PirepState::REJECTED);
|
|
|
|
$this->deletePireps(setting('pireps.delete_cancelled_hours'), PirepState::CANCELLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look for and delete PIREPs which match the criteria
|
|
|
|
*
|
|
|
|
* @param int $expire_time_hours The time in hours to look for PIREPs
|
|
|
|
* @param int $state The PirepState enum value
|
|
|
|
*/
|
|
|
|
protected function deletePireps(int $expire_time_hours, int $state)
|
|
|
|
{
|
2021-04-13 21:25:38 +08:00
|
|
|
$dt = Carbon::now('UTC')->subHours($expire_time_hours);
|
2021-04-10 07:15:22 +08:00
|
|
|
$pireps = Pirep::where('created_at', '<', $dt)->where(['state' => $state])->get();
|
2021-03-03 04:43:34 +08:00
|
|
|
|
|
|
|
/** @var PirepService $pirepSvc */
|
|
|
|
$pirepSvc = app(PirepService::class);
|
|
|
|
|
|
|
|
/** @var Pirep $pirep */
|
|
|
|
foreach ($pireps as $pirep) {
|
|
|
|
Log::info('Cron: Deleting PIREP id='.$pirep->id.', state='.PirepState::label($state));
|
|
|
|
$pirepSvc->delete($pirep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|