1
from logging import info
3
from twisted.internet.defer import succeed
5
from landscape.log import format_object
6
from landscape.lib.log import log_failure
7
from landscape.broker.client import BrokerClientPlugin
10
class MonitorPlugin(BrokerClientPlugin):
12
@cvar persist_name: If specified as a string, a C{_persist} attribute
13
will be available after registration.
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)
27
"""Return our L{Persist}, if any."""
32
"""An alias for the C{client} attribute."""
35
def call_on_accepted(self, type, callable, *args, **kwargs):
37
Register a callback fired upon a C{message-type-acceptance-changed}.
40
def acceptance_changed(acceptance):
42
return callable(*args, **kwargs)
44
self.monitor.reactor.call_on(("message-type-acceptance-changed",
45
type), acceptance_changed)
48
class DataWatcher(MonitorPlugin):
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
55
Subclasses should provide a get_data method, and message_type,
56
message_key, and persist_name class attributes.
62
def get_message(self):
64
Construct a message with the latest data, or None, if the data
65
has not changed since the last call.
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}
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)
79
def persist_data(message_id):
82
result.addCallback(persist_data)
83
result.addErrback(log_failure)
87
def persist_data(self):
89
Sub-classes that need to defer the saving of persistent data
90
should override this method.
94
def exchange(self, urgent=False):
96
Conditionally add a message to the message store if new data
99
return self.registry.broker.call_if_accepted(self.message_type,
100
self.send_message, urgent)