Нет описания

SubscribersCountCacheRecalculation.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php declare(strict_types = 1);
  2. namespace MailPoet\Cron\Workers;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Cache\TransientCache;
  5. use MailPoet\Entities\SegmentEntity;
  6. use MailPoet\Models\ScheduledTask;
  7. use MailPoet\Segments\SegmentsRepository;
  8. use MailPoet\Subscribers\SubscribersCountsController;
  9. use MailPoet\WP\Functions as WPFunctions;
  10. use MailPoetVendor\Carbon\Carbon;
  11. class SubscribersCountCacheRecalculation extends SimpleWorker {
  12. private const EXPIRATION_IN_MINUTES = 30;
  13. const TASK_TYPE = 'subscribers_count_cache_recalculation';
  14. const AUTOMATIC_SCHEDULING = false;
  15. const SUPPORT_MULTIPLE_INSTANCES = false;
  16. /** @var TransientCache */
  17. private $transientCache;
  18. /** @var SegmentsRepository */
  19. private $segmentsRepository;
  20. /** @var SubscribersCountsController */
  21. private $subscribersCountsController;
  22. public function __construct(
  23. TransientCache $transientCache,
  24. SegmentsRepository $segmentsRepository,
  25. SubscribersCountsController $subscribersCountsController,
  26. WPFunctions $wp
  27. ) {
  28. parent::__construct($wp);
  29. $this->transientCache = $transientCache;
  30. $this->segmentsRepository = $segmentsRepository;
  31. $this->subscribersCountsController = $subscribersCountsController;
  32. }
  33. public function processTaskStrategy(ScheduledTask $task, $timer) {
  34. $segments = $this->segmentsRepository->findAll();
  35. foreach ($segments as $segment) {
  36. $this->recalculateSegmentCache($timer, (int)$segment->getId(), $segment);
  37. }
  38. // update cache for subscribers without segment
  39. $this->recalculateSegmentCache($timer, 0);
  40. // remove redundancies from cache
  41. $this->subscribersCountsController->removeRedundancyFromStatisticsCache();
  42. return true;
  43. }
  44. private function recalculateSegmentCache($timer, int $segmentId, ?SegmentEntity $segment = null): void {
  45. $this->cronHelper->enforceExecutionLimit($timer);
  46. $now = Carbon::now();
  47. $item = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId);
  48. if ($item === null || !isset($item['created_at']) || $now->diffInMinutes($item['created_at']) > self::EXPIRATION_IN_MINUTES) {
  49. if ($segment) {
  50. $this->subscribersCountsController->recalculateSegmentStatisticsCache($segment);
  51. if ($segment->isStatic()) {
  52. $this->subscribersCountsController->recalculateSegmentGlobalStatusStatisticsCache($segment);
  53. }
  54. } else {
  55. $this->subscribersCountsController->recalculateSubscribersWithoutSegmentStatisticsCache();
  56. }
  57. }
  58. }
  59. public function getNextRunDate() {
  60. return Carbon::createFromTimestamp($this->wp->currentTime('timestamp'));
  61. }
  62. public function shouldBeScheduled(): bool {
  63. $scheduledOrRunningTask = $this->scheduledTasksRepository->findScheduledOrRunningTask(self::TASK_TYPE);
  64. if ($scheduledOrRunningTask) {
  65. return false;
  66. }
  67. $now = Carbon::now();
  68. $oldestCreatedAt = $this->transientCache->getOldestCreatedAt(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY);
  69. return $oldestCreatedAt === null || $now->diffInMinutes($oldestCreatedAt) > self::EXPIRATION_IN_MINUTES;
  70. }
  71. }