99 lines
3 KiB
PHP
99 lines
3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Jobs;
|
||
|
|
|
||
|
|
use App\Models\Entry;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Foundation\Queue\Queueable;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
use Illuminate\Support\Facades\Process;
|
||
|
|
use Symfony\Component\Console\Exception\RuntimeException;
|
||
|
|
|
||
|
|
class ProcessEntryUpdate implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new job instance.
|
||
|
|
*/
|
||
|
|
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
public int $entryId,
|
||
|
|
public string $action
|
||
|
|
) {
|
||
|
|
//
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the job.
|
||
|
|
*/
|
||
|
|
public function handle(): void
|
||
|
|
{
|
||
|
|
|
||
|
|
$incoming = [
|
||
|
|
'id' => $this->entryId,
|
||
|
|
'action' => $this->action,
|
||
|
|
];
|
||
|
|
|
||
|
|
$jsonData = json_encode($incoming, JSON_THROW_ON_ERROR);
|
||
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'entry_update_');
|
||
|
|
file_put_contents($tempFile, $jsonData);
|
||
|
|
|
||
|
|
$scriptPath = env('HANDLE_ENTRY_UPDATES_SCRIPT', base_path('cmd/handle_cms_updates.sh'));
|
||
|
|
|
||
|
|
try {
|
||
|
|
// Log what we're about to execute
|
||
|
|
Log::info("Executing script: {$scriptPath} with action: {$this->action}", [
|
||
|
|
'entry_id' => $this->entryId,
|
||
|
|
'temp_file' => $tempFile,
|
||
|
|
'script_exists' => file_exists($scriptPath),
|
||
|
|
'script_executable' => is_executable($scriptPath),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$result = Process::run([
|
||
|
|
'bash',
|
||
|
|
$scriptPath,
|
||
|
|
$this->action,
|
||
|
|
$tempFile
|
||
|
|
]);
|
||
|
|
|
||
|
|
if ($result->failed()) {
|
||
|
|
$errorDetails = [
|
||
|
|
'exit_code' => $result->exitCode(),
|
||
|
|
'stdout' => $result->output(),
|
||
|
|
'stderr' => $result->errorOutput(),
|
||
|
|
'command' => ['bash', $scriptPath, $this->action, $tempFile],
|
||
|
|
'script_path' => $scriptPath,
|
||
|
|
'script_exists' => file_exists($scriptPath),
|
||
|
|
'temp_file_exists' => file_exists($tempFile),
|
||
|
|
'temp_file_contents' => file_exists($tempFile) ? file_get_contents($tempFile) : 'N/A',
|
||
|
|
];
|
||
|
|
|
||
|
|
Log::error("Script execution failed", $errorDetails);
|
||
|
|
|
||
|
|
throw new RuntimeException(
|
||
|
|
"Script execution failed with exit code {$result->exitCode()}. " .
|
||
|
|
"STDOUT: " . ($result->output() ?: 'empty') . " " .
|
||
|
|
"STDERR: " . ($result->errorOutput() ?: 'empty')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
Log::info("Script executed successfully", [
|
||
|
|
'stdout' => $result->output(),
|
||
|
|
'entry_id' => $this->entryId,
|
||
|
|
'action' => $this->action,
|
||
|
|
]);
|
||
|
|
} finally {
|
||
|
|
// Clean up temp file
|
||
|
|
if (file_exists($tempFile)) {
|
||
|
|
unlink($tempFile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|