annotate sat/tools/sat_defer.py @ 2745:3ee396b2ecf3

plugin XEP-0384: don't ignore StanzaError if it's not item-not-found
author Goffi <goffi@goffi.org>
date Fri, 04 Jan 2019 09:20:32 +0100
parents 56f94936df1e
children 378188abe941
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 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
27 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
28 from twisted.internet import reactor
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.python import failure
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 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
31 from sat.memory import memory
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
33 KEY_DEFERREDS = "deferreds"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
34 KEY_NEXT = "next_defer"
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
1549
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
37 class DelayedDeferred(object):
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
38 """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
39
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
40 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
41 """
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
42 @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
43 @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
44 """
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
45 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
46 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
47
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
48 def cancel(self):
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
49 try:
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
50 self._timer.cancel()
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
51 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
52 pass
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
53 self._deferred.cancel()
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
54
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
55 def addCallbacks(self, *args, **kwargs):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 self._deferred.addCallbacks(*args, **kwargs)
1549
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
57
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
58 def addCallback(self, *args, **kwargs):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 self._deferred.addCallback(*args, **kwargs)
1549
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
60
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
61 def addErrback(self, *args, **kwargs):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 self._deferred.addErrback(*args, **kwargs)
1549
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
63
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
64 def addBoth(self, *args, **kwargs):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 self._deferred.addBoth(*args, **kwargs)
1549
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
66
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
67 def chainDeferred(self, *args, **kwargs):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 self._deferred.chainDeferred(*args, **kwargs)
1549
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
69
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
70 def pause(self):
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
71 self._deferred.pause()
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
72
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
73 def unpause(self):
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
74 self._deferred.unpause()
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
75
e987325c14ef core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
Goffi <goffi@goffi.org>
parents: 1448
diff changeset
76
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 class RTDeferredSessions(memory.Sessions):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """Real Time Deferred Sessions"""
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 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 super(RTDeferredSessions, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 timeout=timeout, resettable_timeout=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 )
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 def newSession(self, deferreds, profile):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 """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
91
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @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
93 @param profile: %(doc_profile)s
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @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
95 """
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 data = {KEY_NEXT: defer.Deferred()}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 session_id, session_data = super(RTDeferredSessions, self).newSession(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 data, profile=profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 )
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 if isinstance(deferreds, dict):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 session_data[KEY_DEFERREDS] = deferreds.values()
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 iterator = deferreds.iteritems()
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 else:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 session_data[KEY_DEFERREDS] = deferreds
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 iterator = enumerate(deferreds)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 for idx, d in iterator:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 d._RTDeferred_index = idx
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 d._RTDeferred_return = None
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 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
112 return session_id
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 def _purgeSession(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 self, session_id, reason=u"timeout", no_warning=False, got_result=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 ):
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 """Purge the session
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 @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
120 @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
121 @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
122 @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
123 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
124 @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
125 """
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 if not got_result:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 try:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 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
129 except ValueError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 u"was expecting timer, session_data and profile; is profile set ?"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 )
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 # 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
135 # 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
136 next_defer = session_data[KEY_NEXT]
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 if not next_defer.called:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 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
139
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 deferreds = session_data[KEY_DEFERREDS]
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 for d in deferreds:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 d.cancel()
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 if not no_warning:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 u"RTDeferredList cancelled: {} (profile {})".format(reason, profile)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
147 )
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
148
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 super(RTDeferredSessions, self)._purgeSession(session_id)
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 _gotResult(self, session_id, profile):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 """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
153
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 manage the next_defer deferred
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 """
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 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
157 defer_next = session_data[KEY_NEXT]
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if not defer_next.called:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 defer_next.callback(None)
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 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
162 deferred._RTDeferred_return = (True, result)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 self._gotResult(session_id, profile)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
164
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 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
166 deferred._RTDeferred_return = (False, failure)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self._gotResult(session_id, profile)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
168
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 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
170 """Stop this RTDeferredList
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 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
173 @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
174 @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
175 """
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 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
177
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 def getResults(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
179 self, session_id, on_success=None, on_error=None, profile=C.PROF_KEY_NONE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
180 ):
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """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
182
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 result already gotten are deleted
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 @param session_id(str): session id
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @param on_success: can be:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 - None: add success normaly to results
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 - 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
188 @param on_error: can be:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 - None: add error normaly to results
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 - 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
191 - 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
192 @param profile=%(doc_profile)s
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 @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
194 - 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
195 (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
196 - results is a dict where:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 - 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
198 - value = (success, result) where:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 - 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
200 - 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
201 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
202 @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
203 """
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 if profile == C.PROF_KEY_NONE:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 raise exceptions.ProfileNotSetError
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 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
207
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 @defer.inlineCallbacks
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 def next_cb(dummy):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 # we got one or several results
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 results = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
212 filtered_data = [] # used to keep deferreds without results
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 deferreds = session_data[KEY_DEFERREDS]
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 for d in deferreds:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
216 if (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
217 d._RTDeferred_return
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
218 ): # we don't use d.called as called is True before the full callbacks chain has been called
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 # we have a result
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 idx = d._RTDeferred_index
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 success, result = d._RTDeferred_return
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 if success:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 if on_success is not None:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 if callable(on_success):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 result = yield on_success(result)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
227 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
228 "Unknown value of on_success: {}".format(on_success)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
229 )
1448
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 else:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 if on_error is not None:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 if on_error == C.IGNORE:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 continue
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 elif callable(on_error):
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 result = yield on_error(result)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 "Unknown value of on_error: {}".format(on_error)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
240 )
1448
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 results[idx] = (success, result)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 else:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 filtered_data.append(d)
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
244
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 # 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
246 # 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
247 session_data[KEY_DEFERREDS] = filtered_data
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
248
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 if filtered_data:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 # 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
251 session_data[KEY_NEXT] = defer.Deferred()
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 else:
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 # 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
254 # we can cleanly finish the session
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 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
256
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 defer.returnValue((len(filtered_data), results))
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
258
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 # we wait for a result
227856b13d7a core: new tools.sat_defer module, and implementation of RTDeferredSessions:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 return session_data[KEY_NEXT].addCallback(next_cb)