summaryrefslogtreecommitdiff
path: root/src/buildstream/_cas/casdprocessmanager.py
blob: d7494b255ae2c404b163a745f449276d505ae91b (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
#
#  Copyright (C) 2018 Codethink Limited
#  Copyright (C) 2018-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 asyncio
import contextlib
import os
import shutil
import signal
import subprocess
import tempfile
import time

from .. import _signals, utils
from .._message import Message, MessageType

_CASD_MAX_LOGFILES = 10


# CASDProcessManager
#
# This manages the subprocess that runs buildbox-casd.
#
# Args:
#     path (str): The root directory for the CAS repository
#     log_dir (str): The directory for the logs
#     log_level (LogLevel): Log level to give to buildbox-casd for logging
#     cache_quota (int): User configured cache quota
#     protect_session_blobs (bool): Disable expiry for blobs used in the current session
#
class CASDProcessManager:

    def __init__(self, path, log_dir, log_level, cache_quota, protect_session_blobs):
        self._log_dir = log_dir

        # Place socket in global/user temporary directory to avoid hitting
        # the socket path length limit.
        self._socket_tempdir = tempfile.mkdtemp(prefix='buildstream')
        socket_path = os.path.join(self._socket_tempdir, 'casd.sock')
        self.connection_string = "unix:" + socket_path

        casd_args = [utils.get_host_tool('buildbox-casd')]
        casd_args.append('--bind=' + self.connection_string)
        casd_args.append('--log-level=' + log_level.value)

        if cache_quota is not None:
            casd_args.append('--quota-high={}'.format(int(cache_quota)))
            casd_args.append('--quota-low={}'.format(int(cache_quota / 2)))

            if protect_session_blobs:
                casd_args.append('--protect-session-blobs')

        casd_args.append(path)

        self.start_time = time.time()
        self._logfile = self._rotate_and_get_next_logfile()

        with open(self._logfile, "w") as logfile_fp:
            # Block SIGINT on buildbox-casd, we don't need to stop it
            # The frontend will take care of it if needed
            with _signals.blocked([signal.SIGINT], ignore=False):
                self._process = subprocess.Popen(
                    casd_args, cwd=path, stdout=logfile_fp, stderr=subprocess.STDOUT)

        self._failure_callback = None
        self._watcher = None

    # _rotate_and_get_next_logfile()
    #
    # Get the logfile to use for casd
    #
    # This will ensure that we don't create too many casd log files by
    # rotating the logs and only keeping _CASD_MAX_LOGFILES logs around.
    #
    # Returns:
    #   (str): the path to the log file to use
    #
    def _rotate_and_get_next_logfile(self):
        try:
            existing_logs = sorted(os.listdir(self._log_dir))
        except FileNotFoundError:
            os.makedirs(self._log_dir)
        else:
            while len(existing_logs) >= _CASD_MAX_LOGFILES:
                logfile_to_delete = existing_logs.pop(0)
                os.remove(os.path.join(self._log_dir, logfile_to_delete))

        return os.path.join(self._log_dir, str(self.start_time) + ".log")

    # release_resources()
    #
    # Terminate the process and release related resources.
    #
    def release_resources(self, messenger=None):
        self._terminate(messenger)
        self._process = None
        shutil.rmtree(self._socket_tempdir)

    # _terminate()
    #
    # Terminate the buildbox casd process.
    #
    def _terminate(self, messenger=None):
        assert self._watcher is None
        assert self._failure_callback is None

        return_code = self._process.poll()

        if return_code is not None:
            # buildbox-casd is already dead

            if messenger:
                messenger.message(
                    Message(
                        MessageType.BUG,
                        "Buildbox-casd died during the run. Exit code: {}, Logs: {}".format(
                            return_code, self._logfile
                        ),
                    )
                )
            return

        self._process.terminate()

        try:
            # Don't print anything if buildbox-casd terminates quickly
            return_code = self._process.wait(timeout=0.5)
        except subprocess.TimeoutExpired:
            if messenger:
                cm = messenger.timed_activity("Terminating buildbox-casd")
            else:
                cm = contextlib.suppress()
            with cm:
                try:
                    return_code = self._process.wait(timeout=15)
                except subprocess.TimeoutExpired:
                    self._process.kill()
                    self._process.wait(timeout=15)

                    if messenger:
                        messenger.message(
                            Message(MessageType.WARN, "Buildbox-casd didn't exit in time and has been killed")
                        )
                    return

        if return_code != 0 and messenger:
            messenger.message(
                Message(
                    MessageType.BUG,
                    "Buildbox-casd didn't exit cleanly. Exit code: {}, Logs: {}".format(
                        return_code, self._logfile
                    ),
                )
            )

    # set_failure_callback()
    #
    # Call this function if the CASD process stops unexpectedly.
    #
    # Note that we guarantee that the lifetime of any 'watcher' used is bound
    # to the lifetime of the callback - we won't hang on to the asyncio loop
    # longer than necessary.
    #
    # We won't be able to use watchers on win32, so we'll need to support
    # another approach.
    #
    # Args:
    #   func (callable): a callable that takes no parameters
    #
    def set_failure_callback(self, func):
        assert func is not None
        assert self._watcher is None
        assert self._failure_callback is None
        self._failure_callback = func
        self._watcher = asyncio.get_child_watcher()
        self._watcher.add_child_handler(self._process.pid, self._on_casd_failure)

    # clear_failure_callback()
    #
    # No longer call this callable if the CASD process stops unexpectedly
    #
    # Args:
    #   func (callable): The callable that was provided to add_failure_callback().
    #                    Supplying this again allows us to do error checking.
    #
    def clear_failure_callback(self, func):
        assert func is not None
        assert self._failure_callback == func
        self._watcher.remove_child_handler(self._process.pid)
        self._failure_callback = None
        self._watcher = None

    # _on_casd_failure()
    #
    # Handler for casd process terminating unexpectedly
    #
    # Args:
    #   pid (int): the process id under which buildbox-casd was running
    #   returncode (int): the return code with which buildbox-casd exited
    #
    def _on_casd_failure(self, pid, returncode):
        assert self._failure_callback is not None
        self._process.returncode = returncode
        self._failure_callback()