From e0c9e12008754f7349a3a2f6935418d51259453c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 12 Dec 2022 14:35:18 +0100 Subject: [PATCH] Feature: [cron] Create a cron job to refresh materialized view address - geographical unit association --- ...eographicalUnitMaterializedViewCronJob.php | 60 +++++++++++++++++++ ...aphicalUnitMaterializedViewCronJobTest.php | 46 ++++++++++++++ .../ChillMainBundle/config/services.yaml | 9 +-- 3 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJob.php create mode 100644 src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php diff --git a/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJob.php b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJob.php new file mode 100644 index 000000000..a141f5100 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJob.php @@ -0,0 +1,60 @@ +connection = $connection; + } + + public function canRun(?CronJobExecution $cronJobExecution): bool + { + if ($cronJobExecution->getKey() !== $this->getKey()) { + throw new UnexpectedValueException(); + } + + if (null === $cronJobExecution) { + return true; + } + + $now = new DateTimeImmutable('now'); + + 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 + && mt_rand(0, 5) === 0; + } + + public function getKey(): string + { + return 'refresh-materialized-view-address-to-geog-units'; + } + + public function run(): void + { + $this->connection->executeQuery('REFRESH MATERIALIZED VIEW view_chill_main_address_geographical_unit'); + } +} diff --git a/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php b/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php new file mode 100644 index 000000000..f4e4a586e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php @@ -0,0 +1,46 @@ +connection = self::$container->get(Connection::class); + } + + public function testFullRun(): void + { + $job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob( + $this->connection + ); + + $lastExecution = new CronJobExecution($job->getKey()); + $lastExecution->setLastStart(new DateTimeImmutable('2 days ago')); + + $this->assertIsBool($job->canRun($lastExecution)); + + $job->run(); + } +} diff --git a/src/Bundle/ChillMainBundle/config/services.yaml b/src/Bundle/ChillMainBundle/config/services.yaml index f99c80d2c..7bd1b1038 100644 --- a/src/Bundle/ChillMainBundle/config/services.yaml +++ b/src/Bundle/ChillMainBundle/config/services.yaml @@ -102,12 +102,7 @@ services: Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface: '@Chill\MainBundle\Security\Resolver\CenterResolverDispatcher' - Chill\MainBundle\Service\Import\: - resource: '../Service/Import/' - autowire: true - autoconfigure: true - - Chill\MainBundle\Service\RollingDate\: - resource: '../Service/RollingDate/' + Chill\MainBundle\Service\: + resource: '../Service/' autowire: true autoconfigure: true