def _mark_deleted(self): """Mark a record as deleted, returns nothing. Looks up the model instance by pk, sets the not_deleted attribute to None and saves the model instance. Additionally marks any AlertStates referring to this item as inactive. This is provided as a class method which takes an ID rather than as an instance method, in order to use ._base_manager rather than .objects -- this allows us to find the object even if it was already deleted, making this operation idempotent rather than throwing a DoesNotExist on the second try. """ # Not implemented as an instance method because # we will need to use _base_manager to ensure # we can get at the object from django.db.models import signals signals.pre_delete.send(sender=self.__class__, instance=self) with transaction.atomic(): if self.not_deleted: self.not_deleted = None self.save() from chroma_core.lib.job import job_log from chroma_core.models.alert import AlertState updated = AlertState.filter_by_item_id(self.__class__, self.id).update(active=None) job_log.info("Lowered %d alerts while deleting %s %s" % (updated, self.__class__, self.id)) signals.post_delete.send(sender=self.__class__, instance=self)
def high(cls, alert_item, **kwargs): if hasattr(alert_item, 'not_deleted') and alert_item.not_deleted != True: return None attrs_to_save = cls._get_attrs_to_save(kwargs) try: alert_state = cls.filter_by_item(alert_item).get(**kwargs) except cls.DoesNotExist: kwargs.update(attrs_to_save) if not 'alert_type' in kwargs: kwargs['alert_type'] = cls.__name__ if not 'severity' in kwargs: kwargs['severity'] = cls.default_severity alert_state = cls(active = True, dismissed = False, # Users dismiss, not the software alert_item = alert_item, **kwargs) try: alert_state._message = alert_state.alert_message() alert_state.save() job_log.info("AlertState: Raised %s on %s " "at severity %s" % (cls, alert_state.alert_item, alert_state.severity)) except IntegrityError, e: job_log.warning("AlertState: IntegrityError %s saving %s : %s : %s" % (e, cls.__name__, alert_item, kwargs)) # Handle colliding inserts: drop out here, no need to update # the .end of the existing record as we are logically concurrent # with the creator. return None
def get_steps(self): from chroma_core.models import ConfParam from chroma_core.lib.job import job_log mgs = self.mgs.downcast() new_params = ConfParam.objects.filter( version__gt=mgs.conf_param_version_applied, mgs=mgs).order_by("version") steps = [] new_param_count = new_params.count() if new_param_count > 0: job_log.info("ApplyConfParams %d, applying %d new conf_params" % (self.id, new_param_count)) # If we have some new params, create N ConfParamSteps and one ConfParamVersionStep highest_version = 0 for param in new_params: # We must check that the parameter's object is still alive, to avoid trying # to set params on things that don't exist (HYD-2365) if param.object_exists(): steps.append((ConfParamStep, {"conf_param_id": param.id})) highest_version = max(highest_version, param.version) steps.append((ConfParamVersionStep, { "mgs_id": self.mgs.id, "version": highest_version })) else: # If we have no new params, no-op job_log.warning( "ApplyConfParams %d, mgs %d has no params newer than %d" % (self.id, mgs.id, mgs.conf_param_version_applied)) return steps
def on_success(self): obj = self.get_stateful_object() new_state = self.state_transition.new_state obj.set_state(new_state, intentional=True) obj.save() job_log.info("Job %d: StateChangeJob complete, setting state %s on %s" % (self.pk, new_state, obj)) if hasattr(obj, "not_deleted"): job_log.debug("Job %d: %s" % (self.id, id(obj))) job_log.info("Job %d: not_deleted=%s" % (self.id, obj.not_deleted))
def set_state(self, state, intentional=False): job_log.info( "StatefulObject.set_state %s %s->%s (intentional=%s) %s" % (self, self.state, state, intentional, id(self)) ) self.state = state self.state_modified_at = now()