summaryrefslogtreecommitdiff
path: root/src/buildstream/_state.py
blob: 0233dd32339b4efbfb4d831c01542f4086ae774b (plain)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#
#  Copyright (C) 2019 Bloomberg Finance LP
#
#  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/>.
#

import datetime
from collections import OrderedDict


# TaskGroup
#
# The state data stored for a group of tasks (usually scheduler queues)
#
# Args:
#    name (str): The name of the Task Group, e.g. 'build'
#    state (State): The state object
#    complete_name (str): Optional name for frontend status rendering, e.g. 'built'
#
class TaskGroup:
    def __init__(self, name, state, complete_name=None):
        self.name = name
        self.complete_name = complete_name
        self.processed_tasks = 0
        self.skipped_tasks = 0
        # NOTE: failed_tasks is a list of strings instead of an integer count
        #       because the frontend requires the full list of failed tasks to
        #       know whether to print failure messages for a given element.
        self.failed_tasks = []

        self._state = state
        self._update_task_group_cbs = []

    ###########################################
    # Core-facing APIs to drive notifications #
    ###########################################

    # add_processed_task()
    #
    # Update the TaskGroup's count of processed tasks and notify of changes
    #
    # This is a core-facing API and should not be called from the frontend
    #
    def add_processed_task(self):
        self.processed_tasks += 1
        for cb in self._state._task_groups_changed_cbs:
            cb()

    # add_skipped_task()
    #
    # Update the TaskGroup's count of skipped tasks and notify of changes
    #
    # This is a core-facing API and should not be called from the frontend
    #
    def add_skipped_task(self):
        self.skipped_tasks += 1

        for cb in self._state._task_groups_changed_cbs:
            cb()

    # add_failed_task()
    #
    # Update the TaskGroup's list of failed tasks and notify of changes
    #
    # Args:
    #    full_name (str): The full name of the task, distinguishing
    #                     it from other tasks with the same action name
    #                     e.g. an element's name.
    #
    # This is a core-facing API and should not be called from the frontend
    #
    def add_failed_task(self, full_name):
        self.failed_tasks.append(full_name)

        for cb in self._state._task_groups_changed_cbs:
            cb()


# State
#
# The state data that is stored for the purpose of sharing with the frontend.
#
# BuildStream's Core is responsible for making changes to this data.
# BuildStream's Frontend may register callbacks with State to be notified
# when parts of State change, and read State to know what has changed.
#
# Args:
#    session_start (datetime): The time the session started
#
class State:
    def __init__(self, session_start):
        self._session_start = session_start

        self.task_groups = OrderedDict()  # key is TaskGroup name

        # Note: A Task's full_name is technically unique, but only accidentally.
        self.tasks = OrderedDict()  # key is a tuple of action_name and full_name

        self._task_added_cbs = []
        self._task_removed_cbs = []
        self._task_changed_cbs = []
        self._task_groups_changed_cbs = []
        self._task_failed_cbs = []
        self._task_retry_cbs = []

    #####################################
    # Frontend-facing notification APIs #
    #####################################

    # register_task_added_callback()
    #
    # Registers a callback to be notified when a task is added
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    # Callback Args:
    #    task_id (str): The unique identifier of the task
    #
    def register_task_added_callback(self, callback):
        self._task_added_cbs.append(callback)

    # unregister_task_added_callback()
    #
    # Unregisters a callback previously registered by
    # register_task_added_callback()
    #
    # Args:
    #    callback (function): The callback to be removed
    #
    def unregister_task_added_callback(self, callback):
        self._task_added_cbs.remove(callback)

    # register_task_removed_callback()
    #
    # Registers a callback to be notified when a task is removed
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    # Callback Args:
    #    task_id (str): The unique identifier of the task
    #
    def register_task_removed_callback(self, callback):
        self._task_removed_cbs.append(callback)

    # unregister_task_removed_callback()
    #
    # Unregisters a callback previously registered by
    # register_task_removed_callback()
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    def unregister_task_removed_callback(self, callback):
        self._task_removed_cbs.remove(callback)

    # register_task_changed_callback()
    #
    # Register a callback to be notified when a task has changed
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    # Callback Args:
    #    task_id (str): The unique identifier of the task
    #
    def register_task_changed_callback(self, callback):
        self._task_changed_cbs.append(callback)

    # unregister_task_changed_callback()
    #
    # Unregisters a callback previously registered by
    # register_task_changed_callback()
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    def unregister_task_changed_callback(self, callback):
        self._task_changed_cbs.remove(callback)

    # register_task_failed_callback()
    #
    # Registers a callback to be notified when a task has failed
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    # Callback Args:
    #    task_id (str): The unique identifier of the task
    #    element (tuple): (optionally) The element unique_id and display keys if an
    #                                  element job
    #
    def register_task_failed_callback(self, callback):
        self._task_failed_cbs.append(callback)

    # unregister_task_failed_callback()
    #
    # Unregisters a callback previously registered by
    # register_task_failed_callback()
    #
    # Args:
    #    callback (function): The callback to be removed
    #
    def unregister_task_failed_callback(self, callback):
        self._task_failed_cbs.remove(callback)

    # register_task_retry_callback()
    #
    # Registers a callback to be notified when a task is to be retried
    #
    # Args:
    #    callback (function): The callback to be notified
    #
    # Callback Args:
    #    task_id (str): The unique identifier of the task
    #    unique_id: The unique id of the plugin instance to look up
    #
    def register_task_retry_callback(self, callback):
        self._task_retry_cbs.append(callback)

    ##############################################
    # Core-facing APIs for driving notifications #
    ##############################################

    # add_task_group()
    #
    # Notification that a new task group has been added
    #
    # This is a core-facing API and should not be called from the frontend
    #
    # Args:
    #    name (str): The name of the task group, e.g. 'build'
    #    complete_name (str): Optional name to be used for frontend status rendering, e.g. 'built'
    #
    # Returns:
    #    TaskGroup: The task group created
    #
    def add_task_group(self, name, complete_name=None):
        assert name not in self.task_groups, "Trying to add task group '{}' to '{}'".format(name, self.task_groups)
        group = TaskGroup(name, self, complete_name)
        self.task_groups[name] = group

        return group

    # remove_task_group()
    #
    # Notification that a task group has been removed
    #
    # This is a core-facing API and should not be called from the frontend
    #
    # Args:
    #    name (str): The name of the task group, e.g. 'build'
    #
    def remove_task_group(self, name):
        # Rely on 'del' to raise an error when removing nonexistent task groups
        del self.task_groups[name]

    # add_task()
    #
    # Add a task and send appropriate notifications
    #
    # This is a core-facing API and should not be called from the frontend
    #
    # Args:
    #    task_id (str): The unique identifier of the task
    #    action_name (str): The name of the action, e.g. 'build'
    #    full_name (str): The full name of the task, distinguishing
    #                     it from other tasks with the same action name
    #                     e.g. an element's name.
    #    elapsed_offset (timedelta): (Optional) The time the task started, relative
    #                                to buildstream's start time. Note scheduler tasks
    #                                use this as they don't report relative to wallclock time
    #                                if the Scheduler has been suspended.
    #
    def add_task(self, task_id, action_name, full_name, elapsed_offset=None):
        assert task_id not in self.tasks, "Trying to add task '{}:{}' with ID '{}' to '{}'".format(
            action_name, full_name, task_id, self.tasks
        )

        if not elapsed_offset:
            elapsed_offset = self.elapsed_time()

        task = Task(self, task_id, action_name, full_name, elapsed_offset)
        self.tasks[task_id] = task

        for cb in self._task_added_cbs:
            cb(task_id)

        return task

    # remove_task()
    #
    # Remove the task and send appropriate notifications
    #
    # This is a core-facing API and should not be called from the frontend
    #
    # Args:
    #    task_id (str): The unique identifier of the task
    #
    def remove_task(self, task_id):
        # Rely on 'del' to raise an error when removing nonexistent tasks
        del self.tasks[task_id]

        for cb in self._task_removed_cbs:
            cb(task_id)

    # fail_task()
    #
    # Notify all registered callbacks that a task has failed.
    #
    # This is separate from the tasks changed callbacks because a failed task
    # requires the frontend to intervene to decide what happens next.
    #
    # This is a core-facing API and should not be called from the frontend
    #
    # Args:
    #    task_id (str): The unique identifier of the task
    #    element (tuple): (optionally) The element unique_id and display keys if an
    #                                  element job
    #
    def fail_task(self, task_id, element=None):
        for cb in self._task_failed_cbs:
            cb(task_id, element)

    # retry_task()
    #
    # Notify all registered callbacks that a task is to be retried.
    #
    # This is a core-facing API and should not be called from the frontend
    #
    # Args:
    #    task_id (str): The unique identifier of the task
    #    unique_id: The unique id of the plugin instance to look up
    #
    def retry_task(self, task_id: str, unique_id: str) -> None:
        for cb in self._task_retry_cbs:
            cb(task_id, unique_id)

    # elapsed_time()
    #
    # Fetches the current session elapsed time
    #
    # Args:
    #    start_time(time): Optional explicit start time, relative to caller.
    #
    # Returns:
    #    (timedelta): The amount of time since the start of the session,
    #                 discounting any time spent while jobs were suspended if
    #                 start_time given relative to the Scheduler
    #
    def elapsed_time(self, start_time=None):
        time_now = datetime.datetime.now()
        if start_time is None:
            start_time = self._session_start or time_now
        return time_now - start_time

    # offset_start_time()
    #
    # Update the 'start' time of the application by a given offset
    #
    # This allows modifying the time spent building when BuildStream
    # gets paused then restarted, to give an accurate view of the real
    # time spend building.
    #
    # Args:
    #   offset: the offset to add to the start time
    #
    def offset_start_time(self, offset: datetime.timedelta) -> None:
        self._session_start += offset


# Task
#
# The state data stored for an individual task
#
# Args:
#    state (State): The State object
#    task_id (str): The unique identifier of the task
#    action_name (str): The name of the action, e.g. 'build'
#    full_name (str): The full name of the task, distinguishing
#                     it from other tasks with the same action name
#                     e.g. an element's name.
#    elapsed_offset (timedelta): The time the task started, relative to
#                                buildstream's start time.
class Task:
    def __init__(self, state, task_id, action_name, full_name, elapsed_offset):
        self._state = state
        self.id = task_id
        self.action_name = action_name
        self.full_name = full_name
        self.elapsed_offset = elapsed_offset
        self.current_progress = None
        self.maximum_progress = None

        self._render_cb = None  # Callback to call when something could be rendered

    # set_render_cb()
    #
    # Sets the callback to be called when the Task has changed and should be rendered
    #
    # NOTE: This should probably be removed once the frontend is running
    #       separately from the scheduler, since renders could be triggered
    #       by the scheduler.
    def set_render_cb(self, callback):
        self._render_cb = callback

    def set_current_progress(self, progress):
        self.current_progress = progress
        for cb in self._state._task_changed_cbs:
            cb(self.id)
        if self._render_cb:
            self._render_cb()

    def set_maximum_progress(self, progress):
        self.maximum_progress = progress
        for cb in self._state._task_changed_cbs:
            cb(self.id)

        if self._render_cb:
            self._render_cb()

    def add_current_progress(self):
        if self.current_progress is None:
            new_progress = 1
        else:
            new_progress = self.current_progress + 1
        self.set_current_progress(new_progress)