def test_update(self): with storage.get_connection() as conn: storage.put(conn, "a", "a") self.assertEqual(storage.get(conn, "a"), "a") with storage.get_connection() as conn: storage.put(conn, "a", "b") with storage.get_connection() as conn: self.assertEqual(storage.get(conn, "a"), "b")
def test_put_float(self): with storage.get_connection() as conn: storage.put(conn, "a", 1.1) self.assertEqual(storage.get_float(conn, "a"), 1.1) with storage.get_connection() as conn: storage.put(conn, "b", 1.1) with storage.get_connection() as conn: self.assertEqual(storage.get_float(conn, "b"), 1.1)
def start(): logger.info("starting manager[pid=%s]" % common.PID) storage.setup() config = common.load_config() output = config["output"] for camera in config["cameras"].keys(): logger.info("found camera %s" % camera) PROCESSES.append(camera) MODULES[camera] = "runtime.recorder" segment_dir = "%s/%s" % (output, camera) if not exists(segment_dir): makedirs(segment_dir) logger.info("directory %s created" % segment_dir) del config, output with storage.get_connection() as conn: for name in PROCESSES: metric = "%sStatus" % name storage.put(conn, metric, "Launching") running[name] = start_process(name) storage.put(conn, metric, "Launched") try: loop(conn) finally: logger.info("manager[pid=%s] is stopping" % common.PID)
def test_loop(self): file_count = len(listdir(DATA_DIR)) self.assertEqual(file_count, 3) def loop(cameras, loop_interval): with storage.get_connection() as conn: cleaner.loop(conn, cameras, loop_interval) thread = Thread(target=loop, args=({"front": {"segment_dir": DATA_DIR, "keep": 172800, "duration": 30}}, 1,)) thread.daemon = True thread.start() sleep(1) while 1: file_count = len(listdir(DATA_DIR)) if file_count == 2: break sleep(1) self.assertEqual(file_count, 2) cleaner.stop() thread.join() self.assertEqual(storage.get_int(storage.get_connection(), cleaner.DELETED_TOTAL), 1) self.assertEqual(storage.get_int(storage.get_connection(), cleaner.DELETED_SINCE_START), 1) thread = Thread(target=loop, args=({"front": {"segment_dir": DATA_DIR, "keep": 172800, "duration": 30}}, 1,)) thread.daemon = True thread.start() file_count = len(listdir(DATA_DIR)) self.assertEqual(file_count, 2) minus_3_days = datetime.now() - timedelta(days=3) minus_3_days = mktime(minus_3_days.timetuple()) utime(self.newest_path, (minus_3_days, minus_3_days)) utime(self.newest2_path, (minus_3_days, minus_3_days)) while 1: file_count = len(listdir(DATA_DIR)) if file_count == 0: break sleep(1) self.assertEqual(file_count, 0) cleaner.stop() thread.join() self.assertEqual(storage.get_int(storage.get_connection(), cleaner.DELETED_TOTAL), 3) self.assertEqual(storage.get_int(storage.get_connection(), cleaner.DELETED_SINCE_START), 2)
def test_send(self): config = common.load_config() with storage.get_connection() as conn: storage.put(conn, "reporterStatus", "Running") reporter.running = True client = reporter.connect(config["mqtt.broker"], config["mqtt.port"], config["mqtt.user"], config["mqtt.pass"]) reporter.send_report(client, conn)
def start(): logger.info("starting reported[pid=%s]" % common.PID) config = common.load_config() broker = config["mqtt.broker"] port = config["mqtt.port"] user = config.get("mqtt.user") pwd = config.get("mqtt.pass") del config with storage.get_connection() as conn: storage.put(conn, REPORTER_STATUS, "Running") try: loop(conn, broker, port, user, pwd) finally: storage.put(conn, REPORTER_STATUS, "Not Running") logger.info("stopping watcher[pid=%s]" % common.PID)
def start(): logger.info("starting cleaner[pid=%s]" % common.PID) config = common.load_config() output = config["output"] cameras = config["cameras"] loop_interval = 3600 for camera, config in cameras.items(): config["segment_dir"] = join(output, camera) loop_interval = min(loop_interval, int(config["keep"])) del config, output with storage.get_connection() as conn: storage.put(conn, CLEANER_STATUS, "Running") try: loop(conn, cameras, loop_interval) finally: storage.put(conn, CLEANER_STATUS, "Not Running") logger.info("stopping cleaner[pid=%s]" % common.PID)
def start(): logger.info("starting watcher[pid=%s]" % common.PID) config = common.load_config() output = config["output"] threshold = config["filesystem.threshold"] loop_interval = 10 segment_dirs = [] for camera, config in config["cameras"].items(): segment_dirs.append(join(output, camera)) loop_interval = min(loop_interval, int(config["duration"])) del config with storage.get_connection() as conn: storage.put(conn, WATCHER_STATUS, "Running") try: loop(conn, segment_dirs, loop_interval, output, threshold) finally: storage.put(conn, WATCHER_STATUS, "Not Running") logger.info("stopping watcher[pid=%s]" % common.PID)
def test_get_all(self): with storage.get_connection() as conn: storage.put(conn, "a", "a") storage.put(conn, "b", "b") self.assertEqual(storage.get_all(conn), [("a", "a"), ("b", "b")])
def test_get_keys(self): with storage.get_connection() as conn: storage.put(conn, "a", "a") storage.put(conn, "b", "b") self.assertEqual(list(storage.get_keys(conn)), ["a", "b"])
def test_get_not_exists(self): with storage.get_connection() as conn: self.assertEqual(storage.get(conn, "c"), None)
def test_inc_multiple(self): value = 1 with storage.get_connection() as conn: value = storage.inc(conn, "a", value, 2) self.assertEqual(storage.get_float(conn, "a"), value)
def loop(cameras, loop_interval): with storage.get_connection() as conn: cleaner.loop(conn, cameras, loop_interval)
def loop(segment_dirs, loop_interval, output, threshold): with storage.get_connection() as conn: watcher.loop(conn, segment_dirs, loop_interval, output, threshold)
def test_loop(self): file_count = len(listdir(DATA_DIR)) self.assertEqual(file_count, 2) def loop(segment_dirs, loop_interval, output, threshold): with storage.get_connection() as conn: watcher.loop(conn, segment_dirs, loop_interval, output, threshold) thread = Thread(target=loop, args=( [DATA_DIR], 1, DATA_DIR, self.initial_free, )) thread.daemon = True thread.start() sleep(3) file_count = len(listdir(DATA_DIR)) self.assertEqual(file_count, 2) total, _, _ = disk_usage(DATA_DIR) oldest_path_size = int(total / 50) with open(self.newest_path, "wb") as oldest: oldest.seek(oldest_path_size - 1) oldest.write(b"\0") while 1: file_count = len(listdir(DATA_DIR)) if file_count == 1: break sleep(1) self.assertEqual(file_count, 1) watcher.stop() thread.join() self.assertEqual( storage.get_int(storage.get_connection(), watcher.WATCHER_DELETED_TOTAL), 1) self.assertEqual( storage.get_int(storage.get_connection(), watcher.WATCHER_DELETED_SINCE_START), 1) thread = Thread(target=loop, args=( [DATA_DIR], 1, DATA_DIR, self.initial_free, )) thread.daemon = True thread.start() file_count = len(listdir(DATA_DIR)) self.assertEqual(file_count, 1) with open(self.newest_path, "wb") as oldest: oldest.seek(oldest_path_size - 1) oldest.write(b"\0") while 1: file_count = len(listdir(DATA_DIR)) if file_count == 0: break sleep(1) self.assertEqual(file_count, 0) watcher.stop() thread.join() self.assertEqual( storage.get_int(storage.get_connection(), watcher.WATCHER_DELETED_TOTAL), 2) self.assertEqual( storage.get_int(storage.get_connection(), watcher.WATCHER_DELETED_SINCE_START), 1)