clock->now(); if (null !== $cronJobExecution && $now->sub(new \DateInterval('PT23H45M')) < $cronJobExecution->getLastStart()) { return false; } // Run between 6 and 9 AM return in_array((int) $now->format('H'), [6, 7, 8], true); } public function getKey(): string { return 'daily-notification-digest'; } /** * @throws \DateInvalidOperationException * @throws Exception */ public function run(array $lastExecutionData): ?array { $now = $this->clock->now(); if (isset($lastExecutionData['last_execution'])) { $lastExecution = \DateTimeImmutable::createFromFormat( \DateTimeImmutable::ATOM, $lastExecutionData['last_execution'] ); } else { $lastExecution = $now->sub(new \DateInterval('P1D')); } // Get distinct users who received notifications since the last execution $sql = <<<'SQL' SELECT DISTINCT cmnau.user_id FROM chill_main_notification cmn JOIN chill_main_notification_addresses_user cmnau ON cmnau.notification_id = cmn.id WHERE cmn.date >= :lastExecution AND cmn.date <= :now SQL; $sqlStatement = $this->connection->prepare($sql); $sqlStatement->bindValue('lastExecution', $lastExecution->format(\DateTimeInterface::RFC3339)); $sqlStatement->bindValue('now', $now->format(\DateTimeInterface::RFC3339)); $result = $sqlStatement->executeQuery(); $count = 0; foreach ($result->fetchAllAssociative() as $row) { $userId = (int) $row['user_id']; $message = new ScheduleDailyNotificationDigestMessage( $userId, $lastExecution, $now ); $this->messageBus->dispatch($message); ++$count; } $this->logger->info('[DailyNotificationDigestCronjob] Dispatched daily digest messages', [ 'user_count' => $count, 'last_execution' => $lastExecution->format('Y-m-d-H:i:s.u e'), 'current_time' => $now->format('Y-m-d-H:i:s.u e'), ]); return [ 'last_execution' => $now->format('Y-m-d-H:i:s.u e'), ]; } }