def __init__(self, daemonType, daemonId, callback): coreConfig = CoreConfig() self.logFile = coreConfig.getLogFile(daemonType) #, daemonId) self.pidFile = coreConfig.getPidFile(daemonType) #, daemonId) self.name = 'chains-%s-%s' % (daemonType, daemonId) self.daemonType = daemonType self.daemonId = daemonId self.callback = callback
def main(daemonType, fork=True): coreConfig = CoreConfig() conf = coreConfig.data(daemonType) if not conf: raise Exception('No section "daemon_%s" in config' % id) conf['id'] = resolveDaemonId(conf['id']) for k in conf: if k[0:4] == 'env_': log.info('Set ENV.%s = %s' % (k[4:], conf[k])) os.environ[k[4:]] = conf[k] d = Daemon(daemonType, conf['id'], getDaemonMainFunction(daemonType)) if fork: d.fork() else: d.main()
def main(daemonType, fork=True): coreConfig = CoreConfig() conf = coreConfig.data(daemonType) if not conf: raise Exception('No section "daemon_%s" in config' % id) conf['id'] = resolveDaemonId(conf['id']) for k in conf: if k[0:4] == 'env_': log.info('Set ENV.%s = %s' % (k[4:],conf[k])) os.environ[k[4:]] = conf[k] d = Daemon(daemonType, conf['id'], getDaemonMainFunction(daemonType)) if fork: d.fork() else: d.main()
''' Process manager for services using subprocess ''' from chains.common import log from chains.common.config import CoreConfig from chains import service import os, signal, time, subprocess, json, psutil, types processes = {} config = CoreConfig() ''' todo: * capture stdout/stderr? --> no, do it in service-runner - option for relative path in config.command? then need servicedir config or something * move pidfile stuff here, so non-py devs does not have to do it themselves - some duplicate code here and in daemon/__init__.py - consolidate? ''' def start(serviceConfig): serviceId = serviceConfig['main'].get('id') log.info('Start service: %s' % serviceId) pid = isRunning(serviceId) if pid: log.error( "Could not start service %s because already running on pid %s" %
def test_When_getting_pidfile_It_returns_the_correct_path(self): conf = CoreConfig(file=self.path) path = conf.getPidFile('manager') self.assertEquals('/var/run/chains/manager.pid', path)
def test_When_getting_logfile_It_returns_the_correct_path(self): conf = CoreConfig(file=self.path) path = conf.getLogFile('manager') self.assertEquals('/var/log/chains/manager.log', path)
def test_When_config_is_missing_It_is_auto_created(self): conf = CoreConfig(file=self.path) self.assertEquals('/etc/chains', conf.get('confdir')) self.assertEquals('{hostname}', conf.get('id', 'manager'))
# vim: tabstop=4 shiftwidth=4 sts=4 bg=dark expandtab import logging, logging.handlers from chains.common.config import CoreConfig import types, json, os level = None logger = None config = CoreConfig() def setLevel(_level): if not logger: return levels = { 'notice': 5, 'debug': logging.DEBUG, 'info': logging.INFO, 'warn': logging.WARNING, 'error': logging.ERROR } logger.setLevel(levels[_level]) global level level = _level def getLevel(): global level return level def setFileName(name): if not os.path.exists(config.get('logdir')): os.makedirs(config.get('logdir'))