本文整理汇总了PHP中Scalr::ReconfigureObservers方法的典型用法代码示例。如果您正苦于以下问题:PHP Scalr::ReconfigureObservers方法的具体用法?PHP Scalr::ReconfigureObservers怎么用?PHP Scalr::ReconfigureObservers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Scalr
的用法示例。
在下文中一共展示了Scalr::ReconfigureObservers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: startChild
/**
* {@inheritdoc}
* @see Scalr_System_Cronjob_MultiProcess_DefaultWorker::startChild()
*/
function startChild()
{
// Reopen DB connection in child
$this->db = $this->getContainer()->adodb;
// Reconfigure observers;
Scalr::ReconfigureObservers();
}
示例2: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
//Warming up static DI cache
\Scalr::getContainer()->warmup();
// Reconfigure observers
\Scalr::ReconfigureObservers();
try {
$dbFarm = DBFarm::LoadByID($request->farmId);
$curDate = new DateTime();
$tdValue = $dbFarm->GetSetting(DBFarm::SETTING_LEASE_TERMINATE_DATE);
if ($tdValue) {
$td = new DateTime($tdValue);
if ($td < $curDate) {
//Terminates farm
$event = new FarmTerminatedEvent(0, 1, false, 1);
SettingEntity::increase(SettingEntity::LEASE_TERMINATE_FARM);
\Scalr::FireEvent($request->farmId, $event);
$this->log('INFO', sprintf('Farm: %s [ID: %d] was terminated by lease manager', $dbFarm->Name, $dbFarm->ID));
} else {
// only inform user
$days = $td->diff($curDate)->days;
$notifications = json_decode($dbFarm->GetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND), true);
$governance = new Scalr_Governance($dbFarm->EnvID);
$settings = $governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE, 'notifications');
if (is_array($settings)) {
foreach ($settings as $n) {
if (!$notifications[$n['key']] && $n['period'] >= $days) {
$mailer = \Scalr::getContainer()->mailer;
$tdHuman = Scalr_Util_DateTime::convertDateTime($td, $dbFarm->GetSetting(DBFarm::SETTING_TIMEZONE), 'M j, Y');
if ($n['to'] == 'owner') {
$user = new Scalr_Account_User();
$user->loadById($dbFarm->createdByUserId);
if (\Scalr::config('scalr.auth_mode') == 'ldap') {
$email = $user->getSetting(Scalr_Account_User::SETTING_LDAP_EMAIL);
if (!$email) {
$email = $user->getEmail();
}
} else {
$email = $user->getEmail();
}
$mailer->addTo($email);
} else {
foreach (explode(',', $n['emails']) as $email) {
$mailer->addTo(trim($email));
}
}
$mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_terminate.eml', array('{{terminate_date}}' => $tdHuman, '{{farm}}' => $dbFarm->Name, '{{envName}}' => $dbFarm->GetEnvironmentObject()->name, '{{envId}}' => $dbFarm->GetEnvironmentObject()->id));
$notifications[$n['key']] = 1;
$dbFarm->SetSetting(DBFarm::SETTING_LEASE_NOTIFICATION_SEND, json_encode($notifications));
$this->log('INFO', "Notification was sent by key: %s about farm: %s [ID: %d] by lease manager", $n['key'], $dbFarm->Name, $dbFarm->ID);
}
}
}
}
}
} catch (Exception $e) {
throw $e;
}
return $request;
}
示例3: startChild
function startChild()
{
// Reopen DB connection in child
$this->db = Core::GetDBInstance(null, true);
// Reconfigure observers;
Scalr::ReconfigureObservers();
}
示例4: startChild
function startChild()
{
// Reopen DB connection in child
$this->db = Core::GetDBInstance(null, true);
// Reconfigure observers;
Scalr::ReconfigureObservers();
$this->snmpWatcher = new SNMPWatcher();
foreach (array_keys($this->watchers) as $watcher_name) {
$this->snmpWatcher->SetOIDs($watcher_name);
}
}
示例5: StartThread
public function StartThread($data)
{
// Reconfigure observers;
Scalr::ReconfigureObservers();
//
// Create pid file
//
@file_put_contents(CACHEPATH . "/" . __CLASS__ . ".Daemon.pid", posix_getpid());
// Get memory usage on start
$memory_usage = $this->GetMemoryUsage();
$this->Logger->info("SzrMessaging daemon started. Memory usage: {$memory_usage}M");
// Get DB instance
$db = Core::GetDBInstance();
$FarmObservers = array();
$job = new Scalr_Cronjob_ScalarizrMessaging();
while (true) {
$rows = $db->Execute("SELECT distinct(server_id) FROM messages WHERE type = ? AND status = ? AND isszr = ?", array("in", MESSAGE_STATUS::PENDING, 1));
while ($row = $rows->FetchRow()) {
try {
$serverId = $row['server_id'];
$job->handleWork($serverId);
} catch (Exception $e) {
}
}
// Cleaning
unset($current_memory_usage);
unset($event);
// Check memory usage
$current_memory_usage = $this->GetMemoryUsage() - $memory_usage;
if ($current_memory_usage > $this->DaemonMemoryLimit) {
$this->Logger->warn("SzrMessaging daemon reached memory limit {$this->DaemonMemoryLimit}M, Used:{$current_memory_usage}M");
$this->Logger->warn("Restart daemon.");
exit;
}
// Sleep for 60 seconds
sleep(5);
// Clear stat file cache
clearstatcache();
// Check daemon file for modifications.
if ($this->DaemonMtime && $this->DaemonMtime < @filemtime(__FILE__)) {
$this->Logger->warn(__FILE__ . " - updated. Exiting for daemon reload.");
exit;
}
}
}
示例6: StartThread
public function StartThread($data)
{
// Reconfigure observers;
Scalr::ReconfigureObservers();
//
// Create pid file
//
@file_put_contents(CACHEPATH . "/" . __CLASS__ . ".Daemon.pid", posix_getpid());
// Get memory usage on start
$memory_usage = $this->GetMemoryUsage();
$this->Logger->info("DBQueueEventProcess daemon started. Memory usage: {$memory_usage}M");
// Get DB instance
$db = Core::GetDBInstance();
$FarmObservers = array();
while (true) {
// Process tasks from Deferred event queue
while ($Task = TaskQueue::Attach(QUEUE_NAME::DEFERRED_EVENTS)->Poll()) {
$Task->Run();
}
// Reset task
TaskQueue::Attach(QUEUE_NAME::DEFERRED_EVENTS)->Reset();
// Cleaning
unset($current_memory_usage);
unset($event);
// Check memory usage
$current_memory_usage = $this->GetMemoryUsage() - $memory_usage;
if ($current_memory_usage > $this->DaemonMemoryLimit) {
$this->Logger->warn("DBQueueEventProcess daemon reached memory limit {$this->DaemonMemoryLimit}M, Used:{$current_memory_usage}M");
$this->Logger->warn("Restart daemon.");
exit;
}
// Sleep for 60 seconds
sleep(15);
// Clear stat file cache
clearstatcache();
// Check daemon file for modifications.
if ($this->DaemonMtime && $this->DaemonMtime < @filemtime(__FILE__)) {
$this->Logger->warn(__FILE__ . " - updated. Exiting for daemon reload.");
exit;
}
}
}
示例7: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
$dtNow = new DateTime('now');
try {
$dbServer = DBServer::LoadByID($request->serverId);
} catch (ServerNotFoundException $e) {
$this->log('INFO', "Server:%s does not exist:%s", $request->serverId, $e->getMessage());
return false;
}
if (!in_array($dbServer->status, [SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::PENDING_SUSPEND, SERVER_STATUS::TERMINATED, SERVER_STATUS::SUSPENDED])) {
return false;
}
// Check and skip locked instances
if ($dbServer->status == SERVER_STATUS::PENDING_TERMINATE && $dbServer->GetProperty(EC2_SERVER_PROPERTIES::IS_LOCKED) == 1) {
if (($checkDateTime = $dbServer->GetProperty(EC2_SERVER_PROPERTIES::IS_LOCKED_LAST_CHECK_TIME)) <= time()) {
if (!$dbServer->GetRealStatus(true)->isTerminated()) {
$isLocked = $dbServer->GetEnvironmentObject()->aws($dbServer->GetCloudLocation())->ec2->instance->describeAttribute($dbServer->GetCloudServerID(), InstanceAttributeType::disableApiTermination());
if ($isLocked) {
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($dbServer->GetFarmObject()->ID, sprintf("Server '%s' has disableAPITermination flag and can't be terminated (Platform: %s) (ServerTerminate).", $dbServer->serverId, $dbServer->platform), $dbServer->serverId));
$startTime = strtotime($dbServer->dateShutdownScheduled);
// 1, 2, 3, 4, 5, 6, 9, 14, ... 60
$diff = round((($checkDateTime < $startTime ? $startTime : $checkDateTime) - $startTime) / 60 * 0.5) * 60;
$diff = $diff == 0 ? 60 : ($diff > 3600 ? 3600 : $diff);
$dbServer->SetProperty(EC2_SERVER_PROPERTIES::IS_LOCKED_LAST_CHECK_TIME, time() + $diff);
return false;
} else {
$dbServer->SetProperty(EC2_SERVER_PROPERTIES::IS_LOCKED, $isLocked);
}
}
} else {
return false;
}
}
//Warming up static DI cache
\Scalr::getContainer()->warmup();
// Reconfigure observers
\Scalr::ReconfigureObservers();
if ($dbServer->status == SERVER_STATUS::TERMINATED || $dbServer->dateShutdownScheduled <= $dtNow->format('Y-m-d H:i:s')) {
try {
$p = PlatformFactory::NewPlatform($dbServer->platform);
$environment = $dbServer->GetEnvironmentObject();
if (!$environment->isPlatformEnabled($dbServer->platform)) {
throw new Exception(sprintf("%s platform is not enabled in the '%s' (%d) environment.", $dbServer->platform, $environment->name, $environment->id));
}
if ($dbServer->GetCloudServerID()) {
$serverHistory = $dbServer->getServerHistory();
$isTermination = in_array($dbServer->status, [SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE]);
$isSuspension = in_array($dbServer->status, [SERVER_STATUS::SUSPENDED, SERVER_STATUS::PENDING_SUSPEND]);
/* @var $terminationData TerminationData */
$terminationData = null;
//NOTE: in any case, after call, be sure to set callback to null
$this->setupClientCallback($p, function ($request, $response) use($dbServer, &$terminationData) {
$terminationData = new TerminationData();
$terminationData->serverId = $dbServer->serverId;
if ($request instanceof \http\Client\Request) {
$terminationData->requestUrl = $request->getRequestUrl();
$terminationData->requestQuery = $request->getQuery();
$terminationData->request = $request->toString();
}
if ($response instanceof \http\Client\Response) {
$terminationData->response = $response->toString();
$terminationData->responseCode = $response->getResponseCode();
$terminationData->responseStatus = $response->getResponseStatus();
}
}, $dbServer);
try {
$status = $dbServer->GetRealStatus();
} catch (Exception $e) {
//eliminate callback
$this->setupClientCallback($p, null, $dbServer);
throw $e;
}
//eliminate callback
$this->setupClientCallback($p, null, $dbServer);
if ($dbServer->isCloudstack()) {
//Workaround for when expunge flag not working and servers stuck in Destroyed state.
$isTerminated = $status->isTerminated() && $status->getName() != 'Destroyed';
} else {
$isTerminated = $status->isTerminated();
}
if ($isTermination && !$isTerminated || $isSuspension && !$dbServer->GetRealStatus()->isSuspended()) {
try {
if ($dbServer->farmId != 0) {
try {
if ($dbServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
$serversCount = count($dbServer->GetFarmRoleObject()->GetServersByFilter([], ['status' => [SERVER_STATUS::TERMINATED, SERVER_STATUS::SUSPENDED]]));
if ($dbServer->index == 1 && $serversCount > 1) {
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($dbServer->GetFarmObject()->ID, sprintf("RabbitMQ role. Main DISK node should be terminated after all other nodes. Waiting... (Platform: %s) (ServerTerminate).", $dbServer->serverId, $dbServer->platform), $dbServer->serverId));
return false;
}
}
} catch (Exception $e) {
}
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($dbServer->GetFarmObject()->ID, sprintf("Terminating server '%s' (Platform: %s) (ServerTerminate).", $dbServer->serverId, $dbServer->platform), $dbServer->serverId));
}
} catch (Exception $e) {
//.........这里部分代码省略.........
示例8: StartThread
public function StartThread($bundle_task_info)
{
$db = Core::GetDBInstance();
// Reconfigure observers;
Scalr::ReconfigureObservers();
$BundleTask = BundleTask::LoadById($bundle_task_info['id']);
try {
$DBServer = DBServer::LoadByID($BundleTask->serverId);
} catch (ServerNotFoundException $e) {
if (!$BundleTask->snapshotId) {
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::FAILED;
$BundleTask->setDate('finished');
$BundleTask->failureReason = sprintf(_("Server '%s' was terminated during snapshot creation process"), $BundleTask->serverId);
$BundleTask->Save();
return;
}
} catch (Exception $e) {
//$this->Logger->error($e->getMessage());
}
switch ($BundleTask->status) {
case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
if (!PlatformFactory::NewPlatform($DBServer->platform)->IsServerExists($DBServer)) {
$DBServer->status = SERVER_STATUS::TERMINATED;
$DBServer->save();
$BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
exit;
}
// IF server is in pensing state
$status = PlatformFactory::NewPlatform($DBServer->platform)->GetServerRealStatus($DBServer);
$BundleTask->Log(sprintf(_("Server status: %s"), $status->getName()));
if ($status->isPending()) {
$BundleTask->Log(sprintf(_("Waiting for running state."), $status->getName()));
exit;
} elseif ($status->isTerminated()) {
$DBServer->status = SERVER_STATUS::TERMINATED;
$DBServer->save();
$BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
exit;
}
break;
}
switch ($BundleTask->status) {
case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
$ips = PlatformFactory::NewPlatform($DBServer->platform)->GetServerIPAddresses($DBServer);
$DBServer->remoteIp = $ips['remoteIp'];
$DBServer->localIp = $ips['locateIp'];
$DBServer->save();
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV;
$BundleTask->save();
$BundleTask->Log(sprintf(_("Bundle task status: %s"), $BundleTask->status));
break;
case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
$BundleTask->Log(sprintf(_("Initializing SSH2 session to the server")));
try {
$ssh2Client = $DBServer->GetSsh2Client();
$ssh2Client->connect($DBServer->remoteIp, 22);
} catch (Exception $e) {
$BundleTask->Log(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $DBServer->remoteIp, 22, $e->getMessage()));
//TODO: Set status of bundle log to failed
exit;
}
//Prepare script
$BundleTask->Log(sprintf(_("Uploading builder scripts...")));
$behaviors = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR);
try {
$options = array('server-id' => $DBServer->serverId, 'role-name' => $BundleTask->roleName, 'crypto-key' => $DBServer->GetProperty(SERVER_PROPERTIES::SZR_KEY), 'platform' => $DBServer->platform, 'behaviour' => trim(str_replace("base", "", $behaviors)), 'queryenv-url' => CONFIG::$HTTP_PROTO . "://" . CONFIG::$EVENTHANDLER_URL . "/query-env", 'messaging-p2p.producer-url' => CONFIG::$HTTP_PROTO . "://" . CONFIG::$EVENTHANDLER_URL . "/messaging");
$command = 'scalarizr --import -y';
foreach ($options as $k => $v) {
$command .= sprintf(' -o %s=%s', $k, $v);
}
if ($DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_MYSQL_SERVER_TYPE) == 'percona') {
$recipes = 'mysql=percona';
} else {
$recipes = '';
}
$scalarizrBranch = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_DEV_SCALARIZR_BRANCH);
$scriptContents = @file_get_contents(APPPATH . "/templates/services/role_builder/chef_import.tpl");
$scriptContents = str_replace(array("%PLATFORM%", "%BEHAVIOURS%", "%SZR_IMPORT_STRING%", "%DEV%", "%SCALARIZR_BRANCH%", "%RECIPES%", "%BUILD_ONLY%", "%CHEF_SERVER_URL%", "%CHEF_VALIDATOR_NAME%", "%CHEF_VALIDATOR_KEY%", "%CHEF_ENVIRONMENT%", "%CHEF_ROLE%", "%CHEF_NODE_NAME%", "\r\n"), array($DBServer->platform, trim(str_replace("base", "", str_replace(",", " ", $behaviors))), $command, $scalarizrBranch ? '1' : '0', $scalarizrBranch, $recipes, '0', '', '', '', '', '', '', "\n"), $scriptContents);
if (!$ssh2Client->sendFile('/tmp/scalr-builder.sh', $scriptContents, "w+", false)) {
throw new Exception("Cannot upload script");
}
$BundleTask->Log(sprintf(_("Uploading chef recipes...")));
if (!$ssh2Client->sendFile('/tmp/recipes.tar.gz', APPPATH . '/www/storage/chef/recipes.tar.gz')) {
throw new Exception("Cannot upload chef recipes");
}
} catch (Exception $e) {
$BundleTask->Log(sprintf(_("Scripts upload failed: %s"), $e->getMessage()));
//TODO: Set status of bundle log to failed
exit;
}
$BundleTask->Log("Launching role builder routines on server");
$ssh2Client->exec("chmod 0777 /tmp/scalr-builder.sh");
$ssh2Client->exec("setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &");
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE;
$BundleTask->save();
break;
case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
try {
//.........这里部分代码省略.........
示例9: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
//Warming up static DI cache
\Scalr::getContainer()->warmup();
// Reconfigure observers
\Scalr::ReconfigureObservers();
try {
$dbFarm = DBFarm::LoadByID($request->farmId);
$curDate = new DateTime();
$tdValue = $dbFarm->GetSetting(Entity\FarmSetting::LEASE_TERMINATE_DATE);
if ($tdValue) {
$td = new DateTime($tdValue);
if ($td < $curDate) {
//Terminates farm
SettingEntity::increase(SettingEntity::LEASE_TERMINATE_FARM);
//Ajdusts both account & environment for the audit log
\Scalr::getContainer()->auditlogger->setAccountId($dbFarm->ClientID)->setAccountId($dbFarm->EnvID);
\Scalr::FireEvent($request->farmId, new FarmTerminatedEvent(0, 1, false, 1, true, null));
$this->log('INFO', sprintf('Farm: %s [ID: %d] was terminated by lease manager', $dbFarm->Name, $dbFarm->ID));
} else {
// only inform user
$days = $td->diff($curDate)->days;
$notifications = json_decode($dbFarm->GetSetting(Entity\FarmSetting::LEASE_NOTIFICATION_SEND), true);
$governance = new Scalr_Governance($dbFarm->EnvID);
$settings = $governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE, 'notifications');
if (is_array($settings)) {
foreach ($settings as $n) {
if (!$notifications[$n['key']] && $n['period'] >= $days) {
$mailer = \Scalr::getContainer()->mailer;
$tdHuman = Scalr_Util_DateTime::convertDateTime($td, $dbFarm->GetSetting(Entity\FarmSetting::TIMEZONE), 'M j, Y');
/* @var $user Entity\Account\User */
if ($n['to'] == 'owner') {
if ($dbFarm->ownerId) {
$user = Entity\Account\User::findPk($dbFarm->ownerId);
} else {
$user = Entity\Account\User::findOne([['accountId' => $dbFarm->ClientID], ['type' => Entity\Account\User::TYPE_ACCOUNT_OWNER]]);
}
if (\Scalr::config('scalr.auth_mode') == 'ldap') {
$email = $user->getSetting(Entity\Account\User\UserSetting::NAME_LDAP_EMAIL);
if (!$email) {
$email = $user->email;
}
} else {
$email = $user->email;
}
$mailer->addTo($email);
} else {
foreach (explode(',', $n['emails']) as $email) {
$mailer->addTo(trim($email));
}
}
$mailer->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/farm_lease_terminate.eml.php', array('terminateDate' => $tdHuman, 'farm' => $dbFarm->Name, 'envName' => $dbFarm->GetEnvironmentObject()->name, 'envId' => $dbFarm->GetEnvironmentObject()->id, 'showOwnerWarning' => !$dbFarm->ownerId));
$notifications[$n['key']] = 1;
$dbFarm->SetSetting(Entity\FarmSetting::LEASE_NOTIFICATION_SEND, json_encode($notifications));
$this->log('INFO', "Notification was sent by key: %s about farm: %s [ID: %d] by lease manager", $n['key'], $dbFarm->Name, $dbFarm->ID);
}
}
}
}
}
} catch (Exception $e) {
throw $e;
}
return $request;
}
示例10: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
$serverId = $request->serverId;
$logger = Logger::getLogger(__CLASS__);
$this->log("INFO", "Processing messages for %s server", $serverId);
try {
$dbserver = DBServer::LoadByID($serverId);
if ($dbserver->farmId) {
if ($dbserver->GetFarmObject()->Status == FARM_STATUS::TERMINATED) {
throw new ServerNotFoundException("Farm related to this server has been terminated.");
}
}
} catch (ServerNotFoundException $e) {
//By some reason server does not exist
$this->db->Execute("\n DELETE m FROM messages m\n WHERE m.server_id = ? AND m.`type` = ? AND m.`status` = ?\n ", [$serverId, "in", MESSAGE_STATUS::PENDING]);
return false;
}
//Warming up static DI cache
\Scalr::getContainer()->warmup();
// Reconfigure observers
\Scalr::ReconfigureObservers();
$rs = $this->db->Execute("\n SELECT m.* FROM messages m\n WHERE m.server_id = ? AND m.type = ? AND m.status = ?\n ORDER BY m.dtadded ASC\n ", [$serverId, "in", MESSAGE_STATUS::PENDING]);
while ($row = $rs->FetchRow()) {
try {
if ($row["message_format"] == 'xml') {
$message = $this->serializer->unserialize($row["message"]);
} else {
$message = $this->jsonSerializer->unserialize($row["message"]);
$dbserver->SetProperty(SERVER_PROPERTIES::SZR_MESSAGE_FORMAT, 'json');
}
$message->messageIpAddress = $row['ipaddress'];
$event = null;
$startTime = microtime(true);
// Update scalarizr package version
if ($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]) {
$dbserver->setScalarizrVersion($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]);
}
if ($message->meta[Scalr_Messaging_MsgMeta::SZR_UPD_CLIENT_VERSION]) {
$dbserver->SetProperty(SERVER_PROPERTIES::SZR_UPD_CLIENT_VERSION, $message->meta[Scalr_Messaging_MsgMeta::SZR_UPD_CLIENT_VERSION]);
}
if ($dbserver->GetProperty(SERVER_PROPERTIES::SYSTEM_IGNORE_INBOUND_MESSAGES)) {
continue;
}
if ($message instanceof \Scalr_Messaging_Msg) {
$this->log('INFO', "Handling '%s' for '%s' server", $message->getName(), $serverId);
}
try {
if ($message instanceof Scalr_Messaging_Msg_OperationResult) {
if ($message->status == 'ok' || $message->status == 'completed') {
if ($message->name == 'Grow MySQL/Percona data volume' || $message->name == 'mysql.grow-volume') {
$volumeConfig = $message->data ? $message->data : $message->result;
$oldVolumeId = $dbserver->GetFarmRoleObject()->GetSetting(Scalr_Db_Msr::VOLUME_ID);
$engine = $dbserver->GetFarmRoleObject()->GetSetting(Scalr_Db_Msr::DATA_STORAGE_ENGINE);
try {
$storageVolume = Scalr_Storage_Volume::init();
try {
$storageVolume->loadById($volumeConfig->id);
$storageVolume->setConfig($volumeConfig);
$storageVolume->save();
} catch (Exception $e) {
if (strpos($e->getMessage(), 'not found')) {
$storageVolume->loadBy(array('id' => $volumeConfig->id, 'client_id' => $dbserver->clientId, 'env_id' => $dbserver->envId, 'name' => "'{$volumeConfig->tags->service}' data volume", 'type' => $engine, 'platform' => $dbserver->platform, 'size' => $volumeConfig->size, 'fstype' => $volumeConfig->fstype, 'purpose' => $volumeConfig->tags->service, 'farm_roleid' => $dbserver->farmRoleId, 'server_index' => $dbserver->index));
$storageVolume->setConfig($volumeConfig);
$storageVolume->save(true);
} else {
throw $e;
}
}
$dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::VOLUME_ID, $volumeConfig->id, DBFarmRole::TYPE_LCL);
if ($engine == MYSQL_STORAGE_ENGINE::EBS) {
$dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_EBS_SIZE, $volumeConfig->size, DBFarmRole::TYPE_CFG);
} elseif ($engine == MYSQL_STORAGE_ENGINE::RAID_EBS) {
$dbserver->GetFarmRoleObject()->SetSetting(Scalr_Db_Msr::DATA_STORAGE_RAID_DISK_SIZE, $volumeConfig->size, DBFarmRole::TYPE_CFG);
}
// Remove old
$storageVolume->delete($oldVolumeId);
} catch (Exception $e) {
Logger::getLogger(__CLASS__)->error(new FarmLogMessage($dbserver->farmId, "Cannot save storage volume: {$e->getMessage()}"));
}
}
} elseif ($message->status == 'error' || $message->status == 'failed') {
if ($message->name == 'Initialization' || $message->name == 'system.init') {
$dbserver->SetProperty(SERVER_PROPERTIES::SZR_IS_INIT_FAILED, 1);
if (is_object($message->error)) {
$errorText = $message->error->message;
} elseif ($message->error) {
$errorText = $message->error;
}
$dbserver->SetProperty(SERVER_PROPERTIES::SZR_IS_INIT_ERROR_MSG, $errorText);
$event = new HostInitFailedEvent($dbserver, $errorText);
}
}
} elseif ($message instanceof Scalr_Messaging_Msg_InitFailed) {
$errorText = $message->reason;
$dbserver->SetProperty(SERVER_PROPERTIES::SZR_IS_INIT_ERROR_MSG, $errorText);
$event = new HostInitFailedEvent($dbserver, $errorText);
//.........这里部分代码省略.........
示例11: StartThread
public function StartThread($bundle_task_info)
{
$db = \Scalr::getDb();
// Reconfigure observers;
Scalr::ReconfigureObservers();
$BundleTask = BundleTask::LoadById($bundle_task_info['id']);
try {
$DBServer = DBServer::LoadByID($BundleTask->serverId);
} catch (\Scalr\Exception\ServerNotFoundException $e) {
if (!$BundleTask->snapshotId) {
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::FAILED;
$BundleTask->setDate('finished');
$BundleTask->failureReason = sprintf(_("Server '%s' was terminated during snapshot creation process"), $BundleTask->serverId);
$BundleTask->Save();
return;
}
} catch (Exception $e) {
//$this->Logger->error($e->getMessage());
}
switch ($BundleTask->status) {
case SERVER_SNAPSHOT_CREATION_STATUS::ESTABLISHING_COMMUNICATION:
$ctrlPort = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_CTRL_PORT);
if (!$ctrlPort) {
$ctrlPort = 8013;
}
if (\Scalr::config('scalr.instances_connection_policy') == 'local') {
$requestHost = $DBServer->localIp;
} elseif (\Scalr::config('scalr.instances_connection_policy') == 'public') {
$requestHost = $DBServer->remoteIp;
} elseif (\Scalr::config('scalr.instances_connection_policy') == 'auto') {
if ($DBServer->remoteIp) {
$requestHost = $DBServer->remoteIp;
} else {
$requestHost = $DBServer->localIp;
}
}
$conn = @fsockopen($requestHost, $ctrlPort, $errno, $errstr, 10);
if ($conn) {
$DBServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION, 1);
$BundleTask->Log("Outbound connection successfully established. Awaiting user action: prebuild automation selection");
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::AWAITING_USER_ACTION;
$BundleTask->Log(sprintf(_("Bundle task status: %s"), $BundleTask->status));
$BundleTask->Save();
} else {
$errstr = sprintf("Unable to establish outbound (Scalr -> Scalarizr) communication (%s:%s): %s.", $requestHost, $ctrlPort, $errstr);
$errMsg = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION_ERROR);
if (!$errMsg || $errstr != $errMsg) {
$DBServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION_ERROR, $errstr);
$BundleTask->Log("{$errstr} Will try again in a few minutes.");
}
}
exit;
break;
case SERVER_SNAPSHOT_CREATION_STATUS::AWAITING_USER_ACTION:
//NOTHING TO DO;
exit;
break;
case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
$BundleTask->setDate('started');
case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
if (!PlatformFactory::NewPlatform($DBServer->platform)->GetServerID($DBServer)) {
$BundleTask->Log(sprintf(_("Waiting for temporary server")));
exit;
}
if (!PlatformFactory::NewPlatform($DBServer->platform)->IsServerExists($DBServer)) {
$DBServer->status = SERVER_STATUS::TERMINATED;
$DBServer->save();
$BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
exit;
}
// IF server is in pensing state
$status = PlatformFactory::NewPlatform($DBServer->platform)->GetServerRealStatus($DBServer);
if ($status->isPending()) {
$BundleTask->Log(sprintf(_("Server status: %s"), $status->getName()));
$BundleTask->Log(sprintf(_("Waiting for running state."), $status->getName()));
exit;
} elseif ($status->isTerminated()) {
$BundleTask->Log(sprintf(_("Server status: %s"), $status->getName()));
$DBServer->status = SERVER_STATUS::TERMINATED;
$DBServer->save();
$BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
exit;
}
break;
}
switch ($BundleTask->status) {
case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
$ips = PlatformFactory::NewPlatform($DBServer->platform)->GetServerIPAddresses($DBServer);
$DBServer->remoteIp = $ips['remoteIp'];
$DBServer->localIp = $ips['localIp'];
$DBServer->save();
$BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV;
$BundleTask->save();
$BundleTask->Log(sprintf(_("Bundle task status: %s"), $BundleTask->status));
break;
case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
$BundleTask->Log(sprintf(_("Initializing SSH2 session to the server")));
if ($DBServer->platform == SERVER_PLATFORMS::IDCF && !$DBServer->remoteIp) {
try {
//.........这里部分代码省略.........
示例12: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
$db = \Scalr::getDb();
//Speed up poller
if ($this->config()->daemon) {
//Warming up static DI cache
\Scalr::getContainer()->warmup();
}
// Reconfigure observers
\Scalr::ReconfigureObservers();
$DBFarm = DBFarm::LoadByID($request->farmId);
$account = Scalr_Account::init()->loadById($DBFarm->ClientID);
$payAsYouGoTime = $account->getSetting(Scalr_Account::SETTING_BILLING_PAY_AS_YOU_GO_DATE);
$transactionId = abs(crc32(posix_getpid() . $request->farmId));
$this->getLogger()->info("[%s] Begin polling farm (ID: %d, Name: %s, Status: %s)", $transactionId, $DBFarm->ID, $DBFarm->Name, $DBFarm->Status);
//Retrieves the number of either terminated or suspended servers for the farm
$servers_count = $db->GetOne("\n SELECT COUNT(*) AS cnt FROM servers WHERE farm_id = ? AND status NOT IN (?,?)\n ", [$DBFarm->ID, SERVER_STATUS::TERMINATED, SERVER_STATUS::SUSPENDED]);
if ($DBFarm->Status == FARM_STATUS::TERMINATED && $servers_count == 0) {
//There are no servers for this farm
return;
}
$this->getLogger()->info("%d server%s for the farm: %d", $servers_count, $servers_count == 1 ? '' : 's', $DBFarm->ID);
$config = \Scalr::getContainer()->config;
foreach ($DBFarm->GetServersByFilter(array(), array('status' => SERVER_STATUS::PENDING_LAUNCH)) as $DBServer) {
/* @var $DBServer \DBServer */
try {
if ($DBServer->cloudLocation) {
try {
$this->getLogger()->info("Retrieving the list of the instances for %s, server: %s", $DBServer->cloudLocation, $DBServer->serverId);
$p = PlatformFactory::NewPlatform($DBServer->platform);
$p->GetServersList($DBServer->GetEnvironmentObject(), $DBServer->cloudLocation);
} catch (Exception $e) {
$this->getLogger()->error("[Server: %s] Could not retrieve the list of the instances: %s", $DBServer->serverId, $e->getMessage());
continue;
}
}
if ($DBServer->status != SERVER_STATUS::PENDING && $DBServer->status != SERVER_STATUS::PENDING_TERMINATE) {
if (!$p->IsServerExists($DBServer)) {
try {
$serverInfo = $p->GetServerExtendedInformation($DBServer);
} catch (Exception $e) {
$this->getLogger()->error("[CRASH][FarmID: %d] Crash check for server '%s' failed: %s", $DBFarm->ID, $DBServer->serverId, $e->getMessage());
}
if (!$serverInfo) {
if (!in_array($DBServer->status, [SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::TERMINATED])) {
if ($DBServer->isOpenstack() && $DBServer->status == SERVER_STATUS::SUSPENDED) {
continue;
} elseif ($DBServer->platform == \SERVER_PLATFORMS::GCE && $DBServer->status == SERVER_STATUS::SUSPENDED) {
$DBServer->terminate(DBServer::TERMINATE_REASON_CRASHED);
Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf("Server '%s' was terminated", $DBServer->serverId), $DBServer->serverId));
continue;
}
if ($DBServer->GetProperty(SERVER_PROPERTIES::CRASHED) == 1) {
$action = 'terminate';
if ($config->defined("scalr.{$DBServer->platform}.action_on_missing_server")) {
$action = $config->get("scalr.{$DBServer->platform}.action_on_missing_server");
}
if ($action == 'flag') {
$DBServer->SetProperty(SERVER_PROPERTIES::MISSING, 1);
} else {
$DBServer->terminate(DBServer::TERMINATE_REASON_CRASHED);
\Scalr::FireEvent($DBFarm->ID, new HostCrashEvent($DBServer));
}
} else {
$DBServer->SetProperties([SERVER_PROPERTIES::REBOOTING => 0, SERVER_PROPERTIES::CRASHED => 1, SERVER_PROPERTIES::MISSING => 1]);
Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf("Server '%s' found in database but not found on %s. Crashed.", $DBServer->serverId, $DBServer->platform), $DBServer->serverId));
}
continue;
}
} else {
//http.persistent.handles.limit must be set to 0 for pecl-http version 1
$this->getLogger()->error("[CRASH][FarmID: %d] False-positive crash check: %s (EnvID: %d). Please verify current scalr install with app/www/testenvironment.php", $DBFarm->ID, $DBServer->serverId, $DBServer->envId);
//More debug
$this->getLogger()->error(sprintf("[CRASH][FarmID: %d] Debug: %s", $DBFarm->ID, json_encode($p->instancesListCache)));
$this->getLogger()->error(sprintf("[CRASH][FarmID: %d] {$DBServer->GetCloudServerID()}"));
}
} else {
$DBServer->SetProperties([SERVER_PROPERTIES::CRASHED => 0, SERVER_PROPERTIES::MISSING => 0]);
}
}
} catch (Exception $e) {
if (stristr($e->getMessage(), "AWS was not able to validate the provided access credentials") || stristr($e->getMessage(), "You are not authorized to perform this operation") || stristr($e->getMessage(), "Unable to sign AWS API request. Please, check your X.509")) {
/* @var $env \Scalr_Environment */
$env = Scalr_Environment::init()->LoadById($DBFarm->EnvID);
$env->status = Scalr_Environment::STATUS_INACTIVE;
$env->save();
//Saving the reason why this environment is disabled
$env->setPlatformConfig(['system.auto-disable-reason' => $e->getMessage()]);
return;
} elseif (stristr($e->getMessage(), "Could not connect to host")) {
continue;
}
$this->getLogger()->warn("Exception for farm: %d with the message: %s, in the %s:%s", $request->farmId, $e->getMessage(), $e->getFile(), $e->getLine());
continue;
}
try {
//.........这里部分代码省略.........
示例13: StartThread
public function StartThread($mysql_farm_role)
{
// Reconfigure observers;
Scalr::ReconfigureObservers();
$db = \Scalr::getDb();
$DBFarmRole = DBFarmRole::LoadByID($mysql_farm_role['id']);
try {
$DBFarm = $DBFarmRole->GetFarmObject();
} catch (Exception $e) {
return;
}
//skip terminated farms
if ($DBFarm->Status != FARM_STATUS::RUNNING) {
return;
}
$tz = Scalr_Environment::init()->loadById($DBFarm->EnvID)->getPlatformConfigValue(Scalr_Environment::SETTING_TIMEZONE);
if ($tz) {
date_default_timezone_set($tz);
}
//
// Check replication status
//
$this->Logger->info("[FarmID: {$DBFarm->ID}] Checking replication status");
$servers = $DBFarmRole->GetServersByFilter(array('status' => SERVER_STATUS::RUNNING));
//
// Check backups and mysql bandle procedures
//
//Backups
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BCP_ENABLED) && $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BCP_EVERY) != 0) {
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_IS_BCP_RUNNING) == 1) {
// Wait for timeout time * 2 (Example: NIVs problem with big mysql snapshots)
// We must wait for running bundle process.
$bcp_timeout = $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BCP_EVERY) * 60 * 5;
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BCP_TS) + $bcp_timeout < time()) {
$bcp_timeouted = true;
}
if (!empty($bcp_timeouted)) {
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_IS_BCP_RUNNING, 0, Entity\FarmRoleSetting::TYPE_LCL);
$this->Logger->info("[FarmID: {$DBFarm->ID}] MySQL Backup already running. Timeout. Clear lock.");
}
} else {
$timeout = $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BCP_EVERY) * 60;
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BCP_TS) + $timeout < time()) {
$this->Logger->info("[FarmID: {$DBFarm->ID}] Need new backup");
$servers = $DBFarm->GetMySQLInstances(false, true);
if (empty($servers[0])) {
$servers = $DBFarm->GetMySQLInstances(true);
} else {
$servers = array_reverse($servers);
}
$DBServer = isset($servers[0]) ? $servers[0] : null;
if ($DBServer) {
if ($DBServer->status == SERVER_STATUS::RUNNING) {
$msg = new Scalr_Messaging_Msg_Mysql_CreateBackup($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_ROOT_PASSWORD));
$DBServer->SendMessage($msg);
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_IS_BCP_RUNNING, 1, Entity\FarmRoleSetting::TYPE_LCL);
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_BCP_SERVER_ID, $DBServer->serverId, Entity\FarmRoleSetting::TYPE_LCL);
}
} else {
$this->Logger->info("[FarmID: {$DBFarm->ID}] There is no running mysql instances for run backup procedure!");
}
}
}
}
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_ENABLED) && $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_EVERY) != 0) {
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_IS_BUNDLE_RUNNING) == 1) {
// Wait for timeout time * 2 (Example: NIVs problem with big mysql snapshots)
// We must wait for running bundle process.
$bundle_timeout = $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_EVERY) * (3600 * 2);
if ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BUNDLE_TS) + $bundle_timeout < time()) {
$bundle_timeouted = true;
}
if (!empty($bundle_timeouted)) {
$DBFarmRole->SetSetting(Entity\FarmRoleSetting::MYSQL_IS_BUNDLE_RUNNING, 0, Entity\FarmRoleSetting::TYPE_LCL);
$this->Logger->info("[FarmID: {$DBFarm->ID}] MySQL Bundle already running. Timeout. Clear lock.");
}
} else {
/*
* Check bundle window
*/
$bundleEvery = $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_EVERY);
$timeout = $bundleEvery * 3600;
$lastBundleTime = $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BUNDLE_TS);
$performBundle = false;
if ($bundleEvery % 24 == 0) {
if ($lastBundleTime) {
$days = $bundleEvery / 24;
$bundleDay = (int) date("md", strtotime("+{$days} day", $lastBundleTime));
if ($bundleDay > (int) date("md")) {
return;
}
}
$pbwFrom = (int) ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_WINDOW_START_HH) . $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_WINDOW_START_MM));
$pbwTo = (int) ($DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_WINDOW_END_HH) . $DBFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_WINDOW_END_MM));
if ($pbwFrom && $pbwTo) {
$current_time = (int) date("Hi");
if ($pbwFrom <= $current_time && $pbwTo >= $current_time) {
$performBundle = true;
}
} else {
//.........这里部分代码省略.........
示例14: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
$db = \Scalr::getDb();
//The list of the suspension information about cloud platforms
$this->aSuspensionInfo = [];
//Speed up poller
if ($this->config()->daemon) {
//Warming up static DI cache
\Scalr::getContainer()->warmup();
}
// Reconfigure observers
\Scalr::ReconfigureObservers();
$DBFarm = DBFarm::LoadByID($request->farmId);
$account = Scalr_Account::init()->loadById($DBFarm->ClientID);
$payAsYouGoTime = $account->getSetting(Scalr_Account::SETTING_BILLING_PAY_AS_YOU_GO_DATE);
$transactionId = abs(crc32(posix_getpid() . $request->farmId));
$this->getLogger()->info("[%s] Begin polling farm (ID: %d, Name: %s, Status: %s, Platform:%s)", $transactionId, $DBFarm->ID, $DBFarm->Name, $DBFarm->Status, $request->platform);
$jobStartTime = microtime(true);
//Retrieves the number of either terminated or suspended servers for the farm
$servers_count = $db->GetOne("\n SELECT COUNT(*) AS cnt FROM servers\n WHERE farm_id = ? AND platform = ? AND status NOT IN (?,?)\n ", [$DBFarm->ID, $request->platform, SERVER_STATUS::TERMINATED, SERVER_STATUS::SUSPENDED]);
if ($DBFarm->Status == FARM_STATUS::TERMINATED && $servers_count == 0) {
//There are no servers for this farm and platform
return;
}
$this->getLogger()->info("%d server%s for the farm: %d and platform: %s", $servers_count, $servers_count == 1 ? '' : 's', $DBFarm->ID, $request->platform);
$config = \Scalr::getContainer()->config;
/*
if ($request->platform) {
$p = PlatformFactory::NewPlatform($request->platform);
$p->ClearCache();
}
*/
$p = PlatformFactory::NewPlatform($request->platform);
foreach ($DBFarm->GetServersByFilter(['platform' => $request->platform], ['status' => SERVER_STATUS::PENDING_LAUNCH]) as $DBServer) {
/* @var $DBServer \DBServer */
//Get platform suspension info
$suspensionInfo = $this->getSuspensionInfo($DBServer->platform, $DBServer->envId);
//If the cloud platform is suspended we should not process it
if ($suspensionInfo->isSuspended()) {
continue;
}
try {
//1. We need to check that server is exists in cloud and not missed.
// (On Openstack server can be missed and should not be terminated)
$cacheKey = sprintf('%s:%s', $DBServer->envId, $DBServer->cloudLocation);
if ($DBServer->cloudLocation && count($p->instancesListCache[$cacheKey]) == 0) {
try {
$this->getLogger()->info("Retrieving the list of the instances for %s, server: %s, platform: %s", $DBServer->cloudLocation, $DBServer->serverId, $request->platform);
if ($DBServer->platform == \SERVER_PLATFORMS::AZURE) {
//For Azure we need to pass resource group instead of cloudLocation
$p->GetServersList($DBServer->GetEnvironmentObject(), $DBServer->GetProperty(\AZURE_SERVER_PROPERTIES::RESOURCE_GROUP));
} else {
$p->GetServersList($DBServer->GetEnvironmentObject(), $DBServer->cloudLocation);
}
//We successfully polled cloud so can resume suspension status for the cloud platform
if ($suspensionInfo->isPendingSuspend()) {
$suspensionInfo->resume();
}
} catch (Exception $e) {
if (CloudPlatformSuspensionInfo::isSuspensionException($e)) {
$suspensionInfo->registerError($e->getMessage());
}
$this->getLogger()->error("[Server: %s] Could not retrieve the list of the instances: %s", $DBServer->serverId, $e->getMessage());
continue;
}
}
if ($DBServer->status != SERVER_STATUS::PENDING && $DBServer->status != SERVER_STATUS::PENDING_TERMINATE) {
if (!$p->IsServerExists($DBServer)) {
try {
$serverInfo = $p->GetServerExtendedInformation($DBServer);
} catch (Exception $e) {
$this->getLogger()->error("[CRASH][FarmID: %d] Crash check for server '%s' failed: %s", $DBFarm->ID, $DBServer->serverId, $e->getMessage());
continue;
}
if (!$serverInfo) {
if (!in_array($DBServer->status, [SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::TERMINATED])) {
if ($DBServer->isOpenstack() && $DBServer->status == SERVER_STATUS::SUSPENDED) {
continue;
} elseif ($DBServer->platform == \SERVER_PLATFORMS::GCE && $DBServer->status == SERVER_STATUS::SUSPENDED) {
$DBServer->terminate(DBServer::TERMINATE_REASON_CRASHED);
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf(_("Server '%s' was terminated"), $DBServer->serverId), $DBServer->serverId));
continue;
}
$action = 'terminate';
if ($config->defined("scalr.{$DBServer->platform}.action_on_missing_server")) {
$action = $config->get("scalr.{$DBServer->platform}.action_on_missing_server");
}
if ($action == 'flag' && !$DBServer->GetProperty(SERVER_PROPERTIES::MISSING)) {
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf("Server '%s' found in Scalr but not found in the cloud (%s). Marking as Missing.", $DBServer->serverId, $DBServer->platform), $DBServer->serverId));
$DBServer->SetProperties([SERVER_PROPERTIES::REBOOTING => 0, SERVER_PROPERTIES::MISSING => 1]);
} else {
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($DBFarm->ID, sprintf("Server '%s' found in Scalr but not found in the cloud (%s). Terminating.", $DBServer->serverId, $DBServer->platform), $DBServer->serverId));
$DBServer->terminate(DBServer::TERMINATE_REASON_CRASHED);
}
continue;
}
//.........这里部分代码省略.........
示例15: worker
/**
* {@inheritdoc}
* @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
*/
public function worker($request)
{
$dtNow = new DateTime('now');
try {
$dbServer = DBServer::LoadByID($request->serverId);
} catch (ServerNotFoundException $e) {
$this->log('INFO', "Server:%s does not exist:%s", $request->serverId, $e->getMessage());
return false;
}
if (!in_array($dbServer->status, array(SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::PENDING_SUSPEND, SERVER_STATUS::TERMINATED, SERVER_STATUS::SUSPENDED))) {
return false;
}
//Skip Locked instances
if ($dbServer->status == SERVER_STATUS::PENDING_TERMINATE && $dbServer->GetProperty(EC2_SERVER_PROPERTIES::IS_LOCKED) == 1) {
return false;
}
//Warming up static DI cache
\Scalr::getContainer()->warmup();
// Reconfigure observers
\Scalr::ReconfigureObservers();
if (in_array($dbServer->status, array(SERVER_STATUS::TERMINATED)) || $dbServer->dateShutdownScheduled <= $dtNow->format('Y-m-d H:i:s')) {
try {
$p = PlatformFactory::NewPlatform($dbServer->platform);
$environment = $dbServer->GetEnvironmentObject();
if (!$environment->isPlatformEnabled($dbServer->platform)) {
throw new Exception(sprintf("%s platform is not enabled in the '%s' (%d) environment.", $dbServer->platform, $environment->name, $environment->id));
}
if ($dbServer->GetCloudServerID()) {
$serverHistory = $dbServer->getServerHistory();
$isTermination = in_array($dbServer->status, array(SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE));
$isSuspension = in_array($dbServer->status, array(SERVER_STATUS::SUSPENDED, SERVER_STATUS::PENDING_SUSPEND));
$status = $dbServer->GetRealStatus();
if ($dbServer->isCloudstack()) {
//Workaround for when expunge flag not working and servers stuck in Destroyed state.
$isTerminated = $status->isTerminated() && $status->getName() != 'Destroyed';
} else {
$isTerminated = $status->isTerminated();
}
if ($isTermination && !$isTerminated || $isSuspension && !$dbServer->GetRealStatus()->isSuspended()) {
try {
if ($dbServer->farmId != 0) {
try {
if ($dbServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::RABBITMQ)) {
$serversCount = count($dbServer->GetFarmRoleObject()->GetServersByFilter([], ['status' => [SERVER_STATUS::TERMINATED, SERVER_STATUS::SUSPENDED]]));
if ($dbServer->index == 1 && $serversCount > 1) {
Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($dbServer->GetFarmObject()->ID, sprintf("RabbitMQ role. Main DISK node should be terminated after all other nodes. " . "Waiting... (Platform: %s) (ServerTerminate).", $dbServer->serverId, $dbServer->platform), $dbServer->serverId));
return false;
}
}
} catch (Exception $e) {
}
Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($dbServer->GetFarmObject()->ID, sprintf("Terminating server '%s' (Platform: %s) (ServerTerminate).", $dbServer->serverId, $dbServer->platform), $dbServer->serverId));
}
} catch (Exception $e) {
$this->getLogger()->warn("Server: %s caused exception: %s", $request->serverId, $e->getMessage());
}
$terminationTime = $dbServer->GetProperty(SERVER_PROPERTIES::TERMINATION_REQUEST_UNIXTIME);
if (!$terminationTime || time() - $terminationTime > 180) {
if ($isTermination) {
$p->TerminateServer($dbServer);
} else {
$p->SuspendServer($dbServer);
}
$dbServer->SetProperty(SERVER_PROPERTIES::TERMINATION_REQUEST_UNIXTIME, time());
if ($dbServer->farmId) {
$wasHostDownFired = \Scalr::getDb()->GetOne("\n SELECT id FROM events WHERE event_server_id = ? AND type = ? AND is_suspend = '0'", array($request->serverId, 'HostDown'));
if (!$wasHostDownFired) {
$event = new HostDownEvent($dbServer);
$event->isSuspended = !$isTermination;
\Scalr::FireEvent($dbServer->farmId, $event);
}
}
}
} else {
if ($dbServer->status == SERVER_STATUS::TERMINATED) {
if (!$dbServer->dateShutdownScheduled || time() - strtotime($dbServer->dateShutdownScheduled) > 600) {
$errorResolution = true;
$serverHistory->setTerminated();
$dbServer->Remove();
}
} else {
if ($dbServer->status == SERVER_STATUS::PENDING_TERMINATE) {
$dbServer->status = SERVER_STATUS::TERMINATED;
$dbServer->Save();
$errorResolution = true;
} else {
if ($dbServer->status == SERVER_STATUS::PENDING_SUSPEND) {
$dbServer->status = SERVER_STATUS::SUSPENDED;
$dbServer->remoteIp = '';
$dbServer->localIp = '';
$dbServer->Save();
$errorResolution = true;
}
}
}
if (!empty($errorResolution) && $request->attempts > 0 && ($ste = ServerTerminationError::findPk($request->serverId))) {
//.........这里部分代码省略.........