-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathprocessor_job.py
More file actions
152 lines (125 loc) · 4.9 KB
/
Copy pathprocessor_job.py
File metadata and controls
152 lines (125 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
##
# Contains ProcessorJobListView, ProcessorJobDetailView, and the needed serializer
##
from django.utils.decorators import method_decorator
from rest_framework import filters, generics, serializers
import boto3
from django_filters.rest_framework import DjangoFilterBackend
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from data_refinery_api.exceptions import InvalidFilters
from data_refinery_api.utils import check_filters
from data_refinery_common.logging import get_and_configure_logger
from data_refinery_common.models import ProcessorJob
from data_refinery_common.utils import get_env_variable
logger = get_and_configure_logger(__name__)
AWS_REGION = get_env_variable(
"AWS_REGION", "us-east-1"
) # Default to us-east-1 if the region variable can't be found
# Job definitons are AWS objects so they have to be namespaced for our stack.
JOB_DEFINITION_PREFIX = get_env_variable("JOB_DEFINITION_PREFIX", "")
batch = boto3.client("batch", region_name=AWS_REGION)
class ProcessorJobSerializer(serializers.ModelSerializer):
class Meta:
model = ProcessorJob
fields = (
"id",
"pipeline_applied",
"num_retries",
"retried",
"worker_id",
"ram_amount",
"volume_index",
"batch_job_queue",
"worker_version",
"failure_reason",
"batch_job_id",
"success",
"original_files",
"datasets",
"start_time",
"end_time",
"created_at",
"last_modified_at",
)
read_only_fields = fields
@method_decorator(
name="get",
decorator=swagger_auto_schema(
manual_parameters=[
openapi.Parameter(
name="sample_accession_code",
in_=openapi.IN_QUERY,
type=openapi.TYPE_STRING,
description="List the processor jobs associated with a sample",
),
]
),
)
class ProcessorJobListView(generics.ListAPIView):
"""
List of all ProcessorJobs.
"""
model = ProcessorJob
serializer_class = ProcessorJobSerializer
filter_backends = (
DjangoFilterBackend,
filters.OrderingFilter,
)
filterset_fields = ProcessorJobSerializer.Meta.fields
ordering_fields = ("id", "created_at")
ordering = ("-id",)
def list(self, request, *args, **kwargs):
response = super(ProcessorJobListView, self).list(request, args, kwargs)
results = response.data["results"]
batch_job_ids = [job["batch_job_id"] for job in results if job.get("batch_job_id", None)]
running_job_ids = set()
if batch_job_ids:
try:
described_jobs = batch.describe_jobs(jobs=batch_job_ids)
for job in described_jobs["jobs"]:
if job["status"] in ["SUBMITTED", "PENDING", "RUNNABLE", "STARTING", "RUNNING"]:
running_job_ids.add(job["jobId"])
except Exception as e:
logger.exception(f"Failure to query about batch_job_ids.")
for result in results:
batch_job_id = result.get("batch_job_id", None)
result["is_queued"] = bool(batch_job_id and batch_job_id in running_job_ids)
return response
def get_queryset(self):
invalid_filters = check_filters(self, ["sample_accession_code"])
if invalid_filters:
raise InvalidFilters(invalid_filters=invalid_filters)
queryset = ProcessorJob.objects.all()
sample_accession_code = self.request.query_params.get("sample_accession_code", None)
if sample_accession_code:
queryset = queryset.filter(
original_files__samples__accession_code=sample_accession_code
).distinct()
return queryset
class ProcessorJobDetailView(generics.RetrieveAPIView):
""" Retrieves a ProcessorJob by ID """
lookup_field = "id"
model = ProcessorJob
queryset = ProcessorJob.objects.all()
serializer_class = ProcessorJobSerializer
def get(self, request, *args, **kwargs):
response = super(ProcessorJobDetailView, self).get(request, args, kwargs)
if "batch_job_id" in response.data and response.data["batch_job_id"]:
try:
described_jobs = batch.describe_jobs(jobs=[response.data["batch_job_id"]])
response.data["is_queued"] = described_jobs["jobs"][0]["status"] in [
"SUBMITTED",
"PENDING",
"RUNNABLE",
"STARTING",
"RUNNING",
]
except Exception as e:
logger.exception(
f"Failure to query about batch_job_id.",
batch_job_id=response.data["batch_job_id"],
)
else:
response.data["is_queued"] = False
return response