Switch to unified view

a/src/mediaserver/cdplugins/uprcl/uprcl-app.py b/src/mediaserver/cdplugins/uprcl/uprcl-app.py
...
...
27
import uprclfolders
27
import uprclfolders
28
import uprcltags
28
import uprcltags
29
import uprcluntagged
29
import uprcluntagged
30
import uprclsearch
30
import uprclsearch
31
import uprclhttp
31
import uprclhttp
32
from uprclutils import uplog, g_myprefix
32
from uprclutils import uplog, g_myprefix,rcldirentry
33
33
34
# The recoll documents
34
# The recoll documents
35
g_rcldocs = []
35
g_rcldocs = []
36
36
37
# Func name to method mapper
37
# Func name to method mapper
38
dispatcher = cmdtalkplugin.Dispatch()
38
dispatcher = cmdtalkplugin.Dispatch()
39
# Pipe message handler
39
# Pipe message handler
40
msgproc = cmdtalkplugin.Processor(dispatcher)
40
msgproc = cmdtalkplugin.Processor(dispatcher)
41
41
42
def uprcl_init():
42
def _uprcl_init_worker():
43
    global httphp, pathprefix, rclconfdir, g_rcldocs
43
    global httphp, pathprefix, rclconfdir, g_rcldocs
44
    
44
    
45
    if "UPMPD_PATHPREFIX" not in os.environ:
45
    if "UPMPD_PATHPREFIX" not in os.environ:
46
        raise Exception("No UPMPD_PATHPREFIX in environment")
46
        raise Exception("No UPMPD_PATHPREFIX in environment")
47
    pathprefix = os.environ["UPMPD_PATHPREFIX"]
47
    pathprefix = os.environ["UPMPD_PATHPREFIX"]
...
...
69
    g_rcldocs = uprclfolders.inittree(rclconfdir, httphp, pathprefix)
69
    g_rcldocs = uprclfolders.inittree(rclconfdir, httphp, pathprefix)
70
    uprcltags.recolltosql(g_rcldocs)
70
    uprcltags.recolltosql(g_rcldocs)
71
    uprcluntagged.recoll2untagged(g_rcldocs)
71
    uprcluntagged.recoll2untagged(g_rcldocs)
72
72
73
    host,port = httphp.split(':')
73
    host,port = httphp.split(':')
74
    if False:
74
    if True:
75
        # Running the server as a thread. We get into trouble because
75
        # Running the server as a thread. We get into trouble because
76
        # something somewhere writes to stdout a bunch of --------.
76
        # something somewhere writes to stdout a bunch of --------.
77
        # Could not find where they come from, happens after a sigpipe
77
        # Could not find where they come from, happens after a sigpipe
78
        # when a client closes a stream. The --- seem to happen before
78
        # when a client closes a stream. The --- seem to happen before
79
        # and after the exception strack trace, e.g:
79
        # and after the exception strack trace, e.g:
...
...
81
        #   Exception happened during processing of request from ('192...
81
        #   Exception happened during processing of request from ('192...
82
        #   Traceback...
82
        #   Traceback...
83
        #   [...]
83
        #   [...]
84
        # error: [Errno 32] Broken pipe
84
        # error: [Errno 32] Broken pipe
85
        # ----------------------------------------
85
        # ----------------------------------------
86
        # 
86
        # Finally: the handle_error SocketServer method was writing to stdout.
87
        # **Finally**: found it: the handle_error SocketServer method
87
        # Overriding it should have fixed the issue.
88
        # was writing to stdout.  Overriding it should have fixed the
89
        # issue. Otoh the separate process approach works, so we kept
90
        # it for now
88
        httpthread = threading.Thread(target=uprclhttp.runHttp,
91
        httpthread = threading.Thread(target=uprclhttp.runHttp,
89
                                      kwargs = {'host':host ,
92
                                      kwargs = {'host':host ,
90
                                                'port':int(port),
93
                                                'port':int(port),
91
                                                'pthstr':pthstr,
94
                                                'pthstr':pthstr,
92
                                                'pathprefix':pathprefix})
95
                                                'pathprefix':pathprefix})
...
...
98
        cmd = subprocess.Popen((cmdpath, host, port, pthstr, pathprefix),
101
        cmd = subprocess.Popen((cmdpath, host, port, pthstr, pathprefix),
99
                               stdin = open('/dev/null'),
102
                               stdin = open('/dev/null'),
100
                               stdout = sys.stderr,
103
                               stdout = sys.stderr,
101
                               stderr = sys.stderr,
104
                               stderr = sys.stderr,
102
                               close_fds = True)
105
                               close_fds = True)
103
            
106
    global _initrunning
107
    _initrunning = False
108
    msgproc.log("Init done")
109
110
_initrunning = True
111
def _uprcl_init():
112
    global _initrunning
113
    _initrunning = True
114
    initthread = threading.Thread(target=_uprcl_init_worker)
115
    initthread.daemon = True 
116
    initthread.start()
117
118
def _ready():
119
    global _initrunning
120
    if _initrunning:
121
        return False
122
    else:
123
        return True
124
    
104
@dispatcher.record('trackuri')
125
@dispatcher.record('trackuri')
105
def trackuri(a):
126
def trackuri(a):
106
    # This is used for plugins which generate temporary local urls
127
    # This is used for plugins which generate temporary local urls
107
    # pointing to the microhttpd instance. The microhttpd
128
    # pointing to the microhttpd instance. The microhttpd
108
    # answer_to_connection() routine in plgwithslave calls 'trackuri'
129
    # answer_to_connection() routine in plgwithslave calls 'trackuri'
...
...
139
    uplog("Browse root: rootmap now %s" % rootmap)
160
    uplog("Browse root: rootmap now %s" % rootmap)
140
    return entries
161
    return entries
141
162
142
def _browsedispatch(objid, bflg, httphp, pathprefix):
163
def _browsedispatch(objid, bflg, httphp, pathprefix):
143
    for id,mod in rootmap.iteritems():
164
    for id,mod in rootmap.iteritems():
144
        uplog("Testing %s against %s" % (objid, id))
165
        #uplog("Testing %s against %s" % (objid, id))
145
        if objid.startswith(id):
166
        if objid.startswith(id):
146
            if mod == 'folders':
167
            if mod == 'folders':
147
                return uprclfolders.browse(objid, bflg, httphp, pathprefix)
168
                return uprclfolders.browse(objid, bflg, httphp, pathprefix)
148
            elif mod == 'tags':
169
            elif mod == 'tags':
149
                return uprcltags.browse(objid, bflg, httphp, pathprefix)
170
                return uprcltags.browse(objid, bflg, httphp, pathprefix)
...
...
156
@dispatcher.record('browse')
177
@dispatcher.record('browse')
157
def browse(a):
178
def browse(a):
158
    msgproc.log("browse: %s" % a)
179
    msgproc.log("browse: %s" % a)
159
    if 'objid' not in a:
180
    if 'objid' not in a:
160
        raise Exception("No objid in args")
181
        raise Exception("No objid in args")
182
161
    objid = a['objid']
183
    objid = a['objid']
162
    bflg = a['flag'] if 'flag' in a else 'children'
184
    bflg = a['flag'] if 'flag' in a else 'children'
163
    
185
    
164
    if not objid.startswith(g_myprefix):
186
    if not objid.startswith(g_myprefix):
165
        raise Exception("bad objid <%s>" % objid)
187
        raise Exception("bad objid <%s>" % objid)
166
188
167
    idpath = objid.replace(g_myprefix, '', 1)
189
    idpath = objid.replace(g_myprefix, '', 1)
168
    msgproc.log("browse: idpath: <%s>" % idpath)
190
    msgproc.log("browse: idpath: <%s>" % idpath)
169
191
192
170
    entries = []
193
    entries = []
171
194
    nocache = "0"
172
    if bflg == 'meta':
195
    if bflg == 'meta':
173
        raise Exception("uprcl-app: browse: can't browse meta for now")
196
        raise Exception("uprcl-app: browse: can't browse meta for now")
174
    else:
197
    else:
198
        if not _ready():
199
            entries = [rcldirentry(objid + 'notready', objid,
200
                                   'Initializing...'),]
201
            nocache = "1"
175
        if not idpath:
202
        elif not idpath:
176
            entries = _rootentries()
203
            entries = _rootentries()
177
        else:
204
        else:
178
            entries = _browsedispatch(objid, bflg, httphp, pathprefix)
205
            entries = _browsedispatch(objid, bflg, httphp, pathprefix)
179
206
180
    #msgproc.log("%s" % entries)
207
    #msgproc.log("%s" % entries)
181
    encoded = json.dumps(entries)
208
    encoded = json.dumps(entries)
182
    return {"entries" : encoded}
209
    return {"entries" : encoded, "nocache":nocache}
183
210
184
211
185
@dispatcher.record('search')
212
@dispatcher.record('search')
186
def search(a):
213
def search(a):
187
    msgproc.log("search: [%s]" % a)
214
    msgproc.log("search: [%s]" % a)
188
    objid = a['objid']
215
    objid = a['objid']
189
    if re.match('0\$uprcl\$', objid) is None:
216
    if re.match('0\$uprcl\$', objid) is None:
190
        raise Exception("bad objid [%s]" % objid)
217
        raise Exception("bad objid [%s]" % objid)
191
218
192
    upnps = a['origsearch']
219
    upnps = a['origsearch']
220
    nocache = "0"
193
221
222
    if not _ready():
223
        entries = [rcldirentry(objid + 'notready', objid, 'Initializing...'),]
224
        nocache = "1"
225
    else:
194
    entries = uprclsearch.search(rclconfdir, objid, upnps, g_myprefix,
226
        entries = uprclsearch.search(rclconfdir, objid, upnps, g_myprefix,
195
                                 httphp, pathprefix)
227
                                     httphp, pathprefix)
196
    
228
197
    encoded = json.dumps(entries)
229
    encoded = json.dumps(entries)
198
    return {"entries" : encoded}
230
    return {"entries" : encoded, "nocache":nocache}
199
231
200
232
201
uprcl_init()
233
_uprcl_init()
202
234
203
msgproc.log("Uprcl running")
235
msgproc.log("Uprcl running")
204
msgproc.mainloop()
236
msgproc.mainloop()