comparison service/mqtt_to_rdf/mqtt_to_rdf.py @ 1647:34eb87f68ab8

WIP rules reloader (doesn't reload yet)
author drewp@bigasterisk.com
date Fri, 17 Sep 2021 11:07:21 -0700
parents 9e7f571deedf
children 2085ed9cfcc4
comparison
equal deleted inserted replaced
1646:af136cf6dd26 1647:34eb87f68ab8
345 345
346 def unbind(self): 346 def unbind(self):
347 self.loop.stop() 347 self.loop.stop()
348 348
349 349
350 @dataclass
351 class WatchFiles:
352 # could be merged with rdfdb.service and GraphFile code
353 globPattern: str
354 outGraph: Graph
355
356 def __post_init__(self):
357 self.lastUpdate = 0
358 task.LoopingCall(self.refresh).start(1)
359 log.info(f'start watching {self.globPattern}')
360
361 def refresh(self):
362 files = glob.glob(self.globPattern)
363 for fn in files:
364 if os.path.getmtime(fn) > self.lastUpdate:
365 break
366 else:
367 return
368 self.lastUpdate = time.time()
369 self.outGraph.remove((None, None, None))
370 log.info('reread config')
371 for fn in files:
372 # todo: handle read errors
373 self.outGraph.parse(fn, format='n3')
374 # and notify this change,so we can recalc the latest output
375
376
350 if __name__ == '__main__': 377 if __name__ == '__main__':
351 arg = docopt(""" 378 arg = docopt("""
352 Usage: mqtt_to_rdf.py [options] 379 Usage: mqtt_to_rdf.py [options]
353 380
354 -v Verbose 381 -v Verbose
356 """) 383 """)
357 verboseLogging(arg['-v']) 384 verboseLogging(arg['-v'])
358 logging.getLogger('mqtt').setLevel(logging.INFO) 385 logging.getLogger('mqtt').setLevel(logging.INFO)
359 logging.getLogger('mqtt_client').setLevel(logging.INFO) 386 logging.getLogger('mqtt_client').setLevel(logging.INFO)
360 logging.getLogger('infer').setLevel(logging.INFO) 387 logging.getLogger('infer').setLevel(logging.INFO)
388 logging.getLogger('cbind').setLevel(logging.INFO)
389 # log.setLevel(logging.DEBUG)
361 log.info('log start') 390 log.info('log start')
362 391
363 config = Graph() 392 config = ConjunctiveGraph()
364 for fn in Path('.').glob('conf/*.n3'): 393 watcher = WatchFiles('conf/rules.n3', config)
365 if not arg['--cs'] or str(arg['--cs']) in str(fn): 394 # for fn in Path('.').glob('conf/*.n3'):
366 log.debug(f'loading {fn}') 395 # if not arg['--cs'] or str(arg['--cs']) in str(fn):
367 config.parse(str(fn), format='n3') 396 # log.debug(f'loading {fn}')
368 else: 397 # config.parse(str(fn), format='n3')
369 log.debug(f'skipping {fn}') 398 # else:
399 # log.debug(f'skipping {fn}')
370 400
371 masterGraph = PatchableGraph() 401 masterGraph = PatchableGraph()
372 402
373 brokerHost = 'mosquitto-frontdoor.default.svc.cluster.local' 403 brokerHost = 'mosquitto-frontdoor.default.svc.cluster.local'
374 brokerPort = 10210 404 brokerPort = 10210