def do_killall(self, line_): ret = self.celery.control.inspect().active() if not ret: self.logger.warn('0 tasks killed') return for client, actives in ret.items(): for active in actives: self.logger.warn('{} tasks killed'.format(active['id'])) revoke(active['id'], terminate=True)
def cancel_task(id): res = celery.AsyncResult(id) if res.state == states.PENDING: revoke(id, terminate=True) return "task '{id}' cancelled".format(id=id) else: return str(res.result)
def inner_redo(rj): if rj.celery_task_id is not None: revoke(rj.celery_task_id, terminate=True) # 1. Revoke all downstream runjobs for o in rj.outputs.all(): r = o.resource or o.resource_list for i in r.inputs.filter(Q(run_job__workflow_run=rj.workflow_run) & ~Q(run_job__status=task_status.SCHEDULED)): inner_redo(i.run_job) # 2. Clear output resources if isinstance(r, Resource): rs = [r] else: rs = r.resources.all() for rr in rs: rr.has_thumb = False # 3. Reset status and clear interactive data original_settings = {} for k, v in rj.job_settings.iteritems(): if not k.startswith('@'): original_settings[k] = v rj.job_settings = original_settings rj.status = task_status.SCHEDULED rj.save(update_fields=['status', 'job_settings'])
def poll_delete_reminder(sender, instance, **kwargs): """Revoke the task if a voting is deleted.""" if not settings.CELERY_ALWAYS_EAGER: if instance.task_start_id: celery_control.revoke(instance.task_start_id) if instance.task_end_id: celery_control.revoke(instance.task_end_id)
def terminate_tasks(self, request, queryset): connection = default_app.broker_connection() try: for state in queryset: revoke(state.task_id, connection=connection, terminate=True) finally: connection.close()
def delete(self, task_id): """ Revoke a task **Example request**: .. sourcecode:: http DELETE /tasks/revoke/d776e835-33ac-447f-b27d-bb8529718ae6/ HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate, compress Content-Length: 0 Content-Type: application/json; charset=utf-8 Host: localhost:8888 User-Agent: HTTPie/0.8.0 **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Length: 51 Content-Type: application/json; charset=UTF-8 Server: TornadoServer/3.2 { "task-id": "d776e835-33ac-447f-b27d-bb8529718ae6" } :statuscode 200: no error :statuscode 400: invalid request """ revoke(task_id) self.write({"task-id": task_id})
def cancel(self, save=True): if self.task_id: revoke(self.task_id, terminate=True) self.mailcancel_set.get_or_create(task_id=self.task_id) self.task_id = '' if save: self.save()
def plan_next_starvation(self): """ If the food income in a village is negative, compute the date of the starvation and plans a call to check_starvation for this date """ logger.info("planning next starvation") if self.starvation_task_id: revoke(self.starvation_task_id) logger.info("found old starvation task -- revoking") if self.income.food < 0: logger.info("negative income. computing starvation time") next_starvation_delta =\ (STARVATION_LIMIT - float(self.resources.food))\ / float(self.income.food) countdown = (next_starvation_delta * 3600).__int__() logger.info("planning next starvation time for village {}" ". countdown : {}".format(self.id, countdown)) res = tasks.check_starvation.apply_async( args=[self.id], countdown=countdown, ) self.starvation_task_id = res.id self.save() else: logger.info("positive income. No planned starvation")
def revoke_publisher_task(self, task, callback_url, terminate=False, remove=False): u""" This do not delete tasks from tasks database (if remove=False) but set revoked attribute in tasks database and broadcast revoke request to publication units with celery. If the task is actually running it will be cancelled if terminated = True. In any case, the output media asset will be deleted (task running or successfully finished). """ if valid_uuid(task, none_allowed=False): task = self.get_publisher_task({u'_id': task}) task.is_valid(True) if task.status in PublisherTask.CANCELED_STATUS: raise ValueError(to_bytes(u'Cannot revoke a publication task with status {0}.'.format(task.status))) if not self.config.is_mock: revoke(task._id, terminate=terminate) if task.status == PublisherTask.SUCCESS and not self.config.is_mock: # Send revoke task to the worker that published the media callback = Callback(self.config.api_url + callback_url, u'node', self.config.node_secret) queue = task.get_hostname() result = PublisherWorker.revoke_publisher_task.apply_async( args=(task.publish_uri, object2json(callback, False)), queue=queue) if not result.id: raise ValueError(to_bytes(u'Unable to transmit task to queue {0}.'.format(queue))) logging.info(u'New revoke publication task {0} -> queue {1}.'.format(result.id, queue)) self.update_publisher_task_and_media(task, revoke_task_id=result.id, status=PublisherTask.REVOKING) else: self.update_publisher_task_and_media(task, status=PublisherTask.REVOKED) if remove: self._db.publisher_tasks.remove({u'_id': task._id})
def handle(self, *args, **options): for d in Document.objects.filter(status = Document.STATUS.waiting): print "revoke task for %s" % d revoke(d.task_id)
def sync_with_reality(storagerouter_guid=None, max_attempts=3): """ Try to run sync_with_reality, retry in case of failure always run sync, as tasks calling this expect this to be sync :param storagerouter_guid: :return: """ cache = VolatileFactory.get_client() mutex = VolatileMutex('ovs_disk_sync_with_reality_{0}'.format(storagerouter_guid)) key = 'ovs_dedupe_sync_with_reality_{0}'.format(storagerouter_guid) attempt = 1 while attempt < max_attempts: task_id = cache.get(key) if task_id: revoke(task_id) try: mutex.acquire(wait=120) return DiskController._sync_with_reality(storagerouter_guid) except Exception as ex: logger.warning('Sync with reality failed. {0}'.format(ex)) attempt += 1 time.sleep(attempt*30) finally: mutex.release() raise RuntimeError('Sync with reality failed after 3 attempts')
def killRequest(req): if req.method == 'GET': pid = req.GET['connId'] elif req.method == 'PUT': data = json.loads(req.body) pid = str(data['connId']) flag = True i = inspect() hostname = str(i.active().keys()[0]) for conn in i.active()[hostname]: if pid == str(conn['id']): flag = False break reqResult = AsyncResult(pid) if reqResult.result != 'OK' and str(reqResult.result) != 'revoked' and str(reqResult.result) != 'terminated': if flag == False: revoke(pid, terminate=True) return JsonResponse({'status' : 'Killed'}) else: return JsonResponse({'Exception' : 'Invalid connectionID : ' + pid}) else: if reqResult.result == 'OK': return JsonResponse({'Exception' : 'Task was already completed'}) else: return JsonResponse({'Exception' : 'Task was already revoked'})
def revoke_tasks(self, request, queryset): connection = establish_connection() try: for state in queryset: revoke(state.task_id, connection=connection) finally: connection.close()
def schedule_battle(sender, instance, **kwargs): if instance.task_id is not None: revoke(instance.task_id, terminate=True) eta = max(timezone.now(), instance.start_time) task = stream_twitter.apply_async([instance.id], eta=eta) Battle.objects.filter(id=instance.id).update(task_id=task.id)
def wrapped(*args, **kwargs): request = args[0] if only_staff: if not request.user.is_staff: raise PermissionDenied elif not request.user.is_active: raise PermissionDenied if task_key and CeleryTasks.objects.filter(key=task_key, status__in=["waiting", "active"]): json_data = json.dumps("Error: %s Task is already running" % task_key) return HttpResponse(json_data, content_type='application/json') try: task_id = fn(*args, **kwargs) except: json_data = json.dumps("Error: %s Task failed to run" % task_key) return HttpResponse(json_data, content_type='application/json') try: CeleryTasks.objects.create(task_id=task_id, user=request.user, key=task_key) except IntegrityError: # We don't want to have 2 tasks with the same ID logger.critical("There ware 2 tasks with the same ID. Trying to terminate the task.", exc_info=True) from celery.task.control import revoke revoke(task_id, terminate=True) raise json_data = json.dumps(task_id) return HttpResponse(json_data, content_type='application/json')
def startRequest(req): if 'connId' in req.GET.keys() and 'timeOut' in req.GET.keys() and len(req.GET) == 2: flag = True i = inspect() hostname = str(i.active().keys()[0]) for conn in i.active()[hostname]: if req.GET['connId'] == str(conn['id']): flag = False break if flag == True: try: currentTimeStamp = datetime.datetime.now() endingTimeStamp = currentTimeStamp + datetime.timedelta(seconds = int(req.GET['timeOut'])) res = test.apply_async(args = [str(endingTimeStamp)] , task_id = req.GET['connId']) try: res.get(timeout = int(req.GET['timeOut'])) return JsonResponse({'status' : 'OK'}) except TimeoutError: revoke(res.id, terminate=True) return JsonResponse({'status' : 'OK'}) except Exception: return JsonResponse({'Exception' : 'The request was either hard killed or was already processed'}) else: return JsonResponse({'Exception' : 'Request is already running'}) else: return JsonResponse({'Exception' : 'Invalid API call'})
def control(self, **kwargs): if kwargs['command'] == 'cancel': revoke(kwargs['taskId'], terminate=True) self.send({'command': 'cancel', 'success': 'true'}) else: self.send({'command': kwargs['command'], 'success': 'false', 'message': 'Unknown command'})
def perform_destroy(self, instance): if instance.status in (task_status.SCHEDULED, task_status.PROCESSING): raise CustomAPIException( "Please cancel the processing of this package before deleting.", status=status.HTTP_400_BAD_REQUEST ) if instance.celery_task_id: revoke(instance.celery_task_id, terminate=True) # revoke scheduled expiry task instance.delete()
def stop(self): revoke(self.task_id, terminate=True) try: os.kill(self.p_id, signal.SIGKILL) except OSError: pass self.closed = datetime.now() self.save()
def revoke(self, connection=None, connect_timeout=None): """Send revoke signal to all workers. The workers will ignore the task if received. """ from celery.task import control control.revoke(self.task_id)
def user_status_email_reminder(sender, instance, created, raw, **kwargs): """Send email notifications when a user submits an unavailability notice. """ rep_profile = instance.user.userprofile # Make sure that the user has a mentor mentor_profile = None if instance.user.userprofile.mentor: mentor_profile = instance.user.userprofile.mentor.userprofile if created: subject_rep = 'Confirm if you are available for Reps activities' rep_template = 'emails/rep_availability_reminder.txt' notification_datetime = datetime.datetime.combine( instance.expected_date - datetime.timedelta(days=1), datetime.datetime.min.time()) data = {'user_status': instance} time_diff = (timezone.make_aware(notification_datetime, pytz.UTC) - timezone.now()) # If the notification date is in the past, then send the notification # x/2 hours from now, where x is the diff between the return date and # now if time_diff.days < 0: hours_added = (time_diff.seconds / 3600) / 2 notification_datetime = (timezone.now() + datetime.timedelta(hours=hours_added)) rep_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={'recipients_list': [rep_profile.user.id], 'email_template': rep_template, 'subject': subject_rep, 'data': data}) if mentor_profile: mentor_template = 'emails/mentor_availability_reminder.txt' subject_mentor = ('Reach out to {0} - ' 'expected to be available again' .format(instance.user.get_full_name())) mentor_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={'recipients_list': [mentor_profile.user.id], 'email_template': mentor_template, 'subject': subject_mentor, 'data': data}) (UserProfile.objects.filter(pk=mentor_profile.id) .update(unavailability_task_id=mentor_reminder.task_id)) # Update user profiles with the task IDs (UserProfile.objects.filter(pk=rep_profile.id) .update(unavailability_task_id=rep_reminder.task_id)) elif not settings.CELERY_ALWAYS_EAGER: # revoke the tasks in case a user returns sooner if rep_profile.unavailability_task_id: celery_control.revoke(rep_profile.unavailability_task_id) if mentor_profile.unavailability_task_id: celery_control.revoke(mentor_profile.unavailability_task_id)
def removeTask(task_id): ''' this method revokes scheduled tasks as well as the tasks in progress ''' try: from celery.task.control import revoke revoke(task_id,terminate=True) except Exception,e: print "task cannot be removed/deleted. Error : ".format(str(e))
def stop_task(self, id): server = ServersOpenStack.query.filter(ServersOpenStack.id == id).first() revoke(server.task_id, terminate=True) server.status = None server.address = None server.instance = None server.installed_time = None db.session.add(server) db.session.commit()
def revoke_maintenance(self, request): if self.is_waiting_to_run: control.revoke(self.celery_task_id,) self.status = self.REVOKED self.revoked_by = request.user.username self.save() return True
def post(self): jobid = self.get_argument("jobname") print jobid loc_user = self.get_secure_cookie("user").replace('\"','') r=redis.StrictRedis(host='127.0.0.1', port=6379, db=0) revoke(jobid, terminate=True) self.set_status(200) self.flush() self.finish()
def cancel(self): """ Cancel existing task """ # Since we shuld have process lock grabbed at this place, # even if celery starts executing the task on a worker, # the task not started, so I think it's safe to terminate it revoke(self.task.external_task_id, terminate=True) super(AbstractJobActivation, self).cancel.original()
def maintenance_pre_delete(sender, **kwargs): """ maintenance pre delete signal. Revoke scheduled task and remove its HostMaintenance objects """ maintenance = kwargs.get("instance") LOG.debug("maintenance pre-delete triggered") HostMaintenance.objects.filter().delete() control.revoke(task_id=maintenance.celery_task_id)
def diabaleAlarm(self, request): alarmId = request.GET.get('alarmId', '') if alarmId != "": theAlarm = Alarms.objects.get(id=alarmId) theAlarm.state = "Disabled" if theAlarm.celeryTaskId != "TaskNotStarted": revoke(theAlarm.celeryTaskId, terminate=True) #may need to change tuminate to false (will stop it from canceling exicusion if set to fasle) theAlarm.celeryTaskId = "TaskNotStarted" theAlarm.save()
def save(self, *args, **kwargs): #if the task exists, kill it, just in case they updated the time. if(self.task_id): revoke(self.task_id) task = send_tweet.apply_async((self.id,)) self.task_id = task.task_id super(ScheduledTweet, self).save(*args, **kwargs)
def post(self, request, token): async_result = tasks.find(request, token) if async_result and async_result.task_id: revoke(async_result.task_id, terminate=True, signal='SIGKILL') messages.info(request, _('The task execution has been aborted.')) else: messages.error(request, DEFAULT_ERROR_MESSAGE) return HttpResponseAjax([ commands.hide_progress(), ], request)
def edit_scan_def_view(request, scan_def_id): request_user_id = request.user.id scan_definition = get_object_or_404(ScanDefinition, id=scan_def_id) scan_cats = EnginePolicyScope.objects.all().values() scan_policies = list(EnginePolicy.objects.all().prefetch_related( "engine", "scopes")) scan_engines = [] for sc in EngineInstance.objects.all().values( 'engine__name', 'engine__id').order_by('engine__name').distinct(): scan_engines.append({ 'id': sc['engine__id'], 'name': sc['engine__name'] }) scan_engines_json = json.dumps( list(EngineInstance.objects.all().values('id', 'name', 'engine__name', 'engine__id'))) teams_list = request.user.users_team.values('id', 'name').order_by('name') scan_policies_json = [] for p in scan_policies: scan_policies_json.append(p.as_dict()) form = None if request.method == 'GET': form = ScanDefinitionForm(instance=scan_definition, user=request.user) elif request.method == 'POST': form = ScanDefinitionForm(request.POST, user=request.user) if form.is_valid(): scan_definition.title = form.cleaned_data['title'] scan_definition.status = "edited" scan_definition.description = form.cleaned_data['description'] # scan_definition.enabled = form.cleaned_data['enabled'] is True scan_definition.engine_policy = form.cleaned_data['engine_policy'] scan_definition.engine_type = scan_definition.engine_policy.engine if form.cleaned_data['engine'] is not None and len( form.cleaned_data['engine']) > 0: # todo: check if the engine is compliant with the scan policy scan_definition.engine = EngineInstance.objects.get( id=form.data['engine']) else: scan_definition.engine = None if scan_definition.owner is None: scan_definition.owner = request.user # Check and add team if 'scan_team' in form.data.keys(): scan_definition.teams.clear() if form.data['scan_team'] == 'yes': scan_definition.teams.add( request.user.users_team.get( id=form.data['scan_team_list'])) # Update assets scan_definition.assets_list.clear() scan_definition.assetgroups_list.clear() assets_list = [] for asset_id in form.data.getlist('assets_list'): asset = Asset.objects.for_user(request.user).get(id=asset_id) scan_definition.assets_list.add(asset) assets_list.append({ "id": asset.id, "value": asset.value.strip(), "criticity": asset.criticity, "datatype": asset.type }) for assetgroup_id in form.data.getlist('assetgroups_list'): assetgroup = AssetGroup.objects.get(id=assetgroup_id) scan_definition.assetgroups_list.add(assetgroup) for a in assetgroup.assets.all(): scan_definition.assets_list.add(a) assets_list.append({ "id": a.id, "value": a.value.strip(), "criticity": a.criticity, "datatype": a.type }) if form.cleaned_data['scan_type'] == 'single': scan_definition.scan_type = 'single' scan_definition.every = None scan_definition.period = None task_title = '[PO] {}@{}'.format(scan_definition.title, scan_definition.id) periodic_task = PeriodicTask.objects.filter( name=task_title).first() if periodic_task is not None: periodic_task.delete() # Check scheduling params if any if form.data['start_scan'] == "scheduled" and form.cleaned_data[ 'scheduled_at'] is not None: try: # check if it's future or not if form.cleaned_data['scheduled_at'] > tz.now(): scan_definition.scheduled_at = form.cleaned_data[ 'scheduled_at'] scan_definition.enabled = True except Exception: scan_definition.scheduled_at = None scan_definition.enabled = False # Todo: update scan task (task_id) for s in scan_definition.scan_set.all(): revoke(s.task_id, terminate=True) else: scan_definition.scheduled_at = None if form.cleaned_data['scan_type'] == 'periodic': scan_definition.scan_type = 'periodic' scan_definition.every = int(form.cleaned_data['every']) scan_definition.period = form.cleaned_data['period'] schedule, created = IntervalSchedule.objects.get_or_create( every=int(scan_definition.every), period=scan_definition.period, ) parameters = { "scan_params": { "assets": assets_list, "options": scan_definition.engine_policy.options, }, "scan_definition_id": scan_definition.id, "engine_name": str(scan_definition.engine_type.name).lower(), "owner_id": request_user_id, } if form.cleaned_data['engine'] is not None and form.data[ 'engine'] != '' and int(form.data['engine']) > 0: parameters.update({ "engine_id": EngineInstance.objects.get(id=form.data['engine']).id, "scan_params": { "engine_id": EngineInstance.objects.get( id=form.data['engine']).id } }) # Update the PeriodicTask try: task_title = '[PO] {}@{}'.format(scan_definition.title, scan_definition.id) old_periodic_task = PeriodicTask.objects.filter( name=task_title) if old_periodic_task: # Update the current periodic task old_periodic_task.update(interval=schedule, args=json.dumps([parameters])) else: # Create a new one (just in case of another #?!-$ bug on Celery) new_periodic_task = PeriodicTask.objects.create( interval=schedule, name=task_title, task='engines.tasks.start_periodic_scan_task', args=json.dumps([parameters]), queue='scan', last_run_at=None) scan_definition.periodic_task = new_periodic_task except Exception: pass _update_celerybeat() # Finally, save it and run it baby scan_definition.save() if form.data['start_scan'] == "now": _run_scan(scan_definition.id, request.user.id) elif form.data[ 'start_scan'] == "scheduled" and scan_definition.scheduled_at is not None: _run_scan(scan_definition.id, request.user.id, eta=scan_definition.scheduled_at) messages.success(request, 'Update submission successful') return redirect('list_scan_def_view') return render( request, 'edit-scan-definition.html', { 'form': form, 'scan_def': scan_definition, 'scan_engines': scan_engines, 'scan_engines_json': scan_engines_json, 'scan_cats': scan_cats, 'scan_policies_json': json.dumps(scan_policies_json), 'scan_policies': scan_policies, 'teams_list': teams_list })
def stop_visualization(app, visualizerFile, optOnly=False): """ Clean visualization tasks from the visualizerFile It basically terminates all the running/pending tasks from the visualizerFile When optOnly==True, if only terminates the running/pending tasks that was started from plot options changing requests NOTE: error should be handled by the caller @type app: Celery application @param app: Celery application instance @type visualizerFile: string @param visualizerFile: name of the visualizationStatus file that stores the visualization manager/task id's and its updated status @type optOnly: bool @param optOnly: flag indicating whether only terminates the running/pending tasks that was started from plot options changing requests """ # return if visualizerFile has been generated yet if not os.path.isfile(visualizerFile): return # extract previously saved visualization status visualizationManager_preState = {} visualizationTask_preState = {} pattern_visMgr = re.compile( r"Visualization manager \(id: ([^\s]+), options: (.+)\) status: ([A-Z]+)\n" ) pattern_visTsk = re.compile( r"Visualization task \(id: ([^\s]+), options: (.+), file: ([^\s]+)\) status: ([A-Z]+)\n" ) for line in open(visualizerFile).readlines(): if pattern_visMgr.match(line): (mid, moptions, mstate) = pattern_visMgr.match(line).groups() visualizationManager_preState[mid] = (mstate, moptions) if pattern_visTsk.match(line): (tid, toptions, tfile, tstate) = pattern_visTsk.match(line).groups() visualizationTask_preState[tid] = (tstate, tfile) # if the visualizationManager was not found if not visualizationManager_preState: # raise an exception because this should never happend raise RuntimeError # for each visualization manager (with or without options) for mid, (mstate, moptions) in visualizationManager_preState.items(): # skip visualization for None options when requested if optOnly and moptions == 'None': continue # get the current visualization manager status visMgr_state = AsyncResult(mid).state # collect the current visualization task information when necessary visualizationTask_info = {} if visMgr_state in ['PROGRESS', 'SUCCESS']: visualizationTask_info = AsyncResult(mid).result # kill the visualization manager if possible if visMgr_state in ['PENDING', 'STARTED', 'PROGRESS']: revoke(mid, terminate=True) # kill the visualization tasks if any for (visualizationTask_id, plotManifestFileName) in visualizationTask_info.iteritems(): visTsk_state = AsyncResult(visualizationTask_id).state if visTsk_state in ['PENDING', 'STARTED']: revoke(visualizationTask_id, terminate=True)
def revoke(self, process_id, kill=False): task = self.get(process_id=process_id) kwargs = {} if not kill else {'signal': 'SIGKILL'} revoke(task.celery_task_id, terminate=True, **kwargs) self.destroy(process_id)
def revoke(self, node_id): task = self.get(node_id=node_id) revoke(task.celery_task_id) self.destroy(node_id)
#cmslist=list_of_groups(cmstypes,5) applist=[] for cms in cmstypes: proxy=None if len(proxylist)>0: proxy=proxylist[random.randint(0,len(proxylist)-1)] applist.append(scan.delay(options.url,cms,user_agent=options.ua,timeout=options.timeout,proxy_settings=proxy,interval=options.interval,confirm=options.fast)) result=[] strarrs = ['/', '|', '\\'] total=len(applist) confirmed=False while len(applist): for x in applist: if confirmed: for x in applist: revoke(x.id, Terminate=True) applist=[] break try: if(x.ready()): current_result=x.get() result.append(current_result) #print current_result if(options.fast and current_result['confirm']): confirmed=True #result.append(current_result) break #result.append(current_result) applist.remove(x) row,col=os.popen('stty size','r').read().split() col=int(col)
def delete(self): #もしタスクがあれば破棄 if self.celery_id != None: revoke(self.celery_id, terminate=True) #実際のdelete呼び出し super(Article, self).delete()
def revoke(self): logger.debug('Revoking task ({})'.format(self.pk)) revoke(self.celery_id, terminate=True) self.celery_id = uuid.uuid4() self.save() logger.info('Revoked task ({})'.format(self.pk))
def restart_listener(sender, **kw): task_id = r.get(LISTENER_TASK_KEY) if task_id: revoke(str(task_id, 'utf-8')) task_id = app.send_task('wechat.tasks.listener') r.set(LISTENER_TASK_KEY, task_id)
def terminate_tasks(self, request, queryset): with current_app.default_connection() as connection: for state in queryset: revoke(state.task_id, connection=connection, terminate=True)