Esempio n. 1
0
 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);
         }
     }
 }
Esempio n. 2
0
 public function getStats(\DateTime $from, \DateTime $to, $language = null)
 {
     return MailStatistic::getCampaignStats($this->id(), $from, $to, $language);
 }