Mercurial > libervia-backend
annotate src/tools/sat_defer.py @ 1543:62596ec69bbd
social contract: fixed a typo
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Nov 2015 22:02:37 +0100 |
parents | 227856b13d7a |
children | e987325c14ef |
rev | line source |
---|---|
1448
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
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 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) |
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 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.python import failure |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 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
|
28 from sat.memory import memory |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 KEY_DEFERREDS = 'deferreds' |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 KEY_NEXT = 'next_defer' |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 class RTDeferredSessions(memory.Sessions): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 """Real Time Deferred Sessions""" |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 def __init__(self, timeout=120): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 """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
|
40 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 @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
|
42 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 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
|
44 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 def newSession(self, deferreds, profile): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 """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
|
47 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 @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
|
49 @param profile: %(doc_profile)s |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 @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
|
51 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 data = {KEY_NEXT: defer.Deferred()} |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 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
|
54 if isinstance(deferreds, dict): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 session_data[KEY_DEFERREDS] = deferreds.values() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 iterator = deferreds.iteritems() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 session_data[KEY_DEFERREDS] = deferreds |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 iterator = enumerate(deferreds) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 for idx, d in iterator: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 d._RTDeferred_index = idx |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 d._RTDeferred_return = None |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 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
|
65 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
|
66 return session_id |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 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
|
69 """Purge the session |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 @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
|
72 @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
|
73 @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
|
74 @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
|
75 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
|
76 @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
|
77 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 if not got_result: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 try: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 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
|
81 except ValueError: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 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
|
83 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 # 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
|
85 # 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
|
86 next_defer = session_data[KEY_NEXT] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 if not next_defer.called: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 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
|
89 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 deferreds = session_data[KEY_DEFERREDS] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 for d in deferreds: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 d.cancel() |
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 if not no_warning: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 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
|
96 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 super(RTDeferredSessions, self)._purgeSession(session_id) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 def _gotResult(self, session_id, profile): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 """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
|
101 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 manage the next_defer deferred |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 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
|
105 defer_next = session_data[KEY_NEXT] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 if not defer_next.called: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 defer_next.callback(None) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 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
|
110 deferred._RTDeferred_return = (True, result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 self._gotResult(session_id, profile) |
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 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
|
114 deferred._RTDeferred_return = (False, failure) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 self._gotResult(session_id, profile) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 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
|
118 """Stop this RTDeferredList |
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 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
|
121 @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
|
122 @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
|
123 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 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
|
125 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 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
|
127 """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
|
128 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 result already gotten are deleted |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 @param session_id(str): session id |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 @param on_success: can be: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 - None: add success normaly to results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 - 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
|
134 @param on_error: can be: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 - None: add error normaly to results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 - 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
|
137 - 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
|
138 @param profile=%(doc_profile)s |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 @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
|
140 - 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
|
141 (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
|
142 - results is a dict where: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 - 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
|
144 - value = (success, result) where: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 - 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
|
146 - 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
|
147 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
|
148 @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
|
149 """ |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 if profile == C.PROF_KEY_NONE: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 raise exceptions.ProfileNotSetError |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 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
|
153 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 @defer.inlineCallbacks |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 def next_cb(dummy): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 # we got one or several results |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 results = {} |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 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
|
159 deferreds = session_data[KEY_DEFERREDS] |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 for d in deferreds: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 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
|
163 # we have a result |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 idx = d._RTDeferred_index |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 success, result = d._RTDeferred_return |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 if success: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 if on_success is not None: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 if callable(on_success): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 result = yield on_success(result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 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
|
172 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 if on_error is not None: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 if on_error == C.IGNORE: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 continue |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 elif callable(on_error): |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 result = yield on_error(result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 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
|
181 results[idx] = (success, result) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 filtered_data.append(d) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 # 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
|
186 # 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
|
187 session_data[KEY_DEFERREDS] = filtered_data |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 if filtered_data: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 # 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
|
191 session_data[KEY_NEXT] = defer.Deferred() |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 else: |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 # 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
|
194 # we can cleanly finish the session |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 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
|
196 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 defer.returnValue((len(filtered_data), results)) |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 # we wait for a result |
227856b13d7a
core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 return session_data[KEY_NEXT].addCallback(next_cb) |