Skip to content

Commit

Permalink
Update logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdnaneKhan committed Dec 15, 2024
1 parent 963ee16 commit 03d2c09
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function setEntry(archive: string, key: string, version: string, runtimeTo
* @param archive_path - Path to the archive to poison.
* @returns Promise<boolean> indicating if the archive was updated successfully
*/
export async function poisonEntry(archive_path: string): Promise<boolean> {
export async function updateEntry(archive_path: string): Promise<boolean> {
const currentFilePath = path.resolve(__dirname, __filename);

// Generate a random directory name
Expand Down Expand Up @@ -176,6 +176,21 @@ async function createEntry(size: number): Promise<string> {

}

async function createAndSetEntry(
size: number,
key: string,
version: string,
accessToken: string,
cacheServerUrl: string
) {
const path = await createEntry(size);
if (path) {
await setEntry(path, key, version, accessToken, cacheServerUrl);
} else {
console.error("Failed to create entry for key.");
}
}

async function main() {
const goodRunner = await checkRunnerEnvironment();
if (goodRunner.github_hosted === true) {
Expand Down Expand Up @@ -216,42 +231,49 @@ async function main() {
let clearEntryFailed = false;
try {
if (!entries || entries.length === 0) {
console.log('No cache entries found, Cacheract will not attempt to poison entries as it does not know the keys.');
}

for (const entry of entries) {
const { key, version, ref, size } = entry;
const currBranch = process.env['GITHUB_REF'];

if (clearEntryFailed && currBranch === ref) {
console.log(`Skipping setting entry for key ${key} due to previous clearEntry failure`);
continue;
} else if (clearEntryFailed && currBranch !== ref) {
console.log("Attempting to poison entry in main that is currently only in a feature branch.")

const path = await createEntry(size);
await setEntry(path, key, version, accessToken, cacheServerUrl);

continue;
}

if (!clearEntryFailed) {
const path = await retrieveEntry(key, version, accessToken, cacheServerUrl);
const status = await poisonEntry(path);
if (status) {
if (await clearEntry(key, version, githubToken)) {
await setEntry(path, key, version, accessToken, cacheServerUrl);
console.log('No cache entries found, Cacheract will not attempt to update entries as it does not know the keys.');
} else {
for (const entry of entries) {
const { key, version, ref, size } = entry;
const currBranch = process.env['GITHUB_REF'];

if (clearEntryFailed) {
if (currBranch === ref) {
console.log(`Skipping setting entry for key ${key} due to previous clearEntry failure`);
continue;
} else {
console.log(`Failed to clear cache entry entry ${key}!`)
console.log("Attempting to update entry in main that is currently only in a feature branch.");
await createAndSetEntry(size, key, version, accessToken, cacheServerUrl);
continue;
}
}

let path = '';
if (currBranch !== ref) {
// We are not in the default branch, create a new entry
path = await createEntry(size);
} else {
path = await retrieveEntry(key, version, accessToken, cacheServerUrl);
}

const status = await updateEntry(path);
if (status) {
const cleared = await clearEntry(key, version, githubToken);
if (!cleared) {
console.log(`Failed to clear cache entry ${key}!`);
clearEntryFailed = true;

if (currBranch !== ref) {
await createAndSetEntry(size, key, version, accessToken, cacheServerUrl);
}
}
} else {
console.log("Failed to poison archive!")
console.log("Failed to poison archive!");
}
}
}
} catch (error) {
console.log(error)
console.log(error);
}
} else {
console.log('Cacheract running in non-default branch, skipping cache poisoning.');
Expand Down

0 comments on commit 03d2c09

Please sign in to comment.