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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#!/usr/bin/env python3
#
# Copyright (C) 2016 Codethink Limited
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
# Jürg Billeter <juerg.billeter@codethink.co.uk>
# System imports
from collections import deque
from enum import Enum
import traceback
# Local imports
from .job import Job
# BuildStream toplevel imports
from .._exceptions import BstError, set_last_task_error
from .._message import Message, MessageType
# Indicates the kind of activity
#
#
class QueueType():
# Tasks which download stuff from the internet
FETCH = 1
# CPU/Disk intensive tasks
BUILD = 2
# Tasks which upload stuff to the internet
PUSH = 3
# Queue status for a given element
#
#
class QueueStatus(Enum):
# The element is waiting for dependencies.
WAIT = 1
# The element can skip this queue.
SKIP = 2
# The element is ready for processing in this queue.
READY = 3
# Queue()
#
#
class Queue():
# These should be overridden on class data of of concrete Queue implementations
action_name = None
complete_name = None
queue_type = None
def __init__(self):
self.scheduler = None
self.wait_queue = deque()
self.done_queue = deque()
self.active_jobs = []
self.max_retries = 0
# For the frontend to know how many elements
# were successfully processed, failed, or skipped
# as they did not require processing.
#
self.failed_elements = []
self.processed_elements = []
self.skipped_elements = []
# Assert the subclass has setup class data
assert self.action_name is not None
assert self.complete_name is not None
assert self.queue_type is not None
#####################################################
# Abstract Methods for Queue implementations #
#####################################################
# process()
#
# Abstract method for processing an element
#
# Args:
# element (Element): An element to process
#
# Returns:
# (any): An optional something to be returned
# for every element successfully processed
#
#
def process(self, element):
pass
# status()
#
# Abstract method for reporting the status of an element.
#
# Args:
# element (Element): An element to process
#
# Returns:
# (QueueStatus): The element status
#
def status(self, element):
return QueueStatus.READY
# prepare()
#
# Abstract method for handling job preparation in the main process.
#
# Args:
# element (Element): The element which is scheduled
#
def prepare(self, element):
pass
# done()
#
# Abstract method for handling a successful job completion.
#
# Args:
# element (Element): The element which completed processing
# result (any): The return value of the process() implementation
# success (bool): True if the process() implementation did not
# raise any exception
#
# Returns:
# (bool): True if the element should appear to be processsed,
# Otherwise False will count the element as "skipped"
#
def done(self, element, result, success):
pass
#####################################################
# Queue internals and Scheduler facing APIs #
#####################################################
# Attach to the scheduler
def attach(self, scheduler):
self.scheduler = scheduler
if self.queue_type == QueueType.FETCH or self.queue_type == QueueType.PUSH:
self.max_retries = scheduler.context.sched_network_retries
def enqueue(self, elts):
if not elts:
return
# Place skipped elements directly on the done queue
elts = list(elts)
skip = [elt for elt in elts if self.status(elt) == QueueStatus.SKIP]
wait = [elt for elt in elts if elt not in skip]
self.wait_queue.extend(wait)
self.done_queue.extend(skip)
self.skipped_elements.extend(skip)
def dequeue(self):
while self.done_queue:
yield self.done_queue.popleft()
def dequeue_ready(self):
return any(self.done_queue)
def process_ready(self):
scheduler = self.scheduler
unready = []
while self.wait_queue and scheduler.get_job_token(self.queue_type):
element = self.wait_queue.popleft()
status = self.status(element)
if status == QueueStatus.WAIT:
scheduler.put_job_token(self.queue_type)
unready.append(element)
continue
elif status == QueueStatus.SKIP:
scheduler.put_job_token(self.queue_type)
self.done_queue.append(element)
self.skipped_elements.append(element)
continue
self.prepare(element)
job = Job(scheduler, element, self.action_name,
self.process, self.job_done,
max_retries=self.max_retries)
scheduler.job_starting(job)
job.spawn()
self.active_jobs.append(job)
# These were not ready but were in the beginning, give em
# first priority again next time around
self.wait_queue.extendleft(unready)
def update_workspaces(self, element, job):
# Handle any workspace modifications now
#
if job.workspace_dict:
project = element._get_project()
if project.workspaces.update_workspace(element.name, job.workspace_dict):
try:
project.workspaces.save_config()
except BstError as e:
self.message(element, MessageType.ERROR, "Error saving workspaces", detail=str(e))
except Exception as e: # pylint: disable=broad-except
self.message(element, MessageType.BUG,
"Unhandled exception while saving workspaces",
detail=traceback.format_exc())
def job_done(self, job, element, success, result):
# Remove from our jobs
self.active_jobs.remove(job)
# Update workspaces in the main task before calling any queue implementation
self.update_workspaces(element, job)
# Give the result of the job to the Queue implementor,
# and determine if it should be considered as processed
# or skipped.
try:
processed = self.done(element, result, success)
except BstError as e:
# Report error and mark as failed
#
self.message(element, MessageType.ERROR, "Post processing error", detail=str(e))
self.failed_elements.append(element)
# Treat this as a task error as it's related to a task
# even though it did not occur in the task context
#
# This just allows us stronger testing capability
#
set_last_task_error(e.domain, e.reason)
except Exception as e: # pylint: disable=broad-except
# Report unhandled exceptions and mark as failed
#
self.message(element, MessageType.BUG,
"Unhandled exception in post processing",
detail=traceback.format_exc())
self.failed_elements.append(element)
else:
# No exception occured, handle the success/failure state in the normal way
#
if success:
self.done_queue.append(element)
if processed:
self.processed_elements.append(element)
else:
self.skipped_elements.append(element)
else:
self.failed_elements.append(element)
# Give the token for this job back to the scheduler
# immediately before invoking another round of scheduling
self.scheduler.put_job_token(self.queue_type)
# Notify frontend
self.scheduler.job_completed(self, job, success)
self.scheduler.sched()
# Convenience wrapper for Queue implementations to send
# a message for the element they are processing
def message(self, element, message_type, brief, **kwargs):
context = element._get_context()
message = Message(element._get_unique_id(), message_type, brief, **kwargs)
context.message(message)
|