2021-04-06 23:19:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2022-04-27 14:24:48 +02:00
|
|
|
namespace App\Controller;
|
2021-04-06 23:19:51 +02:00
|
|
|
|
2022-04-27 14:24:48 +02:00
|
|
|
use App\Entity\Job;
|
|
|
|
use App\Entity\Run;
|
|
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2021-05-24 14:08:30 +02:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2021-04-06 23:19:51 +02:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2022-04-27 14:24:48 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-04-06 23:19:51 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2022-05-24 18:09:14 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2021-04-06 23:19:51 +02:00
|
|
|
|
2022-04-27 14:24:48 +02:00
|
|
|
class JobController extends AbstractController
|
2021-04-06 23:19:51 +02:00
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
public function defaultAction(ManagerRegistry $doctrine): Response
|
2021-04-08 14:54:30 +02:00
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
$jobRepo = $doctrine->getRepository(Job::class);
|
2021-04-08 12:54:49 +02:00
|
|
|
$jobs = $jobRepo->getAllJobs();
|
2021-04-08 13:39:18 +02:00
|
|
|
return $this->render('job/index.html.twig', ['jobs' => $jobs]);
|
2021-04-06 23:19:51 +02:00
|
|
|
}
|
2021-04-08 14:54:30 +02:00
|
|
|
|
2022-05-19 20:25:05 +02:00
|
|
|
public function jobAction(Request $request, ManagerRegistry $doctrine, int $id, mixed $all = false): Response
|
2021-04-08 14:54:30 +02:00
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
$jobRepo = $doctrine->getRepository(Job::class);
|
|
|
|
$runRepo = $doctrine->getRepository(Run::class);
|
2021-05-24 14:08:30 +02:00
|
|
|
|
2022-04-27 14:24:48 +02:00
|
|
|
if($request->getMethod() == 'GET') {
|
2022-05-19 20:25:05 +02:00
|
|
|
$job = $jobRepo->find($id);
|
|
|
|
$runs = $runRepo->getRunsForJob($job, $all != 'all');
|
2021-05-27 11:46:30 +02:00
|
|
|
return $this->render('job/view.html.twig', ['job' => $job, 'runs' => $runs, 'allruns' => $all == 'all']);
|
2022-04-27 14:24:48 +02:00
|
|
|
} elseif($request->getMethod() == 'DELETE') {
|
2021-05-24 14:08:30 +02:00
|
|
|
$success = $jobRepo->deleteJob($id);
|
|
|
|
$this->addFlash('success', $success['message']);
|
2022-04-27 14:24:48 +02:00
|
|
|
return new JsonResponse(['return_path' => $this->GenerateUrl('job_index')]);
|
2021-05-24 14:08:30 +02:00
|
|
|
}
|
2022-05-19 20:25:05 +02:00
|
|
|
return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST);
|
2021-04-08 14:54:30 +02:00
|
|
|
}
|
2021-04-12 12:23:50 +02:00
|
|
|
|
2022-05-19 20:25:05 +02:00
|
|
|
public function editAction(Request $request, ManagerRegistry $doctrine, int $id): Response
|
2021-05-21 13:09:48 +02:00
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
if($request->getMethod() == 'GET') {
|
|
|
|
$jobRepo = $doctrine->getRepository(Job::class);
|
2022-05-19 20:25:05 +02:00
|
|
|
$job = $jobRepo->find($id);
|
2022-05-17 16:06:46 +02:00
|
|
|
return $this->render('job/edit.html.twig', ['job' => $job]);
|
2022-04-27 14:24:48 +02:00
|
|
|
} elseif($request->getMethod() == 'POST') {
|
|
|
|
$allValues = $request->request->all();
|
|
|
|
$jobRepo = $doctrine->getRepository(Job::class);
|
2021-05-21 13:09:48 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$joboutput = $jobRepo->editJob($id, $allValues);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
$this->addFlash('danger', $e->getMessage());
|
2022-04-27 14:24:48 +02:00
|
|
|
return new RedirectResponse($this->GenerateUrl('job_edit', ['id' => $allValues['id']]));
|
2021-05-21 13:09:48 +02:00
|
|
|
}
|
|
|
|
$this->addFlash('success', $joboutput['message']);
|
2022-04-27 14:24:48 +02:00
|
|
|
return new RedirectResponse($this->GenerateUrl('job_index'));
|
2021-05-21 13:09:48 +02:00
|
|
|
}
|
2022-05-19 20:25:05 +02:00
|
|
|
return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST);
|
2021-05-21 13:09:48 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 20:25:05 +02:00
|
|
|
public function addAction(Request $request, ManagerRegistry $doctrine): Response
|
2021-04-12 12:23:50 +02:00
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
if($request->getMethod() == 'GET') {
|
|
|
|
return $this->render('job/add.html.twig', ['data' => []]);
|
|
|
|
} elseif ($request->getMethod() == 'POST') {
|
|
|
|
$allValues = $request->request->all();
|
|
|
|
$jobRepo = $doctrine->getRepository(Job::class);
|
2021-05-21 13:09:48 +02:00
|
|
|
try {
|
|
|
|
$joboutput = $jobRepo->addJob($allValues);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
$this->addFlash('danger', $e->getMessage());
|
2022-04-27 14:24:48 +02:00
|
|
|
return new RedirectResponse($this->GenerateUrl('job_add'));
|
2021-04-13 14:07:11 +02:00
|
|
|
}
|
2021-05-21 13:09:48 +02:00
|
|
|
$this->addFlash('success', $joboutput['message']);
|
2022-04-27 14:24:48 +02:00
|
|
|
return new RedirectResponse($this->GenerateUrl('job_index'));
|
2021-04-13 14:07:11 +02:00
|
|
|
} else {
|
|
|
|
return new Response('Not implemented yet', Response::HTTP_TOO_EARLY);
|
|
|
|
}
|
2021-04-12 12:23:50 +02:00
|
|
|
}
|
2021-06-01 17:41:10 +02:00
|
|
|
|
2022-05-24 18:09:14 +02:00
|
|
|
public function runNowAction(Request $request, ManagerRegistry $doctrine, TranslatorInterface $translator, int $id): JsonResponse
|
2022-05-19 20:25:05 +02:00
|
|
|
{
|
2022-04-27 14:24:48 +02:00
|
|
|
if($request->getMethod() == 'GET') {
|
|
|
|
$jobRepo = $doctrine->getRepository(Job::class);
|
2022-05-19 15:48:06 +02:00
|
|
|
$job = $jobRepo->find($id);
|
2022-05-24 18:09:14 +02:00
|
|
|
$runnowResult = $jobRepo->runNow($job);
|
|
|
|
if ($runnowResult['success'] === NULL) {
|
|
|
|
$return = [
|
|
|
|
'status' => 'deferred',
|
|
|
|
'success' => NULL,
|
|
|
|
'title' => $translator->trans('job.runnow.deferred.title'),
|
|
|
|
'message' => $translator->trans('job.runnow.deferred.message')
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$return = [
|
|
|
|
'status' => 'ran',
|
|
|
|
'success' => $runnowResult['success'],
|
|
|
|
'title' => $runnowResult['success'] ? $translator->trans('job.runnow.ran.title.success') : $translator->trans('job.runnow.ran.title.failed'),
|
|
|
|
'message' => $translator->trans('job.runnow.ran.message', [
|
|
|
|
'_runtime_' => number_format($runnowResult['runtime'], 3),
|
|
|
|
'_exitcode_' => $runnowResult['exitcode']
|
|
|
|
]),
|
|
|
|
'exitcode' => $runnowResult['exitcode'],
|
|
|
|
'output' => $runnowResult['output'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return new JsonResponse($return);
|
2021-06-01 17:41:10 +02:00
|
|
|
}
|
2022-05-19 20:25:05 +02:00
|
|
|
return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST);
|
2021-06-01 17:41:10 +02:00
|
|
|
}
|
2021-04-06 23:19:51 +02:00
|
|
|
}
|