Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.daemonizable = Mock()
self.daemon = daemon.Daemon(daemonizable=self.daemonizable)
def test_start(self, mock_DaemonContext, mock_PidFile, mock_logging):
daemon.Daemon.exit_flag = False
res = self.daemon.start()
self.assertIsNone(res)
self.assertEquals(daemon.Daemon.exit_flag, True)
self.assertTrue(self.daemonizable.start.called)
#
def test_create(self):
self.assertIsInstance(self.daemon, daemon.Daemon)
if CONF.no_daemon:
print('Freezer Scheduler running in no-daemon mode')
LOG.debug('Freezer Scheduler running in no-daemon mode')
if winutils.is_windows():
daemon = win_daemon.NoDaemon(daemonizable=freezer_scheduler)
else:
daemon = linux_daemon.NoDaemon(daemonizable=freezer_scheduler)
else:
if winutils.is_windows():
daemon = win_daemon.Daemon(daemonizable=freezer_scheduler,
interval=int(CONF.interval),
job_path=CONF.jobs_dir,
insecure=CONF.insecure,
concurrent_jobs=CONF.concurrent_jobs)
else:
daemon = linux_daemon.Daemon(daemonizable=freezer_scheduler)
if CONF.action == 'start':
daemon.start()
elif CONF.action == 'stop':
daemon.stop()
elif CONF.action == 'restart':
daemon.restart()
elif CONF.action == 'reload':
daemon.reload()
elif CONF.action == 'status':
daemon.status()
# os.RETURN_CODES are only available to posix like systems, on windows
# we need to translate the code to an actual number which is the equivalent
return 0 # os.EX_OK
def __init__(self, daemonizable=None, pid_fname=None):
# daemonizable has to provide start/stop (and possibly reload) methods
Daemon.instance = self
self._pid_fname = pid_fname
self.daemonizable = daemonizable
def signal_map(self):
return {
signal.SIGTERM: Daemon.handle_program_exit,
signal.SIGHUP: Daemon.handle_reload,
}
def start(self, dump_stack_trace=False):
if os.path.exists(self.pid_fname) and \
is_process_running(self.pid):
print('freezer daemon is already running, '
'pid: {0}'.format(self.pid))
return
pidfile = PidFile(self.pid_fname)
files_preserve = get_filenos(LOG.logger)
with DaemonContext(pidfile=pidfile, signal_map=self.signal_map,
files_preserve=files_preserve):
while not Daemon.exit_flag:
try:
LOG.info('freezer daemon starting, pid: {0}'.
format(self.pid))
self.daemonizable.start()
Daemon.exit_flag = True
except Exception as e:
if dump_stack_trace:
LOG.error(traceback.format_exc(e))
LOG.error('Restarting daemonized procedure '
'after Fatal Error: {0}'.format(e))
sleep(10)
LOG.info('freezer daemon done, pid: {0}'.format(self.pid))