Mercurial > libervia-backend
annotate src/tools/sat_defer.py @ 2018:7199e6bdb94e
quick_frontend, primitivus (chat): fixed widget bad locking + don't send notification when locked
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 24 Jul 2016 17:59:24 +0200 |
parents | 2daf7b4c6756 |
children | 8b37a62336c3 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
1448
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SàT: a XMPP client |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
1448
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """tools related to deferred""" |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 log = getLogger(__name__) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core import exceptions |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.internet import defer |
1549
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
26 from twisted.internet import error as internet_error |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
27 from twisted.internet import reactor |
1448
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.python import failure |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from sat.core.constants import Const as C |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from sat.memory import memory |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 KEY_DEFERREDS = 'deferreds' |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 KEY_NEXT = 'next_defer' |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
1549
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
36 class DelayedDeferred(object): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
37 """A Deferred-like which is launched after a delay""" |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
38 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
39 def __init__(self, delay, result): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
40 """ |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
41 @param delay(float): delay before launching the callback, in seconds |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
42 @param result: result used with the callback |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
43 """ |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
44 self._deferred = defer.Deferred() |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
45 self._timer = reactor.callLater(delay, self._deferred.callback, result) |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
46 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
47 def cancel(self): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
48 try: |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
49 self._timer.cancel() |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
50 except internet_error.AlreadyCalled: |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
51 pass |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
52 self._deferred.cancel() |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
53 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
54 def addCallbacks(self, *args, **kwargs): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
55 self._deferred.addCallbacks(*args,**kwargs) |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
56 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
57 def addCallback(self, *args, **kwargs): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
58 self._deferred.addCallback(*args,**kwargs) |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
59 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
60 def addErrback(self, *args, **kwargs): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
61 self._deferred.addErrback(*args,**kwargs) |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
62 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
63 def addBoth(self, *args, **kwargs): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
64 self._deferred.addBoth(*args,**kwargs) |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
65 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
66 def chainDeferred(self, *args, **kwargs): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
67 self._deferred.chainDeferred(*args,**kwargs) |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
68 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
69 def pause(self): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
70 self._deferred.pause() |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
71 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
72 def unpause(self): |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
73 self._deferred.unpause() |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
74 |
e987325c14ef
core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents:
1448
diff
changeset
|
75 |
1448
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 class RTDeferredSessions(memory.Sessions): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 """Real Time Deferred Sessions""" |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def __init__(self, timeout=120): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 """Manage list of Deferreds in real-time, allowing to get intermediate results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 @param timeout (int): nb of seconds before deferreds cancellation |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 super(RTDeferredSessions, self).__init__(timeout=timeout, resettable_timeout=False) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 def newSession(self, deferreds, profile): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 """Launch a new session with a list of deferreds |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 @param deferreds(list[defer.Deferred]): list of deferred to call |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 @param profile: %(doc_profile)s |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 @param return (tupe[str, defer.Deferred]): tuple with session id and a deferred wich fire *WITHOUT RESULT* when all results are received |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 data = {KEY_NEXT: defer.Deferred()} |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 session_id, session_data = super(RTDeferredSessions, self).newSession(data, profile=profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 if isinstance(deferreds, dict): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 session_data[KEY_DEFERREDS] = deferreds.values() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 iterator = deferreds.iteritems() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 session_data[KEY_DEFERREDS] = deferreds |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 iterator = enumerate(deferreds) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 for idx, d in iterator: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 d._RTDeferred_index = idx |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 d._RTDeferred_return = None |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 d.addCallback(self._callback, d, session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 d.addErrback(self._errback, d, session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 return session_id |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 def _purgeSession(self, session_id, reason=u"timeout", no_warning=False, got_result=False): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 """Purge the session |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 @param session_id(str): id of the session to purge |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 @param reason (unicode): human readable reason why the session is purged |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 @param no_warning(bool): if True, no warning will be put in logs |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 @param got_result(bool): True if the session is purged after normal ending (i.e.: all the results have been gotten). |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 reason and no_warning are ignored if got_result is True. |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 @raise KeyError: session doesn't exists (anymore ?) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 if not got_result: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 try: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 timer, session_data, profile = self._sessions[session_id] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 except ValueError: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 raise exceptions.InternalError(u'was expecting timer, session_data and profile; is profile set ?') |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 # next_defer must be called before deferreds, |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 # else its callback will be called by _gotResult |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 next_defer = session_data[KEY_NEXT] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 if not next_defer.called: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 next_defer.errback(failure.Failure(defer.CancelledError(reason))) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 deferreds = session_data[KEY_DEFERREDS] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 for d in deferreds: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 d.cancel() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 if not no_warning: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 log.warning(u"RTDeferredList cancelled: {} (profile {})".format(reason, profile)) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 super(RTDeferredSessions, self)._purgeSession(session_id) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 def _gotResult(self, session_id, profile): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 """Method called after each callback or errback |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 manage the next_defer deferred |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 session_data = self.profileGet(session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 defer_next = session_data[KEY_NEXT] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 if not defer_next.called: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 defer_next.callback(None) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 def _callback(self, result, deferred, session_id, profile): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 deferred._RTDeferred_return = (True, result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 self._gotResult(session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 def _errback(self, failure, deferred, session_id, profile): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 deferred._RTDeferred_return = (False, failure) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 self._gotResult(session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 def cancel(self, session_id, reason=u"timeout", no_log=False): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 """Stop this RTDeferredList |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 Cancel all remaining deferred, and call self.final_defer.errback |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 @param reason (unicode): reason of the cancellation |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 @param no_log(bool): if True, don't log the cancellation |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 self._purgeSession(session_id, reason=reason, no_warning=no_log) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 def getResults(self, session_id, on_success=None, on_error=None, profile=C.PROF_KEY_NONE): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 """Get current results of a real-time deferred session |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 result already gotten are deleted |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 @param session_id(str): session id |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 @param on_success: can be: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 - None: add success normaly to results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 - callable: replace result by the return value of on_success(result) (may be deferred) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 @param on_error: can be: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 - None: add error normaly to results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 - C.IGNORE: don't put errors in results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 - callable: replace failure by the return value of on_error(failure) (may be deferred) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 @param profile=%(doc_profile)s |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 @param result(tuple): tuple(remaining, results) where: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 - remaining[int] is the number of remaining deferred |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 (deferreds from which we don't have result yet) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 - results is a dict where: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 - key is the index of the deferred if deferred is a list, or its key if it's a dict |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 - value = (success, result) where: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 - success is True if the deferred was successful |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 - result is the result in case of success, else the failure |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 If remaining == 0, the session is ended |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 @raise KeyError: the session is already finished or doesn't exists at all |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 if profile == C.PROF_KEY_NONE: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 raise exceptions.ProfileNotSetError |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 session_data = self.profileGet(session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 @defer.inlineCallbacks |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 def next_cb(dummy): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 # we got one or several results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 results = {} |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 filtered_data = [] # used to keep deferreds without results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 deferreds = session_data[KEY_DEFERREDS] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 for d in deferreds: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 if d._RTDeferred_return: # we don't use d.called as called is True before the full callbacks chain has been called |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 # we have a result |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 idx = d._RTDeferred_index |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 success, result = d._RTDeferred_return |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 if success: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 if on_success is not None: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 if callable(on_success): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 result = yield on_success(result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 raise exceptions.InternalError('Unknown value of on_success: {}'.format(on_success)) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 if on_error is not None: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 if on_error == C.IGNORE: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 continue |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 elif callable(on_error): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 result = yield on_error(result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 raise exceptions.InternalError('Unknown value of on_error: {}'.format(on_error)) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 results[idx] = (success, result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 filtered_data.append(d) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 # we change the deferred with the filtered list |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 # in other terms, we don't want anymore deferred from which we have got the result |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 session_data[KEY_DEFERREDS] = filtered_data |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 if filtered_data: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 # we create a new next_defer only if we are still waiting for results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 session_data[KEY_NEXT] = defer.Deferred() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 # no more data to get, the result have been gotten, |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 # we can cleanly finish the session |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 self._purgeSession(session_id, got_result=True) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 defer.returnValue((len(filtered_data), results)) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 # we wait for a result |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 return session_data[KEY_NEXT].addCallback(next_cb) |