public function load($schedule_processed_id) { parent::load($schedule_processed_id); if (isset($this->schedule_process_info->schedule_processed_id)) { $file_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR . "schedule_reports" . DIRECTORY_SEPARATOR . $this->schedule_process_info->schedule_processed_id; if (file_exists($file_path)) { $this->report_complete = file_get_contents($file_path); } } if ($this->schedule_process_info->serialized_report_errors) { $this->errors = unserialize($this->schedule_process_info->serialized_report_errors); } }
public function run($args) { if (!Yii::app()->mutex->lock('ScheduleCommand', 3600)) { Yii::app()->end(); } $synchronization = new Synchronization(); if (!$synchronization->isMaster() and $synchronization->isProcessed()) { return; } $generationTime = time(); $proper_periods = ScheduleCommand::getProperPeriods($generationTime); if (count($proper_periods) === 0) { self::$_logger->log(__METHOD__ . ' Exiting. No proper periods found.' . "\n\n"); // Yii::app()->mutex->unlock(); // Yii::app()->end(); } $criteria = new CDbCriteria(); $criteria->select = array('schedule_id', 'report_type', 'station_id', 'report_format', 'period', 'last_scheduled_run_planned', 'last_scheduled_run_fact', '(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) AS nextScheduleTime', 'UNIX_TIMESTAMP(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) AS nextScheduleUnixTime'); $criteria->with = array('station'); $criteria->compare('period', '>0'); $criteria->compare('period', $proper_periods); $criteria->addCondition('(UNIX_TIMESTAMP(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) <= UNIX_TIMESTAMP() OR `last_scheduled_run_planned` = "0000-00-00 00:00:00")'); /** @var array|ScheduleReport[] $scheduledReports */ $scheduledReports = ScheduleReport::model()->findAll($criteria); if (count($scheduledReports) === 0) { self::$_logger->log(__METHOD__ . ' Exiting. No proper reports found.' . "\n\n"); Yii::app()->mutex->unlock(); Yii::app()->end(); } self::$_logger->log(__METHOD__ . ' New scheduled reports', array('report count' => count($scheduledReports))); $reportProcesses = array(); foreach ($scheduledReports as $scheduledReport) { self::$_logger->log("\n"); self::$_logger->log(__METHOD__ . ' Check scheduled report', array('schedule_id' => $scheduledReport->schedule_id)); $check_period = ScheduleCommand::getCheckPeriod($generationTime, $scheduledReport->period); if ($scheduledReport->report_type === 'data_export') { self::$_logger->log(__METHOD__ . ' scheduledReport->report_type = data_export'); // add record about schedule running to process afterwards for ($i = 0; $i < count($scheduledReport->station); $i++) { $schedule_report_process = new ScheduleReportProcessed(); $schedule_report_process->sr_to_s_id = $scheduledReport->station[$i]->id; $schedule_report_process->check_period_start = $check_period[3]; $schedule_report_process->check_period_end = $check_period[4]; $schedule_report_process->save(); $scheduledReport->last_scheduled_run_fact = $check_period[1]; $scheduledReport->last_scheduled_run_planned = $check_period[2]; if ($scheduledReport->validate()) { $scheduledReport->save(false); } else { self::$_logger->log(__METHOD__ . ' Schedule report not saved ', array('schedule_error' => $scheduledReport->getErrors())); } $reportProcesses[$scheduledReport->station[$i]->id] = array('schedule_id' => $scheduledReport->schedule_id, 'schedule_processed_id' => $schedule_report_process->schedule_processed_id, 'schedule_info' => $scheduledReport, 'check_period_start' => $check_period[3], 'check_period_end' => $check_period[4]); } } else { $logRecords = array(); foreach ($scheduledReport->station as $station) { $criteria = new CDbCriteria(); $criteria->compare('station_id', $station->station_id); $criteria->compare('failed', '0'); $criteria->compare('measuring_timestamp', '>' . $check_period[0]); $criteria->compare('measuring_timestamp', '<=' . $check_period[1]); $criteria->order = 'measuring_timestamp desc, log_id desc'; $criteria->limit = '1'; $logRecord = ListenerLog::model()->find($criteria); if (!is_null($logRecord)) { $logRecords[] = $logRecord; } } // add record about schedule running to process afterwards (only in case system received base message) if (count($logRecords) > 0) { for ($i = 0; $i < count($scheduledReport->station); $i++) { $schedule_report_process = new ScheduleReportProcessed(); $continue = false; foreach ($logRecords as $logRecord) { if ($logRecord->station_id == $scheduledReport->station[$i]->station_id) { $schedule_report_process->listener_log_id = $logRecord->log_id; $listener_log_id = $logRecord->log_id; $continue = false; break; } else { $continue = true; } } if ($continue) { continue; } $schedule_report_process->sr_to_s_id = $scheduledReport->station[$i]->id; $schedule_report_process->check_period_start = $check_period[3]; $schedule_report_process->check_period_end = $check_period[4]; $schedule_report_process->save(); $scheduledReport->last_scheduled_run_fact = $check_period[1]; $scheduledReport->last_scheduled_run_planned = $check_period[2]; if ($scheduledReport->validate()) { $scheduledReport->save(false); } else { self::$_logger->log(__METHOD__ . ' Schedule report not saved ', array('schedule_error' => $scheduledReport->getErrors())); } $reportProcesses[$scheduledReport->station[$i]->id] = array('log_id' => $listener_log_id, 'schedule_id' => $scheduledReport->schedule_id, 'schedule_processed_id' => $schedule_report_process->schedule_processed_id, 'schedule_info' => $scheduledReport, 'check_period_start' => $check_period[3], 'check_period_end' => $check_period[4]); } } } } if (count($reportProcesses) > 0) { $total = count($reportProcesses); $i = 1; foreach ($reportProcesses as $reportProcess) { $weatherReport = null; switch (strtolower($reportProcess['schedule_info']->report_type)) { case 'synop': $weatherReport = WeatherReport::create('Synop', self::$_logger); break; case 'bufr': $weatherReport = WeatherReport::create('Bufr', self::$_logger); break; case 'metar': $weatherReport = WeatherReport::create('Metar', self::$_logger); break; case 'odss': $weatherReport = WeatherReport::create('ODSS', self::$_logger); break; default: $weatherReport = WeatherReport::create('Export', self::$_logger); break; } try { $weatherReport->load($reportProcess['schedule_processed_id']); $weatherReport->generate(); $weatherReport->saveProcess(); $weatherReport->deliverReport(); } catch (Exteption $e) { self::$_logger->log(__METHOD__ . ' Error ', array('err' => $e->getMessage())); } self::$_logger->log(__METHOD__ . ' Completed', array('num' => $i++, 'total' => $total)); } } //send report from sttions together $scheduleProcessedIdArray_schedule_id = array(); foreach ($reportProcesses as $reportProcess) { $scheduleProcessedIdArray_schedule_id[$reportProcess['schedule_id']][] = $reportProcess['schedule_processed_id']; } foreach ($scheduleProcessedIdArray_schedule_id as $scheduleProcessedIdArray) { new WeatherReportMailSender($scheduleProcessedIdArray); } self::$_logger->log(__METHOD__ . ' Schedule report completed' . "\n\n\n\n\n\n\n\n\n"); Yii::app()->mutex->unlock(); }
public function actionScheduleRegenerateReport() { // All reports are generated basing on data in UTC time. TimezoneWork::set('UTC'); $return = array('ok' => 0); $schedule_processed_id = isset($_REQUEST['schedule_processed_id']) ? intval($_REQUEST['schedule_processed_id']) : null; if ($schedule_processed_id) { $data = ScheduleReportProcessed::getInfoForRegenerate($schedule_processed_id); if (!is_null($data)) { $reportType = null; switch ($data->ScheduleReportToStation->schedule_report->report_type) { case 'bufr': $reportType = 'Bufr'; break; case 'synop': $reportType = 'Synop'; break; case 'metar': $reportType = 'Metar'; break; case 'speci': $reportType = 'Speci'; break; default: $reportType = 'Export'; break; } $weather_report = WeatherReport::create($reportType); $weather_report->load($schedule_processed_id); if (!$weather_report->errors) { $weather_report->generate(); $weather_report->prepareReportComplete(); $weather_report->saveProcess(); $return['ok'] = 1; $return['report_string_initial'] = $weather_report->getReportComplete(); } else { $return['errors'] = $weather_report->errors; } } } print json_encode($return); CApplication::end(); }