annotate lib/patchablegraph/patchsource.py @ 1448:420effc5e138

makefile to invoke Ignore-this: 766949fabca4a9edfecba59c493899ef darcs-hash:06deb9d383c5c890f9577edb962e6db36e37e87e
author drewp <drewp@bigasterisk.com>
date Wed, 25 Sep 2019 17:25:16 -0700
parents 6a8b922bbe2e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1395
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
1 import logging, time
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 import traceback
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 from rdflib import ConjunctiveGraph
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 from rdflib.parser import StringInputSource
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 from twisted.internet import reactor, defer
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 from rdfdb.patch import Patch
1372
26dd28671f70 fix old module name twisted_sse_demo
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
8 from twisted_sse.eventsource import EventSource
26dd28671f70 fix old module name twisted_sse_demo
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
9
26dd28671f70 fix old module name twisted_sse_demo
drewp <drewp@bigasterisk.com>
parents: 1317
diff changeset
10 from .patchablegraph import patchFromJson
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 log = logging.getLogger('fetch')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 class PatchSource(object):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 """wrap EventSource so it emits Patch objects and has an explicit stop method."""
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 def __init__(self, url, agent):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 self.url = str(url)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 # add callbacks to these to learn if we failed to connect
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 # (approximately) or if the ccnnection was unexpectedly lost
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 self.connectionFailed = defer.Deferred()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 self.connectionLost = defer.Deferred()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 self._listeners = set()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 log.info('start read from %s', url)
1395
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
26 self._startReadTime = time.time()
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
27 self._patchesReceived = 0 # including fullgraph
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 # note: fullGraphReceived isn't guaranteed- the stream could
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 # start with patches
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 self._fullGraphReceived = False
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 self._eventSource = EventSource(url.toPython().encode('utf8'),
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 userAgent=agent)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 self._eventSource.addEventListener(b'fullGraph', self._onFullGraph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 self._eventSource.addEventListener(b'patch', self._onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 self._eventSource.onerror(self._onError)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 self._eventSource.onConnectionLost = self._onDisconnect
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 def state(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 return {
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 'url': self.url,
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 'fullGraphReceived': self._fullGraphReceived,
1395
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
43 'patchesReceived': self._patchesReceived,
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
44 'time': {
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
45 'open': getattr(self, '_startReadTime', None),
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
46 'fullGraph': getattr(self, '_fullGraphTime', None),
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
47 'latestPatch': getattr(self, '_latestPatchTime', None),
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
48 },
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
49 'closed': self._eventSource is None,
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 }
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 def addPatchListener(self, func):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 func(patch, fullGraph=[true if the patch is the initial fullgraph])
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 self._listeners.add(func)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 def stop(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 log.info('stop read from %s', self.url)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 try:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 self._eventSource.protocol.stopProducing() # needed?
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 except AttributeError:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 pass
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 self._eventSource = None
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 def _onDisconnect(self, reason):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 log.debug('PatchSource._onDisconnect from %s (%s)', self.url, reason)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 # skip this if we're doing a stop?
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 self.connectionLost.callback(None)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 def _onError(self, msg):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 log.debug('PatchSource._onError from %s %r', self.url, msg)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 if not self._fullGraphReceived:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 self.connectionFailed.callback(msg)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 else:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 self.connectionLost.callback(msg)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 def _onFullGraph(self, message):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 try:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 g = ConjunctiveGraph()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 g.parse(StringInputSource(message), format='json-ld')
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 p = Patch(addGraph=g)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 self._sendPatch(p, fullGraph=True)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 except Exception:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 log.error(traceback.format_exc())
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 raise
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 self._fullGraphReceived = True
1395
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
88 self._fullGraphTime = time.time()
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
89 self._patchesReceived += 1
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 def _onPatch(self, message):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 try:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 p = patchFromJson(message)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 self._sendPatch(p, fullGraph=False)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 except:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 log.error(traceback.format_exc())
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 raise
1395
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
98 self._latestPatchTime = time.time()
6a8b922bbe2e more state vars
drewp <drewp@bigasterisk.com>
parents: 1372
diff changeset
99 self._patchesReceived += 1
1317
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 def _sendPatch(self, p, fullGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 log.debug('PatchSource %s received patch %s (fullGraph=%s)',
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103 self.url, p.shortSummary(), fullGraph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 for lis in self._listeners:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
105 lis(p, fullGraph=fullGraph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
106
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 def __del__(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108 if self._eventSource:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 raise ValueError("PatchSource wasn't stopped before del")
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 class ReconnectingPatchSource(object):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 PatchSource api, but auto-reconnects internally and takes listener
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 at init time to not miss any patches. You'll get another
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 fullGraph=True patch if we have to reconnect.
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117 todo: generate connection stmts in here
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
118 """
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
119 def __init__(self, url, listener, reconnectSecs=60, agent='unset'):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120 # type: (str, Any, Any, str)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 self.url = url
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 self._stopped = False
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 self._listener = listener
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 self.reconnectSecs = reconnectSecs
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125 self.agent = agent
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 self._reconnect()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
127
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
128 def _reconnect(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
129 if self._stopped:
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
130 return
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
131 self._ps = PatchSource(self.url, agent=self.agent)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
132 self._ps.addPatchListener(self._onPatch)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
133 self._ps.connectionFailed.addCallback(self._onConnectionFailed)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 self._ps.connectionLost.addCallback(self._onConnectionLost)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
135
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
136 def _onPatch(self, p, fullGraph):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
137 self._listener(p, fullGraph=fullGraph)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
138
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 def state(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
140 return {
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
141 'reconnectedPatchSource': self._ps.state(),
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
142 }
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
143
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
144 def stop(self):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
145 self._stopped = True
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
146 self._ps.stop()
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
147
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
148 def _onConnectionFailed(self, arg):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
149 reactor.callLater(self.reconnectSecs, self._reconnect)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
150
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
151 def _onConnectionLost(self, arg):
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
152 reactor.callLater(self.reconnectSecs, self._reconnect)
43791ec0beb2 make patchablegraph release
drewp <drewp@bigasterisk.com>
parents:
diff changeset
153