~wgrant/ubuntu/natty/landscape-client/natty-updates-broken

« back to all changes in this revision

Viewing changes to landscape/monitor/plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-04-21 19:58:10 UTC
  • mfrom: (1.1.16 upstream)
  • Revision ID: james.westby@ubuntu.com-20100421195810-s30uv3s6i27lue38
Tags: 1.5.2-0ubuntu0.10.10.0
* New upstream version (LP: #594594):
  - A new includes information about active network devices and their
    IP address in sysinfo output (LP: #272344).
  - A new plugin collects information about network traffic (#LP :284662).
  - Report information about which packages requested a reboot (LP: #538253).
  - Fix breakage on Lucid AMIs having no ramdisk (LP: #574810).
  - Migrate the inter-process communication system from DBus to Twisted AMP.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from logging import info
 
2
 
 
3
from twisted.internet.defer import succeed
 
4
 
 
5
from landscape.log import format_object
 
6
from landscape.lib.log import log_failure
 
7
from landscape.broker.client import BrokerClientPlugin
 
8
 
 
9
 
 
10
class MonitorPlugin(BrokerClientPlugin):
 
11
    """
 
12
    @cvar persist_name: If specified as a string, a C{_persist} attribute
 
13
    will be available after registration.
 
14
    """
 
15
 
 
16
    persist_name = None
 
17
 
 
18
    def register(self, monitor):
 
19
        super(MonitorPlugin, self).register(monitor)
 
20
        if self.persist_name is not None:
 
21
            self._persist = self.monitor.persist.root_at(self.persist_name)
 
22
        else:
 
23
            self._persist = None
 
24
 
 
25
    @property
 
26
    def persist(self):
 
27
        """Return our L{Persist}, if any."""
 
28
        return self._persist
 
29
 
 
30
    @property
 
31
    def monitor(self):
 
32
        """An alias for the C{client} attribute."""
 
33
        return self.client
 
34
 
 
35
    def call_on_accepted(self, type, callable, *args, **kwargs):
 
36
        """
 
37
        Register a callback fired upon a C{message-type-acceptance-changed}.
 
38
        """
 
39
 
 
40
        def acceptance_changed(acceptance):
 
41
            if acceptance:
 
42
                return callable(*args, **kwargs)
 
43
 
 
44
        self.monitor.reactor.call_on(("message-type-acceptance-changed",
 
45
                                       type), acceptance_changed)
 
46
 
 
47
 
 
48
class DataWatcher(MonitorPlugin):
 
49
    """
 
50
    A utility for plugins which send data to the Landscape server
 
51
    which does not constantly change. New messages will only be sent
 
52
    when the result of get_data() has changed since the last time it
 
53
    was called.
 
54
 
 
55
    Subclasses should provide a get_data method, and message_type,
 
56
    message_key, and persist_name class attributes.
 
57
    """
 
58
 
 
59
    message_type = None
 
60
    message_key = None
 
61
 
 
62
    def get_message(self):
 
63
        """
 
64
        Construct a message with the latest data, or None, if the data
 
65
        has not changed since the last call.
 
66
        """
 
67
        data = self.get_data()
 
68
        if self._persist.get("data") != data:
 
69
            self._persist.set("data", data)
 
70
            return {"type": self.message_type, self.message_key: data}
 
71
 
 
72
    def send_message(self, urgent):
 
73
        message = self.get_message()
 
74
        if message is not None:
 
75
            info("Queueing a message with updated data watcher info "
 
76
                 "for %s.", format_object(self))
 
77
            result = self.registry.broker.send_message(message, urgent=urgent)
 
78
 
 
79
            def persist_data(message_id):
 
80
                self.persist_data()
 
81
 
 
82
            result.addCallback(persist_data)
 
83
            result.addErrback(log_failure)
 
84
            return result
 
85
        return succeed(None)
 
86
 
 
87
    def persist_data(self):
 
88
        """
 
89
        Sub-classes that need to defer the saving of persistent data
 
90
        should override this method.
 
91
        """
 
92
        pass
 
93
 
 
94
    def exchange(self, urgent=False):
 
95
        """
 
96
        Conditionally add a message to the message store if new data
 
97
        is available.
 
98
        """
 
99
        return self.registry.broker.call_if_accepted(self.message_type,
 
100
                                                     self.send_message, urgent)