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 Exception: raise IrmaTaskError("Celery error - {0}".format(taskname))
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))
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(type(e).__name__ + " : " + str(e)) raise IrmaTaskError("Celery error - {0}".format(taskname))
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: log.debug("app: %s task: %s.%s timeout:%s", celery_app, taskpath, taskname, timeout) 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 as e: log.exception(type(e).__name__ + " : " + str(e)) raise IrmaTaskError("Celery timeout - {0}".format(taskname))
def cancel(scan, session): """ cancel all remaining jobs for specified scan :param scan_id: 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("scan %s: cancel", 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) log.error("scan %s: %s", scan.external_id, reason) 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']) log.error("scan %s: %s", scan.external_id, reason) raise IrmaValueError(reason) else: raise IrmaTaskError(res)