Example #1
0
def submit_job(job_file,
               temp_dir,
               dbname,
               user_name,
               callback_url=None,
               foreign_calc_id=None,
               hazard_output_id=None,
               hazard_job_id=None,
               logfile=None):
    """
    Create a job object from the given job.ini file in the job directory
    and submit it to the job queue.
    """
    ini = os.path.join(temp_dir, job_file)
    job, exctype = safely_call(oq_engine.job_from_file,
                               (ini, user_name, DEFAULT_LOG_LEVEL, '',
                                hazard_output_id, hazard_job_id))
    if exctype:
        tasks.update_calculation(callback_url, status="failed", einfo=job)
        raise exctype(job)

    future = executor.submit(tasks.safely_call, tasks.run_calc, job.id,
                             temp_dir, callback_url, foreign_calc_id, dbname,
                             logfile)
    return job, future
Example #2
0
def run_calc(request, job_type):
    """
    Run a calculation.

    :param request:
        a `django.http.HttpRequest` object.
    :param job_type:
        string 'hazard' or 'risk'
    """
    callback_url = request.POST.get('callback_url')
    foreign_calc_id = request.POST.get('foreign_calculation_id')

    hazard_output_id = request.POST.get('hazard_output_id')
    hazard_job_id = request.POST.get('hazard_job_id')

    einfo, exctype = safely_call(
        _prepare_job, (request, hazard_output_id, hazard_job_id,
                       create_detect_job_file("job.ini", "job_risk.ini")))
    if exctype:
        tasks.update_calculation(callback_url, status="failed", einfo=einfo)
        raise exctype(einfo)
    else:
        job_file, temp_dir = einfo
    job, _fut = submit_job(job_file, temp_dir, request.POST['database'],
                           callback_url, foreign_calc_id,
                           hazard_output_id, hazard_job_id)
    try:
        response_data = _get_calc_info(job_type, job.calculation.id)
    except ObjectDoesNotExist:
        return HttpResponseNotFound()

    return HttpResponse(content=json.dumps(response_data), content_type=JSON)
Example #3
0
def submit_job(job_file, temp_dir, dbname,
               callback_url=None, foreign_calc_id=None,
               hazard_output_id=None, hazard_calculation_id=None,
               logfile=None):
    """
    Create a job object from the given job.ini file in the job directory
    and submit it to the job queue.
    """
    try:
        job = oq_engine.job_from_file(
            job_file, "platform", DEFAULT_LOG_LEVEL, [], hazard_output_id,
            hazard_calculation_id)
    except:  # catch errors in the job creation phase
        etype, exc, tb = sys.exc_info()
        einfo = "".join(traceback.format_tb(tb))
        einfo += '%s: %s' % (etype.__name__, exc)
        tasks.update_calculation(callback_url, status="failed", einfo=einfo)
        raise

    calc = job.calculation
    job_type = 'risk' if job.calculation is job.risk_calculation else 'hazard'
    future = executor.submit(
        tasks.run_calc, job_type, calc.id, temp_dir,
        callback_url, foreign_calc_id, dbname, logfile)
    return job, future
Example #4
0
def run_calc(request):
    """
    Run a calculation.

    :param request:
        a `django.http.HttpRequest` object.
    """
    callback_url = request.POST.get('callback_url')
    foreign_calc_id = request.POST.get('foreign_calculation_id')

    hazard_output_id = request.POST.get('hazard_output_id')
    hazard_job_id = request.POST.get('hazard_job_id')

    if hazard_output_id or hazard_job_id:
        candidates = ("job_risk.ini", "job.ini")
    else:
        candidates = ("job_hazard.ini", "job.ini")
    einfo, exctype, monitor = safely_call(
        _prepare_job, (request, hazard_output_id, hazard_job_id, candidates))
    if exctype:
        tasks.update_calculation(callback_url, status="failed", einfo=einfo)
        return HttpResponse(json.dumps(einfo.splitlines()),
                            content_type=JSON, status=500)
    if not einfo:
        msg = 'Could not find any file of the form %s' % str(candidates)
        logging.error(msg)
        return HttpResponse(content=json.dumps([msg]), content_type=JSON,
                            status=500)

    temp_dir = os.path.dirname(einfo[0])

    user = utils.get_user_data(request)

    try:
        job, _fut = submit_job(einfo[0], temp_dir, request.POST['database'],
                               user['name'], callback_url, foreign_calc_id,
                               hazard_output_id, hazard_job_id)
    except Exception as exc:  # no job created, for instance missing .xml file
        # get the exception message
        exc_msg = exc.args[0]
        if isinstance(exc_msg, bytes):
            exc_msg = exc_msg.decode('utf-8')   # make it a unicode object
        else:
            assert isinstance(exc_msg, unicode), exc_msg
        logging.error(exc_msg)
        response_data = exc_msg.splitlines()
        status = 500
    else:
        calc = oqe_models.OqJob.objects.get(pk=job.id)
        response_data = vars(calc.get_oqparam())
        response_data['job_id'] = job.id
        response_data['status'] = calc.status
        status = 200
    return HttpResponse(content=json.dumps(response_data), content_type=JSON,
                        status=status)
Example #5
0
def run_calc(request):
    """
    Run a calculation.

    :param request:
        a `django.http.HttpRequest` object.
    """
    callback_url = request.POST.get('callback_url')
    foreign_calc_id = request.POST.get('foreign_calculation_id')

    hazard_output_id = request.POST.get('hazard_output_id')
    hazard_job_id = request.POST.get('hazard_job_id')

    is_risk = hazard_output_id or hazard_job_id
    if is_risk:
        candidates = ("job_risk.ini", "job.ini")
    else:
        candidates = ("job_hazard.ini", "job.ini")
    einfo, exctype = safely_call(
        _prepare_job, (request, hazard_output_id, hazard_job_id, candidates))
    if exctype:
        tasks.update_calculation(callback_url, status="failed", einfo=einfo)
        return HttpResponse(json.dumps(einfo.splitlines()),
                            content_type=JSON,
                            status=500)
    if not einfo:
        msg = 'Could not find any file of the form %s' % str(candidates)
        logging.error(msg)
        return HttpResponse(content=json.dumps([msg]),
                            content_type=JSON,
                            status=500)

    temp_dir = os.path.dirname(einfo[0])

    user_name = utils.getusername(request)

    try:
        job, _fut = submit_job(einfo[0], temp_dir, request.POST['database'],
                               user_name, callback_url, foreign_calc_id,
                               hazard_output_id, hazard_job_id)
    except Exception as exc:  # no job created, for instance missing .xml file
        logging.error(exc)
        response_data = str(exc).splitlines()
        status = 500
    else:
        calc = oqe_models.OqJob.objects.get(pk=job.id)
        response_data = vars(calc.get_oqparam())
        response_data['job_id'] = job.id
        response_data['status'] = calc.status
        status = 200
    return HttpResponse(content=json.dumps(response_data),
                        content_type=JSON,
                        status=status)
Example #6
0
def submit_job(job_file, temp_dir, dbname,
               callback_url=None, foreign_calc_id=None,
               hazard_output_id=None, hazard_job_id=None,
               logfile=None):
    """
    Create a job object from the given job.ini file in the job directory
    and submit it to the job queue.
    """
    job, exctype = safely_call(
        oq_engine.job_from_file, (job_file, "platform", DEFAULT_LOG_LEVEL, [],
                                  hazard_output_id, hazard_job_id))
    if exctype:
        tasks.update_calculation(callback_url, status="failed", einfo=job)
        raise exctype(job)

    future = executor.submit(
        tasks.safely_call, tasks.run_calc, job.job_type, job.id, temp_dir,
        callback_url, foreign_calc_id, dbname, logfile)
    return job, future
Example #7
0
def run_calc(request):
    """
    Run a calculation.

    :param request:
        a `django.http.HttpRequest` object.
    """
    callback_url = request.POST.get('callback_url')
    foreign_calc_id = request.POST.get('foreign_calculation_id')

    hazard_output_id = request.POST.get('hazard_output_id')
    hazard_job_id = request.POST.get('hazard_job_id')

    is_risk = hazard_output_id or hazard_job_id
    if is_risk:
        detect_job_file = create_detect_job_file("job_risk.ini", "job.ini")
    else:
        detect_job_file = create_detect_job_file("job_hazard.ini", "job.ini")
    einfo, exctype = safely_call(
        _prepare_job, (request, hazard_output_id, hazard_job_id,
                       detect_job_file))
    if exctype:
        tasks.update_calculation(callback_url, status="failed", einfo=einfo)
        raise exctype(einfo)
    else:
        job_file, temp_dir = einfo
    job, _fut = submit_job(job_file, temp_dir, request.POST['database'],
                           callback_url, foreign_calc_id,
                           hazard_output_id, hazard_job_id)
    try:
        calc = oqe_models.OqJob.objects.get(pk=job.id)
        response_data = vars(calc.get_oqparam())
        response_data['status'] = calc.status
    except ObjectDoesNotExist:
        return HttpResponseNotFound()

    return HttpResponse(content=json.dumps(response_data), content_type=JSON)