From 475c52b217149dd4ce67f9a65b96518629aa8fee Mon Sep 17 00:00:00 2001 From: aynsix Date: Wed, 24 Apr 2024 18:00:07 +0300 Subject: [PATCH] log_docs mark as canceled too --- .../WorkerRunningJobRepository.php | 7 ++++- .../AdminConfigurationController.php | 30 ++++++++++++++++++- .../Provider/ControllerServiceProvider.php | 4 ++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/Alchemy/Phrasea/Model/Repositories/WorkerRunningJobRepository.php b/lib/Alchemy/Phrasea/Model/Repositories/WorkerRunningJobRepository.php index 965e554537..cd32204602 100644 --- a/lib/Alchemy/Phrasea/Model/Repositories/WorkerRunningJobRepository.php +++ b/lib/Alchemy/Phrasea/Model/Repositories/WorkerRunningJobRepository.php @@ -503,7 +503,7 @@ public function updateStatusRunningToCanceledSinceCreated($hour = 0) ]); } - public function getRunningSinceCreated($hour = 0) + public function getRunningSinceCreated($hour = 0, array $action = null) { $rsm = new ResultSetMappingBuilder($this->_em); $rsm->addRootEntityFromClassMetadata('Alchemy\Phrasea\Model\Entities\WorkerRunningJob', 'w'); @@ -516,6 +516,11 @@ public function getRunningSinceCreated($hour = 0) AND (TO_SECONDS(CURRENT_TIMESTAMP()) - TO_SECONDS(w.created)) > :second' ; + if ($action != null) { + $action = join('" ,"', $action); + $sql .= ' AND work IN("' . $action . '")'; + } + $q = $this->_em->createNativeQuery($sql, $rsm); $q->setParameters([ 'second' => $hour * 3600, diff --git a/lib/Alchemy/Phrasea/WorkerManager/Controller/AdminConfigurationController.php b/lib/Alchemy/Phrasea/WorkerManager/Controller/AdminConfigurationController.php index 9c890c7e21..5f4e2684c1 100644 --- a/lib/Alchemy/Phrasea/WorkerManager/Controller/AdminConfigurationController.php +++ b/lib/Alchemy/Phrasea/WorkerManager/Controller/AdminConfigurationController.php @@ -3,6 +3,7 @@ namespace Alchemy\Phrasea\WorkerManager\Controller; use Alchemy\Phrasea\Application as PhraseaApplication; +use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware; use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Model\Entities\WorkerRunningJob; use Alchemy\Phrasea\Model\Repositories\WorkerRunningJobRepository; @@ -32,6 +33,8 @@ class AdminConfigurationController extends Controller { + use DataboxLoggerAware; + public function indexAction(PhraseaApplication $app, Request $request) { return $this->render('admin/worker-manager/index.html.twig', [ @@ -223,8 +226,10 @@ public function changeStatusAction(Request $request, $workerId) $workerRunningJob = $repoWorker->find($workerId); $workerRunningJob->setStatus($request->request->get('status')); + $finishedDate = new \DateTime('now'); + if($request->request->get('finished') == '1') { - $workerRunningJob->setFinished(new \DateTime('now'))->setFlock(null); + $workerRunningJob->setFinished($finishedDate)->setFlock(null); } $em = $repoWorker->getEntityManager(); @@ -232,6 +237,10 @@ public function changeStatusAction(Request $request, $workerId) $em->flush(); + if (in_array($workerRunningJob->getWork(), ['subdefCreation', 'writeMetadatas'])) { + $this->updateLogDocs($workerRunningJob, $workerRunningJob->getStatus(), $finishedDate); + } + return $this->app->json(['success' => true]); } @@ -250,8 +259,16 @@ public function doChangeStatusToCanceledAction(PhraseaApplication $app, Request { /** @var WorkerRunningJobRepository $repoWorker */ $repoWorker = $this->app['repo.worker-running-job']; + $workerRunningJobs = $repoWorker->getRunningSinceCreated($request->request->get('hour'), ['subdefCreation', 'writeMetadatas']); + $repoWorker->updateStatusRunningToCanceledSinceCreated($request->request->get('hour')); + $finishedDate = new \DateTime('now'); + /** @var WorkerRunningJob $workerRunningJob */ + foreach ($workerRunningJobs as $workerRunningJob) { + $this->updateLogDocs($workerRunningJob, 'canceled', $finishedDate); + } + return $this->app->json(['success' => true]); } @@ -557,6 +574,17 @@ public function populateStatusAction(PhraseaApplication $app, Request $request) return $repoWorkerJob->checkPopulateStatusByDataboxIds($databoxIds); } + private function updateLogDocs(WorkerRunningJob $workerRunningJob, $status, $finishedDate) + { + $databox = $this->findDataboxById($workerRunningJob->getDataboxId()); + $record = $databox->get_record($workerRunningJob->getRecordId()); + $subdefName = $workerRunningJob->getWorkOn(); + $action = $workerRunningJob->getWork(); + + $this->getDataboxLogger($databox)->initOrUpdateLogDocsFromWorker($record, $databox, $workerRunningJob, $subdefName, $action, $finishedDate, $status); + + } + private function getDefaultRecordsActionsSettings() { return <<share(function (PhraseaApplication $app) { - return new AdminConfigurationController($app); + return (new AdminConfigurationController($app)) + ->setDataboxLoggerLocator($app['phraseanet.logger']) + ; }); // example of route to check webhook