1
2 """
3 Callbacks to be used with asynchronous services. The
4 ProcessCallbackI is also included in the omero.scripts
5 module for backwards compatibility.
6
7 Copyright 2010 Glencoe Software, Inc. All rights reserved.
8 Use is subject to license terms supplied in LICENSE.txt
9
10 """
11
12 import os
13 import Ice
14 import logging
15 import exceptions
16
17 import omero
18 import omero_Scripts_ice
19 import omero.util.concurrency
20 import omero_ext.uuid as uuid
21
22 from omero.rtypes import *
23
24
25 PROC_LOG = logging.getLogger("omero.scripts.ProcessCallback")
26 DEL_LOG = logging.getLogger("omero.api.DeleteCallback")
27
28
30 """
31 Simple callback which registers itself with the given process.
32 """
33
34 FINISHED = "FINISHED"
35 CANCELLED = "CANCELLED"
36 KILLED = "KILLED"
37
38 - def __init__(self, adapter_or_client, process, poll = True):
39 self.event = omero.util.concurrency.get_event(name="ProcessCallbackI")
40 self.result = None
41 self.poll = poll
42 self.process = process
43 self.adapter = adapter_or_client
44 self.id = Ice.Identity(str(uuid.uuid4()), "ProcessCallback")
45 if not isinstance(self.adapter, Ice.ObjectAdapter):
46 self.adapter = self.adapter.adapter
47 self.prx = self.adapter.add(self, self.id)
48 self.prx = omero.grid.ProcessCallbackPrx.uncheckedCast(self.prx)
49 process.registerCallback(self.prx)
50
52 """
53 Should only be used if the default logic of the process methods is kept
54 in place. If "event.set" does not get called, this method will always
55 block for the given milliseconds.
56 """
57 if self.poll:
58 try:
59 rc = self.process.poll()
60 if rc is not None:
61 self.processFinished(rc.getValue())
62 except exceptions.Exception, e:
63 PROC_LOG.warn("Error calling poll: %s" % e)
64
65 self.event.wait(float(ms) / 1000)
66 if self.event.isSet():
67 return self.result
68 return None
69
73
77
81
83 self.adapter.remove(self.id)
84
85
87 """
88 Callback used for waiting until DeleteHandlePrx will return true on
89 finished(). The block(long) method will wait the given number of
90 milliseconds and then return the number of errors if any or None
91 if the delete is not yet complete.
92
93 Example usage:
94
95 cb = DeleteCallbackI(client, handle)
96 errors = None
97 while (errors is None):
98 errors = cb.block(500)
99 """
100
101 - def __init__(self, adapter_or_client, handle, poll = True):
102 self.event = omero.util.concurrency.get_event(name="DeleteCallbackI")
103 self.result = None
104 self.poll = poll
105 self.handle = handle
106 self.adapter = adapter_or_client
107 self.id = Ice.Identity(str(uuid.uuid4()), "DeleteHandleCallback")
108 if not isinstance(self.adapter, Ice.ObjectAdapter):
109 self.adapter = self.adapter.adapter
110
111
112
113
114 - def loop(self, loops, ms):
115 """
116 Calls block(long) "loops" number of times with the "ms"
117 argument. This means the total wait time for the delete to occur
118 is: loops X ms. Sensible values might be 10 loops for 500 ms, or
119 5 seconds.
120
121 @param loops Number of times to call block(long)
122 @param ms Number of milliseconds to pass to block(long
123 @throws omero.LockTimeout if block(long) does not return
124 a non-null value after loops calls.
125 """
126
127 count = 0
128 errors = None
129 while errors is None and count < loops:
130 errors = self.block(ms)
131 count += 1
132
133 if errors is None:
134 waited = (ms / 1000) * loops
135 raise omero.LockTimeout(None, None,
136 "Delete unfinished after %s seconds" % waited,
137 5000L, waited)
138 else:
139 return self.handle.report()
140
142 """
143 Should only be used if the default logic of the handle methods is kept
144 in place. If "event.set" does not get called, this method will always
145 block for the given milliseconds.
146 """
147 if self.poll:
148 try:
149 if self.handle.finished():
150 try:
151 self.finished(self.handle.errors())
152 except exceptions.Exception, e:
153 DEL_LOG.warn("Error calling DeleteCallbackI.finished: %s" % e, exc_info=True)
154 except Ice.ObjectNotExistException, onee:
155 raise omero.ClientError("Handle is gone! %s" % self.handle)
156 except:
157 DEL_LOG.warn("Error polling DeleteHandle:" + str(self.handle), exc_info=True)
158
159
160 self.event.wait(float(ms) / 1000)
161 if self.event.isSet():
162 return self.result
163 return None
164
165
167 self.result = errors
168 self.event.set()
169
171
172 try:
173 self.handle.close()
174 except exceptions.Exception, e:
175 DEL_LOG.warn("Error calling DeleteHandlePrx.close: %s" % self.handle, exc_info=True)
176