1#  Copyright (C) 2018 Codethink Limited
2#
3#  This program is free software; you can redistribute it and/or
4#  modify it under the terms of the GNU Lesser General Public
5#  License as published by the Free Software Foundation; either
6#  version 2 of the License, or (at your option) any later version.
7#
8#  This library is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
11#  Lesser General Public License for more details.
12#
13#  You should have received a copy of the GNU Lesser General Public
14#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
15#
16#  Author:
17#        Tristan Daniël Maat <tristan.maat@codethink.co.uk>
18#
19from .job import Job, JobStatus
20
21
22class CleanupJob(Job):
23    def __init__(self, *args, complete_cb, **kwargs):
24        super().__init__(*args, **kwargs)
25        self._complete_cb = complete_cb
26
27        context = self._scheduler.context
28        self._artifacts = context.artifactcache
29
30    def child_process(self):
31        def progress():
32            self.send_message('update-cache-size',
33                              self._artifacts.get_cache_size())
34        return self._artifacts.clean(progress)
35
36    def handle_message(self, message_type, message):
37
38        # Update the cache size in the main process as we go,
39        # this provides better feedback in the UI.
40        if message_type == 'update-cache-size':
41            self._artifacts.set_cache_size(message)
42            return True
43
44        return False
45
46    def parent_complete(self, status, result):
47        if status == JobStatus.OK:
48            self._artifacts.set_cache_size(result)
49
50        if self._complete_cb:
51            self._complete_cb(status, result)
52