def initialize(): """ This imports all packages that are descendant of this package, and, after that, it register all subclases of service provider as """ import os.path import pkgutil import sys from uds.core import jobs from uds.core.managers.TaskManager import TaskManager # Dinamycally import children of this package. pkgpath = os.path.dirname(sys.modules[__name__].__file__) for _, name, _ in pkgutil.iter_modules([pkgpath]): logger.debug('Importing {}'.format(name)) __import__(name, globals(), locals(), [], 1) p = jobs.Job # This is marked as error in IDE, but it's not (__subclasses__) for cls in p.__subclasses__(): logger.debug('Examining worker {}'.format(cls.__module__)) # Limit to autoregister just workers jobs inside this module if cls.__module__[0:16] == 'uds.core.workers': logger.debug('Added worker {} to list'.format(cls.__module__)) TaskManager.registerJob(cls)
def handle(self, *args, **options): logger.info("Running task manager command") GlobalConfig.initialize() start = options['start'] and True or False stop = options['stop'] and True or False pid = None try: pid = int(open(getPidFile(), 'r').readline()) except Exception: pid = None if stop is True and pid is not None: try: logger.info('Stopping task manager. pid: %s', pid) os.kill(pid, signal.SIGTERM) time.sleep(1) # Wait a bit before running new one os.unlink(getPidFile()) except Exception: logger.error( "Could not stop task manager (maybe it's not runing?)") os.unlink(getPidFile()) if start is True: logger.info('Starting task manager.') become_daemon(settings.BASE_DIR, settings.LOGDIR + '/taskManagerStdout.log', settings.LOGDIR + '/taskManagerStderr.log') pid = str(os.getpid()) open(getPidFile(), 'w+').write('{}\n'.format(pid)) manager = TaskManager() manager.run() if start is False and stop is False: if pid is not None: sys.stdout.write( "Task manager found running (pid file exists: {0})\n". format(pid)) else: sys.stdout.write( "Task manager not foud (pid file do not exits)\n")
def handle(self, *args, **options): logger.info("Running task manager command") GlobalConfig.initialize() start = options['start'] and True or False stop = options['stop'] and True or False pid = None try: pid = int(file(getPidFile(), 'r').readline()) except Exception: pid = None if stop is True and pid is not None: try: logger.info('Stopping task manager. pid: {0}'.format(pid)) os.kill(pid, signal.SIGTERM) time.sleep(1) # Wait a bit before running new one os.unlink(getPidFile()) except Exception: logger.error("Could not stop task manager (maybe it's not runing?)") os.unlink(getPidFile()) if start is True: logger.info('Starting task manager.') become_daemon(settings.BASE_DIR, settings.LOGDIR + '/taskManagerStdout.log', settings.LOGDIR + '/taskManagerStderr.log') pid = six.text_type(os.getpid()) open(getPidFile(), 'w+').write("%s\n" % pid) manager = TaskManager() manager.run() if start is False and stop is False: if pid is not None: sys.stdout.write("Task manager found running (pid file exists: {0})\n".format(pid)) else: sys.stdout.write("Task manager not foud (pid file do not exits)\n")