Skip to content

Commit

Permalink
added cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mryzhov committed Mar 20, 2024
1 parent 261d6d9 commit f6f6378
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ inputs:
description:
'An ordered list of keys to use for restoring stale cache if no cache hit
occurred for key. Note `cache-hit` returns false in this case.'
default: ''
required: false
fail-on-cache-miss:
description: 'Fail the workflow if cache entry is not found'
Expand All @@ -33,6 +34,12 @@ inputs:
'Run the post step to save the cache even if another step before fails'
default: 'false'
required: false
max-cache-size:
description:
'Maximum cache storage sizr in Gb. Least recently used caches will be
automatically evicted to limit the total cache storage'
default: '10'
required: false

outputs:
cache-hit:
Expand Down
30 changes: 23 additions & 7 deletions .github/actions/cache/dist/restore/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 135 additions & 3 deletions .github/actions/cache/dist/save/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .github/actions/cache/src/save.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { save } = require('./saveImpl')
const { save, cleanUp } = require('./saveImpl')

save()
cleanUp()
66 changes: 64 additions & 2 deletions .github/actions/cache/src/saveImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const core = require('@actions/core')
const tar = require('tar')
const fs = require('fs')
const path = require('path')

const {
getSortedCacheFiles,
humanReadableFileSize,
calculateTotalSize
} = require('./utils')
/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
Expand Down Expand Up @@ -51,6 +55,64 @@ async function save() {
}
}

// Function to remove old files if their combined size exceeds 50 GB
async function cleanUp() {
try {
const cacheRemotePath = core.getInput('cache-path', { required: true })
const key = core.getInput('key', { required: true })
const keysRestore = core
.getInput('restore-keys', { required: false })
.split('\n')
.map(s => s.replace(/^!\s+/, '!').trim())
.filter(x => x !== '')
const maxCacheSize = core.getInput('max-cache-size', { required: false })

core.debug(`cache-path: ${cacheRemotePath}`)
core.debug(`key: ${key}`)
core.debug(`restore-keys: ${keysRestore}`)

var keyPattern = key
if (keysRestore && keysRestore.length) {
keyPattern = keysRestore.join('|')
}

const files = await getSortedCacheFiles(cacheRemotePath, keyPattern)
let totalSize = await calculateTotalSize(cacheRemotePath, files)
let maxCacheSizeInBytes = maxCacheSize * 1024 * 1024 * 1024

if (totalSize > maxCacheSizeInBytes) {
core.info(
`The cache storage size ${humanReadableFileSize(totalSize)} exceeds allowed size ${humanReadableFileSize(maxCacheSizeInBytes)}`
)
for (let i = files.length - 1; i >= 0; i--) {
var file = files[i]
const filePath = path.join(directory, file)
const fileStats = await stat(filePath)

if (fileStats.isFile() && fileStats.ctime < oneWeekAgo) {
console.log(`Removing file: ${filePath}`)
await unlink(filePath)
totalSize -= fileStats.size
}

if (totalSize <= maxCacheSizeInBytes) {
// Check if total size
break // Exit loop if total size is within limit
}
}
core.info('Old cache files removed successfully')
} else {
core.info(
`The cache storage size ${humanReadableFileSize(totalSize)} less then allowed size ${humanReadableFileSize(maxCacheSizeInBytes)}`
)
}
} catch (error) {
core.error('Error removing old cache files')
core.setFailed(error.message)
}
}

module.exports = {
save
save,
cleanUp
}
30 changes: 23 additions & 7 deletions .github/actions/cache/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function getSortedCacheFiles(path, key = '') {
return []
}

const cache_pattern = new RegExp(`^(${key}.*[.]cache)$`)
const cache_pattern = new RegExp(`^((${key}).*[.]cache)$`)

const files = await fs.promises.readdir(path)
filesSorded = files
Expand All @@ -22,25 +22,41 @@ async function getSortedCacheFiles(path, key = '') {
core.debug(
filesSorded.map(fileName => ({
name: fileName,
time: fs.statSync(`${path}/${fileName}`).atime.getTime()
time: fs.statSync(`${path}/${fileName}`).mtime.getTime()
}))
)
return filesSorded
}

function humanReadableFileSize(sizeInBytes) {
const units = ['B', 'KB', 'MB', 'GB', 'TB']
let index = 0
let id = 0

while (sizeInBytes >= 1024 && index < units.length - 1) {
while (sizeInBytes >= 1024 && id < units.length - 1) {
sizeInBytes /= 1024
index++
id++
}

return sizeInBytes.toFixed(2) + ' ' + units[index]
return sizeInBytes.toFixed(2) + ' ' + units[id]
}

// Function to calculate the total size of files in bytes
async function calculateTotalSize(dir, files) {
let totalSize = 0

for (const file of files) {
const filePath = path.join(dir, file)
const fileStats = await stat(filePath)

if (fileStats.isFile()) {
totalSize += fileStats.size
}
}
return totalSize
}

module.exports = {
getSortedCacheFiles,
humanReadableFileSize
humanReadableFileSize,
calculateTotalSize
}

0 comments on commit f6f6378

Please sign in to comment.