~allanlesage/uci-engine/coverage-extractor

« back to all changes in this revision

Viewing changes to coverage-retriever/coverageretriever/run_worker.py

  • Committer: Allan LeSage
  • Date: 2014-10-03 20:48:44 UTC
  • Revision ID: allan.lesage@canonical.com-20141003204844-r0aty01f0y2dm5ml
Initial coverage-retriever for review.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Ubuntu CI Engine
 
3
# Copyright 2014 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU Affero General Public License version 3, as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU Affero General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Affero General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
from ci_utils import amqp_utils, amqp_worker
 
18
from ci_utils.data_store import DataStoreException
 
19
from coverageretriever import (snip_coverage_xml,
 
20
                               CoverageRetrieverException,
 
21
                               retry)
 
22
 
 
23
SUCCESS_MESSAGE_TEMPLATE = 'Deposited coverage.xml into swift for ticket {}.'
 
24
PARSE_FAILURE_TEMPLATE = 'Failed to parse build log for ticket {}.'
 
25
SWIFT_FAILURE_TEMPLATE = ('Failed to retrieve build log from swift '
 
26
                          'for ticket {}.')
 
27
 
 
28
 
 
29
class CoverageRetrieverWorker(amqp_worker.AMQPWorker):
 
30
 
 
31
    def __init__(self):
 
32
        super(CoverageRetrieverWorker, self).__init__('coverageretriever')
 
33
 
 
34
    @retry(DataStoreException, tries=5, delay=1, backoff=2)
 
35
    def get_build_log(self, data_store, build_log_filename):
 
36
        """Get build log; retry swift if our log's not there yet."""
 
37
        return data_store.get_file(build_log_filename)
 
38
 
 
39
    def handle_request(self, params, logger):
 
40
        """Snip coverage.xml from incoming build log and put in swift."""
 
41
        build_log_filename = params['build_log_filename']
 
42
        data_store = self._create_data_store(params['ticket_id'])
 
43
        try:
 
44
            log_text = self.get_build_log(data_store, build_log_filename)
 
45
            coverage_xml_text = snip_coverage_xml(log_text)
 
46
            result_link = data_store.put_file('coverage.xml',
 
47
                                              coverage_xml_text,
 
48
                                              'text/xml')
 
49
        except CoverageRetrieverException:
 
50
            message = PARSE_FAILURE_TEMPLATE.format(
 
51
                params['ticket_id'])
 
52
            logger.error(message)
 
53
            # FGINTHER: what more to put here?
 
54
            result_data = {'message': message}
 
55
            return amqp_utils.progress_failed, result_data
 
56
        except DataStoreException:
 
57
            message = SWIFT_FAILURE_TEMPLATE.format(
 
58
                params['ticket_id'])
 
59
            logger.error(message)
 
60
            # FGINTHER: what more to put here?
 
61
            result_data = {'message': message}
 
62
            return amqp_utils.progress_failed, result_data
 
63
        message = SUCCESS_MESSAGE_TEMPLATE.format(
 
64
            params['ticket_id'])
 
65
        logger.info(message)
 
66
        result_data = {
 
67
            'message': message,
 
68
            'result_link': result_link  # FGINTHER: necessary?
 
69
            # FGINTHER: what else do we need to report here?
 
70
            # FGINTHER: should this result be stashed in swift also?
 
71
        }
 
72
        return amqp_utils.progress_completed, result_data
 
73
 
 
74
 
 
75
# FGINTHER: correct?  also when/how is this called?
 
76
if __name__ == '__main__':
 
77
    CoverageRetrieverWorker().main(amqp_utils.COVERAGE_RETRIEVER_QUEUE)