89 lines
2.8 KiB
PHP
Executable File
89 lines
2.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Console;
|
|
|
|
use Config;
|
|
use File;
|
|
use Illuminate\Console\Command;
|
|
use Log;
|
|
|
|
class DeleteOldLabels extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'command:DeleteOldLabelsCmd';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Delete Old Labels Command';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
ini_set('max_execution_time', Config::get('arrays.PHPTimeOutCL'));
|
|
ini_set('max_input_time', Config::get('arrays.PHPTimeOutCL'));
|
|
ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOutCL'));
|
|
ini_set('memory_limit', '1024M');
|
|
|
|
try {
|
|
$this->info('Start Delete Old Labels Command...');
|
|
Log::channel('dailyCron')->info('Cron...Start Delete Old Labels Command...');
|
|
|
|
$labelsDirectoryTmp = '/www/sentm3live/htdocs/labels/';
|
|
$subFolders = ['CarrierLabel/', 'ContentLabel/', 'PackingList/']; // ,'CarrierLabel/','ContentLabel','PackingList',''
|
|
$daysOld = 180; // Number of days to consider a file old : 180 =Six Months
|
|
$filesToDeleteArr = [];
|
|
|
|
foreach ($subFolders as $subFolder) {
|
|
$labelsDirectory = $labelsDirectoryTmp . $subFolder;
|
|
|
|
$this->info('Getting Files From Directory: ' . $labelsDirectory);
|
|
Log::channel('dailyCron')->info('Getting Files From Directory: ' . $labelsDirectory);
|
|
|
|
exec('find ' . $labelsDirectory . ' -ctime +180 -type f -exec rm {} \;');
|
|
}
|
|
|
|
$labelsFolder = $labelsDirectoryTmp;
|
|
// Get Files from the specified Directory
|
|
$filesToDelete = File::allFiles($labelsFolder);
|
|
|
|
foreach ($filesToDelete as $file) {
|
|
$lastAccessTime = new \DateTime('@' . $file->getATime());
|
|
$now = new \DateTime;
|
|
$diff = $now->diff($lastAccessTime)->days;
|
|
|
|
if ($diff >= $daysOld) {
|
|
$this->info('File: ' . $file . ' is Days Old: ' . $diff);
|
|
// Log::channel('dailyCron')->info("File: ".$file." is Days Old: ".$diff);
|
|
// Add the File to Array
|
|
$filesToDeleteArr[] = $file;
|
|
}
|
|
}
|
|
|
|
// Delete Old Files
|
|
File::delete($filesToDeleteArr);
|
|
|
|
Log::channel('dailyCron')->info('Cron...End Delete Old Labels Command...');
|
|
$this->info('End Delete Old Labels Command...');
|
|
|
|
} catch (\Throwable $th) {
|
|
Log::channel('dailyCron')->error($th);
|
|
}
|
|
}
|
|
}
|