Skip to content
Snippets Groups Projects
Commit c7d503cf authored by François  LAURENT's avatar François LAURENT
Browse files

feat: token expiry with automatic cleanup

parent c758c2b1
No related branches found
No related tags found
1 merge request!25Set of commits to be tagged v0.20.1
Pipeline #154584 passed
#!/usr/bin/env bash
set -e
for flag in "$@"; do
if [ "$flag" = "-h" -o "$flag" = "--help" ]; then
echo "Command-line installer for LarvaTagger"
......@@ -278,9 +280,6 @@ EOF
}
if [ -n "$WITH_BACKEND" ]; then
if [ "`uname`" = "Darwin" ]; then
echo "WARNING: the default tagging backend is not supported by macOS"
fi
if ! command -v python$PYTHON_VERSION &>/dev/null; then
if command -v pyenv &>/dev/null; then
[ `pyenv versions | grep $PYTHON_VERSION` ] || pyenv install $PYTHON_VERSION
......
......@@ -14,13 +14,15 @@ struct LTBackend
root
tokens
lock
token_expiry
end
function LTBackend()
root = Ref{AbstractString}("")
tokens = Dict{String, Dict{String, Dict{String, Float64}}}()
lock = ReentrantLock()
LTBackend(root, tokens, lock)
token_expiry = Ref{Union{Nothing, Real}}(nothing)
LTBackend(root, tokens, lock, token_expiry)
end
Base.lock(f::Function, backend::LTBackend) = lock(f, backend.lock)
......@@ -66,6 +68,7 @@ end
function gettoken(lt_backend, backend_dir, model_instance)
tagger = gettagger(lt_backend, backend_dir, model_instance)
resetdata(lt_backend) # perform server-wide maintenance
return tagger.sandbox
end
......@@ -88,6 +91,29 @@ function resetdata(lt_backend, backend_dir, model_instance, token, datadir=nothi
nothing
end
function resetdata(lt_backend, min_age)
isnothing(min_age) && return
@assert min_age isa Real
lock(lt_backend) do
for (backend_dir, instances) in pairs(lt_backend.tokens)
for (model_instance, tokens) in pairs(instances)
for (token, created) in pairs(copy(tokens))
age = time() - created
if min_age <= age
@info "resetdata" backend_dir model_instance token age
tagger = gettagger(lt_backend, backend_dir, model_instance, token)
Taggers.resetdata(tagger)
pop!(tokens, token)
end
end
end
end
end
nothing
end
resetdata(lt_backend) = resetdata(lt_backend, lt_backend.token_expiry[])
function listfiles(lt_backend, backend_dir, model_instance, token, data_dir)
tagger = gettagger(lt_backend, backend_dir, model_instance, token)
dir = Taggers.datadir(tagger, data_dir)
......
......@@ -17,8 +17,9 @@ end
# state
const lt_backend = LTBackend()
function run_backend(root::AbstractString; kwargs...)
function run_backend(root::AbstractString, token_expiry=nothing; kwargs...)
lt_backend.root[] = root
lt_backend.token_expiry[] = token_expiry
run_backend(; kwargs...)
end
......@@ -49,6 +50,14 @@ end
end
@get "/reset-data/{min_age}" function (
request,
min_age::Int,
)
resetdata(lt_backend, min_age)
end
@get "/reset-data/{backend_dir}/{model_instance}/{token}" function(
request,
backend_dir::String,
......
......@@ -24,7 +24,7 @@ julia "+$JULIA_VERSION" --project="${larvatagger_jl_project_root}" -q -e "using
# run and background the backend server
JULIA_PROJECT="${larvatagger_project_root}/TaggingBackends" \
julia "+$JULIA_VERSION" --project="${larvatagger_jl_project_root}" -i \
-e "using LarvaTagger.REST.Server; run_backend(\"${larvatagger_project_root}\"; port=${lt_backend_port})" &
-e "using LarvaTagger.REST.Server; run_backend(\"${larvatagger_project_root}\", 300; port=${lt_backend_port})" &
lt_backend_pid=$!
# run the frontend server
......@@ -36,6 +36,8 @@ JULIA="julia +$JULIA_VERSION" ${larvatagger_jl_project_root}/scripts/larvatagger
# expected: a predicted.label is generated and the GUI reloads
# * load a second tracking data file (binary if first was ascii or vice versa), select another model instance, click again on "Autotag";
# expected: a new token was issued + similar outcome as previous step, with tracking data file and tagging model properly identified in the predicted.label file
# * wait for 5 min and click again on "Autotag";
# expected: the data directories corresponding to the previous tokens are empty
kill $lt_backend_pid
wait $lt_backend_pid
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment