/** * @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; }
public function run($args) { $logger = LoggerFactory::getFileLogger('check_processes'); $logger->log(__METHOD__ . ' Start checkprocesses command.'); $criteria = new CDbCriteria(); $criteria->compare('stopped', 0); $connections = Listener::model()->findAll($criteria); $logger->log(__METHOD__ . ' Found ' . count($connections) . ' connections.'); foreach ($connections as $connection) { $logger->log(__METHOD__ . ' Listener info:', array('process_pid' => $connection->process_pid, 'listener_id' => $connection->listener_id, 'source' => $connection->source)); if (ProcessPid::isActiveProcess($connection->process_pid) === false) { ListenerProcess::addComment($connection->listener_id, 'comment', 'System found out that process is not active any more. Process will be re-run right now.'); Listener::stopConnection($connection->listener_id, time()); Listener::runConnection($connection->source, $connection->additional_param, 'auto'); } } // ProcessPid::killProcess(12376); $synchronization = new Synchronization(); if ($synchronization->isProcessed() && ProcessPid::isActiveProcess($synchronization->getTcpServerPid()) === false) { $synchronization->startTcpServer(); } if ($synchronization->isProcessed() && ProcessPid::isActiveProcess($synchronization->getTcpClientPid()) === false) { $synchronization->startTcpClient(); } }
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(); }
/** * Ini system param */ public function init() { parent::init(); ini_set('memory_limit', '-1'); set_time_limit(0); error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT); $this->_synchronization = new Synchronization(); $this->_logger = LoggerFactory::getFileLogger('TcpClientCommand'); // $this->_logger = LoggerFactory::getConsoleLogger(); if (Synchronization::setTcpClientPid(getmypid())) { $this->_logger->log(__METHOD__ . ' ' . 'process can take pid = ' . getmypid()); } else { $this->_logger->log(__METHOD__ . ' ' . 'process can not take pid.'); } }
/** * Test fir synchronize method */ public function testSynchronize() { $content = 'content'; $relativeFileName = 'config.xml'; $filePath = realpath(__DIR__ . '/_files/'); $storageFactoryMock = $this->getMock('Magento\\Core\\Model\\File\\Storage\\DatabaseFactory', array('create', '_wakeup'), array(), '', false); $storageMock = $this->getMock('Magento\\Core\\Model\\File\\Storage\\Database', array('getContent', 'getId', 'loadByFilename', '__wakeup'), array(), '', false); $storageFactoryMock->expects($this->once())->method('create')->will($this->returnValue($storageMock)); $storageMock->expects($this->once())->method('getContent')->will($this->returnValue($content)); $storageMock->expects($this->once())->method('getId')->will($this->returnValue(true)); $storageMock->expects($this->once())->method('loadByFilename'); $file = $this->getMock('Magento\\Framework\\Filesystem\\File\\Write', array('lock', 'write', 'unlock', 'close'), array(), '', false); $file->expects($this->once())->method('lock'); $file->expects($this->once())->method('write')->with($content); $file->expects($this->once())->method('unlock'); $file->expects($this->once())->method('close'); $directory = $this->getMock('Magento\\Framework\\Filesystem\\Direcoty\\Write', array('openFile', 'getRelativePath'), array(), '', false); $directory->expects($this->once())->method('getRelativePath')->will($this->returnArgument(0)); $directory->expects($this->once())->method('openFile')->with($filePath)->will($this->returnValue($file)); $filesystem = $this->getMock('Magento\\Framework\\App\\Filesystem', array('getDirectoryWrite'), array(), '', false); $filesystem->expects($this->once())->method('getDirectoryWrite')->with(\Magento\Framework\App\Filesystem::PUB_DIR)->will($this->returnValue($directory)); $model = new Synchronization($storageFactoryMock, $filesystem); $model->synchronize($relativeFileName, $filePath); }
public function init() { $this->_logger->log(__METHOD__); $this->settings = Settings::model()->find(); $this->_logger->log(__METHOD__ . $this->communication_type); $this->listener = Listener::getCurrent($this->source, $this->communication_type); $this->_logger->log(__METHOD__ . ': ' . print_r($this->listener, 1)); if (!$this->listener->started) { $this->listener->process_pid = getmypid(); $this->listener->started = time(); $this->listener->save(); Synchronization::trySetActualListenerId($this->source, $this->listener->listener_id); ListenerProcess::addComment($this->listener->listener_id, 'started', 'by ' . $this->by); } else { ListenerProcess::addComment($this->listener->listener_id, 'still_in_process'); } }
public function init() { $this->_configForm = new ConfigForm(Synchronization::getSettingsFilePath()); $this->_data = $this->_configForm->getConfig(); $this->server_ip = $this->_data['SERVER_IP']['value']; $this->server_port = $this->_data['SERVER_PORT']['value']; $this->remote_server_ip = $this->_data['REMOTE_SERVER_IP']['value']; $this->remote_server_port = $this->_data['REMOTE_SERVER_PORT']['value']; $this->switch_variant = $this->_data['SWITCH_VARIANT']['value']; $this->flexibility_role = $this->_data['FLEXIBILITY_ROLE']['value']; $this->process_status = $this->_data['PROCESS_STATUS']['value']; $this->main_role = $this->_data['MAIN_ROLE']['value']; $this->forwarding_messages_ip = $this->_data['FOR_COMES_FORWARDING_MESSAGES_IP']['value']; $this->forwarding_messages_port = $this->_data['FOR_COMES_FORWARDING_MESSAGES_PORT']['value']; $this->for_send_messages_to_ip = $this->_data['FOR_SEND_MESSAGES_TO_IP']['value']; $this->for_send_messages_port = $this->_data['FOR_SEND_MESSAGES_PORT']['value']; $this->identificator = $this->_data['IDENTIFICATOR']['value']; $this->tcp_server_command_port = $this->_data['TCP_SERVER_COMMAND_PORT']['value']; $this->tcp_client_command_port = $this->_data['TCP_CLIENT_COMMAND_PORT']['value']; $this->tcp_server_command_pid = $this->_data['TCP_SERVER_COMMAND_PID']; $this->tcp_client_command_pid = $this->_data['TCP_CLIENT_COMMAND_PID']; parent::init(); }
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(); }
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 } }
/** * @return bool */ protected function isFlexibility() { return $this->settings->isFlexibilitySwitchVariant(); }
">v <?php Yii::app()->params['version']['sprint']; ?> </a> </div> <div id="copyright"> <?php echo It::t('site_label', 'main_layout__footer_year'); ?> <?php echo It::t('site_label', 'main_layout__footer_copyright'); ?> </div> <div style="clear:both"></div> <?php $synchronization = new Synchronization(); ?> <div class="status" > Main role: <?php echo $synchronization->getMainRole(); ?> . Current role: <?php echo $synchronization->getRole(); ?> . <?php if ($synchronization->isProcessed()) { ?> Synchronization is in process.
<?php require_once "src/Synchronization.php"; $handler1 = new Synchronization("http://eric-zieger.de/humans.txt"); if ($handler1->updateLocalFile()) { echo "Datei: <strong>" . $handler1->getLocalFilename() . "</strong> wurde geupdated.<br>"; } else { echo "Keine Neuerung an der Datei: <strong>" . $handler1->getRemoteUrl() . "</strong><br>"; } $handler2 = new Synchronization("http://eric-zieger.de/robots.txt"); if ($handler2->updateLocalFile()) { echo "Datei: <strong>" . $handler2->getLocalFilename() . "</strong> wurde geupdated."; } else { echo "Keine Neuerung an der Datei: <strong>" . $handler2->getRemoteUrl() . "</strong>"; }
/** * 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()); } }
public function actionSyncSettings() { // $parseMessage = new ParseMessage(LoggerFactory::getFileLogger('parse_message'),'@DAWS01150429145700TP21005BV1133CH1s51560648206968071350008706527043570250700718PR109940TP1101561782E43$'); // echo "<pre>"; // print_r( $parseMessage->getStationIdCode()); // echo "</pre>";exit; $form = new SynchronizationForm(); $synchronization = new Synchronization(); if (Yii::app()->request->isPostRequest && isset($_POST['SynchronizationForm']) && isset($_POST['__save'])) { $form->attributes = $_POST['SynchronizationForm']; if ($form->validate()) { if ($form->process_status == 0) { if ($form->flexibility_role == 1) { $synchronization->setInMaster(); } if ($form->flexibility_role == 2) { $synchronization->setInSlave(); } } $form->save(); It::memStatus('Setting up synchronization was updated'); } } if (Yii::app()->request->isPostRequest && isset($_POST['process_start'])) { $synchronization->switchProcess(); It::memStatus($synchronization->process_status == 'stopped' ? 'Process was stopped' : 'Process was started'); } $disabled = ''; if ($synchronization->process_status == 'processed') { $disabled = 'disabled'; } $this->render('sync_settings', array('form' => $form, 'synchronization' => $synchronization, 'disabled' => $disabled)); }