class LoopRunner(): LOOP_INTERVAL = 65 def __init__(self): logger_manager = LoggerManager() self.log = logger_manager.get_new_logger("loop runner") self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/foo.pid' self.pidfile_timeout = 5 self.automatic_correction_runner = AutomaticCorrectionRunner() self.administrator_mail = AdministratorMail() def stall_loop(self, start_timestamp, finish_timestamp): delta = finish_timestamp - start_timestamp time_to_wait = self.loop_interval - delta.seconds # if the process took less than 30 seconds, we will wait if (time_to_wait > 0): time.sleep(time_to_wait) else: print("not sleeping... delta: " + str(delta)) start_timestamp = datetime.today() def print_result(self, result): # TODO: pass to a log entry print str(datetime.today()) + " | " + str(result) def run(self): self.log = LoggerManager().get_new_logger("daemon control") self.log.info("Daemon's game loop started.") while True: start_timestamp = datetime.today() try: result = self.automatic_correction_runner.run() self.log.info( "Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d", result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY], result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY]) except: self.log.exception( "The automatic correction process failed. Have in mind that is required that the web service must be online." ) try: self.administrator_mail.send_mails() except: self.log.exception( "The mail sending process failed. Have in mind that is required that the web service must be online." ) finish_timestamp = datetime.today() self.stall_loop(start_timestamp, finish_timestamp)
def __init__(self): logger_manager = LoggerManager() self.log = logger_manager.get_new_logger("loop runner") self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/foo.pid' self.pidfile_timeout = 5 self.automatic_correction_runner = AutomaticCorrectionRunner() self.administrator_mail = AdministratorMail()
class LoopRunner(): LOOP_INTERVAL = 65 def __init__(self): logger_manager = LoggerManager() self.log = logger_manager.get_new_logger("loop runner") self.loop_interval = LoopRunner.LOOP_INTERVAL # in seconds self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/foo.pid' self.pidfile_timeout = 5 self.automatic_correction_runner = AutomaticCorrectionRunner() self.administrator_mail = AdministratorMail() def stall_loop(self, start_timestamp, finish_timestamp): delta = finish_timestamp - start_timestamp time_to_wait = self.loop_interval - delta.seconds # if the process took less than 30 seconds, we will wait if (time_to_wait > 0): time.sleep(time_to_wait) else: print("not sleeping... delta: " + str(delta)) start_timestamp = datetime.today() def print_result(self, result): # TODO: pass to a log entry print str(datetime.today()) + " | " + str(result) def run(self): self.log = LoggerManager().get_new_logger("daemon control") self.log.info("Daemon's game loop started.") while True: start_timestamp = datetime.today() try: result = self.automatic_correction_runner.run() self.log.info("Automatic correction process completed.\nResult summary: \n\tsuccessfull: %d\n\tfailed: %d", result[AutomaticCorrectionRunner.SUCCESSFULL_RESULTS_KEY], result[AutomaticCorrectionRunner.FAILED_RESULTS_KEY]) except: self.log.exception("The automatic correction process failed. Have in mind that is required that the web service must be online.") try: self.administrator_mail.send_mails() except: self.log.exception("The mail sending process failed. Have in mind that is required that the web service must be online.") finish_timestamp = datetime.today() self.stall_loop(start_timestamp, finish_timestamp)
def testSendMailsMustIterateTheMailsToBeSentAndPassThemToTheServerSessionObtainedFromTheServerHandler(self): mail_administrator = AdministratorMail() session_helper_mock = Mock() session_mock = Mock() session_helper_mock.get_server_session.return_value = session_mock mail_fetch_strategy_mock = Mock() pending_mails = (Mock(), Mock(), Mock()) mail_fetch_strategy_mock.get_pending_mails.return_value = pending_mails mail_administrator.mail_handle_strategy = mail_fetch_strategy_mock mail_administrator.session_helper = session_helper_mock mail_administrator.send_mails() session_helper_mock.get_server_session.assert_called() session_helper_mock.close_server_session.assert_called() session_mock.sendmail.assert_called()