diff --git a/objectifier-module.nix b/objectifier-module.nix index cf3d207..c1e5bd3 100644 --- a/objectifier-module.nix +++ b/objectifier-module.nix @@ -82,54 +82,56 @@ in { }; }; - systemd.services.objectifier = { - after = [ "network-online.target" ]; - wantedBy = [ "multi-user.target" ]; - environment = { - OBJECTIFIER_YOLOV3_CONFIG = "${pkgs.yolov3-data}/yolov3.cfg"; - OBJECTIFIER_YOLOV3_WEIGHTS = "${pkgs.yolov3-data}/yolov3.weights"; - OBJECTIFIER_YOLOV3_LABELS = "${pkgs.yolov3-data}/labels"; - OBJECTIFIER_BUFFER_SIZE = "524288"; - OBJECTIFIER_CLEANUP_MAX_AGE = toString cfg.cleanup.max_file_age; - OBJECTIFIER_CLEANUP_DELAY = toString cfg.cleanup.delay; - OBJECTIFIER_TIMEOUT = toString cfg.detection-timeout; - OBJECTIFIER_POOL_SIZE = toString cfg.pool-size; - }; - serviceConfig = { - PrivateUsers = true; - PrivateDevices = true; - PrivateTmp = true; - PrivateMounts = true; - ProtectControlGroups = true; - ProtectKernelTunables = true; - ProtectKernelModules = true; - ProtectSystem = true; - ProtectHostname = true; - ProtectHome = true; - ProtectClock = true; - ProtectKernelLogs = true; - # DynamicUser = true; - MemoryDenyWriteExecute = true; - RestrictRealtime = true; - LockPersonality = true; - PermissionsStartOnly = true; - WorkingDirectory = "${pkgs.objectifier}"; - StateDirectory = "objectifier"; - CacheDirectory = "objectifier"; - RuntimeDirectory = "objectifier"; - LimitNOFILE = 4096; - Restart = "on-failure"; - RestartSec = "5s"; - Type = "simple"; - ExecStart = let - bindClause = concatStringsSep " " - (map (addr: "--bind ${addr}:${toString cfg.port}") - cfg.listen-addresses); - in (concatStringsSep " " [ - "${pkgs.objectifier}/bin/objectifier" - bindClause - "--workers ${toString cfg.workers}" - ]); + systemd.services = { + objectifier = { + after = [ "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; + environment = { + OBJECTIFIER_YOLOV3_CONFIG = "${pkgs.yolov3-data}/yolov3.cfg"; + OBJECTIFIER_YOLOV3_WEIGHTS = "${pkgs.yolov3-data}/yolov3.weights"; + OBJECTIFIER_YOLOV3_LABELS = "${pkgs.yolov3-data}/labels"; + OBJECTIFIER_BUFFER_SIZE = "524288"; + OBJECTIFIER_CLEANUP_MAX_AGE = toString cfg.cleanup.max_file_age; + OBJECTIFIER_CLEANUP_DELAY = toString cfg.cleanup.delay; + OBJECTIFIER_TIMEOUT = toString cfg.detection-timeout; + OBJECTIFIER_POOL_SIZE = toString cfg.pool-size; + }; + serviceConfig = { + PrivateUsers = true; + PrivateDevices = true; + PrivateTmp = true; + PrivateMounts = true; + ProtectControlGroups = true; + ProtectKernelTunables = true; + ProtectKernelModules = true; + ProtectSystem = true; + ProtectHostname = true; + ProtectHome = true; + ProtectClock = true; + ProtectKernelLogs = true; + # DynamicUser = true; + MemoryDenyWriteExecute = true; + RestrictRealtime = true; + LockPersonality = true; + PermissionsStartOnly = true; + WorkingDirectory = "${pkgs.objectifier}"; + StateDirectory = "objectifier"; + CacheDirectory = "objectifier"; + RuntimeDirectory = "objectifier"; + LimitNOFILE = 4096; + Restart = "on-failure"; + RestartSec = "5s"; + Type = "simple"; + ExecStart = let + bindClause = concatStringsSep " " + (map (addr: "--bind ${addr}:${toString cfg.port}") + cfg.listen-addresses); + in (concatStringsSep " " [ + "${pkgs.objectifier}/bin/objectifier" + bindClause + "--workers ${toString cfg.workers}" + ]); + }; }; }; }; diff --git a/src/objectifier.py b/src/objectifier.py index 2cec8c6..3c23ab7 100644 --- a/src/objectifier.py +++ b/src/objectifier.py @@ -4,7 +4,7 @@ from detector import Detector from fastapi import FastAPI, HTTPException, Request, UploadFile, File from fastapi.responses import FileResponse from os import listdir, remove -from os.path import getatime, splitext, basename, isfile +from os.path import getctime, splitext, basename, isfile from pathlib import Path from threading import Thread from time import sleep @@ -35,7 +35,7 @@ yolo_weights = get_envvar_or_fail('OBJECTIFIER_YOLOV3_WEIGHTS') yolo_labels_file = get_envvar_or_fail('OBJECTIFIER_YOLOV3_LABELS') buffer_size = to_int(get_envvar('OBJECTIFIER_BUFFER_SIZE')) or 524288 max_file_age = to_int(get_envvar('OBJECTIFIER_CLEANUP_MAX_AGE')) -file_cleanup_delay = to_int(get_envvar('OBJECTIFIER_CLEANUP_DELAY')) +file_cleanup_delay = to_int(get_envvar('OBJECTIFIER_CLEANUP_DELAY')) or 60 detection_timeout = to_int(get_envvar('OBJECTIFIER_TIMEOUT')) or 5 pool_size = to_int(get_envvar('OBJECTIFIER_POOL_SIZE')) or 10 @@ -61,12 +61,13 @@ img_formats = [ "png", "jpg", "gif", "bmp" ] def cleanup_old_files(path, extensions, max_age): for filename in listdir(path): - if (splitext(filename) in extensions) and (getatime(filename) - time.time() > max_age): + if (splitext(filename) in extensions) and (getctime(filename) - time.time() > max_age): print("removing old output file: " + filename) remove(filename) def run_cleanup_thread(path, extensions, age, delay): while True: + print("running cleanup thread") cleanup_old_files(path, extensions, age) sleep(delay)