public function OnFarmLaunched(FarmLaunchedEvent $event)
{
$DBFarm = DBFarm::LoadByID($this->FarmID);
$this->DB->Execute("UPDATE farms SET status = ?, dtlaunched = NOW() WHERE id = ? LIMIT 1", [FARM_STATUS::RUNNING, $this->FarmID]);
$this->getContainer()->auditlogger->log('farm.launch', $DBFarm, $event->auditLogExtra);
$governance = new Scalr_Governance($DBFarm->EnvID);
if ($governance->isEnabled(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE) && $DBFarm->GetSetting(Entity\FarmSetting::LEASE_STATUS)) {
$dt = new DateTime();
$dt->add(new DateInterval('P' . intval($governance->getValue(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE, 'defaultLifePeriod')) . 'D'));
$DBFarm->SetSetting(Entity\FarmSetting::LEASE_EXTEND_CNT, 0);
$DBFarm->SetSetting(Entity\FarmSetting::LEASE_TERMINATE_DATE, $dt->format('Y-m-d H:i:s'));
$DBFarm->SetSetting(Entity\FarmSetting::LEASE_NOTIFICATION_SEND, '');
}
$roles = $DBFarm->GetFarmRoles();
foreach ($roles as $dbFarmRole) {
if ($dbFarmRole->GetSetting(Entity\FarmRoleSetting::SCALING_ENABLED) && !$DBFarm->GetSetting(Entity\FarmSetting::EC2_VPC_ID)) {
$scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
$scalingDecision = $scalingManager->makeScalingDecision();
if ($scalingDecision == Scalr_Scaling_Decision::UPSCALE) {
$ServerCreateInfo = new ServerCreateInfo($dbFarmRole->Platform, $dbFarmRole);
try {
$DBServer = \Scalr::LaunchServer($ServerCreateInfo, null, true, DBServer::LAUNCH_REASON_FARM_LAUNCHED, isset($event->userId) ? $event->userId : null);
$dbFarmRole->SetSetting(Entity\FarmRoleSetting::SCALING_UPSCALE_DATETIME, time(), Entity\FarmRoleSetting::TYPE_LCL);
$role = $dbFarmRole->GetRoleObject();
\Scalr::getContainer()->logger(LOG_CATEGORY::FARM)->info(new FarmLogMessage($DBFarm->ID, sprintf("Farm %s, role %s scaling up. Starting new instance. ServerID = %s.", !empty($DBFarm->Name) ? $DBFarm->Name : null, !empty($role->name) ? $role->name : null, !empty($DBServer->serverId) ? $DBServer->serverId : null), !empty($DBServer->serverId) ? $DBServer->serverId : null, !empty($DBServer->envId) ? $DBServer->envId : null, !empty($DBServer->farmRoleId) ? $DBServer->farmRoleId : null));
} catch (Exception $e) {
\Scalr::getContainer()->logger(LOG_CATEGORY::SCALING)->error($e->getMessage());
}
}
}
}
}