Skip to content

Commit

Permalink
Adjust admin to display cron job execution, will help for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Apr 21, 2024
1 parent 43af43b commit 05b1949
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 25 deletions.
21 changes: 18 additions & 3 deletions packages/cron-job/Entity/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Cron\CronExpression;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

Expand Down Expand Up @@ -52,7 +54,7 @@ class CronJob implements \Stringable
private ?int $priority = null;

/**
* @var Collection<CronJobExecution>
* @var Selectable&Collection<CronJobExecution>
*/
#[
ORM\OneToMany(
Expand All @@ -63,7 +65,7 @@ class CronJob implements \Stringable
orphanRemoval: true,
)
]
private Collection $executions;
private Selectable&Collection $executions;

public function __construct()
{
Expand Down Expand Up @@ -152,13 +154,26 @@ public function setPriority(?int $priority): self
}

/**
* @return Collection<CronJobExecution>
* @return Selectable&Collection<CronJobExecution>
*/
public function getExecutions(): Collection
{
return $this->executions;
}

/**
* @return Selectable&Collection<CronJobExecution>
*/
public function getRecentExecutions(): Selectable&Collection
{
return $this->executions
->matching(
Criteria::create()
->orderBy(['requestedAt' => 'DESC'])
->setMaxResults(10)
);
}

public function isDue(): bool
{
if (null === $this->getSchedule()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cron-job/Entity/CronJobExecution.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CronJobExecution implements \Stringable
private \DateTimeImmutable $requestedAt;

#[ORM\Column(name: '`force`', type: 'boolean', nullable: false, options: ['default' => false])]
private bool $force = false;
private bool $force;

#[ORM\Column(name: 'execution_started_at', type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $executionStartedAt = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public function pagerTypeDefaultValue(?string $value): self
return $this;
}

public function translationDomainDefaultValue(?string $value): self
{
$this->children['translation_domain']->defaultValue($value);

return $this;
}

public static function configureFromConfiguration(Definition $definition, array $config): Definition
{
return $definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function configureShowFields(ShowMapper $show): void
->add('priority')
->ifTrue(!$this->getSubject()->getExecutions()->isEmpty())
->add(
'executions',
'recentExecutions',
'grid',
[
'fieldValueOnly' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,88 @@
namespace Draw\Bundle\SonataIntegrationBundle\CronJob\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;

class CronJobExecutionAdmin extends AbstractAdmin
{
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add(
'cronJob',
ModelFilter::class,
[
'field_type' => ModelAutocompleteType::class,
'field_options' => [
'property' => 'name',
],
'show_filter' => true,
]
)
->add('requestedAt')
->add('force')
->add('executionStartedAt')
->add('executionEndedAt')
->add('executionDelay')
->add(
'exitCode',
filterOptions: [
'show_filter' => true,
]
);

}

protected function configureListFields(ListMapper $list): void
{
$list
->addIdentifier('id')
->add(
'cronJob',
fieldDescriptionOptions: [
'sortable' => 'cronJob.name',
]
)
->add('requestedAt')
->add('force')
->add('executionStartedAt')
->add('executionEndedAt')
->add('executionDelay')
->add('exitCode')
->add(
ListMapper::NAME_ACTIONS,
ListMapper::TYPE_ACTIONS,
[
'actions' => [
'show' => [],
'delete' => [],
],
]
);
}

protected function configureShowFields(ShowMapper $show): void
{
$show
->add('requestedAt')
->add('force')
->add('executionStartedAt')
->add('executionEndedAt')
->add('executionDelay')
->add('exitCode')
->add('error', 'json');
}

protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->clearExcept(['list', 'show', 'delete']);
}

public function configureGridFields(array $fields): array
{
return array_merge(
Expand Down Expand Up @@ -40,21 +116,4 @@ public function configureGridFields(array $fields): array
]
);
}

protected function configureShowFields(ShowMapper $show): void
{
$show
->add('requestedAt')
->add('force')
->add('executionStartedAt')
->add('executionEndedAt')
->add('executionDelay')
->add('exitCode')
->add('error', 'json');
}

protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->clearExcept('show');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ private function createCronJobNode(): ArrayNodeDefinition
->controllerClassDefaultValue(CronJobController::class)
->labelDefaultValue('Cron Job')
->iconDefaultValue('fas fa-clock')
->translationDomainDefaultValue('DrawCronJobAdmin')
)
->append(
(new SonataAdminNodeConfiguration(CronJobExecution::class, 'Cron Job', 'cron_job_execution'))
->addDefaultsIfNotSet()
->pagerTypeDefaultValue('simple')
->labelDefaultValue('Cron Job Execution')
->iconDefaultValue('fas fa-clock')
->translationDomainDefaultValue('DrawCronJobAdmin')
)
->end()
->end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ public function getDefaultConfiguration(): array
'label' => 'Cron Job',
'pager_type' => 'simple',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
'translation_domain' => 'DrawCronJobAdmin',
],
'cron_job_execution' => [
'group' => 'Cron Job',
'entity_class' => CronJobExecution::class,
'controller_class' => 'sonata.admin.controller.crud',
'icon' => null,
'icon' => 'fas fa-clock',
'label' => 'Cron Job Execution',
'pager_type' => 'simple',
'show_in_dashboard' => true,
'translation_domain' => 'SonataAdminBundle',
'translation_domain' => 'DrawCronJobAdmin',
],
],
],
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Method Draw\\\\Component\\\\CronJob\\\\Entity\\\\CronJob\\:\\:getRecentExecutions\\(\\) should return Doctrine\\\\Common\\\\Collections\\\\Collection&Doctrine\\\\Common\\\\Collections\\\\Selectable&iterable\\<Draw\\\\Component\\\\CronJob\\\\Entity\\\\CronJobExecution\\> but returns Doctrine\\\\Common\\\\Collections\\\\ReadableCollection\\<\\(int\\|string\\), mixed\\>&Doctrine\\\\Common\\\\Collections\\\\Selectable\\<\\(int\\|string\\), mixed\\>\\.$#"
count: 1
path: packages/cron-job/Entity/CronJob.php

-
message: "#^Parameter \\#1 \\$node of method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeBuilder\\:\\:append\\(\\) expects Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition, Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\|null given\\.$#"
count: 1
Expand Down

0 comments on commit 05b1949

Please sign in to comment.