mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
Feature: [cron] Create a cron job to refresh materialized view address - geographical unit association
This commit is contained in:
parent
4fcfe3f5d2
commit
e0c9e12008
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Service\AddressGeographicalUnit;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Cron\CronJobInterface;
|
||||||
|
use Chill\MainBundle\Entity\CronJobExecution;
|
||||||
|
use DateInterval;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use UnexpectedValueException;
|
||||||
|
use function in_array;
|
||||||
|
|
||||||
|
class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface
|
||||||
|
{
|
||||||
|
private const ACCEPTED_HOURS = ['0', '1', '2', '3', '4', '5'];
|
||||||
|
|
||||||
|
private Connection $connection;
|
||||||
|
|
||||||
|
public function __construct(Connection $connection)
|
||||||
|
{
|
||||||
|
$this->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');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Tests\Services\AddressGeographicalUnit;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\CronJobExecution;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
|
final class RefreshAddressToGeographicalUnitMaterializedViewCronJobTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
private Connection $connection;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::bootKernel();
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
@ -102,12 +102,7 @@ services:
|
|||||||
|
|
||||||
Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface: '@Chill\MainBundle\Security\Resolver\CenterResolverDispatcher'
|
Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface: '@Chill\MainBundle\Security\Resolver\CenterResolverDispatcher'
|
||||||
|
|
||||||
Chill\MainBundle\Service\Import\:
|
Chill\MainBundle\Service\:
|
||||||
resource: '../Service/Import/'
|
resource: '../Service/'
|
||||||
autowire: true
|
|
||||||
autoconfigure: true
|
|
||||||
|
|
||||||
Chill\MainBundle\Service\RollingDate\:
|
|
||||||
resource: '../Service/RollingDate/'
|
|
||||||
autowire: true
|
autowire: true
|
||||||
autoconfigure: true
|
autoconfigure: true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user