Skip to content

Commit

Permalink
Refactor cache cleanup logic in entrypoint.sh
Browse files Browse the repository at this point in the history
This ensures that CONTAINER_CACHE is set before we do cache management.
  • Loading branch information
felddy committed Nov 17, 2023
1 parent e72f6e3 commit c342cd4
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -206,41 +206,40 @@ if [ $install_required = true ]; then

if [[ "${CONTAINER_CACHE:-}" ]]; then
log "Preserving release archive file in cache."
else
log "Deleting release archive file."
rm "${release_filename}"
fi

# Check if CONTAINER_CACHE_SIZE is set and if so, ensure it's greater than 0
if [[ -n "${CONTAINER_CACHE_SIZE:-}" ]]; then
if ! [[ "${CONTAINER_CACHE_SIZE}" -gt 0 ]] 2> /dev/null; then
log_error "If set, CONTAINER_CACHE_SIZE must be 1 or greater. Found: ${CONTAINER_CACHE_SIZE}"
exit 1
fi

log "Cleaning up cache directory: ${CONTAINER_CACHE}"
log "Keeping ${CONTAINER_CACHE_SIZE} latest versions."
# Initialize counter
cache_files_removed_count=0

# Store the list of cache files to remove
file_list=$(find "${CONTAINER_CACHE}" -maxdepth 1 -name 'foundryvtt-*.zip' \
| sort -Vr \
| awk -v keep="${CONTAINER_CACHE_SIZE}" 'NR > keep')

# Iterate over the file list
if [ -n "$file_list" ]; then
for file in $file_list; do
log_warn "Removing: $file"
rm -f "$file"
cache_files_removed_count=$((cache_files_removed_count + 1))
done
log "Completed cache cleanup. Removed ${cache_files_removed_count} files."
# Check if CONTAINER_CACHE_SIZE is set and if so, ensure it's greater than 0
if [[ -n "${CONTAINER_CACHE_SIZE:-}" ]]; then
if ! [[ "${CONTAINER_CACHE_SIZE}" -gt 0 ]] 2> /dev/null; then
log_error "If set, CONTAINER_CACHE_SIZE must be 1 or greater. Found: ${CONTAINER_CACHE_SIZE}"
exit 1
fi

log "Cleaning up cache directory: ${CONTAINER_CACHE}"
log "Keeping ${CONTAINER_CACHE_SIZE} latest versions."
# Initialize counter
cache_files_removed_count=0

# Store the list of cache files to remove
file_list=$(find "${CONTAINER_CACHE}" -maxdepth 1 -name 'foundryvtt-*.zip' \
| sort -Vr \
| awk -v keep="${CONTAINER_CACHE_SIZE}" 'NR > keep')

# Iterate over the file list
if [ -n "$file_list" ]; then
for file in $file_list; do
log_warn "Removing: $file"
rm -f "$file"
cache_files_removed_count=$((cache_files_removed_count + 1))
done
log "Completed cache cleanup. Removed ${cache_files_removed_count} files."
else
log "No cache cleanup was necessary."
fi
else
log "No cache cleanup was necessary."
log_debug "CONTAINER_CACHE_SIZE is not set. Skipping cache cleanup."
fi
else
log_debug "CONTAINER_CACHE_SIZE is not set. Skipping cache cleanup."
log "Deleting release archive file."
rm "${release_filename}"
fi

# apply URL patches if requested
Expand Down

0 comments on commit c342cd4

Please sign in to comment.