annotate service/reasoning/patchsource.py @ 1117:d5687ba23279

fix input graph web display by dirtying combinedGraph better. Ignore-this: e40e7499e3d675b029f829a1f269b83a darcs-hash:c15a1820ca7b5e40b2a27946bfe1c87cc80612fa
author drewp <drewp@bigasterisk.com>
date Sun, 09 Oct 2016 13:57:50 -0700
parents aa70001ea0c9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1117
d5687ba23279 fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents: 1111
diff changeset
1 import sys, logging
1107
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 import traceback
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 from twisted.internet import reactor, defer
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 from twisted_sse_demo.eventsource import EventSource
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 from rdflib import ConjunctiveGraph
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 from rdflib.parser import StringInputSource
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 sys.path.append("../../lib")
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 from patchablegraph import patchFromJson
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 sys.path.append("/my/proj/light9")
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 from light9.rdfdb.patch import Patch
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
1117
d5687ba23279 fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents: 1111
diff changeset
14 log = logging.getLogger('fetch')
1107
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 class PatchSource(object):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 """wrap EventSource so it emits Patch objects and has an explicit stop method."""
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 def __init__(self, url):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 self.url = url
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 # add callbacks to these to learn if we failed to connect
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 # (approximately) or if the ccnnection was unexpectedly lost
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 self.connectionFailed = defer.Deferred()
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 self.connectionLost = defer.Deferred()
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 self._listeners = set()
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 log.info('start read from %s', url)
1117
d5687ba23279 fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents: 1111
diff changeset
28 # note: fullGraphReceived isn't guaranteed- the stream could
d5687ba23279 fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents: 1111
diff changeset
29 # start with patches
1107
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 self._fullGraphReceived = False
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 self._eventSource = EventSource(url.toPython().encode('utf8'))
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 self._eventSource.protocol.delimiter = '\n'
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 self._eventSource.addEventListener('fullGraph', self._onFullGraph)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 self._eventSource.addEventListener('patch', self._onPatch)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 self._eventSource.onerror(self._onError)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 origSet = self._eventSource.protocol.setFinishedDeferred
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39 def sfd(d):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 origSet(d)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 d.addCallback(self._onDisconnect)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 self._eventSource.protocol.setFinishedDeferred = sfd
1111
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
43
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
44 def stats(self):
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
45 return {
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
46 'url': self.url,
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
47 'fullGraphReceived': self._fullGraphReceived,
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
48 }
1107
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 def addPatchListener(self, func):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 """
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 func(patch, fullGraph=[true if the patch is the initial fullgraph])
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 """
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 self._listeners.add(func)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 def stop(self):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57 log.info('stop read from %s', self.url)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58 try:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 self._eventSource.protocol.stopProducing() # needed?
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 except AttributeError:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 pass
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 self._eventSource = None
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 def _onDisconnect(self, a):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 log.debug('PatchSource._onDisconnect from %s', self.url)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 # skip this if we're doing a stop?
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 self.connectionLost.callback(None)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 def _onError(self, msg):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 log.debug('PatchSource._onError from %s %r', self.url, msg)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 if not self._fullGraphReceived:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 self.connectionFailed.callback(msg)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 else:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 self.connectionLost.callback(msg)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 def _onFullGraph(self, message):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 try:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 g = ConjunctiveGraph()
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 g.parse(StringInputSource(message), format='json-ld')
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 p = Patch(addGraph=g)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 self._sendPatch(p, fullGraph=True)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 except:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 log.error(traceback.format_exc())
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 raise
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 self._fullGraphReceived = True
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 def _onPatch(self, message):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 try:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 p = patchFromJson(message)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 self._sendPatch(p, fullGraph=False)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 except:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 log.error(traceback.format_exc())
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 raise
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 def _sendPatch(self, p, fullGraph):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 log.debug('PatchSource %s received patch %s (fullGraph=%s)', self.url, p.shortSummary(), fullGraph)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 for lis in self._listeners:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98 lis(p, fullGraph=fullGraph)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 def __del__(self):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 if self._eventSource:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 raise ValueError
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 class ReconnectingPatchSource(object):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
105 """
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
106 PatchSource api, but auto-reconnects internally and takes listener
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 at init time to not miss any patches. You'll get another
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
108 fullGraph=True patch if we have to reconnect.
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 todo: generate connection stmts in here
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 """
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112 def __init__(self, url, listener):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 self.url = url
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114 self._stopped = False
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 self._listener = listener
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 self._reconnect()
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
117
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
118 def _reconnect(self):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
119 if self._stopped:
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120 return
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 self._ps = PatchSource(self.url)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 self._ps.addPatchListener(self._onPatch)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 self._ps.connectionFailed.addCallback(self._onConnectionFailed)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 self._ps.connectionLost.addCallback(self._onConnectionLost)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 def _onPatch(self, p, fullGraph):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
127 self._listener(p, fullGraph=fullGraph)
1111
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
128
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
129 def stats(self):
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
130 return {
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
131 'reconnectedPatchSource': self._ps.stats(),
aa70001ea0c9 sse_collector stats page
drewp <drewp@bigasterisk.com>
parents: 1107
diff changeset
132 }
1107
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
133
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 def stop(self):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
135 self._stopped = True
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
136 self._ps.stop()
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
137
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
138 def _onConnectionFailed(self, arg):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 reactor.callLater(60, self._reconnect)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
140
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
141 def _onConnectionLost(self, arg):
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
142 reactor.callLater(60, self._reconnect)
e68f6e5712c6 factor out patchsource
drewp <drewp@bigasterisk.com>
parents:
diff changeset
143