Skip to content

Commit

Permalink
use highlevel actions cache api
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jul 5, 2023
1 parent d8dfea6 commit 7997b87
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
40 changes: 31 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.downloadFileFromActionsCache = void 0;
const cache = __importStar(__nccwpck_require__(7799));
const path_1 = __importDefault(__nccwpck_require__(1017));
const downloadFileFromActionsCache = (destFileName, cacheKey, cacheVersion) => cache.restoreCache([path_1.default.dirname(destFileName)], cacheKey, [
const downloadFileFromActionsCache = (destFileName, cacheKey,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion) => cache.restoreCache([path_1.default.dirname(destFileName)], cacheKey + 'x', [
cacheKey
]);
exports.downloadFileFromActionsCache = downloadFileFromActionsCache;
Expand Down Expand Up @@ -110,14 +112,16 @@ const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, fu
}
}
});
const uploadFileToActionsCache = (filePath, cacheKey, cacheVersion) => __awaiter(void 0, void 0, void 0, function* () {
yield resetCacheWithOctokit(cacheKey);
const uploadFileToActionsCache = (filePath, cacheKey,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion) => __awaiter(void 0, void 0, void 0, function* () {
yield resetCacheWithOctokit(cacheKey + 'x');
const fileSize = fs_1.default.statSync(filePath).size;
if (fileSize === 0) {
core.info(`the cache ${cacheKey} will be removed`);
return;
}
cache.saveCache([filePath], cacheKey);
cache.saveCache([filePath], cacheKey + 'x');
});
exports.uploadFileToActionsCache = uploadFileToActionsCache;

Expand Down Expand Up @@ -1691,24 +1695,42 @@ import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
const CACHE_KEY = '_state';
const CACHE_VERSION = '1';
const STATE_FILE = 'state.txt';
const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03';
const mkTempDir = () => {
const tmpDir = path_1.default.join(os_1.default.tmpdir(), STALE_DIR);
fs_1.default.mkdirSync(tmpDir, { recursive: true });
return tmpDir;
};
const unlinkSafely = (filePath) => {
try {
fs_1.default.unlinkSync(filePath);
}
catch (foo) {
/* ignore */
}
};
class StateCacheStorage {
save(serializedState) {
return __awaiter(this, void 0, void 0, function* () {
const tmpDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'state-'));
const file = path_1.default.join(tmpDir, STATE_FILE);
fs_1.default.writeFileSync(file, serializedState);
const tmpDir = mkTempDir();
const fileName = path_1.default.join(tmpDir, STATE_FILE);
fs_1.default.writeFileSync(fileName, serializedState);
try {
yield (0, upload_1.uploadFileToActionsCache)(file, CACHE_KEY, CACHE_VERSION);
yield (0, upload_1.uploadFileToActionsCache)(fileName, CACHE_KEY, CACHE_VERSION);
}
catch (error) {
core.warning(`Saving the state was not successful due to "${error.message || 'unknown reason'}"`);
}
finally {
unlinkSafely(fileName);
}
});
}
restore() {
return __awaiter(this, void 0, void 0, function* () {
const tmpDir = fs_1.default.mkdtempSync('state-');
const tmpDir = mkTempDir(); //fs.mkdtempSync('state-');
const fileName = path_1.default.join(tmpDir, STATE_FILE);
unlinkSafely(fileName);
try {
yield (0, download_1.downloadFileFromActionsCache)(fileName, CACHE_KEY, CACHE_VERSION);
if (!fs_1.default.existsSync(fileName)) {
Expand Down
3 changes: 2 additions & 1 deletion src/classes/actions-cache-hilevel/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import path from 'path';
export const downloadFileFromActionsCache = (
destFileName: string,
cacheKey: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion: string
): Promise<void> =>
cache.restoreCache([path.dirname(destFileName)], cacheKey, [
cache.restoreCache([path.dirname(destFileName)], cacheKey + 'x', [
cacheKey
]) as Promise<void>;
5 changes: 3 additions & 2 deletions src/classes/actions-cache-hilevel/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
export const uploadFileToActionsCache = async (
filePath: string,
cacheKey: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
cacheVersion: string
) => {
await resetCacheWithOctokit(cacheKey);
await resetCacheWithOctokit(cacheKey + 'x');
const fileSize = fs.statSync(filePath).size;

if (fileSize === 0) {
core.info(`the cache ${cacheKey} will be removed`);
return;
}

cache.saveCache([filePath], cacheKey);
cache.saveCache([filePath], cacheKey + 'x');
};
28 changes: 23 additions & 5 deletions src/classes/state/state-cache-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,44 @@ import {downloadFileFromActionsCache} from '../actions-cache-internal/download';
const CACHE_KEY = '_state';
const CACHE_VERSION = '1';
const STATE_FILE = 'state.txt';
const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03';

const mkTempDir = (): string => {
const tmpDir = path.join(os.tmpdir(), STALE_DIR);
fs.mkdirSync(tmpDir, {recursive: true});
return tmpDir;
};

const unlinkSafely = (filePath: string) => {
try {
fs.unlinkSync(filePath);
} catch (foo) {
/* ignore */
}
};
export class StateCacheStorage implements IStateStorage {
async save(serializedState: string): Promise<void> {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'state-'));
const file = path.join(tmpDir, STATE_FILE);
fs.writeFileSync(file, serializedState);
const tmpDir = mkTempDir();
const fileName = path.join(tmpDir, STATE_FILE);
fs.writeFileSync(fileName, serializedState);

try {
await uploadFileToActionsCache(file, CACHE_KEY, CACHE_VERSION);
await uploadFileToActionsCache(fileName, CACHE_KEY, CACHE_VERSION);
} catch (error) {
core.warning(
`Saving the state was not successful due to "${
error.message || 'unknown reason'
}"`
);
} finally {
unlinkSafely(fileName);
}
}

async restore(): Promise<string> {
const tmpDir = fs.mkdtempSync('state-');
const tmpDir = mkTempDir(); //fs.mkdtempSync('state-');
const fileName = path.join(tmpDir, STATE_FILE);
unlinkSafely(fileName);
try {
await downloadFileFromActionsCache(fileName, CACHE_KEY, CACHE_VERSION);
if (!fs.existsSync(fileName)) {
Expand Down

0 comments on commit 7997b87

Please sign in to comment.