Skip to content

Commit

Permalink
Add cache purger (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
s01ipsist authored Oct 11, 2024
1 parent ab2a038 commit d62348c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/process-data.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
name: process-data
on:
workflow_dispatch:
schedule:
- cron: "30 20 * * 1" # Run weekly at 20:30 on Monday
# 1 hour after setup-koordinates-exports job runs to allow export archives to process

permissions:
actions: write # For managing Actions cache

jobs:
process-data:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -36,6 +40,12 @@ jobs:
- name: Create download data folder
run: mkdir -p scripts/koordinates/data

- name: Remove stale caches
working-directory: ./scripts
run: bash cache-purger.sh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Restore cached download
id: cache-nz-koordinates
uses: actions/cache/restore@v4
Expand Down
29 changes: 29 additions & 0 deletions scripts/cache-purger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Define the cache key to look for
CACHE_KEY="Linux-nz-koordinates"

# Get the current time in epoch format
CURRENT_TIME=$(date +%s)

# List caches, filter by key, and process each result
# https://cli.github.com/manual/gh_cache_list
gh cache list --json key,createdAt,id | jq -c ".[] | select(.key == \"$CACHE_KEY\")" | while read -r cache; do
# Extract the cache ID and creation timestamp
CACHE_ID=$(echo "$cache" | jq -r '.id')
CREATED_AT=$(echo "$cache" | jq -r '.createdAt')

# Convert the creation time to epoch format
CREATED_AT_EPOCH=$(date -d "$CREATED_AT" +%s)

# Calculate the age of the cache in seconds
AGE=$((CURRENT_TIME - CREATED_AT_EPOCH))

# Check if the cache is older than 24 hours (86400 seconds)
if [ $AGE -gt 86400 ]; then
echo "Deleting cache with ID $CACHE_ID (created at $CREATED_AT)"
gh cache delete "$CACHE_ID"
else
echo "Cache with ID $CACHE_ID is not older than 24 hours (created at $CREATED_AT)"
fi
done

0 comments on commit d62348c

Please sign in to comment.