in source/lambda/schedulers/instance_scheduler.py [0:0]
def _process_new_desired_state(self, account, region, instance, desired_state, desired_type, last_desired_state,
retain_running):
def need_and_can_resize():
if desired_type is not None and instance.instancetype != desired_type:
if not instance.allow_resize:
self._logger.warning(WARN_RESIZE_NOT_SUPPORTED, instance.instance_str, instance.instancetype)
return False
else:
return True
return False
def resize_instance(inst, new_type):
try:
# adjust instance type before starting using the resize_instance method in the service_strategy
self._service.resize_instance({
schedulers.PARAM_SESSION: account.session,
schedulers.PARAM_ACCOUNT: account.name,
schedulers.PARAM_ROLE: account.role,
schedulers.PARAM_REGION: region,
schedulers.PARAM_TRACE: self._configuration.trace,
schedulers.PARAM_INSTANCE: instance,
schedulers.PARAM_DESIRED_TYPE: new_type,
schedulers.PARAM_LOGGER: self._logger,
schedulers.PARAM_CONTEXT: self._context,
schedulers.PARAM_CONFIG: self._scheduler_configuration
})
self._schedule_resize_list.append((inst, new_type))
except Exception as ex:
# if changing the instance type does fail do not add instance to start list so it is handled a next time
self._logger.error(ERR_SETTING_INSTANCE_TYPE, str(ex))
# last desired status was saved as retain-running
if last_desired_state == InstanceSchedule.STATE_RETAIN_RUNNING:
# don't change last desired state desired whilst in a running period
if desired_state == InstanceSchedule.STATE_RUNNING:
pass
# save last desired state as stopped (but do not stop) at the end of running period
elif desired_state == InstanceSchedule.STATE_STOPPED:
# safe new desired stopped state but keep running
self._logger.debug(INF_DO_NOT_STOP_RETAINED_INSTANCE, instance.id, InstanceSchedule.STATE_STOPPED)
self._instance_states.set_instance_state(instance.id, InstanceSchedule.STATE_STOPPED)
else:
# just save new desired state
self._instance_states.set_instance_state(instance.id, desired_state)
else:
if desired_state == InstanceSchedule.STATE_RUNNING:
if not instance.is_running:
inst_type = desired_type if desired_type is not None else instance.instancetype
self._logger.debug(DEBUG_STARTED_REGION_INSTANCES, instance.instance_str, instance.region,
inst_type)
# for instances to be started test if resizing is required
if need_and_can_resize():
resize_instance(instance, desired_type)
# append instance to list of instances to start
self._scheduler_start_list.append(instance)
# instance already running with desired state of running
else:
# if retain running option is used in this save desired state as retained running.
if last_desired_state == InstanceSchedule.STATE_STOPPED:
if retain_running:
self._logger.debug(DEBUG_APPLY_RETAIN_RUNNING_STATE, desired_state, instance.id,
InstanceSchedule.STATE_RETAIN_RUNNING)
self._instance_states.set_instance_state(instance.id, InstanceSchedule.STATE_RETAIN_RUNNING)
else:
# instance is running, set last desired state from stopped to started
self._instance_states.set_instance_state(instance.id, InstanceSchedule.STATE_RUNNING)
# desired state is running but saved state already saves as retain running
elif desired_state in [InstanceSchedule.STATE_STOPPED, InstanceSchedule.STATE_STOPPED_FOR_RESIZE]:
if instance.is_running:
# instance needs to be stopped
self._logger.debug(DEBUG_STOPPED_REGION_INSTANCES, instance.instance_str, instance.region)
# append instance to list of instances to start
if desired_state == InstanceSchedule.STATE_STOPPED_FOR_RESIZE:
instance = instance._replace(resized=True)
self._scheduler_stop_list.append(instance)
# stopped instance with desired state of running but in retained state mode
# (manually stopped in running period and already running at start)
else:
# just save new desired state
self._instance_states.set_instance_state(instance.id, InstanceSchedule.STATE_STOPPED)
else:
self._instance_states.set_instance_state(instance.id, desired_state)