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(); $lastExecution = isset($lastExecutionData['last_execution']) ? new \DateTimeImmutable($lastExecutionData['last_execution']) : $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('Y-m-d H:i:s')); $sqlStatement->bindValue('now', $now->format('Y-m-d H:i:s')); $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'), 'current_time' => $now->format('Y-m-d H:i:s'), ]); return [ 'last_execution' => $now->format('Y-m-d H:i:s'), ]; } }