webcron/src/Controller/JobController.php

126 lines
5.7 KiB
PHP
Raw Normal View History

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();
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
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') {
$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);
2022-09-07 17:36:13 +02:00
$this->addFlash('success', 'job.index.flashes.jobdeleted');
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
}
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
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);
$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', 'job.edit.flashes.jobedited');
2022-04-27 14:24:48 +02:00
return new RedirectResponse($this->GenerateUrl('job_index'));
2021-05-21 13:09:48 +02:00
}
return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST);
2021-05-21 13:09:48 +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
}
$this->addFlash('success', 'job.add.flashes.jobadded');
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
2023-01-10 17:21:49 +01:00
public function runAction(Request $request, ManagerRegistry $doctrine, TranslatorInterface $translator, int $id, int $timestamp): JsonResponse
{
2022-04-27 14:24:48 +02:00
if($request->getMethod() == 'GET') {
$jobRepo = $doctrine->getRepository(Job::class);
$job = $jobRepo->find($id);
2023-01-10 17:21:49 +01:00
$runResult = $jobRepo->run($job, false, $timestamp);
if ($runResult['success'] === NULL) {
2022-05-24 18:09:14 +02:00
$return = [
'status' => 'deferred',
'success' => NULL,
2023-01-10 17:21:49 +01:00
'title' => $translator->trans('job.index.run.deferred.title'),
'message' => $translator->trans('job.index.run.deferred.message')
2022-05-24 18:09:14 +02:00
];
} else {
$return = [
'status' => 'ran',
2023-01-10 17:21:49 +01:00
'success' => $runResult['success'],
'title' => $runResult['success'] ? $translator->trans('job.index.run.ran.title.success') : $translator->trans('job.index.run.ran.title.failed'),
'message' => $translator->trans('job.index.run.ran.message', [
'_runtime_' => number_format($runResult['runtime'], 3),
'_exitcode_' => $runResult['exitcode']
2022-05-24 18:09:14 +02:00
]),
2023-01-10 17:21:49 +01:00
'exitcode' => $runResult['exitcode'],
'output' => $runResult['output'],
2022-05-24 18:09:14 +02:00
];
}
return new JsonResponse($return);
2021-06-01 17:41:10 +02:00
}
return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST);
2021-06-01 17:41:10 +02:00
}
2023-01-16 12:45:27 +01:00
public function hookAction(Request $request, ManagerRegistry $doctrine, int $id, string $token)
{
$jobRepo = $doctrine->getRepository(Job::class);
$job = $jobRepo->find($id);
if(!empty($job->getToken()) && $job->getToken() == $token && $job->getRunning() != 1) {
$jobRepo->setTempVar($job, 'webhook', true);
2023-01-16 12:45:27 +01:00
return new JsonResponse($jobRepo->run($job, false, time()));
}
return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST);
}
2021-04-06 23:19:51 +02:00
}