|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Jobs; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use App\Models\WebhookEvent; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | +use Illuminate\Support\Facades\Log; |
| 9 | +use Illuminate\Queue\SerializesModels; |
| 10 | +use Illuminate\Queue\InteractsWithQueue; |
| 11 | +use Illuminate\Foundation\Queue\Queueable; |
| 12 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 13 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 14 | + |
| 15 | +class CleanWebhookEventsJob implements ShouldQueue |
| 16 | +{ |
| 17 | + use Queueable, InteractsWithQueue, SerializesModels, Dispatchable; |
| 18 | + |
| 19 | + private int $retentionPeriod; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new job instance. |
| 23 | + * |
| 24 | + * @param int $retentionPeriod |
| 25 | + */ |
| 26 | + public function __construct(int $retentionPeriod) |
| 27 | + { |
| 28 | + $this->retentionPeriod = $retentionPeriod; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Execute the job. |
| 33 | + */ |
| 34 | + public function handle() |
| 35 | + { |
| 36 | + Log::info("CleanWebhookEventsJob iniciado."); |
| 37 | + |
| 38 | + // Calcula a data limite com base no período de retenção |
| 39 | + $dateLimit = Carbon::now()->subDays($this->retentionPeriod); |
| 40 | + |
| 41 | + // Exclui os eventos mais antigos que a data limite |
| 42 | + $deletedCount = WebhookEvent::where('created_at', '<', $dateLimit)->delete(); |
| 43 | + |
| 44 | + Log::info("{$deletedCount} registros excluídos."); |
| 45 | + |
| 46 | + // Log para auditoria (opcional) |
| 47 | + Log::info("{$deletedCount} registros de webhook mais antigos que {$this->retentionPeriod} dias foram excluídos."); |
| 48 | + } |
| 49 | +} |
0 commit comments