Beispiel #1
0
    def __add_worker_task(self, task):
        project = task['kwargs']['project']
        if not project in self.messages:
            self.messages[project] = {}

        result = AsyncResult(task['id'])
        if not task['id'] in self.messages[project]:
            try:
                timeSubmitted = datetime.fromtimestamp(time.time() - (
                    kombu.five.monotonic() - task['time_start']))
            except:
                timeSubmitted = current_time(
                )  #TODO: dirty hack to make failsafe with UI
            self.messages[project][task['id']] = {
                'type':
                ('train' if 'train' in task['name'] else 'inference'),  #TODO
                'submitted': str(timeSubmitted),
                'status': celery.states.PENDING,
                'meta': {
                    'message': 'job at worker'
                }
            }

        #TODO: needed?
        if result.ready():
            result.forget()
Beispiel #2
0
    def delete(self, id):
        """
        Delete a result of a job.
        """
        result_task = AsyncResult(id=id, app=backapp)
        state = result_task.state

        # tasks finished so result exists
        if state == states.SUCCESS:
            try:
                result_task.forget()
            except Exception as e:
                return error(result_task)
            return {
                'id': result_task.task_id,
                'desc': 'result for job {} deleted'.format(result_task.task_id)
            }, 200
        # task still pending or unknown - so result do not exists
        elif state == states.PENDING:
            return {'id': result_task.task_id, 'status': state}, 404
        # task started but result do not exists yet
        elif state == states.STARTED:
            return {'id': result_task.task_id, 'status': state}, 404
        else:
            return error(result_task)
    def getTaskResult(self, task_id):
        async_result = AsyncResult(task_id)
        can_forget = async_result.ready()

        try:
            try:
                result = async_result.result
                if isinstance(result, Exception):
                    result_cls_name = result.__class__.__name__
                    try:
                        errno = ERRNO_NS[result_cls_name]
                    except KeyError:
                        LOGGER.error('Undefined errno: %s', result_cls_name)
                        raise spec.ServerError()

                    value = [errno, result.message]
                else:
                    value = result

            except Exception as exc:
                LOGGER.exception(exc)
                raise spec.ServerError()

            status = getattr(spec.ResultStatus, async_result.status)
            return spec.AsyncResult(status=status, value=json.dumps(value))

        finally:
            if can_forget:
                async_result.forget()
                LOGGER.info('Forgot the result of task %s', task_id)
Beispiel #4
0
 def test_forget(self):
     tb = CacheBackend(backend="memory://")
     tid = gen_unique_id()
     tb.mark_as_done(tid, {"foo": "bar"})
     x = AsyncResult(tid, backend=tb)
     x.forget()
     self.assertIsNone(x.result)
Beispiel #5
0
 def test_forget(self):
     tb = CacheBackend(backend="memory://")
     tid = gen_unique_id()
     tb.mark_as_done(tid, {"foo": "bar"})
     x = AsyncResult(tid, backend=tb)
     x.forget()
     self.assertIsNone(x.result)
Beispiel #6
0
    def render_to_response(self, context, **response_kwargs):
        result = None
        if 0 and not self.request.is_ajax():
            raise Http404
        post_pk = self.kwargs.get('post_pk')
        count = Comment.objects.filter(pk=post_pk).count()
        if not post_pk:
            raise Http404

        id = self.request.GET.get('comments_refresh_id')
        forget = self.request.GET.get('comments_refresh_forget')
        if id:
            if id == 'new':
                res = tasks.celery_test_task.delay(post_pk)
                return JsonResponse({'comments_refresh_id': res.id})
            res = AsyncResult(id, app=celery.app)
            if res.state == 'SUCCESS':
                result = res.get()
                return JsonResponse(result)
            return JsonResponse({})
        elif forget:
            res = AsyncResult(forget, app=celery.app)
            res.forget()
            return JsonResponse({'forgotten': id})
        raise Http404
Beispiel #7
0
def index(request: HttpRequest):
    expire_old_scans()

    result = None

    domain, sess_domain, profile, sess_profile, is_public = process_request_data(
        request)

    if "task_id" in request.session:
        task_id = request.session["task_id"]
        async_result = AsyncResult(task_id)
        result = async_result.result
        async_result.forget()
        del request.session["task_id"]

    recent_scans = (Scan.objects.filter(
        end_datetime__isnull=False,
        is_public=True,
    ).order_by("-start_datetime")[:NUM_RECENT_SCANS].values_list("domain",
                                                                 flat=True))

    return render(
        request,
        "index.html",
        {
            "result": result,
            "domain_name": sess_domain,
            "profile_name": sess_profile,
            "is_public": is_public,
            "waiting_scans": waiting_scans(),
            "max_waiting_scans": MAX_WAITING_SCANS,
            "recent_scans": recent_scans,
        },
    )
Beispiel #8
0
    def getTaskResult(self, task_id):
        async_result = AsyncResult(task_id)
        can_forget = async_result.ready()

        try:
            try:
                result = async_result.result
                if isinstance(result, Exception):
                    result_cls_name = result.__class__.__name__
                    try:
                        errno = ERRNO_NS[result_cls_name]
                    except KeyError:
                        LOGGER.error('Undefined errno: %s', result_cls_name)
                        raise spec.ServerError()

                    value = [errno, result.message]
                else:
                    value = result

            except Exception as exc:
                LOGGER.exception(exc)
                raise spec.ServerError()

            status = getattr(spec.ResultStatus, async_result.status)
            return spec.AsyncResult(status=status, value=json.dumps(value))

        finally:
            if can_forget:
                async_result.forget()
                LOGGER.info('Forgot the result of task %s', task_id)
Beispiel #9
0
 def test_forget(self):
     tb = DatabaseBackend(backend="memory://")
     tid = uuid()
     tb.mark_as_done(tid, {"foo": "bar"})
     x = AsyncResult(tid)
     x.forget()
     self.assertIsNone(x.result)
Beispiel #10
0
def build_graph(request, course, course_key):
    if request.method != "POST" or not request.is_ajax():
        return HttpResponseBadRequest()

    task_state = json.loads(request.body.decode("utf-8"))

    if task_state["task_id"]:
        # Task is pending, check state and return result if ready
        async_result = AsyncResult(task_state["task_id"])
        if async_result.ready():
            task_state["ready"] = True
            task_state["task_id"] = None
            if async_result.state == "SUCCESS":
                task_state["graph_data"] = async_result.get()
                async_result.forget()
            else:
                task_state["graph_data"] = {}
    elif not task_state["ready"]:
        graph_data = json.loads(course.similarity_graph_json or '{}')
        min_similarity, min_matches = task_state["min_similarity"], task_state["min_matches"]
        if graph_data and graph_data["min_similarity"] == min_similarity and graph_data["min_matches"] == min_matches:
            # Graph was already cached
            task_state["graph_data"] = graph_data
            task_state["ready"] = True
        else:
            # No graph cached, build
            p_config = provider_config(course.provider)
            if not p_config.get("async_graph", True):
                task_state["graph_data"] = graph.generate_match_graph(course.key, float(min_similarity), int(min_matches))
                task_state["ready"] = True
            else:
                async_task = graph.generate_match_graph.delay(course.key, float(min_similarity), int(min_matches))
                task_state["task_id"] = async_task.id

    return JsonResponse(task_state)
    def getTasksInfo(tasks, forgetIfFinished=True):
        if tasks is None:
            return None, False, None
        if isinstance(tasks, str):
            tasks = json.loads(tasks)
        errors = []
        for t in range(len(tasks)):
            result = AsyncResult(tasks[t]['id'])
            if result.ready():
                tasks[t]['successful'] = result.successful()
                if tasks[t]['successful']:
                    tasks[t]['info'] = None
                else:
                    try:
                        error = str(result.get())
                        errors.append(error)
                    except Exception as e:
                        error = str(e)
                        errors.append(error)
                    tasks[t]['info'] = {}
                    tasks[t]['info']['message'] = error
                if forgetIfFinished:
                    result.forget()
            elif result.info is not None:
                tasks[t]['info'] = result.info
            if result.status is not None:
                tasks[t]['status'] = result.status
            if 'children' in tasks[t]:
                numDone = 0
                for key in tasks[t]['children']:
                    cResult = AsyncResult(tasks[t]['children'][key]['id'])
                    if cResult.ready():
                        numDone += 1
                        tasks[t]['children'][key][
                            'successful'] = cResult.successful()
                        if tasks[t]['children'][key]['successful']:
                            tasks[t]['children'][key]['info'] = None
                        else:
                            try:
                                error = str(cResult.get())
                                errors.append(error)
                            except Exception as e:
                                error = str(e)
                                errors.append(error)
                            tasks[t]['children'][key]['info'] = {}
                            tasks[t]['children'][key]['info'][
                                'message'] = error
                        if forgetIfFinished:
                            cResult.forget()
                    elif cResult.info is not None:
                        tasks[t]['children'][key]['info'] = cResult.info
                    if cResult.status is not None:
                        tasks[t]['children'][key]['status'] = cResult.status
                tasks[t]['num_done'] = numDone

        lastResult = AsyncResult(tasks[-1]['id'])
        hasFinished = lastResult.ready()

        return tasks, hasFinished, errors
Beispiel #12
0
 def test_forget(self):
     b = DatabaseBackend()
     tid = gen_unique_id()
     b.mark_as_done(tid, {'foo': 'bar'})
     x = AsyncResult(tid)
     self.assertEqual(x.result.get('foo'), 'bar')
     x.forget()
     self.assertIsNone(x.result)
Beispiel #13
0
    def on_success(self, retval, task_id, args, kwargs):
        print('on_success called!')
        print(retval, task_id, args, kwargs)

        res = AsyncResult(task_id)
        print(res.status)
        res.forget()
        print(res.status)
Beispiel #14
0
 def test_forget(self):
     b = DatabaseBackend()
     tid = gen_unique_id()
     b.mark_as_done(tid, {"foo": "bar"})
     x = AsyncResult(tid)
     self.assertEqual(x.result.get("foo"), "bar")
     x.forget()
     self.assertIsNone(x.result)
Beispiel #15
0
 def test_forget(self):
     b = DatabaseBackend()
     tid = gen_unique_id()
     b.mark_as_done(tid, {'foo': 'bar'})
     x = AsyncResult(tid)
     self.assertEqual(x.result.get('foo'), 'bar')
     x.forget()
     self.assertIsNone(x.result)
Beispiel #16
0
    def reset(self):
        if self.task:
            res = AsyncResult(self.task)
            if res.state != 'PENDING':
                res.forget()
                self.task = None

        self.save()
 def test_forget(self):
     tb = DatabaseBackend(backend='memory://')
     tid = uuid()
     tb.mark_as_done(tid, {'foo': 'bar'})
     tb.mark_as_done(tid, {'foo': 'bar'})
     x = AsyncResult(tid, backend=tb)
     x.forget()
     self.assertIsNone(x.result)
Beispiel #18
0
    def reset(self):
        if self.task:
            res = AsyncResult(self.task)
            if res.state != 'PENDING':
                res.forget()
                self.task = None

        self.save()
Beispiel #19
0
 def test_forget(self):
     tb = DatabaseBackend(backend='memory://')
     tid = uuid()
     tb.mark_as_done(tid, {'foo': 'bar'})
     tb.mark_as_done(tid, {'foo': 'bar'})
     x = AsyncResult(tid, backend=tb)
     x.forget()
     self.assertIsNone(x.result)
Beispiel #20
0
 def test_forget(self):
     b = DatabaseBackend()
     tid = gen_unique_id()
     b.mark_as_done(tid, {"foo": "bar"})
     x = AsyncResult(tid)
     self.assertEqual(x.result.get("foo"), "bar")
     x.forget()
     self.assertIsNone(x.result)
Beispiel #21
0
        def get_response():
            failure_reason = 'An error has occurred.'

            serializer = serializers.ExecutionStateSerializer(
                data=request.query_params)
            if serializer.is_valid(raise_exception=True):
                result = AsyncResult(request.user.username)
                if result is not None:
                    # if result.children is None:
                    #     failure_reason = 'Could not connect to the execution backend.'
                    #     result.forget()
                    if result.ready():
                        exec_result = CeleryExecutionResult(*result.result)
                        result.forget(
                        )  # Don't cache the result if we send it out
                        if not exec_result.mainExecError:
                            if exec_result.finished:
                                reqs = [(res[0], TestResult(*res[1]))
                                        for res in exec_result.results]
                                profile = models.BaseProfile.objects.get(
                                    user=request.user)
                                section = models.Section.objects.get(
                                    id=exec_result.section_id)
                                num_complete = 0
                                for req in reqs:
                                    if req[1].success:
                                        num_complete += 1
                                        profile.completed_section_requirements.add(
                                            section.requirements.get(
                                                id=req[0]))
                                if len(reqs
                                       ) == num_complete and len(reqs) != 0:
                                    profile.completed_sections.add(section)
                                raw_reqs = [req[1] for req in reqs]
                                return ExecutionResult(
                                    status=ExecutionState.success,
                                    result=exec_result.mainExecOutput,
                                    results=raw_reqs)
                            else:
                                return ExecutionResult(
                                    status=ExecutionState.input_required,
                                    result=exec_result.mainExecOutput)
                        else:
                            return ExecutionResult(
                                status=ExecutionState.failed,
                                result=exec_result.mainExecOutput,
                                error=exec_result.mainExecError)
                    else:
                        if result.failed():
                            failure_reason = 'The code attempted to run, but an error occurred.'
                        else:
                            return ExecutionResult(
                                status=ExecutionState.running)
                else:
                    failure_reason = 'The provided token is invalid.'

            return ExecutionResult(status=ExecutionState.metafail,
                                   reason=failure_reason)
Beispiel #22
0
def forget_tasks(jobs: QuerySet):
    tasks = [
        task[0] for task in [
            sublist for biglist in [job.data for job in jobs]
            for sublist in biglist
        ]
    ]
    for task_id in tasks:
        task = AsyncResult(id=task_id)
        task.forget()
    jobs.delete()
Beispiel #23
0
 def test_forget(self):
     b = DatabaseBackend(app=app)
     tid = gen_unique_id()
     b.mark_as_done(tid, {'foo': 'bar'})
     x = AsyncResult(tid)
     self.assertEqual(x.result.get('foo'), 'bar')
     x.forget()
     if celery.VERSION[0:3] == (3, 1, 10):
         # bug in 3.1.10 means result did not clear cache after forget.
         x._cache = None
     self.assertIsNone(x.result)
Beispiel #24
0
def show_past_search(task_id):
    if not isLoggedIn(request):
        response_file = open('HTML_pages/Redirect_Home.html')
        return response_file.read(), 401
    requested_with_cookie = request.cookies.get('logged_in_cookie')
    user_object = User.query.filter_by(cookie=requested_with_cookie).first()

    search_object = Search.query.filter_by(task_id=task_id).first()

    if search_object is None:
        return 'Invalid Request', 404

    if search_object.status == 'FAILURE':
        return 'Failed Search'

    if search_object.status != 'SUCCESS':
        res = AsyncResult(task_id, app=cel)
        search_object.status = str(res.state)
        info = res.info
        if search_object.status == 'SUCCESS':
            output_table, header, metadata = res.get()
            search_object.items_searched = metadata
            search_object.search_completed = datetime.datetime.now(
                datetime.timezone.utc)
            search_object.table_data = (output_table, header)
            db.session.commit()
            res.forget()
        elif search_object.status == 'FAILURE':
            search_object.search_completed = datetime.datetime.now(
                datetime.timezone.utc)
            db.session.commit()
            res.forget()
        else:
            return '{"status" : "RUNNING", "progress" : ' + str(
                info['done']) + ', "data_points": ' + str(info['total']) + '}'
    table, header = search_object.table_data

    response_file = open('HTML_pages/load_search_template.html')
    template = response_file.read()

    pd.set_option('display.max_colwidth', -1)
    df = pd.DataFrame(table)
    df.columns = header
    table_html_string = df.to_html(index=False, justify='center')

    time_delt = datetime.timedelta(hours=5)

    response_html = template.format(
        task_id, search_object.user_name,
        (search_object.search_started -
         time_delt).strftime("%b %d %Y %H:%M:%S") + " CDT",
        get_detailed_search_info_html(search_object.items_searched), task_id,
        table_html_string)
    return response_html
Beispiel #25
0
 def test_forget(self):
     b = DatabaseBackend(app=app)
     tid = gen_unique_id()
     b.mark_as_done(tid, {'foo': 'bar'})
     x = AsyncResult(tid)
     self.assertEqual(x.result.get('foo'), 'bar')
     x.forget()
     if celery.VERSION[0:3] == (3, 1, 10):
         # bug in 3.1.10 means result did not clear cache after forget.
         x._cache = None
     self.assertIsNone(x.result)
Beispiel #26
0
def migrate_task_result(task_id):
    """Migrate task results to the database from the broker table."""
    LOG.debug("Migrating task %s result to the DB" % task_id)
    adt = models.ApplicationDeploymentTask.objects.get(celery_id=task_id)
    task = AsyncResult(task_id)
    task_meta = task.backend.get_task_meta(task.id)
    adt.celery_id = None
    adt.status = task_meta.get('status')
    adt.result = json.dumps(task_meta.get('result'))
    adt.traceback = task_meta.get('traceback')
    adt.save()
    task.forget()
Beispiel #27
0
def migrate_task_result(task_id):
    """Migrate task results to the database from the broker table."""
    LOG.debug("Migrating task %s result to the DB" % task_id)
    adt = models.ApplicationDeploymentTask.objects.get(celery_id=task_id)
    task = AsyncResult(task_id)
    task_meta = task.backend.get_task_meta(task.id)
    adt.celery_id = None
    adt.status = task_meta.get('status')
    adt.result = json.dumps(task_meta.get('result'))
    adt.traceback = task_meta.get('traceback')
    adt.save()
    task.forget()
Beispiel #28
0
def invia_mail_forzato(self, pk_tuple):
    """
    Questo task invia forzatamente la mail.
    Nessuna verifica si fa se il messaggio è stato precedentemente inviato
    oppure sia accodato. (come con l'invio normale nella funzione di sopra)
    """
    from celery import uuid
    from celery.result import AsyncResult
    from .models import Messaggio

    logger = get_task_logger(__name__)
    rescheduled_tasks_id = list()

    messages_to_resend = Messaggio.objects.filter(pk__in=pk_tuple)
    for msg in messages_to_resend:
        pk = msg.pk

        logger.info("[forced] Controllo messaggio id=%d" % pk)

        # Controllo se il messaggio ha un task_id,
        # se presente - dimentico il task per assegnare un nuovo task_id al messaggio
        if msg.task_id is not None:
            task = AsyncResult(msg.task_id)
            task.forget()
            logger.info(
                "[forced] Dimentico task_id %s per il messaggio id=%d" %
                (msg.task_id, pk))

        # Creiamo un nuovo task ID e riaccodiamo il task.
        msg.task_id = uuid()
        msg.save()

        logger.warning("[forced] Nuovo task per l'invio accodato con id=%s." %
                       msg.task_id)

        is_sent = msg.invia(forced=True)
        if not is_sent:
            logger.error(
                "[forced] Errore temporaneo, nuovo tentativo richiesto.")
            raise self.retry()

        # Messaggio inviato con successo.
        logger.info("[forced] Messaggio %s inviato. Rimuovo task_id e salvo." %
                    msg.pk)

        rescheduled_tasks_id.append(msg.task_id)
        msg.task_id = None

        msg.save()

    return len(rescheduled_tasks_id)
Beispiel #29
0
 def revoke(self):
     task = AsyncResult(self.task_id, app=sim_worker.celery.app)
     task.revoke(terminate=True)
     task.forget()
     if self.stage == "Configuration":
         self.stage = "Modification"
         self.status = "NA"
     elif self.stage == "Detail Configuration":
         self.stage = "Modification"
         self.status = "NA"
     elif self.stage == "Simulation":
         self.stage = "Configuration"
         self.status = "Success"
     self.save()
Beispiel #30
0
 def delete(self, id):
     """
     Deletes task associated with {id} passed in
     """
     resp = flask.make_response()
     try:
         res = AsyncResult(id)
         res.revoke(terminate=True)
         res.forget()
         resp.status_code = 200
         return resp
     except Exception:
         app.logger.exception('Caught exception deleting result')
     resp.status_code = 500
     return resp
Beispiel #31
0
    def fetch(self, request, pk=None):
        r = AsyncResult(pk)
        if not r.result:
            return Response({"error": "Link expired or request not found"}, status=status.HTTP_404_NOT_FOUND)

        if r.status != "SUCCESS":
            return Response({"error": "Job failed or not ready"}, status=status.HTTP_400_BAD_REQUEST)

        filename = r.result.get("filename")
        b64 = r.result.get("b64")
        r.forget()
        response = HttpResponse(base64.b64decode(b64), content_type='application/octet-stream')
        response['Content-Disposition'] = "attachment; filename={}".format(filename)
        response["Access-Control-Expose-Headers"] = "Content-Disposition"
        return response
Beispiel #32
0
def run_code(inputs):
    tasks.execute_code.apply_async(args=(0, code, inputs, test_list),
                                   task_id=TASK_ID)

    task_complete = False
    result = None
    while not task_complete:
        time.sleep(0.1)
        unknown_result = AsyncResult(TASK_ID)
        if not unknown_result or not unknown_result.ready():
            continue

        task_complete = True
        result = CeleryExecutionResult(*unknown_result.result)
        unknown_result.forget()
    return result
Beispiel #33
0
    def delete(self, id):
        """Delete a task result

        TITLE:Sample
        <pre>
        CURL:"/amazingresources/<id>"
        </pre>
        """

        # verify request
        self.initializeAPI(data={"id": id})
        task = AsyncResult(id, app=CONSUMER_APP)
        if task.state != 'SUCCESS':
            return 'Task result not found', 404
        task.forget()
        return '', 204
Beispiel #34
0
def update_unresolved_searches(username):
    recent_searches = Search.query.filter_by(user_name=username).all()
    for each_search in recent_searches:
        task_id = each_search.task_id
        if each_search.status != 'SUCCESS' and each_search.status != 'FAILURE':
            res = AsyncResult(task_id, app=cel)
            each_search.status = str(res.state)
            if each_search.status == 'SUCCESS':
                output_table, header, metadata = res.get()
                each_search.items_searched = metadata
                each_search.search_completed = datetime.datetime.now(
                    datetime.timezone.utc)
                each_search.table_data = (output_table, header)
                res.forget()
            if each_search.status == 'FAILURE':
                each_search.search_completed = datetime.datetime.now(
                    datetime.timezone.utc)
                res.forget()
    db.session.commit()
Beispiel #35
0
def reset(request):
    #tasks.app.backend.client.flushdb()
    import shutil

    for path in ['/tmp/transcoded/*', '/tmp/oga/*']:
        if os.path.exists(path):
            shutil.rmtree(path)
    
    jobs = {}
    keys = tasks.app.backend.client.keys('*')
    for key in keys:
        if key.startswith('celery-task-meta-'):
            key = key.split('celery-task-meta-')[1]
            print 'FORGETTING', key
            job = AsyncResult(key)
            jobs[key] = job.state
            job.forget()
            
    data = {'state': 'cleared', 'jobs': jobs}
    return HttpResponse(json.dumps(data, indent=4), content_type="application/json", status=200)
Beispiel #36
0
    def get(self, request, task_id):
        """
        delete task
        """
        if not redis_instance.exists(task_id):
            context = dict()
            context['status'] = 'FAIL'
            context['description'] = 'Task not exist'

            param_list = [f'{key}={value}' for key, value in context.items()]
            log_string = ' '.join(param_list)
            logger.warning(f'Task not exist: {log_string}')

            return Response(context, status=status.HTTP_400_BAD_REQUEST)

        redis_instance.delete(task_id)

        task = AsyncResult(task_id, app=app)
        task_status = task.state

        context = dict()

        if task_status == 'SUCCESS':
            context['status'] = 'SUCCESS'
            task.forget()

            param_list = [f'{key}={value}' for key, value in context.items()]
            log_string = ' '.join(param_list)
            logger.info(f'Task was delete: {log_string}')
            cur_status = status.HTTP_200_OK
        else:
            context['status'] = 'FAIL'
            context['description'] = 'Unknown error'
            context['task_status'] = task_status

            param_list = [f'{key}={value}' for key, value in context.items()]
            log_string = ' '.join(param_list)
            logger.error(f'{context["description"]}: {log_string}')
            cur_status = status.HTTP_400_BAD_REQUEST

        return Response(context, status=cur_status)
Beispiel #37
0
def migrate_launch_task(task_id):
    """
    Migrate task result to a persistent model table.

    Task result may contain temporary info that we don't want to keep. This
    task is intended to be called some time after the initial task has run to
    migrate the info we do want to keep to a model table.
    """
    adt = models.ApplicationDeploymentTask.objects.get(celery_id=task_id)
    task = AsyncResult(task_id)
    task_meta = task.backend.get_task_meta(task.id)
    adt.status = task_meta.get('status')
    adt.traceback = task_meta.get('traceback')
    adt.celery_id = None
    sanitized_result = copy.deepcopy(task_meta['result'])
    if sanitized_result.get('cloudLaunch', {}).get('keyPair', {}).get(
            'material'):
        sanitized_result['cloudLaunch']['keyPair']['material'] = None
    adt.result = json.dumps(sanitized_result)
    adt.save()
    task.forget()
Beispiel #38
0
def schedule(task_id, func , request):
    job = AsyncResult(task_id)
    if request.GET.get('force', False) and job.state != 'QUEUED':
        job.forget()
        
    if job.state == 'PENDING':
        print 'SCHEDULE', job.state, 'executing task'
        job = func()
    elif job.state == 'QUEUED':
        from celery.task.control import inspect
        i = inspect()
        task_ids = []
        for workers in [i.active(), i.reserved()]:
            for worker, tasks in workers.items():
                task_ids.extend([task['id'] for task in tasks])
        if task_id not in task_ids:
            print 'SCHEDULE', job.state, 'executing task'
            job = func()
    else:
        print 'SCHEDULE', job.state, 'already there'
    return job
Beispiel #39
0
def migrate_launch_task(task_id):
    """
    Migrate task result to a persistent model table.

    Task result may contain temporary info that we don't want to keep. This
    task is intended to be called some time after the initial task has run to
    migrate the info we do want to keep to a model table.
    """
    adt = models.ApplicationDeploymentTask.objects.get(celery_id=task_id)
    task = AsyncResult(task_id)
    task_meta = task.backend.get_task_meta(task.id)
    adt.status = task_meta.get('status')
    adt.traceback = task_meta.get('traceback')
    adt.celery_id = None
    sanitized_result = copy.deepcopy(task_meta['result'])
    if sanitized_result.get('cloudLaunch', {}).get('keyPair', {}).get(
            'material'):
        sanitized_result['cloudLaunch']['keyPair']['material'] = None
    adt.result = json.dumps(sanitized_result)
    adt.save()
    task.forget()
Beispiel #40
0
def index(request):
    if request.method == 'POST':
        form = AsyncResultForgetForm(request.POST)

        if form.is_valid():
            queue_id = form.cleaned_data['async_result']
            print(f"ID: {queue_id}")


            res = AsyncResult(queue_id)
            print(f"res.ready: {res.ready()}")
            print(f"res.status: {res.status}")
            
            print(res.result)
            res.forget()

            return HttpResponseRedirect('')




    context = dict()
    form = AsyncResultForgetForm()

    inteval_schedules = IntervalSchedule.objects.all()

    melco_kwargs = {"source": "melco"}
    melco_args = "[\"melco\"]"
    periodic_tasks = PeriodicTask.objects.filter(args=melco_args)
    # periodic_tasks = PeriodicTask.objects.all()
    for task in periodic_tasks:
        print(task.args)


    context['interval_schedules'] = inteval_schedules
    context['periodic_tasks'] = periodic_tasks
    context['form'] = form

    return render(request, 'index.html', context)
    def pollStatus(self, project, jobID):
        '''
            Queries the dict of registered jobs and polls
            the respective job for status updates, resp.
            final results. Returns the respective data.
            If the job has terminated or failed, it is re-
            moved from the dict.
            If the job cannot be found in the dict, the
            message broker is being queried for potentially
            missing jobs (e.g. due to multi-threaded web
            server processes), and the missing jobs are
            added accordingly. If the job can still not be
            found, an exception is thrown.
        '''
        status = {}

        # to poll message broker for missing jobs
        def _poll_broker():
            i = self.celery_app.control.inspect()
            stats = i.stats()
            if stats is not None and len(stats):
                active_tasks = i.active()
                for key in stats:
                    for task in active_tasks[key]:
                        # append task if of correct project
                        taskProject = task['delivery_info']['routing_key']
                        if taskProject == project:
                            if not task['id'] in self.jobs[project]:
                                self._register_job(project, task, task['id'])       #TODO: not sure if this works...

        if not project in self.jobs:
            _poll_broker()
            if not project in self.jobs:
                raise Exception('Project {} not found.'.format(project))
        
        if not jobID in self.jobs[project]:
            _poll_broker()
            if not jobID in self.jobs[project]:
                raise Exception('Job with ID {} does not exist.'.format(jobID))

        # poll status
        #TODO
        msg = self.celery_app.backend.get_task_meta(jobID)
        if msg['status'] == celery.states.FAILURE:
            # append failure message
            if 'meta' in msg:
                info = { 'message': html.escape(str(msg['meta']))}
            else:
                info = { 'message': 'an unknown error occurred'}
        else:
            info = msg['result']

            # check if ongoing and remove if done
            result = AsyncResult(jobID)
            if result.ready():
                status['result'] = result.get()
                result.forget()

        status['status'] = msg['status']
        status['meta'] = info

        return status
Beispiel #42
0
 def test_forget(self):
     self.tb.mark_as_done(self.tid, {'foo': 'bar'})
     x = AsyncResult(self.tid, backend=self.tb)
     x.forget()
     self.assertIsNone(x.result)
def mongo_thread_job():
    time.sleep(4)
    from celery.result import AsyncResult
    conn = Connection()
    db = conn['MetadataDB']
    task_coll = db['task_metadata']
    for result in task_coll.find():
        task_status = result[u'status']
        
        # IF TASK DONE:
        if task_status == "SUCCESS":
            task_id = result['_id']
            async_result = AsyncResult(task_id)         # result = a dictionary (the exact data as it is stored in MongoDB
            task_result = async_result.get()            # task_result = AsyncResult object
            
            subm_id = task_result['submission_id']    # Submission_id object looks like: {"submission_id" : {"pk" : "12345df3453453"}}
            task_name = task_result['task_name']
            print "WHAT IS THE TYPE OF TASK?", task_name
            #subm_id = task_result[u'submission_id']
            file_id = task_result['file_id']
            
            
            print "ASYNC result: submissionid=", subm_id, "file:", file_id, "result: ", task_result['task_result'], "task name: ", task_name
    
            # QUERY MongoDB:            
            submissions_set = models.Submission.objects.filter(_id=subm_id)
            if len(submissions_set) == 0:
                continue
            submission = submissions_set.get()
            for submitted_file in submission.files_list:
                if submitted_file.file_id == file_id:
                    if task_name == "serapis.tasks.UploadFileTask":
                        print "TASK is UPLOAD!"
                        if task_status == "SUCCESS":
                            submitted_file.md5 = task_result['md5']         # THIS IS THE SUCCESS branch, hence MD5 must be there
                            submitted_file.file_upload_job_status = "SUCCESS"
                        else:
                            submitted_file.file_upload_job_status = "FAILURE"
                            print "FAILUREEEEEEEEEEE!!! TASK FAILURE REPORTED IN MAIN THREAD!!!"
                        
                    elif task_name == "serapis.tasks.QuerySeqScapeTask":
                        print "TASK is QUERY SEQSC", task_result
                        if task_status == "SUCCESS":
                            submitted_file.study_list.extend(task_result[u'study_list'])
                            submitted_file.library_list.extend(task_result[u'library_list'])
                            submitted_file.sample_list.extend(task_result[u'sample_list'])
                            submitted_file.individuals_list.extend(task_result[u'individuals_list'])
                            submitted_file.file_seqsc_mdata_status = "COMPLETE"
                            # WHAT IF NOT ALL the header mdata was found in seqscape???
                            #submitted_file.file_metadata_status = ""
                        elif task_status == "FAILURE":
                            pass
                        
                    elif task_name == "serapis.tasks.ParseBAMHeaderTask":
                        print "TASK is PARSE BAM"
                        # Hmm on the success branch, there isn't much we can do with the HEADER itself
                        if task_status == "SUCCESS":
                            submitted_file.file_header_mdata_status = "COMPLETE"
                            #header = result[u'task_result']
                            
#                            
                            
                        elif task_status == "FAILURE":
                            print "STATUS OF THIS TASK IS FAILURE!!! "
                            submitted_file.file_header_mdata_status = "TOTALLY_MISSING"
                        
                        # HERE THINGS GET COMPLICATED...-> TREAT CASES WHERE 
                    else:
                        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!! ---TASK IS NOT DEFINED---!!!!!!!!!"
                    
                    print "FILE: ", submitted_file
                    #submitted_file.save()
                    print "SUBMISSION: ", submission.files_list
                    submission.save(validate=False)
                    
                    
#                        try:
#                           submission.validate()
#                        except ValidationError as e:
#                           print "VAlIDATION Error....", e
#                        
                    break
            async_result.forget()
         
        elif task_status == "FAILURE":
            pass
        else:   # status = pending or started or retry
            pass
Beispiel #44
0
def reset_async_result(id, backend="celery"):
    if backend == 'celery':
        res = AsyncResult(id)
        if res:
            res.forget()