job_status_service.py 2.18 KB
Newer Older
David Mendez's avatar
David Mendez committed
1 2 3
"""
Module that provides a service to get or modify the status or jobs
"""
4
from app.models import delayed_job_models
5 6
from app.job_status_daemon import meeseeks_launcher

David Mendez's avatar
David Mendez committed
7

8 9 10
class JobNotFoundError(Exception):
    """Base class for exceptions."""

11

12 13 14
class InputFileNotFoundError(Exception):
    """Base class for exceptions."""

15

16
def get_job_status(job_id, server_base_url='http://0.0.0.0:5000'):
David Mendez's avatar
David Mendez committed
17 18 19
    """
    Returns a dict representation of the job with the id given as parameter
    :param job_id: the id of the job for which the status is required
20
    :param server_base_url: url to use as base for building the output files urls
David Mendez's avatar
David Mendez committed
21 22
    :return: a dict with the public properties of a job.
    """
23 24

    try:
David Mendez's avatar
David Mendez committed
25

26
        job = delayed_job_models.get_job_by_id(job_id, force_refresh=True)
27
        meeseeks_launcher.summon_new_meeseeks_if_previous_died_prematurely(job)
28
        return job.public_dict(server_base_url)
29 30 31
    except delayed_job_models.JobNotFoundError:
        raise JobNotFoundError()

32

33 34 35 36 37 38 39 40 41 42 43 44 45
def get_input_file_path(job_id, input_key):
    """
    :param job_id: the id of the job for which the status is required
    :param input_key: input_key as saved in the database
    :return: the internal path of an input file to be used to be sent as a response
    """

    try:
        input_file = delayed_job_models.get_job_input_file(job_id, input_key)
        return input_file.internal_path
    except delayed_job_models.InputFileNotFoundError:
        raise InputFileNotFoundError()

46

47
def update_job_progress(job_id, progress, status_log, status_description):
David Mendez's avatar
David Mendez committed
48 49 50
    """
    Updates the status of the job wit the id given as parameter.
    :param job_id: job_id of the job to modify
David Mendez's avatar
David Mendez committed
51
    :param progress: the progress percentage of the job
52
    :param status_log: a message to append to the public job status log
David Mendez's avatar
David Mendez committed
53 54
    :return: a dict with the public properties of a job.
    """
55 56

    try:
57
        delayed_job_models.update_job_progress(job_id, progress, status_log, status_description)
David Mendez's avatar
David Mendez committed
58
        job = delayed_job_models.get_job_by_id(job_id)
59
        meeseeks_launcher.summon_new_meeseeks_if_previous_died_prematurely(job)
David Mendez's avatar
David Mendez committed
60
        return job.public_dict()
61 62
    except delayed_job_models.JobNotFoundError:
        raise JobNotFoundError()