def run(self): errors_file = self.config['workDir'] + '/GAMMU_ERRORS' errors_threshold_file = self.config['workDir'] + '/ERRORS_THRESHOLD_BEFORE_REBOOT' sms_processing_error = True try: sms_processed_status = self.__process_next_sms() sms_processing_error = False self.__update_balance_if_necessary() #except: # debug("error occurred while trying to process sms: " + sys.exc_info()[0]) finally: reboot_scheduled = False if sms_processing_error: current_reboot_threshold = 4 #first forced reboot after this number of successive gammu errors schedule_reboot = False if os.path.isfile(errors_threshold_file): with open(errors_threshold_file, 'r') as f: current_reboot_threshold = self.__read_file_and_parse_first_int(f) new_error_count = 1 if os.path.isfile(errors_file): with open(errors_file, 'r+') as f: old_error_count = self.__read_file_and_parse_first_int(f) new_error_count = old_error_count + 1 f.seek(0) self.__write_int_to_file(f, new_error_count) else: with open(errors_file, 'w') as f: self.__write_int_to_file(f, new_error_count) schedule_reboot = new_error_count >= current_reboot_threshold next_reboot_threshold = 2 * current_reboot_threshold debug("caught error number {} while trying to process next sms, scheduling reboot? {}.".format(new_error_count, schedule_reboot)) if schedule_reboot: with open(errors_threshold_file, 'w') as f: self.__write_int_to_file(f, next_reboot_threshold) reboot_scheduled = True return_code = subprocess.call(['/usr/bin/sudo', '/sbin/shutdown', '-r', 'now'], bufsize=-1, stderr=subprocess.STDOUT) debug("reboot scheduled (return code:{}), next gammu error threshold is {} ...".format(return_code, next_reboot_threshold)) else: os.remove(errors_file) if os.path.isfile(errors_file) else None os.remove(errors_threshold_file) if os.path.isfile(errors_threshold_file) else None reboot_interval_days = self.config['rebootIntervalDays'] if not reboot_scheduled and reboot_interval_days > 0: max_uptime_seconds = reboot_interval_days * 24 * 60 * 60 uptime_seconds = systeminfo.get_uptime_seconds() if uptime_seconds > max_uptime_seconds: debug("current uptime {} exceeds max configured uptime {}, scheduling reboot.".format(uptime_seconds, max_uptime_seconds)) subprocess.call(['/usr/bin/sudo', '/sbin/shutdown', '-r', 'now'], bufsize=-1, stderr=subprocess.STDOUT)
def run(self): errors_file = self.config["workDir"] + "/GAMMU_ERRORS" errors_threshold_file = self.config["workDir"] + "/ERRORS_THRESHOLD_BEFORE_REBOOT" sms_processing_error = True try: sms_processed_status = self.__process_next_sms() sms_processing_error = False self.__update_balance_if_necessary() # except: # debug("error occurred while trying to process sms: " + sys.exc_info()[0]) finally: reboot_scheduled = False if sms_processing_error: current_reboot_threshold = 4 # first forced reboot after this number of successive gammu errors schedule_reboot = False if os.path.isfile(errors_threshold_file): with open(errors_threshold_file, "r") as f: current_reboot_threshold = self.__read_file_and_parse_first_int(f) new_error_count = 1 if os.path.isfile(errors_file): with open(errors_file, "r+") as f: old_error_count = self.__read_file_and_parse_first_int(f) new_error_count = old_error_count + 1 f.seek(0) self.__write_int_to_file(f, new_error_count) else: with open(errors_file, "w") as f: self.__write_int_to_file(f, new_error_count) schedule_reboot = new_error_count >= current_reboot_threshold next_reboot_threshold = 2 * current_reboot_threshold debug( "caught error number {} while trying to process next sms, scheduling reboot? {}.".format( new_error_count, schedule_reboot ) ) if schedule_reboot: with open(errors_threshold_file, "w") as f: self.__write_int_to_file(f, next_reboot_threshold) reboot_scheduled = True return_code = subprocess.call( ["/usr/bin/sudo", "/sbin/shutdown", "-r", "now"], bufsize=-1, stderr=subprocess.STDOUT ) debug( "reboot scheduled (return code:{}), next gammu error threshold is {} ...".format( return_code, next_reboot_threshold ) ) else: os.remove(errors_file) if os.path.isfile(errors_file) else None os.remove(errors_threshold_file) if os.path.isfile(errors_threshold_file) else None reboot_interval_days = self.config["rebootIntervalDays"] if not reboot_scheduled and reboot_interval_days > 0: max_uptime_seconds = reboot_interval_days * 24 * 60 * 60 uptime_seconds = systeminfo.get_uptime_seconds() if uptime_seconds > max_uptime_seconds: debug( "current uptime {} exceeds max configured uptime {}, scheduling reboot.".format( uptime_seconds, max_uptime_seconds ) ) subprocess.call( ["/usr/bin/sudo", "/sbin/shutdown", "-r", "now"], bufsize=-1, stderr=subprocess.STDOUT )
# How to run this method from command-line: # python -c 'from process_sms import test; test()' # def test(): debug("welcome to test()") config_parser = ConfigParser.SafeConfigParser() config_parser.read("/home/pi/sms-temperature-control/my.cfg") tc = TemperatureController(config_parser) if __name__ == "__main__": uptime_threshold = 5 * 60 uptime = systeminfo.get_uptime_seconds() if uptime > uptime_threshold: # otherwise allow reboot script to run completely to clean up etc. config_parser = ConfigParser.SafeConfigParser() config_parser.read(CONFIG_FILEPATH) pgrep_pattern = "python .*" + os.path.basename(__file__) + "\\'" pgrep_pids = systeminfo.get_pgrep_pids(pgrep_pattern) if len(pgrep_pids) < 1: debug("pgrep pattern wrong, my own script process not found: {}".format(pgrep_pattern)) elif len(pgrep_pids) == 1 and pgrep_pids[0] == os.getpid(): debug( "START no other pid found for this script (PID: {}), going ahead with sms processing...".format( pgrep_pids )
# How to run this method from command-line: # python -c 'from process_sms import test; test()' # def test(): debug("welcome to test()") config_parser = ConfigParser.SafeConfigParser() config_parser.read('/home/pi/sms-temperature-control/my.cfg') tc = TemperatureController(config_parser) if __name__ == '__main__': uptime_threshold = 5*60 uptime = systeminfo.get_uptime_seconds() if uptime > uptime_threshold: # otherwise allow reboot script to run completely to clean up etc. config_parser = ConfigParser.SafeConfigParser() config_parser.read(CONFIG_FILEPATH) pgrep_pattern = 'python .*' + os.path.basename(__file__) + '\\\'' pgrep_pids = systeminfo.get_pgrep_pids(pgrep_pattern) if len(pgrep_pids) < 1: debug("pgrep pattern wrong, my own script process not found: {}".format(pgrep_pattern)) elif len(pgrep_pids) == 1 and pgrep_pids[0] == os.getpid(): debug("START no other pid found for this script (PID: {}), going ahead with sms processing...".format(pgrep_pids)) temperature_controller = TemperatureController(config_parser) temperature_controller.run() debug("DONE sms processing.")