Files
ThothII/scripts/lib/vector-operation-lock.sh

49 lines
1.8 KiB
Bash

#!/bin/sh
# Shared, daemon-scoped lock for operations that stop or replace one Compose Qdrant volume.
# The stopped container name is the atomic primitive; ownership labels prevent a cleanup trap
# from deleting a lock that it did not create.
vector_operation_lock_init() {
operation_lock_name="${expected_volume_name}-operation-lock"
operation_lock_owner="${project_name}-$$-$(date -u +%Y%m%dT%H%M%S)"
operation_lock_acquired=0
}
acquire_vector_operation_lock() {
if docker create \
--name "$operation_lock_name" \
--label "com.thothii.qdrant-operation-owner=$operation_lock_owner" \
--label "com.thothii.qdrant-operation-project=$project_name" \
--label "com.thothii.qdrant-operation-volume=$expected_volume_name" \
"$helper_image" /bin/true >/dev/null 2>&1; then
operation_lock_acquired=1
return 0
fi
if docker inspect "$operation_lock_name" >/dev/null 2>&1; then
echo "Qdrant operation already in progress for $expected_volume_name" >&2
else
echo "Unable to acquire Qdrant operation lock for $expected_volume_name" >&2
fi
return 2
}
release_vector_operation_lock() {
[ "${operation_lock_acquired:-0}" -eq 1 ] || return 0
metadata=$(docker inspect --format \
'{{ index .Config.Labels "com.thothii.qdrant-operation-owner" }} {{ index .Config.Labels "com.thothii.qdrant-operation-volume" }}' \
"$operation_lock_name" 2>/dev/null || true)
set -- $metadata
if [ "${1-}" != "$operation_lock_owner" ] || [ "${2-}" != "$expected_volume_name" ]; then
echo "Qdrant operation lock ownership changed; refusing to remove it" >&2
operation_lock_acquired=0
return 1
fi
if ! docker rm -f "$operation_lock_name" >/dev/null; then
echo "Unable to release Qdrant operation lock for $expected_volume_name" >&2
operation_lock_acquired=0
return 1
fi
operation_lock_acquired=0
}