public function DBInsertNoReload() { $oMutex = new iTopMutex('ticket_insert'); $oMutex->Lock(); $iNextId = MetaModel::GetNextKey(get_class($this)); $sRef = $this->MakeTicketRef($iNextId); $this->Set('ref', $sRef); $iKey = parent::DBInsertNoReload(); $oMutex->Unlock(); return $iKey; }
case 'restore_exec': require_once APPROOT . "setup/runtimeenv.class.inc.php"; require_once APPROOT . '/application/utils.inc.php'; require_once APPROOT . '/setup/backup.class.inc.php'; require_once dirname(__FILE__) . '/dbrestore.class.inc.php'; IssueLog::Enable(APPROOT . 'log/error.log'); $oPage = new ajax_page(""); $oPage->no_cache(); $oPage->SetContentType('text/html'); if (utils::GetConfig()->Get('demo_mode')) { $oPage->add("<div data-error-stimulus=\"Error\">Sorry, iTop is in <b>demonstration mode</b>: the feature is disabled.</div>"); } else { $sEnvironment = utils::ReadParam('environment', 'production', false, 'raw_data'); $oRestoreMutex = new iTopMutex('restore.' . $sEnvironment); IssueLog::Info("Backup Restore - Acquiring the LOCK 'restore.{$sEnvironment}'"); $oRestoreMutex->Lock(); IssueLog::Info('Backup Restore - LOCK acquired, executing...'); try { set_time_limit(0); // Get the file and destroy the token (single usage) $sToken = utils::ReadParam('token', '', false, 'raw_data'); $sTokenFile = APPROOT . '/data/restore.' . $sToken . '.tok'; if (!is_file($sTokenFile)) { throw new Exception("Error: missing token file: '{$sTokenFile}'"); } $sFile = file_get_contents($sTokenFile); unlink($sTokenFile); $sMySQLBinDir = utils::ReadParam('mysql_bindir', '', false, 'raw_data'); $sDBHost = utils::ReadParam('db_host', '', false, 'raw_data'); $sDBUser = utils::ReadParam('db_user', '', false, 'raw_data'); $sDBPwd = utils::ReadParam('db_pwd', '', false, 'raw_data');
// // Go for parsing and interpretation // try { $oP->add_comment('Load--------------'); $oP->add_comment('------------------'); if ($bSimulate) { CMDBSource::Query('START TRANSACTION'); } $aData = $oCSVParser->ToArray(); $iLineCount = count($aData); $sTable = $oDataSource->GetDataTable(); // Prepare insert columns $sInsertColumns = '`' . implode('`, `', $aInputColumns) . '`'; $oMutex = new iTopMutex('synchro_import_' . $oDataSource->GetKey()); $oMutex->Lock(); foreach ($aData as $iRow => $aRow) { $sReconciliationCondition = "`primary_key` = " . CMDBSource::Quote($aRow[$iPrimaryKeyCol]); $sSelect = "SELECT COUNT(*) FROM `{$sTable}` WHERE {$sReconciliationCondition}"; $aRes = CMDBSource::QueryToArray($sSelect); $iCount = $aRes[0]['COUNT(*)']; if ($iCount == 0) { // No record... create it // $iCountCreations++; if ($sOutput == 'details') { $oP->add("{$iRow}: New entry, reconciliation: '{$sReconciliationCondition}'\n"); } $aValues = array(); // Used to build the insert query foreach ($aRow as $iCol => $value) {
/** * Perform a synchronization between the data stored in the replicas (&synchro_data_xxx_xx table) * and the iTop objects. If the lastFullLoadStartDate is NOT specified then the full_load_periodicity * is used to determine which records are obsolete. * @return void */ public function Process() { $this->PrepareLogs(); self::$m_oCurrentTask = $this->m_oDataSource; $oMutex = new iTopMutex('synchro_process_' . $this->m_oDataSource->GetKey()); try { $oMutex->Lock(); $this->DoSynchronize(); $oMutex->Unlock(); $this->m_oStatLog->Set('end_date', time()); $this->m_oStatLog->Set('status', 'completed'); $this->m_oStatLog->DBUpdateTracked($this->m_oChange); $iErrors = $this->m_oStatLog->GetErrorCount(); if ($iErrors > 0) { $sIssuesOQL = "SELECT SynchroReplica WHERE sync_source_id=" . $this->m_oDataSource->GetKey() . " AND status_last_error!=''"; $sAbsoluteUrl = utils::GetAbsoluteUrlAppRoot(); $sIssuesURL = "{$sAbsoluteUrl}synchro/replica.php?operation=oql&datasource=" . $this->m_oDataSource->GetKey() . "&oql=" . urlencode($sIssuesOQL); $sSeeIssues = "<p></p>"; $sStatistics = "<h1>Statistics</h1>\n"; $sStatistics .= "<ul>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('start_date') . ": " . $this->m_oStatLog->Get('start_date') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('end_date') . ": " . $this->m_oStatLog->Get('end_date') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_replica_seen') . ": " . $this->m_oStatLog->Get('stats_nb_replica_seen') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_replica_total') . ": " . $this->m_oStatLog->Get('stats_nb_replica_total') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_deleted') . ": " . $this->m_oStatLog->Get('stats_nb_obj_deleted') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_deleted_errors') . ": " . $this->m_oStatLog->Get('stats_nb_obj_deleted_errors') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_obsoleted') . ": " . $this->m_oStatLog->Get('stats_nb_obj_obsoleted') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_obsoleted_errors') . ": " . $this->m_oStatLog->Get('stats_nb_obj_obsoleted_errors') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_created') . ": " . $this->m_oStatLog->Get('stats_nb_obj_created') . " (" . $this->m_oStatLog->Get('stats_nb_obj_created_warnings') . " warnings)" . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_created_errors') . ": " . $this->m_oStatLog->Get('stats_nb_obj_created_errors') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_updated') . ": " . $this->m_oStatLog->Get('stats_nb_obj_updated') . " (" . $this->m_oStatLog->Get('stats_nb_obj_updated_warnings') . " warnings)" . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_updated_errors') . ": " . $this->m_oStatLog->Get('stats_nb_obj_updated_errors') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_replica_reconciled_errors') . ": " . $this->m_oStatLog->Get('stats_nb_replica_reconciled_errors') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_replica_disappeared_no_action') . ": " . $this->m_oStatLog->Get('stats_nb_replica_disappeared_no_action') . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_new_updated') . ": " . $this->m_oStatLog->Get('stats_nb_obj_new_updated') . " (" . $this->m_oStatLog->Get('stats_nb_obj_new_updated_warnings') . " warnings)" . "</li>\n"; $sStatistics .= "<li>" . $this->m_oStatLog->GetLabel('stats_nb_obj_new_unchanged') . ": " . $this->m_oStatLog->Get('stats_nb_obj_new_unchanged') . " (" . $this->m_oStatLog->Get('stats_nb_obj_new_unchanged_warnings') . " warnings)" . "</li>\n"; $sStatistics .= "</ul>\n"; $this->m_oDataSource->SendNotification("errors ({$iErrors})", "<p>The synchronization has been executed, {$iErrors} errors have been encountered. Click <a href=\"{$sIssuesURL}\">here</a> to see the records being currently in error.</p>" . $sStatistics); } else { //$this->m_oDataSource->SendNotification('success', '<p>The synchronization has been successfully executed.</p>'); } } catch (SynchroExceptionNotStarted $e) { $oMutex->Unlock(); // Set information for reporting... but delete the object in DB $this->m_oStatLog->Set('end_date', time()); $this->m_oStatLog->Set('status', 'error'); $this->m_oStatLog->Set('last_error', $e->getMessage()); $this->m_oStatLog->DBDeleteTracked($this->m_oChange); $this->m_oDataSource->SendNotification('fatal error', '<p>The synchronization could not start: \'' . $e->getMessage() . '\'</p><p>Please check its configuration</p>'); } catch (Exception $e) { $oMutex->Unlock(); $this->m_oStatLog->Set('end_date', time()); $this->m_oStatLog->Set('status', 'error'); $this->m_oStatLog->Set('last_error', $e->getMessage()); $this->m_oStatLog->DBUpdateTracked($this->m_oChange); $this->m_oDataSource->SendNotification('exception', '<p>The synchronization has been interrupted: \'' . $e->getMessage() . '\'</p><p>Please contact the application support team</p>'); } self::$m_oCurrentTask = null; return $this->m_oStatLog; }
public function Process($iUnixTimeLimit) { $oMutex = new iTopMutex('backup.' . utils::GetCurrentEnvironment()); $oMutex->Lock(); try { // Make sure the target directory exists SetupUtils::builddir($this->sBackupDir); $oBackup = new DBBackupScheduled(); // Eliminate files exceeding the retention setting // if ($this->iRetentionCount > 0) { $aFiles = $oBackup->ListFiles($this->sBackupDir); while (count($aFiles) >= $this->iRetentionCount) { $sFileToDelete = array_shift($aFiles); unlink($sFileToDelete); if (file_exists($sFileToDelete)) { // Ok, do not loop indefinitely on this break; } } } // Do execute the backup // $oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', '')); $sBackupFile = MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'file_name_format', '__DB__-%Y-%m-%d_%H_%M'); $sName = $oBackup->MakeName($sBackupFile); if ($sName == '') { $sName = $oBackup->MakeName(BACKUP_DEFAULT_FORMAT); } $sZipFile = $this->sBackupDir . $sName . '.zip'; $sSourceConfigFile = APPCONF . utils::GetCurrentEnvironment() . '/' . ITOP_CONFIG_FILE; $oBackup->CreateZip($sZipFile, $sSourceConfigFile); } catch (Exception $e) { $oMutex->Unlock(); throw $e; } $oMutex->Unlock(); return "Created the backup: {$sZipFile}"; }
/** * Checks if an exclusive lock exists on the specified DBObject. * @param string $sObjClass The class of the object for which to acquire the lock * @param integer $iObjKey The identifier of the object for which to acquire the lock * @return multitype:boolean iTopOwnershipLock Ambigous <boolean, string, DBObjectSet> */ public static function IsLocked($sObjClass, $iObjKey) { $bLocked = false; $oMutex = new iTopMutex('lock_' . $sObjClass . '::' . $iObjKey); $oMutex->Lock(); $oOwnershipLock = new iTopOwnershipLock($sObjClass, $iObjKey); if ($oOwnershipLock->IsOwned()) { $bLocked = true; } $oMutex->Unlock(); return array('locked' => $bLocked, 'owner' => $oOwnershipLock->GetOwner()); }