128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Enums\SigdStatus;
|
|
use App\Models\ActivityCopy;
|
|
use App\Models\ActivityLock;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class CopyActivityQueue extends Command
|
|
{
|
|
protected $signature = 'copy_activity:process';
|
|
protected $description = 'Process for copy the activities queue';
|
|
private $logFilePath;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
set_time_limit(0);
|
|
|
|
$processing = ActivityCopy::where('status', SigdStatus::PROSES)
|
|
->rowActive()
|
|
->first();
|
|
|
|
if ($processing) {
|
|
return;
|
|
}
|
|
|
|
$process = ActivityCopy::where('status', SigdStatus::PENDING)
|
|
->rowActive()
|
|
->orderBy('created_at', 'asc')
|
|
->first();
|
|
|
|
if (!$process) {
|
|
return;
|
|
}
|
|
|
|
$process->update([
|
|
'executed_time' => now(),
|
|
'status' => SigdStatus::PROSES,
|
|
]);
|
|
|
|
$this->createLogFile($process);
|
|
$this->logMessage('info', 'Salin data aktivitas untuk Tahun: ' . $process->to_year . ', dari sumber Tahun: ' . $process->from_year . ', id: ' . $process->id);
|
|
|
|
try {
|
|
$isLocked = ActivityLock::isLocked($process->to_year);
|
|
if ($isLocked) {
|
|
$this->logMessage('warning', 'Salin data aktivitas untuk Tahun: ' . $process->to_year . ', dari sumber Tahun: ' . $process->from_year . ', id: ' . $process->id . ' sedang dikunci dan tidak dapat melakukan salin data.');
|
|
|
|
$process->update([
|
|
'finished_time' => now(),
|
|
'status' => SigdStatus::GAGAL,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->executeService($process);
|
|
|
|
$process->update([
|
|
'finished_time' => now(),
|
|
'status' => SigdStatus::SELESAI,
|
|
]);
|
|
|
|
$this->logMessage('info', 'Salin data aktivitas untuk Tahun: ' . $process->to_year . ', dari sumber Tahun: ' . $process->from_year . ', id: ' . $process->id . ' berhasil diselesaikan.');
|
|
} catch (\Exception $e) {
|
|
$this->logMessage('error', 'Terjadi kesalahan saat proses salin data aktivitas, id: ' . $process->id . '. Error: ' . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function createLogFile(ActivityCopy $process)
|
|
{
|
|
$logDirectory = storage_path('logs/copy/');
|
|
|
|
if (!File::exists($logDirectory)) {
|
|
File::makeDirectory($logDirectory, 0755, true);
|
|
}
|
|
|
|
$logFileName = $logDirectory . $process->to_year . '_' . $process->from_year . '_' . $process->id . '.log';
|
|
file_put_contents($logFileName, '');
|
|
|
|
$this->logFilePath = $logFileName;
|
|
}
|
|
|
|
private function executeService($process)
|
|
{
|
|
$service = "\\App\\Services\\Tool\\CopyActivityService";
|
|
|
|
try {
|
|
$serviceClass = app($service);
|
|
if (method_exists($serviceClass, 'copyAllData')) {
|
|
$serviceClass->copyAllData($process->from_year, $process->to_year);
|
|
} else {
|
|
$this->logMessage('error', get_class($serviceClass) . " tidak memiliki method copyAllData.");
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logMessage('error', "Error saat menjalankan " . get_class($serviceClass) . ": " . $e->getMessage());
|
|
|
|
$process->update([
|
|
'executed_time' => null,
|
|
'finished_time' => null,
|
|
'status' => SigdStatus::PENDING,
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function logMessage($level, $message)
|
|
{
|
|
$this->line($message); // Output to console
|
|
|
|
// Log to the file
|
|
$formattedMessage = '[' . now() . '] ' . strtoupper($level) . ': ' . $message . PHP_EOL;
|
|
if ($this->logFilePath) {
|
|
file_put_contents($this->logFilePath, $formattedMessage, FILE_APPEND);
|
|
}
|
|
}
|
|
}
|