/**
  * @param $msg
  * @param $listener_id
  * @param $rewrite_prev_values
  * @param string $source
  * @param int $station_id
  * @param null $source_info
  * @return int
  * @var $synchronization Synchronization
  */
 public static function addNew($msg, $listener_id, $rewrite_prev_values, $source = '', $station_id = 0, $source_info = null)
 {
     $synchronization = new Synchronization();
     $listener_id_from_master = $synchronization->getListenerId();
     $parseMessage = new ParseMessage(LoggerFactory::getFileLogger('parse_message'), $msg);
     $message_obj = new ListenerLogTemp();
     $message_obj->listener_id = $listener_id;
     $message_obj->station_id_code = $parseMessage->getStationIdCode();
     $message_obj->message = $msg;
     $message_obj->measuring_timestamp = $parseMessage->getMeasuringTimestamp();
     $message_obj->is_processed = 0;
     $message_obj->is_processing = 0;
     if ($listener_id_from_master === $listener_id and $listener_id != 0) {
         $message_obj->from_master = 1;
     } else {
         $message_obj->from_master = 0;
     }
     $message_obj->source = $source;
     $message_obj->source_info = $source_info;
     if ($synchronization->isProcessed()) {
         $message_obj->synchronization_mode = $synchronization->isMaster() ? 'master' : ($synchronization->isSlave() ? 'slave' : 'none');
     } else {
         $message_obj->synchronization_mode = 'none';
     }
     $message_obj->rewrite_prev_values = $rewrite_prev_values;
     $message_obj->save();
     return $message_obj->temp_log_id;
 }
 /**
  * @return string - status
  */
 protected function getCurrentStatus()
 {
     $is_master = $this->settings->isMaster();
     if ($is_master) {
         return SyncStatusHandler::WM_STATUS_MASTER;
     } else {
         return SyncStatusHandler::WM_STATUS_SLAVE;
     }
 }
 protected function _flexibilitySwitchLogic()
 {
     //master vs slave slaveRole fixed
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isSlave() && ($this->_synchronizationRSM->isSlaveMainRole() && $this->_synchronizationRSM->isFixedSwitchVariant()))) {
     }
     // master vs master masterRole flexibility
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isMaster() && ($this->_synchronizationRSM->isMasterMainRole() && $this->_synchronizationRSM->isFlexibilitySwitchVariant()))) {
         $this->_synchronization->setInSlave();
     }
     // master vs master maserRole Fixed
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isMaster() && ($this->_synchronizationRSM->isMasterMainRole() && $this->_synchronizationRSM->isFixedSwitchVariant()))) {
         $this->_synchronization->setInSlave();
     }
     //master vs master SlaveRole Flexibility
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isMaster() && ($this->_synchronizationRSM->isSlaveMainRole() && $this->_synchronizationRSM->isFlexibilitySwitchVariant()))) {
         // do nothing
     }
     // master vs master SlaveRole Fixed
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isMaster() && ($this->_synchronizationRSM->isSlaveMainRole() && $this->_synchronizationRSM->isFixedSwitchVariant()))) {
         // do nothing
     }
     // master  vs slave masterRole fixed
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isSlave() && ($this->_synchronizationRSM->isMasterMainRole() && $this->_synchronizationRSM->isFixedSwitchVariant()))) {
         $this->_synchronization->setInSlave();
     }
     // master  vs slave masterRole Flexibility
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isSlave() && ($this->_synchronizationRSM->isMasterMainRole() && $this->_synchronizationRSM->isFlexibilitySwitchVariant()))) {
         // do nothing
     }
     // master  vs slave slaveRole fixed
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isSlave() && ($this->_synchronizationRSM->isSlaveMainRole() && $this->_synchronizationRSM->isFixedSwitchVariant()))) {
         // do nothing
     }
     // master  vs slave slaveRole Flexibility
     if ($this->_synchronization->isMaster() && ($this->_synchronizationRSM->isSlave() && ($this->_synchronizationRSM->isSlaveMainRole() && $this->_synchronizationRSM->isFixedSwitchVariant()))) {
         // do nothing
     }
 }
 public function beforeSave()
 {
     if (!$this->getUseLong()) {
         if ($this->isNewRecord) {
             $this->created = new CDbExpression('NOW()');
         }
         $this->updated = new CDbExpression('NOW()');
         $synchronization = new Synchronization();
         if ($synchronization->isProcessed()) {
             if ($synchronization->isMaster()) {
                 $this->current_role = 'master';
             } else {
                 $this->current_role = 'slave';
             }
         }
     }
     return parent::beforeSave();
 }
    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();
    }
Example #6
0
 /**
  * Connection
  */
 public function actionConnections()
 {
     $this_server = new Synchronization();
     if (!$this_server->isProcessed() || $this_server->isMaster()) {
         $criteria = new CDbCriteria();
         $criteria->order = "communication_port asc, communication_type asc";
         $stations = Station::model()->findAll($criteria);
         $connections = array();
         if (count($stations) > 0) {
             foreach ($stations as $station) {
                 switch ($station->communication_type) {
                     case 'direct':
                         $communication_type = $station->communication_type;
                         $connection_type = $station->communication_port;
                         $key = $station->communication_port . ' ' . $communication_type;
                         break;
                     case 'sms':
                         $communication_type = $station->communication_type;
                         $connection_type = $station->communication_port;
                         $key = $station->communication_port . ' ' . $communication_type;
                         break;
                     case 'tcpip':
                         $communication_type = '';
                         $connection_type = $station->communication_esp_ip . ':' . $station->communication_esp_port;
                         $key = $station->communication_esp_ip . ':' . $station->communication_esp_port;
                         break;
                     case 'gprs':
                         $communication_type = '';
                         $connection_type = 'poller:' . $station->station_id_code;
                         $key = 'poller:' . $station->station_id_code;
                         break;
                     case 'server':
                         $communication_type = '';
                         $connection_type = 'tcp:' . $station->communication_esp_ip . ':' . $station->communication_esp_port;
                         $key = 'tcp:' . $station->communication_esp_ip . ':' . $station->communication_esp_port;
                         break;
                     default:
                         $communication_type = '';
                         $connection_type = '';
                         $key = '';
                 }
                 $connections[$key]['stations'][] = $station;
                 $connections[$key]['connection_type'] = $connection_type;
                 $connections[$key]['communication_type'] = $communication_type;
                 $connections[$key]['blocked'] = false;
                 if (!isset($connections[$key]['last_connection'])) {
                     $last_connection = Listener::getLastConnectionInfo($connection_type, $communication_type);
                     $connections[$key]['last_connection'] = $last_connection;
                 }
             }
             $connectionsInProcess = array();
             foreach ($connections as $key => $connection) {
                 if (empty($connections[$key]['last_connection']['stopped_show'])) {
                     $connectionsInProcess[$key]['connection_type'] = $connections[$key]['connection_type'];
                     $connectionsInProcess[$key]['communication_type'] = $connections[$key]['communication_type'];
                 }
             }
             foreach ($connections as $key => $connection) {
                 foreach ($connectionsInProcess as $key2 => $connectionActive) {
                     if ($connections[$key]['connection_type'] == $connectionActive['connection_type'] && $connections[$key]['communication_type'] != $connectionActive['communication_type']) {
                         $connections[$key]['blocked'] = true;
                     }
                 }
             }
         }
         $this->render('connections', array('connections' => $connections));
     } else {
         $this->render('connections_slave_mode', array());
     }
 }