mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fixed: ensure to execute Refresh Address To Geographical Unit Cronjob even during the day
This commit is contained in:
parent
f12f640cb4
commit
c8127a1d9d
@ -17,12 +17,9 @@ use DateInterval;
|
|||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use UnexpectedValueException;
|
use UnexpectedValueException;
|
||||||
use function in_array;
|
|
||||||
|
|
||||||
class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface
|
class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface
|
||||||
{
|
{
|
||||||
private const ACCEPTED_HOURS = ['0', '1', '2', '3', '4', '5'];
|
|
||||||
|
|
||||||
private Connection $connection;
|
private Connection $connection;
|
||||||
|
|
||||||
public function __construct(Connection $connection)
|
public function __construct(Connection $connection)
|
||||||
@ -43,9 +40,8 @@ class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJob
|
|||||||
$now = new DateTimeImmutable('now');
|
$now = new DateTimeImmutable('now');
|
||||||
|
|
||||||
return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D'))
|
return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D'))
|
||||||
&& in_array($now->format('H'), self::ACCEPTED_HOURS, true)
|
|
||||||
// introduce a random component to ensure a roll when multiple instances are hosted on same machines
|
// introduce a random component to ensure a roll when multiple instances are hosted on same machines
|
||||||
&& mt_rand(0, 5) === 0;
|
&& mt_rand(0, 10) === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKey(): string
|
public function getKey(): string
|
||||||
|
@ -30,6 +30,34 @@ final class RefreshAddressToGeographicalUnitMaterializedViewCronJobTest extends
|
|||||||
$this->connection = self::$container->get(Connection::class);
|
$this->connection = self::$container->get(Connection::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCanRun(): void
|
||||||
|
{
|
||||||
|
// As the can run is executed one of ten, this should be executed at least one after
|
||||||
|
// 10 + 5 executions
|
||||||
|
$job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(
|
||||||
|
$this->connection
|
||||||
|
);
|
||||||
|
|
||||||
|
$lastExecution = new CronJobExecution($job->getKey());
|
||||||
|
$lastExecution->setLastStart(new DateTimeImmutable('2 days ago'));
|
||||||
|
|
||||||
|
$executedForFirstTime = 0;
|
||||||
|
$executedAfterPreviousExecution = 0;
|
||||||
|
|
||||||
|
for ($round = 0; 20 > $round; ++$round ) {
|
||||||
|
if ($job->canRun(null)) {
|
||||||
|
++$executedForFirstTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($job->canRun($lastExecution)) {
|
||||||
|
++$executedAfterPreviousExecution;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertGreaterThan(0, $executedForFirstTime);
|
||||||
|
$this->assertGreaterThan(0, $executedAfterPreviousExecution);
|
||||||
|
}
|
||||||
|
|
||||||
public function testFullRun(): void
|
public function testFullRun(): void
|
||||||
{
|
{
|
||||||
$job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(
|
$job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user