Use ctime, not atime to find old files

This commit is contained in:
niten 2023-06-05 11:35:04 -07:00
parent 21f897b5c3
commit 3ab9a2faa6
2 changed files with 54 additions and 51 deletions

View File

@ -82,54 +82,56 @@ in {
}; };
}; };
systemd.services.objectifier = { systemd.services = {
after = [ "network-online.target" ]; objectifier = {
wantedBy = [ "multi-user.target" ]; after = [ "network-online.target" ];
environment = { wantedBy = [ "multi-user.target" ];
OBJECTIFIER_YOLOV3_CONFIG = "${pkgs.yolov3-data}/yolov3.cfg"; environment = {
OBJECTIFIER_YOLOV3_WEIGHTS = "${pkgs.yolov3-data}/yolov3.weights"; OBJECTIFIER_YOLOV3_CONFIG = "${pkgs.yolov3-data}/yolov3.cfg";
OBJECTIFIER_YOLOV3_LABELS = "${pkgs.yolov3-data}/labels"; OBJECTIFIER_YOLOV3_WEIGHTS = "${pkgs.yolov3-data}/yolov3.weights";
OBJECTIFIER_BUFFER_SIZE = "524288"; OBJECTIFIER_YOLOV3_LABELS = "${pkgs.yolov3-data}/labels";
OBJECTIFIER_CLEANUP_MAX_AGE = toString cfg.cleanup.max_file_age; OBJECTIFIER_BUFFER_SIZE = "524288";
OBJECTIFIER_CLEANUP_DELAY = toString cfg.cleanup.delay; OBJECTIFIER_CLEANUP_MAX_AGE = toString cfg.cleanup.max_file_age;
OBJECTIFIER_TIMEOUT = toString cfg.detection-timeout; OBJECTIFIER_CLEANUP_DELAY = toString cfg.cleanup.delay;
OBJECTIFIER_POOL_SIZE = toString cfg.pool-size; OBJECTIFIER_TIMEOUT = toString cfg.detection-timeout;
}; OBJECTIFIER_POOL_SIZE = toString cfg.pool-size;
serviceConfig = { };
PrivateUsers = true; serviceConfig = {
PrivateDevices = true; PrivateUsers = true;
PrivateTmp = true; PrivateDevices = true;
PrivateMounts = true; PrivateTmp = true;
ProtectControlGroups = true; PrivateMounts = true;
ProtectKernelTunables = true; ProtectControlGroups = true;
ProtectKernelModules = true; ProtectKernelTunables = true;
ProtectSystem = true; ProtectKernelModules = true;
ProtectHostname = true; ProtectSystem = true;
ProtectHome = true; ProtectHostname = true;
ProtectClock = true; ProtectHome = true;
ProtectKernelLogs = true; ProtectClock = true;
# DynamicUser = true; ProtectKernelLogs = true;
MemoryDenyWriteExecute = true; # DynamicUser = true;
RestrictRealtime = true; MemoryDenyWriteExecute = true;
LockPersonality = true; RestrictRealtime = true;
PermissionsStartOnly = true; LockPersonality = true;
WorkingDirectory = "${pkgs.objectifier}"; PermissionsStartOnly = true;
StateDirectory = "objectifier"; WorkingDirectory = "${pkgs.objectifier}";
CacheDirectory = "objectifier"; StateDirectory = "objectifier";
RuntimeDirectory = "objectifier"; CacheDirectory = "objectifier";
LimitNOFILE = 4096; RuntimeDirectory = "objectifier";
Restart = "on-failure"; LimitNOFILE = 4096;
RestartSec = "5s"; Restart = "on-failure";
Type = "simple"; RestartSec = "5s";
ExecStart = let Type = "simple";
bindClause = concatStringsSep " " ExecStart = let
(map (addr: "--bind ${addr}:${toString cfg.port}") bindClause = concatStringsSep " "
cfg.listen-addresses); (map (addr: "--bind ${addr}:${toString cfg.port}")
in (concatStringsSep " " [ cfg.listen-addresses);
"${pkgs.objectifier}/bin/objectifier" in (concatStringsSep " " [
bindClause "${pkgs.objectifier}/bin/objectifier"
"--workers ${toString cfg.workers}" bindClause
]); "--workers ${toString cfg.workers}"
]);
};
}; };
}; };
}; };

View File

@ -4,7 +4,7 @@ from detector import Detector
from fastapi import FastAPI, HTTPException, Request, UploadFile, File from fastapi import FastAPI, HTTPException, Request, UploadFile, File
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from os import listdir, remove 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 pathlib import Path
from threading import Thread from threading import Thread
from time import sleep 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') yolo_labels_file = get_envvar_or_fail('OBJECTIFIER_YOLOV3_LABELS')
buffer_size = to_int(get_envvar('OBJECTIFIER_BUFFER_SIZE')) or 524288 buffer_size = to_int(get_envvar('OBJECTIFIER_BUFFER_SIZE')) or 524288
max_file_age = to_int(get_envvar('OBJECTIFIER_CLEANUP_MAX_AGE')) 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 detection_timeout = to_int(get_envvar('OBJECTIFIER_TIMEOUT')) or 5
pool_size = to_int(get_envvar('OBJECTIFIER_POOL_SIZE')) or 10 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): def cleanup_old_files(path, extensions, max_age):
for filename in listdir(path): 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) print("removing old output file: " + filename)
remove(filename) remove(filename)
def run_cleanup_thread(path, extensions, age, delay): def run_cleanup_thread(path, extensions, age, delay):
while True: while True:
print("running cleanup thread")
cleanup_old_files(path, extensions, age) cleanup_old_files(path, extensions, age)
sleep(delay) sleep(delay)