public function execute() { $this->_logger->setInstanceName($this->instanceName); $this->_pidFile = new PidFile("", $this->instanceName); while (true) { $startedAt = time(); $startedAt -= $startedAt % 60; $collection = new RecordCollection(new Campaign()); if (!$collection->hasMappers()) { Log::warning('No mappers found'); } foreach ($collection as $campaign) { /** @var Campaign $campaign */ if ($campaign->isDue($startedAt)) { try { Defero::pushCampaign($campaign->id(), $startedAt); if (CronParser::isValid($campaign->sendAt)) { // check average sends on scheduled $avgEndDate = (new \DateTime())->setTimestamp($startedAt); $avgStartDate = CronParser::prevRun($campaign->sendAt, $avgEndDate); $avgEndDate->sub($avgStartDate->diff($avgEndDate)); $avgStartDate->setTime($avgStartDate->format('H') - 1, 0, 0); $latestStats = MailStatistic::getCampaignStats($campaign->id(), $avgStartDate, $avgEndDate); $diff = $avgStartDate->diff($avgEndDate); $diffLatest = max(1, intval($diff->format('%i')) + intval($diff->format('%h') * 60) + intval($diff->format('%d') * 3600)); $latestHourly = $latestStats->sent / $diffLatest * 60; if ($campaign->warnMin && $latestHourly < $campaign->warnMin || $campaign->warnMax && $latestHourly > $campaign->warnMax) { Log::warning('Sending outside threshold', ['campaign' => $campaign->id(), 'average' => $latestHourly, 'warnMin' => $campaign->warnMin, 'warnMax' => $campaign->warnMax]); } } } catch (\Exception $e) { Log::error('Campaign ' . $campaign->id() . ': ' . $e->getMessage() . ' (Line: ' . $e->getLine() . ')'); } } else { Log::debug('Campaign ' . $campaign->id() . ' not due'); } } $endTime = time(); $endTime -= $endTime % 60; if ($endTime == $startedAt) { sleep(30); } } }
/** * @expectedException InvalidArgumentException */ public function testInvalidPattern() { CronParser::isDue('not valid'); $this->fail('Failed to throw InvalidArgumentException'); }
public function nextRun($time = null) { if (!$this->sendAt) { return null; } if (!CronParser::isValid($this->sendAt)) { return DateTimeHelper::dateTimeFromAnything($this->sendAt); } if (is_int($time)) { $time = (new \DateTime())->setTimestamp($time); } $nr = CronParser::nextRun($this->sendAt, $time, true); return $nr ?: null; }