示例#1
0
def async_call(celery_app, taskpath, taskname, **kwargs):
    """ send a task to the celery app with specified args
        without waiting for results.
    """
    try:
        full_task_path = "{0}.{1}".format(taskpath, taskname)
        return celery_app.send_task(full_task_path, **kwargs)
    except:
        raise IrmaTaskError("Celery error - {0}".format(taskname))
示例#2
0
def probe_list():
    """ send a task to the brain asking for active probe list """
    (retcode, res) = sync_call(brain_app, BRAIN_SCAN_TASKS, "probe_list",
                               timeout)
    if retcode != IrmaReturnCode.success:
        raise IrmaTaskError(res)
    if len(res) == 0:
        raise IrmaCoreError("no probe available")
    return res
示例#3
0
def sync_call(celery_app, taskpath, taskname, timeout, **kwargs):
    """ send a task to the celery app with specified args
        and wait until timeout param for result
    """
    try:
        full_task_path = "{0}.{1}".format(taskpath, taskname)
        task = celery_app.send_task(full_task_path, **kwargs)
        (status, res) = task.get(timeout=timeout)
        return (status, res)
    except celery.exceptions.TimeoutError:
        raise IrmaTaskError("Celery timeout - {0}".format(taskname))
示例#4
0
def async_call(celery_app, taskpath, taskname, **kwargs):
    """ send a task to the celery app with specified args
        without waiting for results.
    """
    try:
        log.debug("app: %s task: %s.%s", celery_app, taskpath, taskname)
        full_task_path = "{0}.{1}".format(taskpath, taskname)
        return celery_app.send_task(full_task_path, **kwargs)
    except Exception as e:
        log.exception(e)
        raise IrmaTaskError("Celery error - {0}".format(taskname))
示例#5
0
def mimetype_filter_scan_request(scan_request):
    """ send a task to the brain asking for mimetype filtering
        on probe list
    """
    (retcode, res) = sync_call(brain_app,
                               BRAIN_SCAN_TASKS,
                               "mimetype_filter_scan_request",
                               timeout,
                               args=[scan_request])
    if retcode != IrmaReturnCode.success:
        raise IrmaTaskError(res)
    return res
示例#6
0
def cancel(scan, session):
    """ cancel all remaining jobs for specified scan

    :param scanid: id returned by scan_new
    :rtype: dict of 'cancel_details': total':int, 'finished':int,
        'cancelled':int
    :return:
        informations about number of cancelled jobs by irma-brain
    :raise: IrmaDatabaseError, IrmaTaskError
    """
    log.debug("scanid: %s", scan.external_id)
    if scan.status < IrmaScanStatus.uploaded:
        # If not launched answer directly
        scan.set_status(IrmaScanStatus.cancelled)
        session.commit()
        return None

    if scan.status != IrmaScanStatus.launched:
        # If too late answer directly
        status_str = IrmaScanStatus.label[scan.status]
        if IrmaScanStatus.is_error(scan.status):
            # let the cancel finish and keep the error status
            return None
        else:
            reason = "can not cancel scan in {0} status".format(status_str)
            raise IrmaValueError(reason)

    # Else ask brain for job cancel
    (retcode, res) = celery_brain.scan_cancel(scan.external_id)
    if retcode == IrmaReturnCode.success:
        s_processed = IrmaScanStatus.label[IrmaScanStatus.processed]
        if 'cancel_details' in res:
            scan.set_status(IrmaScanStatus.cancelled)
            session.commit()
            return res['cancel_details']
        elif res['status'] == s_processed:
            # if scan is finished for the brain
            # it means we are just waiting for results
            scan.set_status(IrmaScanStatus.processed)
            session.commit()
        reason = "can not cancel scan in {0} status".format(res['status'])
        raise IrmaValueError(reason)
    else:
        raise IrmaTaskError(res)