2 from zope.interface import Interface, implements
3 from allmydata.util import observer
5 class IMonitor(Interface):
6 """I manage status, progress, and cancellation for long-running operations.
8 Whoever initiates the operation should create a Monitor instance and pass
9 it into the code that implements the operation. That code should
10 periodically check in with the Monitor, perhaps after each major unit of
11 work has been completed, for two purposes.
13 The first is to inform the Monitor about progress that has been made, so
14 that external observers can be reassured that the operation is proceeding
15 normally. If the operation has a well-known amount of work to perform,
16 this notification should reflect that, so that an ETA or 'percentage
17 complete' value can be derived.
19 The second purpose is to check to see if the operation has been
20 cancelled. The impatient observer who no longer wants the operation to
21 continue will inform the Monitor; the next time the operation code checks
22 in, it should notice that the operation has been cancelled, and wrap
23 things up. The same monitor can be passed to multiple operations, all of
24 which may check for cancellation: this pattern may be simpler than having
25 the original caller keep track of subtasks and cancel them individually.
28 # the following methods are provided for the operation code
30 def is_cancelled(self):
31 """Returns True if the operation has been cancelled. If True,
32 operation code should stop creating new work, and attempt to stop any
33 work already in progress."""
35 def raise_if_cancelled(self):
36 """Raise OperationCancelledError if the operation has been cancelled.
37 Operation code that has a robust error-handling path can simply call
40 def set_status(self, status):
41 """Sets the Monitor's 'status' object to an arbitrary value.
42 Different operations will store different sorts of status information
43 here. Operation code should use get+modify+set sequences to update
47 """Return the status object. If the operation failed, this will be a
50 def finish(self, status):
51 """Call this when the operation is done, successful or not. The
52 Monitor's lifetime is influenced by the completion of the operation
53 it is monitoring. The Monitor's 'status' value will be set with the
54 'status' argument, just as if it had been passed to set_status().
55 This value will be used to fire the Deferreds that are returned by
58 Operations that fire a Deferred when they finish should trigger this
59 with d.addBoth(monitor.finish)"""
61 # the following methods are provided for the initiator of the operation
63 def is_finished(self):
64 """Return a boolean, True if the operation is done (whether
65 successful or failed), False if it is still running."""
68 """Return a Deferred that fires when the operation is complete. It
69 will fire with the operation status, the same value as returned by
73 """Cancel the operation as soon as possible. is_cancelled() will
74 start returning True after this is called."""
76 # get_status() is useful too, but it is operation-specific
79 class OperationCancelledError(Exception):
87 self.cancelled = False
90 self.observer = observer.OneShotObserverList()
92 def is_cancelled(self):
95 def raise_if_cancelled(self):
97 raise OperationCancelledError()
99 def is_finished(self):
103 return self.observer.when_fired()
106 self.cancelled = True
108 def finish(self, status_or_failure):
109 self.set_status(status_or_failure)
111 self.observer.fire(status_or_failure)
112 return status_or_failure
114 def get_status(self):
116 def set_status(self, status):