~joetalbott/+junk/fix_tmpdir

« back to all changes in this revision

Viewing changes to core_image_builder/queue.py

  • Committer: Joe Talbott
  • Date: 2015-05-19 21:37:40 UTC
  • Revision ID: joe.talbott@canonical.com-20150519213740-48l0u6bmecbyg9mt
Add core-image-publisher code with name changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# core-image-builder
 
2
# Copyright (C) 2015 Canonical
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
 
 
18
import logging
 
19
 
 
20
import kombu
 
21
 
 
22
from core_image_builder import constants
 
23
 
 
24
logger = logging.getLogger(__name__)
 
25
 
 
26
__all__ = [
 
27
    'CoreImageResultBuilder',
 
28
    'create_connection_from_config',
 
29
]
 
30
 
 
31
 
 
32
def create_connection_from_config(config):
 
33
    """Create a connection given a config object.
 
34
 
 
35
    'config' can either be a ConfigParser instance, or a nested dictionary.
 
36
 
 
37
    """
 
38
    amqp_uris = config.get('amqp', 'uris').split()
 
39
    return kombu.Connection(amqp_uris)
 
40
 
 
41
 
 
42
class CoreImageResultBuilder(object):
 
43
 
 
44
    """A callable that knows how to publish results from this service.
 
45
 
 
46
    For now we're using a simple queue, but this can easily be extended to use
 
47
    a full-blown topic exchange in the future.
 
48
 
 
49
    """
 
50
 
 
51
    def __init__(self, connection):
 
52
        self.connection = connection
 
53
 
 
54
    def __call__(self, payload):
 
55
        """Take 'payload' and enqueue it on the rabbit queue."""
 
56
        queue = self.connection.SimpleQueue(
 
57
            "core.tests.{}".format(constants.API_VERSION)
 
58
        )
 
59
        queue.put(payload)
 
60
        queue.close()