def test_revoke_terminate(self): request = Mock() request.id = tid = uuid() state.reserved_requests.add(request) try: r = control.revoke(Mock(), tid, terminate=True) self.assertIn(tid, revoked) self.assertTrue(request.terminate.call_count) self.assertIn('terminating', r['ok']) # unknown task id only revokes r = control.revoke(Mock(), uuid(), terminate=True) self.assertIn('not found', r['ok']) finally: state.reserved_requests.discard(request)
def test_revoke_terminate(self): request = Mock() request.id = tid = uuid() worker_state.reserved_requests.add(request) try: r = control.revoke(Mock(), tid, terminate=True) self.assertIn(tid, revoked) self.assertTrue(request.terminate.call_count) self.assertIn('terminate:', r['ok']) # unknown task id only revokes r = control.revoke(Mock(), uuid(), terminate=True) self.assertIn('tasks unknown', r['ok']) finally: worker_state.reserved_requests.discard(request)
def test_revoke_terminate(self): request = Mock() request.task_id = tid = uuid() state.active_requests.add(request) try: r = control.revoke(Mock(), tid, terminate=True) self.assertIn(tid, revoked) self.assertTrue(request.terminate.call_count) self.assertIn("terminated", r["ok"]) # unknown task id only revokes r = control.revoke(Mock(), uuid(), terminate=True) self.assertIn("revoked", r["ok"]) finally: state.active_requests.discard(request)
def test_revoke_terminate(self): request = Mock() request.id = tid = uuid() state = self.create_state() state.consumer = Mock() worker_state.task_reserved(request) try: r = control.revoke(state, tid, terminate=True) self.assertIn(tid, revoked) self.assertTrue(request.terminate.call_count) self.assertIn('terminate:', r['ok']) # unknown task id only revokes r = control.revoke(state, uuid(), terminate=True) self.assertIn('tasks unknown', r['ok']) finally: worker_state.task_ready(request)
def test_revoke_terminate(self): request = Mock() request.id = tid = uuid() state = self.create_state() state.consumer = Mock() worker_state.task_reserved(request) try: r = control.revoke(state, tid, terminate=True) assert tid in revoked assert request.terminate.call_count assert 'terminate:' in r['ok'] # unknown task id only revokes r = control.revoke(state, uuid(), terminate=True) assert 'tasks unknown' in r['ok'] finally: worker_state.task_ready(request)
def test_revoke_terminate(self): request = Mock() request.id = tid = uuid() state = self.create_state() state.consumer = Mock() worker_state.reserved_requests.add(request) try: r = control.revoke(state, tid, terminate=True) self.assertIn(tid, revoked) self.assertTrue(request.terminate.call_count) self.assertIn("terminate:", r["ok"]) # unknown task id only revokes r = control.revoke(state, uuid(), terminate=True) self.assertIn("tasks unknown", r["ok"]) finally: worker_state.reserved_requests.discard(request)
def remove_download(id): d = Download.query.filter_by(id=id).first() if d: task_id = d.task_id task = AsyncResult(task_id) try: if not task.ready(): revoke(task_id, terminate=True) except Exception: pass d.delete() return { 'status': 'success', 'message': f'Task {task_id} revoked and download {id} removed' } raise NotFoundException()
def on_event_update(mapper, connection, target): from event import tasks if target.on_start_task_id: revoke(celery_app, target.on_start_task_id) if target.on_finish_task_id: revoke(celery_app, target.on_finish_task_id) if not target.is_active: return if target.start_in: task = tasks.on_event_start.apply_async( (target.id,), countdown=target.start_in.seconds) target.on_start_task_id = task.id if target.finish_in: task = tasks.on_event_finish.apply_async( (target.id,), countdown=target.finish_in.seconds) target.on_finish_task_id = task.id
def task_prerun(task_id=None, sender=None, *args, **kwargs): """ Triggered when take is about to run """ # information about task are located in headers for task messages # using the task protocol version 2. if sender.__name__ not in build_tasks: return if sender.__name__ == 'pull_repo': build_observer.enter_prepare() else: build_observer.enter_build() build_observer.step_running(task_step_mapping[sender.__name__]) logger.warning(sender.__name__ + ' pre_run') current_build_number = kwargs['args'][-1] instance_key = kwargs['args'][-2] course_key = kwargs['args'][-3] logger.info('task_prerun for task id {}'.format(task_id)) logger.info('course_key:{}, instance_key:{}'.format( course_key, instance_key)) with celery.app.app_context(): # Get course instance by course key and instance key ins = CourseInstance.query.filter_by( course_key=course_key, instance_key=instance_key).first() # If no such instance in database, stop the task if ins is None: logger.error('No such course instance inthe database') revoke(task_id, terminate=True) return build_observer.update_database(ins.id, current_build_number, task_step_mapping[sender.__name__], BuildState.RUNNING) # Send the state to frontend build_observer.state_update( ins.id, current_build_number, task_step_mapping[sender.__name__], BuildState.RUNNING, 'Task {} for course_key:{}, instance_key:{} starts running\n'. format(sender.__name__, course_key, instance_key)) build_observer.step_running(task_step_mapping[sender.__name__])
def on_event_delete(mapper, connection, target): if target.on_start_task_id: revoke(celery_app, target.on_start_task_id) if target.on_finish_task_id: revoke(celery_app, target.on_finish_task_id)
def post(self, request, *args, **kwargs): self.object = self.get_object() revoke(task_id=self.object.task_id, state=states.REVOKED, terminate=True) return HttpResponseRedirect(self.success_url)
def stop(self, id): revoke(id, terminate=True) return { 'id': id, 'status': 'ok', }
def _canceller(self): revoke(self.task.id, terminate=True)