Billie/app/Jobs/NodeJS/CopyTestsFolder.php

85 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2025-05-06 02:47:26 +00:00
<?php
namespace App\Jobs\NodeJS;
use App\Models\NodeJS\ExecutionStep;
use App\Models\NodeJS\Submission;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process;
class CopyTestsFolder implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $submission;
public $testsDir;
public $tempDir;
public $command;
/**
* Create a new job instance.
*/
public function __construct($submission, $testsDir, $tempDir, $command)
{
$this->submission = $submission;
$this->testsDir = $testsDir;
$this->tempDir = $tempDir;
$this->command = $command;
}
/**
* Execute the job.
*/
public function handle(): void
{
$submission = $this->submission;
Log::info("Copying tests folder to {$this->tempDir}");
$this->updateSubmissionStatus($submission, Submission::$PROCESSING, "Copying tests folder");
try {
2025-05-06 03:25:09 +00:00
// Ensure tests directory exists
if (!is_dir($this->tempDir . '/tests')) {
mkdir($this->tempDir . '/tests', 0777, true);
2025-05-06 02:47:26 +00:00
}
2025-05-06 03:25:09 +00:00
// Execute all commands to copy test files
foreach ($this->command as $command) {
$process = new Process($command);
2025-05-06 02:47:26 +00:00
$process->start();
$process_pid = $process->getPid();
$process->wait();
2025-05-06 03:25:09 +00:00
2025-05-06 02:47:26 +00:00
if ($process->isSuccessful()) {
2025-05-06 03:25:09 +00:00
Log::info("Copied test file {$command[2]} to {$command[3]}");
2025-05-06 02:47:26 +00:00
} else {
2025-05-06 03:25:09 +00:00
Log::error("Failed to copy test file {$command[2]} to {$command[3]}");
Log::error("Error: " . $process->getErrorOutput());
$this->updateSubmissionStatus($submission, Submission::$FAILED, "Failed to copy test files: " . $process->getErrorOutput());
2025-05-06 02:47:26 +00:00
Process::fromShellCommandline("rm -rf {$this->tempDir}")->run();
Process::fromShellCommandline('kill ' . $process_pid)->run();
throw new \Exception($process->getErrorOutput());
}
}
2025-05-06 03:25:09 +00:00
2025-05-06 02:47:26 +00:00
// completed
2025-05-06 03:25:09 +00:00
Log::info("Copied all test files to {$this->tempDir}");
$this->updateSubmissionStatus($submission, Submission::$COMPLETED, "Copied test files successfully");
2025-05-06 02:47:26 +00:00
} catch (\Throwable $th) {
2025-05-06 03:25:09 +00:00
Log::error("Failed to copy test files to {$this->tempDir}: " . $th->getMessage());
$this->updateSubmissionStatus($submission, Submission::$FAILED, "Failed to copy test files: " . $th->getMessage());
2025-05-06 02:47:26 +00:00
Process::fromShellCommandline("rm -rf {$this->tempDir}")->run();
}
}
private function updateSubmissionStatus(Submission $submission, string $status, string $output): void
{
$stepName = ExecutionStep::$COPY_TESTS_FOLDER;
if ($status != Submission::$PROCESSING) $submission->updateOneResult($stepName, $status, $output);
if ($status != Submission::$COMPLETED) $submission->updateStatus($status);
}
}