logger.error( "Pending %s messages is above data loss threshold %d, sadly pending list set to zero :S", len(pending_documents), PENDING_DOCUMENTS_LENGTH_ERROR) pending_documents = [] # data loss here :'( elif len(pending_documents) > PENDING_DOCUMENTS_LENGTH_WARNING: logger.alert( "Pending %s messages is above warning threshold %d, data loss will occur at %d", len(pending_documents), PENDING_DOCUMENTS_LENGTH_WARNING, PENDING_DOCUMENTS_LENGTH_ERROR) else: logger.warning("Connection with database is failing") raise logger.info("Inserted %d documents", len(insert_batch)) mongo_client.close() except: print "Unexpected error:", traceback.format_exc() logger.error("Unexpected error: %s", traceback.format_exc()) if __name__ == "__main__": Scheduler.start() start() Scheduler.repeat_o_clock_with_offset(PERIOD * 1000, PERIOD / 12 * 1000, upload_data) try: while True: time.sleep(60) except: stop() raise
import raspi_mon_sys.Scheduler as sched import time def say(something, more): print(time.time(), something, more) sched.start() sched.once_after(100, say, "it's", "me") sched.once_when(int(time.time() * 1000 + 200), say, "it's", "me") a = sched.repeat_every(5000, say, "Hello", "World") b = sched.repeat_o_clock("60s", say, "One", "Minute") b = sched.repeat_o_clock_with_offset("60s", 5, say, "One", "Minute plus five") time.sleep(31) sched.remove(a) print("Removed ", time.time()) c = sched.repeat_o_clock_with_offset(500, 5, say, "something", "funny") time.sleep(31) print("Stopping", time.time()) sched.stop() print("Stopped ", time.time())
import raspi_mon_sys.Scheduler as sched import time def say(something, more): print(time.time(), something, more) sched.start() sched.once_after(100, say, "it's", "me") sched.once_when(int(time.time()*1000 + 200), say, "it's", "me") a = sched.repeat_every(5000, say, "Hello", "World") b = sched.repeat_o_clock("60s", say, "One", "Minute") b = sched.repeat_o_clock_with_offset("60s", 5, say, "One", "Minute plus five") time.sleep(31) sched.remove(a) print("Removed ", time.time()) c = sched.repeat_o_clock_with_offset(500, 5, say, "something", "funny") time.sleep(31) print("Stopping", time.time()) sched.stop() print("Stopped ", time.time())
config["when" + suffix] = t # check state transition before message is appended if state != last_state: state_message = {'timestamp': t, 'data': state} messages.append((topic.format(name, "state", mac), state_message)) config["state"] = state # track current state value except: print "Unexpected error:", traceback.format_exc() logger.info("Error happened while processing circles data: %s", traceback.format_exc()) for top, message in messages: client.publish(top, json.dumps(message)) except: print "Unexpected error:", traceback.format_exc() logger.error("Error happened while processing circles data") raise if __name__ == "__main__": Utils.startup_wait() start() Scheduler.start() Scheduler.repeat_o_clock(config["period"], publish) try: while True: time.sleep(60) except: Scheduler.stop() stop()
T1_MILISECOND = 1 T1_CENTISECOND = 10 T1_DECISECOND = 100 T1_SECOND = 1000 T1_MINUTE = 60000 T1_HOUR = 3600000 T1_DAY = 24 * T1_HOUR # Configure logger. logger = LoggerClient.open("MainMonitoringSystem") logger.info("Initializing main monitoring system") # Configure Scheduler. Scheduler.start() logger.info("Scheduler started") # Start all modules. started_modules = [] def try_start(module_info): logger.info("Starting module %s", module_info["import"]) module = importlib.import_module(module_info["import"]) if __try_call(logger, module.start): started_modules.append(module) if "schedules" in module_info: for sched in module_info["schedules"]: method = getattr(Scheduler, sched["method"]) args = [ __replace_vars(x,module) for x in sched["args"] ]