Add strict mode

This commit is contained in:
Luc Perkins 2024-05-17 17:55:54 -03:00
parent 5555b8bb7b
commit d828acb704
No known key found for this signature in database
GPG Key ID: 16DB1108FB591835
4 changed files with 49 additions and 11 deletions

View File

@ -51,6 +51,10 @@ inputs:
source-url:
description: A URL pointing to a `magic-nix-cache` binary. Overrides all other `source-*` options.
required: false
strict-mode:
description: "Whether to fail when any errors are thrown. Used only to test the Action; do not set this in your own workflows."
default: false
required: false
runs:
using: "node20"

25
dist/index.js generated vendored
View File

@ -94810,6 +94810,7 @@ var MagicNixCacheAction = class {
idsProjectName: "magic-nix-cache-closure",
requireNix: "warn"
});
this.failMode = inputs_exports.getBool("fail-mode");
this.client = got_dist_source.extend({
retry: {
limit: 1,
@ -94956,16 +94957,21 @@ var MagicNixCacheAction = class {
notifyPromise.then((_value) => {
resolve();
}).catch((err) => {
reject(new Error(`error in notifyPromise: ${err}`));
const msg = `error in notifyPromise: ${err}`;
reject(new Error(msg));
this.failInFailMode(msg);
});
daemon.on("exit", async (code, signal) => {
let msg;
if (signal) {
reject(new Error(`Daemon was killed by signal ${signal}`));
msg = `Daemon was killed by signal ${signal}`;
} else if (code) {
reject(new Error(`Daemon exited with code ${code}`));
msg = `Daemon exited with code ${code}`;
} else {
reject(new Error(`Daemon unexpectedly exited`));
msg = "Daemon unexpectedly exited";
}
reject(new Error(msg));
this.failInFailMode(msg);
});
});
daemon.unref();
@ -94996,6 +95002,7 @@ var MagicNixCacheAction = class {
core.info(`Error marking the workflow as started:`);
core.info((0,external_node_util_.inspect)(e));
core.info(`Magic Nix Cache may not be running for this workflow.`);
this.failInFailMode(`Magic Nix Cache failed to start: ${(0,external_node_util_.inspect)(e)}`);
}
}
async tearDownAutoCache() {
@ -95007,7 +95014,9 @@ var MagicNixCacheAction = class {
const pid = parseInt(await promises_namespaceObject.readFile(pidFile, { encoding: "ascii" }));
core.debug(`found daemon pid: ${pid}`);
if (!pid) {
throw new Error("magic-nix-cache did not start successfully");
const msg = "magic-nix-cache did not start successfully";
this.failInFailMode(msg);
throw new Error(msg);
}
const log = tailLog(this.daemonDir);
try {
@ -95024,6 +95033,7 @@ var MagicNixCacheAction = class {
process.kill(pid, "SIGTERM");
} catch (e) {
if (typeof e === "object" && e && "code" in e && e.code !== "ESRCH") {
this.failInFailMode(e.toString());
throw e;
}
} finally {
@ -95034,6 +95044,11 @@ var MagicNixCacheAction = class {
}
}
}
failInFailMode(msg) {
if (this.failMode) {
core.setFailed(`fail-mode error: ${msg}`);
}
}
};
function main() {
const cacheAction = new MagicNixCacheAction();

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -28,6 +28,7 @@ const TEXT_TRUST_UNKNOWN =
class MagicNixCacheAction {
idslib: IdsToolbox;
private strictMode: boolean;
private client: Got;
noopMode: boolean;
@ -42,6 +43,8 @@ class MagicNixCacheAction {
requireNix: "warn",
});
this.strictMode = inputs.getBool("strict-mode");
this.client = got.extend({
retry: {
limit: 1,
@ -224,16 +227,21 @@ class MagicNixCacheAction {
})
// eslint-disable-next-line github/no-then
.catch((err) => {
reject(new Error(`error in notifyPromise: ${err}`));
const msg = `error in notifyPromise: ${err}`;
reject(new Error(msg));
this.failInStrictMode(msg);
});
daemon.on("exit", async (code, signal) => {
let msg: string;
if (signal) {
reject(new Error(`Daemon was killed by signal ${signal}`));
msg = `Daemon was killed by signal ${signal}`;
} else if (code) {
reject(new Error(`Daemon exited with code ${code}`));
msg = `Daemon exited with code ${code}`;
} else {
reject(new Error(`Daemon unexpectedly exited`));
msg = "Daemon unexpectedly exited";
}
reject(new Error(msg));
this.failInStrictMode(msg);
});
});
@ -274,6 +282,8 @@ class MagicNixCacheAction {
actionsCore.info(`Error marking the workflow as started:`);
actionsCore.info(inspect(e));
actionsCore.info(`Magic Nix Cache may not be running for this workflow.`);
this.failInStrictMode(`Magic Nix Cache failed to start: ${inspect(e)}`);
}
}
@ -287,7 +297,9 @@ class MagicNixCacheAction {
const pid = parseInt(await fs.readFile(pidFile, { encoding: "ascii" }));
actionsCore.debug(`found daemon pid: ${pid}`);
if (!pid) {
throw new Error("magic-nix-cache did not start successfully");
const msg = "magic-nix-cache did not start successfully";
this.failInStrictMode(msg);
throw new Error(msg);
}
const log = tailLog(this.daemonDir);
@ -309,6 +321,7 @@ class MagicNixCacheAction {
process.kill(pid, "SIGTERM");
} catch (e) {
if (typeof e === "object" && e && "code" in e && e.code !== "ESRCH") {
this.failInStrictMode(e.toString());
throw e;
}
} finally {
@ -319,6 +332,12 @@ class MagicNixCacheAction {
}
}
}
private failInStrictMode(msg: string): void {
if (this.strictMode) {
actionsCore.setFailed(`strict mode error: ${msg}`);
}
}
}
function main(): void {