def process_task(sender, task_id=None, event_status=None, **kwargs): self.log('Got signal for %s task %s', event_status, task_id, level=DEBUG) task_prefix = task_prefix_from_task_id(task_id) if task_prefix[4] != self.dc_id and task_prefix[3] != TG_DC_UNBOUND: self.log('Ignoring dc-bound task %s, because user works in DC %s', task_id, self.dc_id) return if event_status == 'sent': self._task_sent(task_id, event_status, sender, task_prefix[1]) elif event_status == 'event': self._task_event(task_id, sender) elif event_status == 'internal': self._task_internal(task_id, sender, **kwargs) else: self._task_status(task_id, event_status, task_prefix[1])
def _announce_task(event, event_status): task_id = event['uuid'] task_prefix = task_prefix_from_task_id(task_id) if task_prefix[2] == internal_id: # owner_id return # probably beat task if event.get('queue', None) == 'mgmt': return # sent task on mgmt users = User.get_super_admin_ids() # SuperAdmins users.update(User.get_dc_admin_ids(dc_id=task_prefix[4])) # DcAdmins users.add(int(task_prefix[0])) # TaskCreator users.add(int(task_prefix[2])) # ObjectOwner debug('Sending signal for %s task %s to %s', event_status, task_id, users) # Signal! for i in users: new_task = signal('task-for-%s' % i) new_task.send(event, task_id=task_id, event_status=event_status)
def _esrep_callback(self): vm = self.vm task_id = task_id_from_string(settings.SYSTEM_USER, owner_id=vm.owner.id, dc_id=vm.dc.id) task_prefix = ''.join(task_prefix_from_task_id(task_id)) return 'que.replication:esrep_sync_cb:%s' % task_prefix
def prefix(self): return task_prefix_from_task_id(self)
def task_log(task_id, msg, vm=None, obj=None, user=None, api_view=None, task_status=None, task_result=None, owner=None, time=None, detail=None, update_user_tasks=True, dc_id=None, **kwargs): """ Create task log read by the user. Also maintain dictionary of active tasks for specific VM. """ task_id = str(task_id) task_prefix = task_prefix_from_task_id(task_id) dc_id = dc_id or int( task_prefix[4]) # Specifying a custom dc_id is usually not a good idea update_tasks = False # When? (time) if not time: time = timezone.now() # Who? (username, task creator) if user and not isinstance(user, string_types): username = user.username user_id = user.id else: user = None user_id = int(task_prefix[0]) if user_id == int(cq.conf.ERIGONES_TASK_USER): username = cq.conf.ERIGONES_TASK_USERNAME else: try: username = User.objects.get(id=user_id).username except User.DoesNotExist: username = '******' # What? (status and result) if not task_status or not task_result: # Do not follow callback here, because we are called from pending callbacks task = cq.AsyncResult(task_id) if not task_status: task_status = task.state if not task_result: task_result = task.result # Object? (Vm, Node, ...) if vm: obj = vm if obj and isinstance(obj, (tuple, list)): object_name = obj[0] object_alias = obj[1] object_pk = obj[2] content_type = ContentType.objects.get_for_model(obj[3]) object_type = content_type.model obj = None elif obj and not isinstance(obj, string_types): object_name = obj.log_name object_alias = obj.log_alias object_pk = obj.pk content_type = ContentType.objects.get_for_model(obj) object_type = content_type.model if update_user_tasks and hasattr(obj, 'tasks'): update_tasks = True else: if obj: object_name = str(obj) else: object_name = '' object_alias = '' object_pk = '' obj = None content_type = None object_type = None if object_pk is None: object_pk = '' else: object_pk = str(object_pk) # Detail? if not detail: if task_result: # noinspection PyBroadException try: detail = task_result['detail'] except: # noinspection PyBroadException try: detail = task_result['message'] except: detail = '' if detail is None: detail = '' # Owner? if not owner: if obj: # noinspection PyBroadException try: owner = obj.owner except: owner = user elif user: owner = user # noinspection PyBroadException try: owner_id = owner.id except: owner_id = int(task_prefix[2]) if task_status == states.STARTED: task_status = states.PENDING try: # Log! log({ 'dc_id': dc_id, 'time': time, 'task': task_id, 'status': task_status, 'task_type': task_type_from_task_prefix(task_prefix), 'flag': task_flag_from_task_msg(msg), 'user_id': user_id, 'username': username, 'owner_id': owner_id, 'object_pk': object_pk, 'object_type': object_type, 'object_name': object_name, 'object_alias': object_alias, 'content_type': content_type, 'msg': msg, 'detail': detail, }) finally: if update_tasks: # Save task info into UserTasks cache after the task was logged try: if task_status in TASK_STATE_PENDING: obj.tasks_add(task_id, api_view, msg=msg, time=time.isoformat()) return True else: obj.tasks_del(task_id) except Exception as e: logger.exception(e) logger.error( 'Got exception (%s) when updating task %s for %s.', e, task_id, obj) return None