job_status_service.py 2.03 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
import os
David Mendez's avatar
David Mendez committed
5

6
from flask import url_for
7

8
from app.models import delayed_job_models
David Mendez's avatar
David Mendez committed
9

10
11
12
13
14

class JobNotFoundError(Exception):
    """Base class for exceptions."""


David Mendez's avatar
David Mendez committed
15
16
17
18
19
20
def get_job_status(job_id):
    """
    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
    :return: a dict with the public properties of a job.
    """
21
22

    try:
David Mendez's avatar
David Mendez committed
23
        job = delayed_job_models.get_job_by_id(job_id)
24
25
26
27
28
        return job.public_dict()
    except delayed_job_models.JobNotFoundError:
        raise JobNotFoundError()


David Mendez's avatar
David Mendez committed
29
30
31
32
33
34
35
def update_job_status(job_id, new_data):
    """
    Updates the status of the job wit the id given as parameter.
    :param job_id: job_id of the job to modify
    :param new_data: a dict with the properties and their new values
    :return: a dict with the public properties of a job.
    """
36
37

    try:
David Mendez's avatar
David Mendez committed
38
        return delayed_job_models.update_job_status(job_id, new_data)
39
40
    except delayed_job_models.JobNotFoundError:
        raise JobNotFoundError()
41
42


David Mendez's avatar
David Mendez committed
43
44
45
46
47
48
49
def save_uploaded_file(job_id, file):
    """
    Receives an uploaded file and saves it in the static dir, it saves the local and public urls on the job.
    :param job_id: job_id of the job to modify
    :param file: flask file received
    :return: a dict with the result of the process.
    """
50
51

    try:
David Mendez's avatar
David Mendez committed
52
        job = delayed_job_models.get_job_by_id(job_id)
53
        output_dir_path = job.output_dir_path
54
        os.makedirs(output_dir_path, exist_ok=True)
55
56
57
        output_file_path = os.path.join(output_dir_path, file.filename)
        file.save(output_file_path)
        job.output_file_path = output_file_path
58
59
        output_file_url = url_for('static', filename=f'jobs_output/{job.id}/{file.filename}')
        job.output_file_url = output_file_url
60
61
62
63
64
65
        delayed_job_models.save_job(job)
        return {
            'result': 'File received successfully'
        }
    except delayed_job_models.JobNotFoundError:
        raise JobNotFoundError()