Implement invoice period warming queue handling with Redis interface
- Added methods `enqueueInvoicePeriodWarming` and `consumeInvoicePeriodWarmingQueue` to the `Redis` interface for managing warming periods. - Modified `invoice_period_flag_service` to enqueue warming periods on cache misses. - Updated cron job logic to process invoice period warming queues and ensure flags are warmed effectively.
This commit is contained in:
@@ -529,6 +529,11 @@ class invoice_period_flag_service
|
||||
}
|
||||
|
||||
if (!is_array($flags)) {
|
||||
// Cache miss — enqueue for warming on the next cron run
|
||||
try {
|
||||
(new redis())->enqueue_invoice_period_warming($dateFrom, $dateTo);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
@@ -447,6 +447,35 @@ class redis implements redis_i
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function enqueue_invoice_period_warming(string $dateFrom, string $dateTo): self
|
||||
{
|
||||
$this->get_client()->sadd('invoice_period_warming_queue', [$dateFrom . '|' . $dateTo]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function consume_invoice_period_warming_queue(): array
|
||||
{
|
||||
$client = $this->get_client();
|
||||
$members = $client->smembers('invoice_period_warming_queue');
|
||||
if (!empty($members)) {
|
||||
$client->del('invoice_period_warming_queue');
|
||||
}
|
||||
$periods = [];
|
||||
foreach ($members as $member) {
|
||||
$parts = explode('|', (string)$member, 2);
|
||||
if (count($parts) === 2 && $parts[0] !== '' && $parts[1] !== '') {
|
||||
$periods[] = ['dateFrom' => $parts[0], 'dateTo' => $parts[1]];
|
||||
}
|
||||
}
|
||||
return $periods;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
@@ -160,11 +160,26 @@ function WarmInvoicePeriodAutomaticFlagsCron(): void
|
||||
$service = new invoice_period_flag_service();
|
||||
$now = new DateTime();
|
||||
$previousMonth = new DateTime('first day of previous month');
|
||||
|
||||
$toWarm = [];
|
||||
foreach ([$now, $previousMonth] as $date) {
|
||||
$dateFrom = $date->format('Y-m-01');
|
||||
$dateTo = $date->format('Y-m-t');
|
||||
$service->warmOrderItemRowsForPeriod($dateFrom, $dateTo);
|
||||
$service->warmAutomaticFlagsForPeriod($dateFrom, $dateTo);
|
||||
$toWarm[$dateFrom . '|' . $dateTo] = ['dateFrom' => $dateFrom, 'dateTo' => $dateTo];
|
||||
}
|
||||
|
||||
try {
|
||||
$queued = (new redis())->consume_invoice_period_warming_queue();
|
||||
foreach ($queued as $period) {
|
||||
$key = $period['dateFrom'] . '|' . $period['dateTo'];
|
||||
$toWarm[$key] = $period;
|
||||
}
|
||||
} catch (Throwable) {
|
||||
}
|
||||
|
||||
foreach ($toWarm as $period) {
|
||||
$service->warmOrderItemRowsForPeriod($period['dateFrom'], $period['dateTo']);
|
||||
$service->warmAutomaticFlagsForPeriod($period['dateFrom'], $period['dateTo']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -381,4 +381,18 @@ interface redis_i
|
||||
* @return self
|
||||
*/
|
||||
public function clear_permission(string $cache_key): self;
|
||||
|
||||
/**
|
||||
* Enqueue a period for automatic flags warming.
|
||||
* @param string $dateFrom
|
||||
* @param string $dateTo
|
||||
* @return self
|
||||
*/
|
||||
public function enqueue_invoice_period_warming(string $dateFrom, string $dateTo): self;
|
||||
|
||||
/**
|
||||
* Consume all queued warming periods and clear the queue.
|
||||
* @return array<int,array{dateFrom:string,dateTo:string}>
|
||||
*/
|
||||
public function consume_invoice_period_warming_queue(): array;
|
||||
}
|
||||
Reference in New Issue
Block a user