def generate_job_id()

in pygenie/jobs/utils.py [0:0]


def generate_job_id(job_id, return_success=True, override_existing=False, conf=None):
    """
    Generate a new job id.

    By default, will continue to generate an id until either
    1) the generated id is for a job that is running or successful
    2) the generated id is completely new

    If return_success is False and override_existing is False, will continue to
    generate an id until either
    1) the generated id is for a job that is running
    2) the generated id is completely new

    If return_success is False and override_existing is True, will continue to
    generate an id until the generated id is completely new and kill the
    discovered running job(s)

    Args:
        job_id (str): The initial job id to start the generation process.
        return_success (bool, optional): If True, allows returning an id for a successful job.
            Defaults to True.
        override_existing (bool, optional): If True, will terminate any running jobs
            found during id generation. Defaults to False.
        conf (optional): Configuration settings to be used during job reattachment. Defaults to None.

    Returns:
        str: A valid job id that meets the specified conditions.

    Raises:
        ValueError: If both `return_success` and `override_existing` are set to True.
    """

    if return_success and override_existing:
        raise ValueError("return_success and override_existing cannot both be True")

    while True:
        try:
            running_job = reattach_job(job_id, conf=conf)
            logger.debug("job id '%s' exists with status '%s'",
                         job_id,
                         running_job.status)
            if not return_success and override_existing:
                if not running_job.is_done:
                    logger.warning("killing job id %s", job_id)
                    response = running_job.kill()
                    response.raise_for_status()
            elif not running_job.is_done \
                    or (running_job.is_done \
                        and running_job.is_successful \
                        and return_success):
                logger.debug("returning job id '%s' with status '%s'",
                            running_job.job_id,
                            running_job.status)
                return running_job.job_id
            id_parts = running_job.job_id.split('-')
            if id_parts[-1].isdigit():
                id_parts[-1] = str(int(id_parts[-1]) + 1)
            else:
                id_parts.append('1')
            job_id = '-'.join(id_parts)
            logger.debug("trying new job id '%s'", job_id)
        except GenieJobNotFoundError:
            logger.debug("returning new job id '%s'", job_id)
            return job_id