81 lines
2.1 KiB
PHP
81 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Contact;
|
|
use App\Models\Log as LogModel;
|
|
use App\Models\Customers;
|
|
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;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class CleanOldDatasJob implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
// Construct method.
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
try {
|
|
DB::transaction(function () {
|
|
// Delete old contacts.
|
|
$deletedContact = Contact::query()
|
|
->where('contact_updatedate', '<', now()->subYears(3))
|
|
->delete();
|
|
Log::info(\sprintf(
|
|
'CleanOldDatasJob : %s contacts deleted',
|
|
$deletedContact
|
|
));
|
|
|
|
// Delete old customers.
|
|
$deletedCustomers = Customers::query()
|
|
->where('updated_at', '<', now()->subYears(3))
|
|
->delete();
|
|
Log::info(\sprintf(
|
|
'CleanOldDatasJob : %s customers deleted',
|
|
$deletedCustomers
|
|
));
|
|
|
|
// Delete old logs.
|
|
$deletedLogs = LogModel::query()
|
|
->where('created_at', '<', now()->subYears(3))
|
|
->delete();
|
|
Log::info(\sprintf(
|
|
'CleanOldDatasJob : %s logs deleted',
|
|
$deletedLogs
|
|
));
|
|
});
|
|
} catch (\Exception $e) {
|
|
Log::error(
|
|
sprintf(
|
|
"CleanOldDatasJob failed - Code : %s - %s",
|
|
$e->getCode(),
|
|
$e->getMessage(),
|
|
)
|
|
);
|
|
\report($e);
|
|
} //end try
|
|
}
|
|
}
|