public function sendPeriodicalReport(\Magento\Cron\Model\Schedule $schedule = null)
 {
     try {
         $emails = explode(',', $this->_watchlogHelper->getDefaultConfig("watchlog/periodical_report/emails"));
         if ($this->_watchlogHelper->getDefaultConfig("watchlog/periodical_report/enable_reporting") && count($emails) > 0) {
             $log = [];
             $update = $this->_watchlogHelper->getDefaultConfig("watchlog/periodical_report/last_report");
             $cronExpr = json_decode($this->_watchlogHelper->getDefaultConfig("watchlog/periodical_report/cron"));
             $cron['curent']['localDate'] = $this->_coreDate->date('l Y-m-d H:i:s');
             $cron['curent']['gmtDate'] = $this->_coreDate->gmtDate('l Y-m-d H:i:s');
             $cron['curent']['localTime'] = $this->_coreDate->timestamp();
             $cron['curent']['gmtTime'] = $this->_coreDate->gmtTimestamp();
             $cron['file']['localDate'] = $this->_coreDate->date('l Y-m-d H:i:s', $update);
             $cron['file']['gmtDate'] = $update;
             $cron['file']['localTime'] = $this->_coreDate->timestamp($update);
             $cron['file']['gmtTime'] = strtotime($update);
             $cron['offset'] = $this->_coreDate->getGmtOffset("hours");
             $this->_logger->notice("-------------------- REPORT PROCESS --------------------");
             $this->_logger->notice('   * Last update : ' . $cron['file']['gmtDate'] . " GMT / " . $cron['file']['localDate'] . ' GMT+' . $cron['offset']);
             $this->_logger->notice('   * Current date : ' . $cron['curent']['gmtDate'] . " GMT / " . $cron['curent']['localDate'] . ' GMT+' . $cron['offset']);
             $i = 0;
             if (isset($cronExpr) && $cronExpr != null) {
                 foreach ($cronExpr->days as $d) {
                     foreach ($cronExpr->hours as $h) {
                         $time = explode(':', $h);
                         if (date('l', $cron['curent']['gmtTime']) == $d) {
                             $cron['tasks'][$i]['localTime'] = strtotime($this->_coreDate->date('Y-m-d')) + $time[0] * 60 * 60 + $time[1] * 60;
                             $cron['tasks'][$i]['localDate'] = date('l Y-m-d H:i:s', $cron['tasks'][$i]['localTime']);
                         } else {
                             $cron['tasks'][$i]['localTime'] = strtotime("last " . $d, $cron['curent']['localTime']) + $time[0] * 60 * 60 + $time[1] * 60;
                             $cron['tasks'][$i]['localDate'] = date('l Y-m-d H:i:s', $cron['tasks'][$i]['localTime']);
                         }
                         if ($cron['tasks'][$i]['localTime'] >= $cron['file']['localTime'] && $cron['tasks'][$i]['localTime'] <= $cron['curent']['localTime'] && $done != true) {
                             $this->_logger->notice('   * Scheduled : ' . ($cron['tasks'][$i]['localDate'] . " GMT" . $cron['offset']));
                             $this->sendReport($emails);
                             $this->_logger->notice("Report sent to " . implode(", ", $emails));
                             $this->_watchlogHelper->setDefaultConfig("watchlog/settings/last_report", $this->_coreDate->gmtDate("Y-m-d H:i:s"));
                         }
                         $i++;
                     }
                 }
             }
         }
     } catch (\Exception $e) {
         if ($schedule != null) {
             $schedule->setStatus('failed');
             $schedule->setMessage($e->getMessage());
             $schedule->save();
         }
         $this->_logger->notice("Process failed");
         $this->_logger->notice($e->getMessage());
     }
 }
Exemple #2
0
 public function purge(\Magento\Cron\Model\Schedule $schedule = null)
 {
     try {
         $timestamp = $this->_datetime->gmtTimestamp();
         $histolength = $this->_watchlogHelper->getDefaultConfig("watchlog/settings/history");
         $deleteBefore = $timestamp - $histolength * 60 * 60 * 24;
         if ($histolength != 0) {
             $this->_logger->notice("-------------------- PURGE PROCESS --------------------");
             $this->_logger->notice("-- current date : " . $this->_datetime->gmtDate('Y-m-d H:i:s', $timestamp));
             $this->_logger->notice("-- deleting row before : " . $this->_datetime->gmtDate('Y-m-d H:i:s', $deleteBefore));
             $nbDeleted = $this->_attemptsCollectionFactory->create()->purge($deleteBefore);
             $this->_logger->notice("-- {$nbDeleted} rows deleted");
         }
     } catch (\Exception $e) {
         if ($schedule) {
             $schedule->setStatus('failed');
             $schedule->setMessage($e->getMessage());
             $schedule->save();
         }
         $this->_logger->notice("MASSIVE ERROR ! ");
         $this->_logger->notice($e->getMessage());
     }
 }
 /**
  * Execute job by calling specific class::method
  *
  * @param int $scheduledTime
  * @param int $currentTime
  * @param string[] $jobConfig
  * @param Schedule $schedule
  * @param string $groupId
  * @return void
  * @throws \Exception
  */
 protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId)
 {
     $scheduleLifetime = (int) $this->_scopeConfig->getValue('system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_LIFETIME, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     $scheduleLifetime = $scheduleLifetime * self::SECONDS_IN_MINUTE;
     if ($scheduledTime < $currentTime - $scheduleLifetime) {
         $schedule->setStatus(Schedule::STATUS_MISSED);
         throw new \Exception('Too late for the schedule');
     }
     if (!isset($jobConfig['instance'], $jobConfig['method'])) {
         $schedule->setStatus(Schedule::STATUS_ERROR);
         throw new \Exception('No callbacks found');
     }
     $model = $this->_objectManager->create($jobConfig['instance']);
     $callback = [$model, $jobConfig['method']];
     if (!is_callable($callback)) {
         $schedule->setStatus(Schedule::STATUS_ERROR);
         throw new \Exception(sprintf('Invalid callback: %s::%s can\'t be called', $jobConfig['instance'], $jobConfig['method']));
     }
     $schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))->save();
     try {
         call_user_func_array($callback, [$schedule]);
     } catch (\Exception $e) {
         $schedule->setStatus(Schedule::STATUS_ERROR);
         throw $e;
     }
     $schedule->setStatus(Schedule::STATUS_SUCCESS)->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()));
 }
Exemple #4
0
 /**
  * Check if google shopping data feeds need to be generated
  * @param \Magento\Cron\Model\Schedule $schedule
  */
 public function checkToGenerate(\Magento\Cron\Model\Schedule $schedule)
 {
     try {
         $log = [];
         $this->_logger->notice("-------------------- CRON PROCESS --------------------");
         $log[] = "-------------------- CRON PROCESS --------------------";
         $coll = $this->_collectionFactory->create();
         $cnt = 0;
         $first = true;
         foreach ($coll as $feed) {
             $done = false;
             try {
                 $feed->isCron = true;
                 if ($first) {
                     $feed->loadCustomFunctions();
                     $first = false;
                 }
                 $this->_logger->notice("--> Running profile : " . $feed->getSimplegoogleshoppingFilename() . " [#" . $feed->getSimplegoogleshoppingId() . "] <--");
                 $log[] = "--> Running profile : " . $feed->getSimplegoogleshoppingFilename() . " [#" . $feed->getSimplegoogleshoppingId() . "] <--";
                 $cron = [];
                 $cron['curent']['localDate'] = $this->_coreDate->date('l Y-m-d H:i:s');
                 $cron['curent']['gmtDate'] = $this->_coreDate->gmtDate('l Y-m-d H:i:s');
                 $cron['curent']['localTime'] = $this->_coreDate->timestamp();
                 $cron['curent']['gmtTime'] = $this->_coreDate->gmtTimestamp();
                 $cron['file']['localDate'] = $this->_coreDate->date('l Y-m-d H:i:s', $feed->getSimplegoogleshoppingTime());
                 $cron['file']['gmtDate'] = $feed->getSimplegoogleshoppingTime();
                 $cron['file']['localTime'] = $this->_coreDate->timestamp($feed->getSimplegoogleshoppingTime());
                 $cron['file']['gmtTime'] = strtotime($feed->getSimplegoogleshoppingTime());
                 $cron['offset'] = $this->_coreDate->getGmtOffset("hours");
                 $log[] = '   * Last update : ' . $cron['file']['gmtDate'] . " GMT / " . $cron['file']['localDate'] . ' GMT' . $cron['offset'] . "";
                 $log[] = '   * Current date : ' . $cron['curent']['gmtDate'] . " GMT / " . $cron['curent']['localDate'] . ' GMT' . $cron['offset'] . "";
                 $this->_logger->notice('   * Last update : ' . $cron['file']['gmtDate'] . " GMT / " . $cron['file']['localDate'] . ' GMT' . $cron['offset']);
                 $this->_logger->notice('   * Current date : ' . $cron['curent']['gmtDate'] . " GMT / " . $cron['curent']['localDate'] . ' GMT' . $cron['offset']);
                 $cronExpr = json_decode($feed->getCronExpr());
                 $i = 0;
                 if ($cronExpr != null && isset($cronExpr->days)) {
                     foreach ($cronExpr->days as $d) {
                         foreach ($cronExpr->hours as $h) {
                             $time = explode(':', $h);
                             if (date('l', $cron['curent']['gmtTime']) == $d) {
                                 $cron['tasks'][$i]['localTime'] = strtotime($this->_coreDate->date('Y-m-d')) + $time[0] * 60 * 60 + $time[1] * 60;
                                 $cron['tasks'][$i]['localDate'] = date('l Y-m-d H:i:s', $cron['tasks'][$i]['localTime']);
                             } else {
                                 $cron['tasks'][$i]['localTime'] = strtotime("last " . $d, $cron['curent']['localTime']) + $time[0] * 60 * 60 + $time[1] * 60;
                                 $cron['tasks'][$i]['localDate'] = date('l Y-m-d H:i:s', $cron['tasks'][$i]['localTime']);
                             }
                             if ($cron['tasks'][$i]['localTime'] >= $cron['file']['localTime'] && $cron['tasks'][$i]['localTime'] <= $cron['curent']['localTime'] && $done != true) {
                                 $this->_logger->notice('   * Scheduled : ' . ($cron['tasks'][$i]['localDate'] . " GMT" . $cron['offset']));
                                 $log[] = '   * Scheduled : ' . ($cron['tasks'][$i]['localDate'] . " GMT" . $cron['offset']) . "";
                                 $this->_logger->notice("   * Starting generation");
                                 $result = $feed->generateXml();
                                 if ($result === $feed) {
                                     $done = true;
                                     $this->_logger->notice("   * EXECUTED!");
                                     $log[] = "   * EXECUTED!";
                                 } else {
                                     $this->_logger->notice("   * ERROR! " . $result);
                                     $log[] = "   * ERROR! " . $result . "";
                                 }
                                 $cnt++;
                                 break 2;
                             }
                             $i++;
                         }
                     }
                 }
             } catch (\Exception $e) {
                 $cnt++;
                 $this->_logger->notice("   * ERROR! " . $e->getMessage());
                 $log[] = "   * ERROR! " . $e->getMessage() . "";
             }
             if (!$done) {
                 $this->_logger->notice("   * SKIPPED!");
                 $log[] = "   * SKIPPED!";
             }
         }
         if ($this->_coreHelper->getStoreConfig("simplegoogleshopping/settings/enable_reporting")) {
             $emails = explode(',', $this->_coreHelper->getStoreConfig("simplegoogleshopping/settings/emails"));
             if (count($emails) > 0) {
                 try {
                     if ($cnt) {
                         $template = "wyomind_simplegoogleshopping_cron_report";
                         $transport = $this->_transportBuilder->setTemplateIdentifier($template)->setTemplateOptions(['area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])->setTemplateVars(['report' => implode("<br/>", $log), 'subject' => $this->_coreHelper->getStoreConfig('simplegoogleshopping/settings/report_title')])->setFrom(['email' => $this->_coreHelper->getStoreConfig('simplegoogleshopping/settings/sender_email'), 'name' => $this->_coreHelper->getStoreConfig('simplegoogleshopping/settings/sender_name')])->addTo($emails[0]);
                         $count = count($emails);
                         for ($i = 1; $i < $count; $i++) {
                             $transport->addCc($emails[$i]);
                         }
                         $transport->getTransport()->sendMessage();
                     }
                 } catch (\Exception $e) {
                     $this->_logger->notice('   * EMAIL ERROR! ' . $e->getMessage());
                     $log[] = '   * EMAIL ERROR! ' . $e->getMessage();
                 }
             }
         }
     } catch (\Exception $e) {
         $schedule->setStatus('failed');
         $schedule->setMessage($e->getMessage());
         $schedule->save();
         $this->_logger->notice("MASSIVE ERROR ! ");
         $this->_logger->notice($e->getMessage());
     }
 }