summaryrefslogtreecommitdiff
path: root/src/buildstream/_scheduler/queues/fetchqueue.py
blob: 4f38f377aac625a32048801f8ea907c884609d61 (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
#
#  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>

# BuildStream toplevel imports
from ... import Consistency

# Local imports
from . import Queue, QueueStatus
from ..resources import ResourceType
from ..jobs import JobStatus


# A queue which fetches element sources
#
class FetchQueue(Queue):

    action_name = "Fetch"
    complete_name = "Sources Fetched"
    resources = [ResourceType.DOWNLOAD]

    def __init__(self, scheduler, skip_cached=False, fetch_original=False):
        super().__init__(scheduler)

        self._skip_cached = skip_cached
        self._should_fetch_original = fetch_original

    def get_process_func(self):
        if self._should_fetch_original:
            return FetchQueue._fetch_original
        else:
            return FetchQueue._fetch_not_original

    def status(self, element):
        # Optionally skip elements that are already in the artifact cache
        if self._skip_cached:
            if not element._can_query_cache():
                return QueueStatus.PENDING

            if element._cached():
                return QueueStatus.SKIP

        # This will automatically skip elements which
        # have no sources.

        if not element._should_fetch(self._should_fetch_original):
            return QueueStatus.SKIP

        return QueueStatus.READY

    def done(self, _, element, result, status):

        if status is JobStatus.FAIL:
            return

        element._fetch_done()

        # Successful fetch, we must be CACHED or in the sourcecache
        if self._should_fetch_original:
            assert element._get_consistency() == Consistency.CACHED
        else:
            assert element._source_cached()

    def register_pending_element(self, element):
        # Set a "can_query_cache" callback for an element not yet ready
        # to be processed in the fetch queue.
        element._set_can_query_cache_callback(self._enqueue_element)

    @staticmethod
    def _fetch_not_original(element):
        element._fetch(fetch_original=False)

    @staticmethod
    def _fetch_original(element):
        element._fetch(fetch_original=True)