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")