The Problem
Data Exchange needs around eleven separate Python apps running to work, and keeping them in separate terminal tabs is both tedious and slow. I want a way to start/stop/restart them quickly.
The Solution
Dorian had the idea of using the Twisted ProcessMonitor which implements the IServiceCollection. The code (git link) ends up pretty compact:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | from twisted.application import service from twisted.runner import procmon import logging from os import environ dx_components = [ 'cache', 'controller', 'csv', 'distributor', 'fetcher', 'notification', 'persister', 'proxy', 'pub_sub', 'user_notification', 'user_pub_sub', ] logging.basicConfig(level=logging.DEBUG, \ format='%(asctime)s %(levelname)s (%(funcName)s) %(message)s') logging.info('Starting up...') mon = procmon.ProcessMonitor() for comp in dx_components: logging.info('Adding ' + comp) mon.addProcess(comp, ['python', './ooidx/%s.py' % comp], env=environ.data) application = service.Application('dx_process_monitor') mon.setServiceParent(service.IServiceCollection(application)) logging.info('Now starting programs...') |
Notes and limitations
- For some reason, if you don't populate the env yourself as shown, you get a bare-bones version with no python in the path. Probably a bug in Twisted.
- Logging is now a mess. I'm working on this - might need to override the stdout/stderr for each app and redirect to a file.
Next steps
- This starts and stops all apps en masse. Perhaps a http interface or command input to restart individual apps.
- Change logging on the fly would be useful, especially for a specific app.