Esempio n. 1
0
def prepare_job(user_name="openquake"):
    """Create job for the given user, return it."""
    # See if the current user exists
    # If not, create a record for them
    owner = prepare_user(user_name)
    job = OqJob(owner=owner)
    job.save()
    return job
Esempio n. 2
0
def _job_from_file(config_file,
                   output_type,
                   owner_username='******',
                   force_inputs=False):
    """
    Create a job from external configuration files.

    NOTE: This function is deprecated. Please use
    :function:`openquake.engine.import_job_profile`.

    :param config_file:
        The external configuration file path
    :param output_type:
        Where to store results:
        * 'db' database
        * 'xml' XML files *plus* database
    :param owner_username:
        oq_user.user_name which defines the owner of all DB artifacts created
        by this function.
    :param bool force_inputs: If `True` the model input files will be parsed
        and the resulting content written to the database no matter what.
    """

    # output_type can be set, in addition to 'db' and 'xml', also to
    # 'xml_without_db', which has the effect of serializing only to xml
    # without requiring a database at all.
    # This allows to run tests without requiring a database.
    # This is not documented in the public interface because it is
    # essentially a detail of our current tests and ci infrastructure.
    assert output_type in ('db', 'xml')

    params, sections = _parse_config_file(config_file)
    params, sections = _prepare_config_parameters(params, sections)

    validator = jobconf.default_validators(sections, params)
    is_valid, errors = validator.is_valid()

    if not is_valid:
        raise jobconf.ValidationException(errors)

    owner = OqUser.objects.get(user_name=owner_username)
    # openquake-server creates the job record in advance and stores
    # the calculation id in the config file
    job_id = params.get('OPENQUAKE_JOB_ID')
    if not job_id:
        # create the database record for this job
        job = OqJob(owner=owner, path=None)
        job.save()
        job_id = job.id
    else:
        job = OqJob.objects.get(job_id)

    job_profile = _prepare_job(params, sections, owner_username, job,
                               force_inputs)

    if output_type == 'db':
        serialize_results_to = ['db']
    else:
        serialize_results_to = ['db', 'xml']

    base_path = params['BASE_PATH']

    job_ctxt = JobContext(params,
                          job_id,
                          sections=sections,
                          base_path=base_path,
                          serialize_results_to=serialize_results_to,
                          oq_job=job,
                          oq_job_profile=job_profile)
    job_ctxt.to_kvs()

    return job_ctxt