Skip to content

Commit

Permalink
Use the hashed inputs as cache key to allow multiple steps at the sam…
Browse files Browse the repository at this point in the history
…e time
  • Loading branch information
autoantwort committed May 23, 2024
1 parent 90b6672 commit 7b84952
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
57 changes: 40 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1591,12 +1591,10 @@ exports.StateCacheStorage = void 0;
const fs_1 = __importDefault(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const os_1 = __importDefault(__nccwpck_require__(2037));
const process_1 = __importDefault(__nccwpck_require__(7282));
const core = __importStar(__nccwpck_require__(2186));
const github_1 = __nccwpck_require__(5438);
const plugin_retry_1 = __nccwpck_require__(6298);
const cache = __importStar(__nccwpck_require__(7799));
const CACHE_KEY = encodeURIComponent('_state' + process_1.default.env.GITHUB_WORKFLOW + '-' + process_1.default.env.GITHUB_JOB);
const STATE_FILE = 'state.txt';
const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03';
const mkTempDir = () => {
Expand Down Expand Up @@ -1645,22 +1643,25 @@ const resetCacheWithOctokit = (cacheKey) => __awaiter(void 0, void 0, void 0, fu
}
});
class StateCacheStorage {
constructor(cacheKey) {
this.cacheKey = cacheKey;
}
save(serializedState) {
return __awaiter(this, void 0, void 0, function* () {
const tmpDir = mkTempDir();
const filePath = path_1.default.join(tmpDir, STATE_FILE);
fs_1.default.writeFileSync(filePath, serializedState);
try {
const cacheExists = yield checkIfCacheExists(CACHE_KEY);
const cacheExists = yield checkIfCacheExists(this.cacheKey);
if (cacheExists) {
yield resetCacheWithOctokit(CACHE_KEY);
yield resetCacheWithOctokit(this.cacheKey);
}
const fileSize = fs_1.default.statSync(filePath).size;
if (fileSize === 0) {
core.info(`the state will be removed`);
return;
}
yield cache.saveCache([path_1.default.dirname(filePath)], CACHE_KEY);
yield cache.saveCache([path_1.default.dirname(filePath)], this.cacheKey);
}
catch (error) {
core.warning(`Saving the state was not successful due to "${error.message || 'unknown reason'}"`);
Expand All @@ -1676,12 +1677,12 @@ class StateCacheStorage {
const filePath = path_1.default.join(tmpDir, STATE_FILE);
unlinkSafely(filePath);
try {
const cacheExists = yield checkIfCacheExists(CACHE_KEY);
const cacheExists = yield checkIfCacheExists(this.cacheKey);
if (!cacheExists) {
core.info('The saved state was not found, the process starts from the first issue.');
return '';
}
yield cache.restoreCache([path_1.default.dirname(filePath)], CACHE_KEY);
yield cache.restoreCache([path_1.default.dirname(filePath)], this.cacheKey);
if (!fs_1.default.existsSync(filePath)) {
core.warning('Unknown error when unpacking the cache, the process starts from the first issue.');
return '';
Expand Down Expand Up @@ -2686,16 +2687,46 @@ exports.LoggerService = LoggerService;
/***/ }),

/***/ 6330:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getStateInstance = void 0;
const crypto = __importStar(__nccwpck_require__(6113));
const state_1 = __nccwpck_require__(5186);
const state_cache_storage_1 = __nccwpck_require__(3709);
function sha256(message) {
const hash = crypto.createHash('sha256');
hash.update(message);
return hash.digest('hex');
}
const getStateInstance = (options) => {
const storage = new state_cache_storage_1.StateCacheStorage();
const cacheKey = sha256(JSON.stringify(options));
const storage = new state_cache_storage_1.StateCacheStorage(cacheKey);
return new state_1.State(storage, options);
};
exports.getStateInstance = getStateInstance;
Expand Down Expand Up @@ -89200,14 +89231,6 @@ module.exports = require("perf_hooks");

/***/ }),

/***/ 7282:
/***/ ((module) => {

"use strict";
module.exports = require("process");

/***/ }),

/***/ 5477:
/***/ ((module) => {

Expand Down
20 changes: 11 additions & 9 deletions src/classes/state/state-cache-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import {IStateStorage} from '../../interfaces/state/state-storage';
import fs from 'fs';
import path from 'path';
import os from 'os';
import process from 'process';
import * as core from '@actions/core';
import {context, getOctokit} from '@actions/github';
import {retry as octokitRetry} from '@octokit/plugin-retry';
import * as cache from '@actions/cache';

const CACHE_KEY = encodeURIComponent(
'_state' + process.env.GITHUB_WORKFLOW + '-' + process.env.GITHUB_JOB
);
const STATE_FILE = 'state.txt';
const STALE_DIR = '56acbeaa-1fef-4c79-8f84-7565e560fb03';

Expand Down Expand Up @@ -68,15 +64,21 @@ const resetCacheWithOctokit = async (cacheKey: string): Promise<void> => {
}
};
export class StateCacheStorage implements IStateStorage {
private cacheKey: string;

constructor(cacheKey: string) {
this.cacheKey = cacheKey;
}

async save(serializedState: string): Promise<void> {
const tmpDir = mkTempDir();
const filePath = path.join(tmpDir, STATE_FILE);
fs.writeFileSync(filePath, serializedState);

try {
const cacheExists = await checkIfCacheExists(CACHE_KEY);
const cacheExists = await checkIfCacheExists(this.cacheKey);
if (cacheExists) {
await resetCacheWithOctokit(CACHE_KEY);
await resetCacheWithOctokit(this.cacheKey);
}
const fileSize = fs.statSync(filePath).size;

Expand All @@ -85,7 +87,7 @@ export class StateCacheStorage implements IStateStorage {
return;
}

await cache.saveCache([path.dirname(filePath)], CACHE_KEY);
await cache.saveCache([path.dirname(filePath)], this.cacheKey);
} catch (error) {
core.warning(
`Saving the state was not successful due to "${
Expand All @@ -102,15 +104,15 @@ export class StateCacheStorage implements IStateStorage {
const filePath = path.join(tmpDir, STATE_FILE);
unlinkSafely(filePath);
try {
const cacheExists = await checkIfCacheExists(CACHE_KEY);
const cacheExists = await checkIfCacheExists(this.cacheKey);
if (!cacheExists) {
core.info(
'The saved state was not found, the process starts from the first issue.'
);
return '';
}

await cache.restoreCache([path.dirname(filePath)], CACHE_KEY);
await cache.restoreCache([path.dirname(filePath)], this.cacheKey);

if (!fs.existsSync(filePath)) {
core.warning(
Expand Down
10 changes: 9 additions & 1 deletion src/services/state.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import * as crypto from 'crypto';
import {IState} from '../interfaces/state/state';
import {State} from '../classes/state/state';
import {IIssuesProcessorOptions} from '../interfaces/issues-processor-options';
import {StateCacheStorage} from '../classes/state/state-cache-storage';

function sha256(message: string): string {
const hash = crypto.createHash('sha256');
hash.update(message);
return hash.digest('hex');
}

export const getStateInstance = (options: IIssuesProcessorOptions): IState => {
const storage = new StateCacheStorage();
const cacheKey = sha256(JSON.stringify(options));
const storage = new StateCacheStorage(cacheKey);
return new State(storage, options);
};

0 comments on commit 7b84952

Please sign in to comment.