Skip to content

Commit

Permalink
PS-693 add clear method to workflow doctrine state cache
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Oct 22, 2024
1 parent 10e4327 commit 63b8fc9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion databox/api/src/Controller/Admin/AssetCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function triggerIngest(AdminContext $context): Response
WorkflowState::INITIATOR_ID => $user->getId(),
]);

return $this->redirect($context->getReferrer());
return $this->returnToReferer($context);
}

public function configureCrud(Crud $crud): Crud
Expand Down
7 changes: 4 additions & 3 deletions databox/api/src/Controller/Admin/JobStateCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\RedirectResponse;

class JobStateCrudController extends AbstractAdminCrudController
Expand Down Expand Up @@ -63,7 +64,7 @@ public function retryJob(AdminContext $context): RedirectResponse
$jobState = $context->getEntity()->getInstance();
$this->workflowOrchestrator->retryFailedJobs($jobState->getWorkflow()->getId(), $jobState->getJobState()->getJobId());

return new RedirectResponse($context->getReferrer());
return $this->returnToReferer($context);
}

public function cancelJob(AdminContext $context): RedirectResponse
Expand All @@ -72,7 +73,7 @@ public function cancelJob(AdminContext $context): RedirectResponse
$jobState = $context->getEntity()->getInstance();
$this->workflowOrchestrator->cancelWorkflow($jobState->getWorkflow()->getId());

return new RedirectResponse($context->getReferrer());
return $this->returnToReferer($context);
}

public function rerunJob(AdminContext $context): RedirectResponse
Expand All @@ -81,7 +82,7 @@ public function rerunJob(AdminContext $context): RedirectResponse
$jobState = $context->getEntity()->getInstance();
$this->workflowOrchestrator->rerunJobs($jobState->getWorkflow()->getId(), $jobState->getJobState()->getJobId());

return new RedirectResponse($context->getReferrer());
return $this->returnToReferer($context);
}

public function configureCrud(Crud $crud): Crud
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function cancelWorkflow(AdminContext $context): RedirectResponse
$workflowState = $context->getEntity()->getInstance();
$this->workflowOrchestrator->cancelWorkflow($workflowState->getId());

return new RedirectResponse($context->getReferrer());
return $this->returnToReferer($context);
}

public function configureActions(Actions $actions): Actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\HttpFoundation\RedirectResponse;

abstract class AbstractAdminCrudController extends AbstractCrudController
{
Expand All @@ -26,4 +29,10 @@ public function configureActions(Actions $actions): Actions
return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL);
}

protected function returnToReferer(AdminContext $context): RedirectResponse
{
return $this->redirect($context->getReferrer()
?? $this->container->get(AdminUrlGenerator::class)->setAction(Action::INDEX)->generateUrl());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;

#[AsEventListener(event: WorkerMessageHandledEvent::class, method: 'clear')]
#[AsEventListener(event: WorkerMessageFailedEvent::class, method: 'clear')]
#[AsEventListener(event: KernelEvents::TERMINATE, method: 'clear')]
class DoctrineStateRepository implements LockAwareStateRepositoryInterface
{
private array $jobs = [];
Expand All @@ -27,6 +34,11 @@ public function __construct(
$this->jobStateEntity = $jobStateEntity ?? JobStateEntity::class;
}

public function clear(): void
{
$this->jobs = [];
}

public function getWorkflowState(string $id): WorkflowState
{
$entity = $this->em->getRepository($this->workflowStateEntity)->find($id);
Expand Down Expand Up @@ -90,6 +102,8 @@ public function acquireJobLock(string $workflowId, string $jobId): void

if ($entity instanceof JobStateEntity) {
$this->jobs[$workflowId][$jobId] = $entity;
} else {
unset($this->jobs[$workflowId][$jobId]);
}
} catch (\Throwable $e) {
$this->em->rollback();
Expand Down Expand Up @@ -151,6 +165,8 @@ private function fetchJobEntity(string $workflowId, string $jobId): ?JobStateEnt

if ($entity) {
$this->jobs[$workflowId][$jobId] = $entity;
} else {
unset($this->jobs[$workflowId][$jobId]);
}

return $entity;
Expand Down

0 comments on commit 63b8fc9

Please sign in to comment.