Skip to content

Commit

Permalink
fix: use minute period and one-time-trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Jun 12, 2024
1 parent aa92d42 commit 7780494
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/Service/OneTimeTrigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Atoolo\Runtime\Check\Service;

use DateTime;
use DateTimeImmutable;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class OneTimeTrigger implements TriggerInterface
{
private ?DateTimeImmutable $run = null;

/**
* @inheritDoc
*/
public function __toString(): string
{
if ($this->run === null) {
return 'one time';
}
return 'one time (already running)';
}

/**
* @inheritDoc
*/
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
if (null === $this->run) {
$this->run = $run;
return $run;
}
return null;
}
}
6 changes: 5 additions & 1 deletion src/Service/WorkerCheckScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function getSchedule(): Schedule
{
return $this->schedule ??= (new Schedule())
->add(
RecurringMessage::trigger(
new OneTimeTrigger(),
new WorkerCheckEvent()
),
RecurringMessage::every(
$this->workerStatusFile->updatePeriodInMinutes . ' seconds',
$this->workerStatusFile->updatePeriodInMinutes . ' minutes',
new WorkerCheckEvent()
),
)->lock($this->lockFactory->createLock(
Expand Down
58 changes: 58 additions & 0 deletions test/Service/OneTimeTriggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Atoolo\Runtime\Check\Test\Service;

use Atoolo\Runtime\Check\Service\OneTimeTrigger;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(OneTimeTrigger::class)]
class OneTimeTriggerTest extends TestCase
{
public function testToString(): void
{
$trigger = new OneTimeTrigger();

$this->assertEquals(
'one time',
(string) $trigger
);
}

public function testToStringAlreadyRunning(): void
{
$trigger = new OneTimeTrigger();
$trigger->getNextRunDate(new \DateTimeImmutable());

$this->assertEquals(
'one time (already running)',
(string) $trigger
);
}

public function testGetNextRunDate(): void
{
$trigger = new OneTimeTrigger();

$run = new \DateTimeImmutable();
$this->assertEquals(
$run,
$trigger->getNextRunDate($run),
'The first call to getNextRunDate should return the same date'
);
}

public function testSecondGetNextRunDate(): void
{
$trigger = new OneTimeTrigger();
$run = new \DateTimeImmutable();
$trigger->getNextRunDate($run);

$this->assertNull(
$trigger->getNextRunDate($run),
'The second call to getNextRunDate should return null'
);
}
}
2 changes: 1 addition & 1 deletion test/Service/WorkerCheckSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testGetSchedule(): void
$schedule = $workerCheckScheduler->getSchedule();

$this->assertEquals(
1,
2,
count($schedule->getRecurringMessages())
);
}
Expand Down

0 comments on commit 7780494

Please sign in to comment.