Skip to content

Commit

Permalink
[CronJob/SonataIntegration] Support Execution Timeout on cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jun 25, 2024
1 parent d3745ec commit 72134d8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
29 changes: 29 additions & 0 deletions app/migrations/Version20240625205840.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240625205840 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE cron_job__cron_job ADD execution_timeout INT DEFAULT NULL');
}

public function down(Schema $schema): void
{
}
}
2 changes: 1 addition & 1 deletion packages/cron-job/CronJobProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function process(CronJobExecution $execution): void
$this->parameterBag->resolveValue(
$event->getCommand()
),
timeout: 1800
timeout: $execution->getCronJob()->getExecutionTimeout()
);

try {
Expand Down
15 changes: 15 additions & 0 deletions packages/cron-job/Entity/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class CronJob implements \Stringable
]
private int $timeToLive = 0;

#[ORM\Column(name: 'execution_timeout', type: 'integer', nullable: true)]
private ?int $executionTimeout = null;

#[ORM\Column(name: 'priority', type: 'integer', nullable: true)]
#[Assert\Range(min: 0, max: 255)]
private ?int $priority = null;
Expand Down Expand Up @@ -144,6 +147,18 @@ public function setTimeToLive(int $timeToLive): self
return $this;
}

public function getExecutionTimeout(): ?int
{
return $this->executionTimeout;
}

public function setExecutionTimeout(?int $executionTimeout): self
{
$this->executionTimeout = $executionTimeout;

return $this;
}

public function getPriority(): ?int
{
return $this->priority;
Expand Down
6 changes: 4 additions & 2 deletions packages/cron-job/Tests/CronJobProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public function testProcess(
$returnedPreCronJobExecutionEvent->setCommand($overwrittenCommand);
}

$execution->getCronJob()->setExecutionTimeout($executionTimeout = random_int(1, 100));

$this->eventDispatcher
->expects(static::exactly(2))
->method('dispatch')
Expand Down Expand Up @@ -150,7 +152,7 @@ public function testProcess(
null,
null,
null,
1800
$executionTimeout,
)
->willReturn($process = $this->createMock(Process::class));

Expand Down Expand Up @@ -243,7 +245,7 @@ public function testProcessWithError(): void
null,
null,
null,
1800
$execution->getCronJob()->getExecutionTimeout()
)
->willReturn($process);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function configureListFields(ListMapper $list): void
->add('schedule')
->add('active', null, ['editable' => true])
->add('timeToLive')
->add('executionTimeout')
->add('priority')
->add(
ListMapper::NAME_ACTIONS,
Expand Down Expand Up @@ -66,6 +67,7 @@ protected function configureFormFields(FormMapper $form): void
)
->add('schedule')
->add('active')
->add('executionTimeout')
->end()
->with('Queue Configuration', ['class' => 'col-md-4'])
->add('timeToLive')
Expand All @@ -84,6 +86,7 @@ protected function configureShowFields(ShowMapper $show): void
->add('command')
->add('schedule')
->add('active')
->add('executionTimeout')
->add('timeToLive')
->add('priority')
->ifTrue(!$this->getSubject()->getExecutions()->isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ protected function configureListFields(ListMapper $list): void
protected function configureShowFields(ShowMapper $show): void
{
$show
->add('cronJob', null, ['route' => ['name' => 'show']])
->add('requestedAt')
->add('state')
->add('force')
Expand Down

0 comments on commit 72134d8

Please sign in to comment.