Пример #1
0
def send_text():
    number = request.json['number']
    message = request.json['message']
    task = celery.send_task("tasks.text_message", args=[number, message])
    res = celery.AsyncResult(task.id)
    # TODO: handle background task failure
    return jsonify(res.state), 200
Пример #2
0
def taskstatus(task_id):
	task = celery.AsyncResult(task_id)
	if task.state == 'PENDING':
		response = {
			'state': task.state,
			'current': 0,
			'total': 1,
			'status': 'Pending...'
		}
	elif task.state != 'FAILURE':
	response = {
			'state': task.state,
			'current': task.info.get('current', 0),
			'total': task.info.get('total', 1),
			'status': task.info.get('status', '')
		}
		if 'result' in task.info:
			response['result'] = task.info['result']
	else:
		# something went wrong in the background job
		response = {
			'state': task.state,
			'current': 1,
			'total': 1,
			'status': str(task.info),  # this is the exception raised
		}
	return jsonify(response)
Пример #3
0
def check_task(task_id: str) -> str:
    res = celery.AsyncResult(task_id)
    if res.state == states.PENDING:
        return {"status": res.state}
    else:
        #Get requst

        if request.method == 'GET':
            result, arguments = res.result
            return {"result": result, "status": "ok"}
        # Post request
        elif request.method == 'POST':
            request_data = request.get_json()
            result, arguments = res.result
            if request_data and "adaptor" in request_data:
                adaptor = Adaptor()
                try:
                    transformed_result = adaptor.get_result(
                        request_data["adaptor"],
                        result=result,
                        arguments=arguments,
                        request_data=request_data)
                    return transformed_result
                except:
                    return "Adaptor Not Found", 400
            else:
                # Return the default result format
                return {"result": result, "status": "ok"}
Пример #4
0
async def check(id: str):
    task = celery.AsyncResult(id)
    if task.state == 'SUCCESS':
        response = {'status': task.state, 'result': task.result, 'task_id': id}
    else:
        response = {'status': task.state, 'result': task.info, 'task_id': id}
    return response
Пример #5
0
def check_task(task_id: str) -> str:
    app.logger.info('[route] /check/' + task_id)
    res = celery.AsyncResult(task_id)
    if res.state == states.PENDING:
        return res.state
    else:
        return str(res.result)
Пример #6
0
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)
Пример #7
0
def get_job(jobId):
    res = celery.AsyncResult(jobId)
    my_job = [job for job in jobs if job['id'] == jobId]
    if len(my_job) == 0:
        abort(404)

    if res.state == 'in-progress':
        my_job[0]['status'] = 'in-progres'
        if res.info.get('created'):
            my_job[0]['created'] = res.info.get('created')
        if res.info.get('pending'):
            my_job[0]['uploaded']['pending'] = res.info.get('pending')
        if res.info.get('completed') and res.info.get(
                'completed') not in my_job[0]['uploaded']['completed']:
            my_job[0]['uploaded']['completed'].append(
                res.info.get('completed'))
            pprint(res.info.get('completed'))
        return jsonify(my_job[0])
    elif res.state == 'failed':
        if res.info.get('error'):
            my_job[0]['uploaded']['failed'] = res.info.get('error')
    else:

        if res.info.get('completed') and res.info.get(
                'completed') not in my_job[0]['uploaded']['completed']:
            my_job[0]['uploaded']['completed'] = (res.info.get('completed'))
        if res.info.get('created'):
            my_job[0]['created'] = res.info.get('created')
        if res.info.get('error'):
            my_job[0]['uploaded']['failed'] = res.info.get('error')
        my_job[0]['uploaded']['pending'] = []
        my_job[0]['finished'] = res.info.get('finished')
        my_job[0]['status'] = res.info.get('status')
        return jsonify(my_job[0])
Пример #8
0
def status(task_id):
    """Get status for the task using task id."""
    task = celery.AsyncResult(task_id)
    if task.state == states.PENDING:
        response = {
            'state': task.state,
            'current': 0,
            'total': 1,
            'status': 'Pending...'
        }
    elif task.state == states.FAILURE:
        # something went wrong in the background job
        response = {
            'state': task.state,
            'current': 1,
            'total': 1,
            'status': str(task.info),  # this is the exception raised
        }
    else:
        # task is started
        response = {
            'state': task.state,
            'current': task.info.get('current', 0),
            'total': task.info.get('total', 1),
            'status': task.info.get('status', '')
        }
        if 'result' in task.info:
            response['result'] = task.info['result']

    return jsonify(response)
Пример #9
0
def check_task(id):
    res = celery.AsyncResult(id)
    if res.state == states.PENDING:
        return res.state
    else:
        db_client = MongoClient("mongodb://database:27017/")
        output_database = db_client.output_database
        output_collections = output_database.output_collections
        exists = output_collections.find_one({"_hash": str(res.result)})
        if exists is not None:
            file_db = db_client.file_db
            fs = gridfs.GridFS(file_db)
            for file_id in exists["ids"]:
                data = fs.get(file_id).read()
                data_array = np.fromstring(data, sep="\t")
                x = data_array[0::2]
                y = data_array[1::2]
                graph = create_graph(x, y)
                ids = ['graph-{}'.format(i) for i, _ in enumerate(graph)]
                # objects to their JSON equivalents
                graphJSON = json.dumps(graph,
                                       cls=plotly.utils.PlotlyJSONEncoder)

                return render_template('layouts/index.html',
                                       ids=ids,
                                       graphJSON=graphJSON)

        return str(res.result)
Пример #10
0
def task(task_id: str) -> str:
    task = celery.AsyncResult(task_id)
    message = states.PENDING
    if task.state != message:
        message = str(task.result)

    return jsonify(status='ok', message=message)
Пример #11
0
def check_task(task_id: str) -> str:
    res = celery.AsyncResult(task_id)
    # res = celery.AsynchronousResult(task_id)
    if res.state == states.PENDING:
        return res.state
    else:
        return str(res.result)
Пример #12
0
async def check_task(task_id: str) -> JSONResponse:
    res = celery.AsyncResult(task_id)
    if res.state == states.PENDING:
        answer = res.state
    else:
        answer = res.result
    return JSONResponse(content=jsonable_encoder(answer))
Пример #13
0
def send_email():
    email = request.json['email']
    subject = request.json['subject']
    message = request.json['message']
    task = celery.send_task("tasks.email", args=[email, subject, message])
    res = celery.AsyncResult(task.id)
    # TODO: handle background task failure
    return jsonify(res.state), 200
Пример #14
0
def info_task(task_id):
    """
    :type task_id: str
    :rtype: Resp
    """

    res = celery.AsyncResult(task_id)
    msg = {'status': res.state, 'result': res.result}
    return Resp().set_status(200).set_msg(msg).to_json()
Пример #15
0
def cancel_task(task_id):
    """
    :type task_id: str
    :rtype: Resp
    """

    celery.control.revoke(task_id, terminate=True)
    res = celery.AsyncResult(task_id)
    msg = {'status': res.state, 'result': res.result}
    return Resp().set_status(200).set_msg(msg).to_json()
Пример #16
0
def check_task(task_id: str) -> str:
    # res is what you get back from task.segment
    res = celery.AsyncResult(task_id)
    if res.state == states.PENDING:
        print("this is res.stat", res.state)
        return res.state
    else:
        sep = '?'
        trunc_res = str(res.result).split(sep, 1)[0].replace(sep, "")
        return trunc_res
Пример #17
0
def check_task(task_id):
    res = celery.AsyncResult(task_id)
    if res.state == states.PENDING:
        state = "Model is Training"
    else:
        state = "Ready to Use"
    to_return = {'status': state}

    return flask.Response(response=json.dumps(to_return),
                          status=200,
                          mimetype='text/plain')
Пример #18
0
def get_status(job_id):
    status = {'success': 0, 'inprogress': 0}
    if not r.exists(job_id):
        return make_response("Job ID given doesnt exist %s \n" % job_id, 404)
    task_ids = r.hgetall(job_id)
    for task_id in task_ids.keys():
        res = celery.AsyncResult(task_id)
        if res.state == states.PENDING:
            status['inprogress'] += 1
        if res.state == states.SUCCESS:
            status['success'] += 1
    return make_response(jsonify({'id': job_id, 'status': status}), 200)
Пример #19
0
def taskstatus(task_id):
    task = celery.AsyncResult(task_id)
    if task.info:
        file = str(task.info)
    else:
        file = ""

    return jsonify({
        'state': task.state,
        'info': task.info,
        'result_url': url_for('download_file', file=file)
    }), 200
    def on_get(self, req, resp, **kwargs):
        if kwargs['task_id'] is not None:
            task_id = kwargs['task_id']
            res = celery.AsyncResult(task_id)

            if res.state == states.PENDING:
                msg = {'state': res.state}
            else:
                msg = {'state': res.state, 'result': res.result}
        else:
            msg = {"message": "no valid format."}
        resp.body = json.dumps(msg)
Пример #21
0
 def update(context):
     bot = context.bot
     job = context.job
     res = celery.AsyncResult(task_id)
     if res.state == states.PENDING:
         bot.send_message(chat_id=chat_id, text="Waiting..")
     else:
         output_file = res.result
         bot.send_message(chat_id=chat_id,
                          text=f"Done (session={session_id})!")
         bot.send_document(chat_id=chat_id,
                           document=open(output_file, "rb"))
         job.schedule_removal()
Пример #22
0
def check_task(id: str):
    task = celery.AsyncResult(id)
    if task.state == 'SUCCESS':
        response = {'status': task.state, 'result': task.result, 'task_id': id}
    elif task.state == 'FAILURE':
        response = json.loads(
            task.backend.get(task.backend.get_key_for_task(
                task.id)).decode('utf-8'))
        del response['children']
        del response['traceback']
    else:
        response = {'status': task.state, 'result': task.info, 'task_id': id}
    return response
Пример #23
0
def check():
    res = celery.AsyncResult(request.args.get('id'))
    if res.state == states.PENDING:
        return 'task not found', 404
    if res.state == states.FAILURE:
        return 'error', 500
    elif res.state == states.STARTED:
        return jsonify({'status': res.state})
    else:
        return jsonify({
            'md5': res.result[0],
            'status': res.state,
            'url': res.result[1]
        })
Пример #24
0
def get_results(job_id):
    results = {}
    if not r.exists(job_id):
        return make_response("Job ID given doesnt exist %s \n" % job_id, 404)
    task_ids = r.hgetall(job_id)
    for task_id in task_ids.keys():
        result = []
        res = celery.AsyncResult(task_id)
        if res.state == states.PENDING:
            result = []
        if res.state == states.SUCCESS:
            result = res.result
        results[task_ids[task_id]] = result
    return make_response(jsonify({'id': job_id, 'results': results}), 200)
Пример #25
0
def check_task(task_id: str) -> str:
    res = celery.AsyncResult(task_id)
    status = res.state
    link = None
    errorMessage = None
    expiresOn = None
    expiresIn = None
    images = []
    if res.state == states.SUCCESS:
        if directory_exists(task_id):
            link = url_for('routes.download_file',
                           task_id=task_id,
                           _external=True,
                           _scheme='https')
            result = json.loads(res.result)
            expiresIn = result['expires_in']
            expiresOn = result['expires_on']
            images = get_images(task_id)
        else:
            status = 'EXPIRED'
            result = json.loads(res.result)
            expiresIn = result['expires_in']
            expiresOn = result['expires_on']

    if res.state == states.PENDING:
        if directory_exists(task_id):
            directory = os.path.join(app.config['RESULTS_FOLDER'], folder_id)
            zip_filename = get_filename(directory,
                                        app.config['OUTPUT_FILE_EXTENSION'])
            if zip_filename != '':
                status = SUCCESS
                link = url_for('routes.download_file',
                               task_id=task_id,
                               _external=True,
                               _scheme='https')
        else:
            status = 'EXPIRED'

    if res.state == states.FAILURE:
        errorMessage = str(res.result)

    return make_response(
        jsonify(id=task_id,
                status=status,
                link=link,
                expiresIn=expiresIn,
                expiresOn=expiresOn,
                errorMessage=errorMessage,
                images=images), 200)
Пример #26
0
def check_task(task_id: str) -> str:
    res = celery.AsyncResult(task_id)

    result = {
        'id': task_id,
        'status': None,
        'result': None,
        'refused_policy': None,
        'amount': None,
        'terms': None
    }

    if res.state == states.PENDING:
        pending_result = fill_result(result, {'status': 'processing'})

        return jsonify(pending_result)
    else:
        final_result = fill_result(result, res.result)

        return jsonify(final_result)
Пример #27
0
def stop_pipeline():
    engine = create_engine(DATABASE_URI)
    Session = sessionmaker(bind=engine)

    session = Session()
    tasks = session.query(Task).all()
    session.close()
    pipeline_progress = [-1 for i in range(len(tasks))]
    for task in tasks:
        task_id = task.celery_task_uid
        if task_id:
            res = celery.AsyncResult(task_id)
            res.revoke(terminate=True)

    Base.metadata.drop_all(engine, checkfirst=True)
    CeleryTask.__table__.drop(engine, checkfirst=True)
    CeleryTask.__table__.create(engine, checkfirst=True)
    Base.metadata.create_all(engine)
    session.commit()
    session.close()
    return "Deleted"
Пример #28
0
def add(param1, param2):
    #clientId = request.sid
    task = celery.send_task('mytasks.add', args=[param1, param2], kwargs={})

    # initiate progress
    socketio.emit('progress', {'status': 10}, namespace='/cruncher')
    time.sleep(1)

    # check progress
    res = celery.AsyncResult(task.id)
    if res.state == states.PENDING:
        socketio.emit('progress', {'status': 50}, namespace='/cruncher')
    time.sleep(2)
    #clientId.socketio.emit('progress', {'status': 100, 'result': str(res.result)}, namespace='/cruncher')
    socketio.emit('progress', {
        'status': 100,
        'result': str(res.result)
    },
                  namespace='/cruncher')
    return jsonify(task_id=task.id,
                   status_url=url_for('main.check_task',
                                      id=task.id,
                                      _external=True))
Пример #29
0
def checkStatus(task_id):

    task = celery.AsyncResult(task_id)

    status = task.info.get('output')

    if status != 'FAILURE':
        response = {'state': status, 'client': task.info.get('client')}

        if 'isAuthValid' in task.info:
            response['isAuthValid'] = task.info.get('isAuthValid', False)
    else:
        # something went wrong in the background job
        response = {
            'state': 'FAILURE',
            'client': task.info.get('client'),
            'msg': task.info.get('msg', ''),
            'isAuthValid': False  # Invalid authentification
        }

    if status != 'PROGRESS':  # If task is finished, clear results
        task.forget()

    return jsonify(response)
Пример #30
0
def add(param1: int, param2: int) -> str:
    task = celery.send_task('tasks.add', args=[param1, param2], kwargs={})
    get_result = celery.AsyncResult(task.task_id)
    return jsonify({'status': 200, 'data': get_result.get(timeout=10)})