def initiate(act_id): query_kwargs = { 'pk': act_id, 'state': states.CREATED, } try: act = ActivityModel.objects.get(**query_kwargs) except ActivityModel.DoesNotExist: logger.info( messages.build_message(messages.ACT_MODEL_NOT_EXIST, query_kwargs)) else: logger.info("initiate activity #%s" % act.pk) with global_exception_handler(act): module_name, _, cls_name = act.name.rpartition('.') with import_exception_handler(): module = __import__(module_name, globals(), locals(), ['*']) cls = getattr(module, cls_name) with instantiation_exception_handler(): backend = cls(act.pk, act.name) with runtime_exception_handler(backend): backend._initiate(*act.args, **act.kwargs) # if parent activity has an appointment state, inherit it. if isinstance(act.parent, ActivityModel) \ and act.parent.appointment in states.APPOINTABLE_STATES: act._appoint(act.parent.appointment) act._transit(states.READY, snapshot=pickle.dumps(backend)) with runtime_exception_handler(backend): backend._destroy()
def schedule(act_id): query_kwargs = { 'pk': act_id, 'state': states.READY, } try: act = ActivityModel.objects.get(**query_kwargs) except ActivityModel.DoesNotExist: logger.info( messages.build_message(messages.ACT_MODEL_NOT_EXIST, query_kwargs)) else: logger.info("schedule activity #%s" % act_id) with global_exception_handler(act): if act._transit(states.RUNNING): backend = pickle.loads(act.snapshot.data) with runtime_exception_handler(backend): backend._resume() stackless.schedule() while backend._schedule(): stackless.schedule() act._transit(states.BLOCKED, snapshot=pickle.dumps(backend)) with runtime_exception_handler(backend): backend._destroy()
def _get_model(self): """ Get model object of this activity. """ query_kwargs = { 'pk': self._act_id, } try: return ActivityModel.objects.get(**query_kwargs) except ActivityModel.DoesNotExist: raise RuntimeError( messages.build_message(messages.ACT_MODEL_NOT_EXIST, query_kwargs))
def acknowledge(act_id): query_kwargs = { 'pk': act_id, 'acknowledgment': 0, } try: act = ActivityModel.objects.get(**query_kwargs) except ActivityModel.DoesNotExist: logger.info( messages.build_message(messages.ACT_MODEL_NOT_EXIST, query_kwargs)) else: logger.info("acknowledge activity #%s" % act_id) signals.activity_finished.send(sender=acknowledge, instance=act)
def transit(act_id, to_state): query_kwargs = { 'pk': act_id, } try: act = ActivityModel.objects.get(**query_kwargs) except ActivityModel.DoesNotExist: logger.info( messages.build_message(messages.ACT_MODEL_NOT_EXIST, query_kwargs)) else: logger.info("transit activity #%s" % act_id) with global_exception_handler(act): act._transit(to_state)