Run post phase only if main didn't error

This commit is contained in:
Luc Perkins 2024-06-03 15:45:32 -07:00
parent 0cb62a86ec
commit 58113297de
No known key found for this signature in database
GPG Key ID: 16DB1108FB591835
3 changed files with 33 additions and 12 deletions

20
dist/index.js generated vendored
View File

@ -95468,6 +95468,7 @@ var ENV_DAEMON_DIR = "MAGIC_NIX_CACHE_DAEMONDIR";
var FACT_ENV_VARS_PRESENT = "required_env_vars_present";
var FACT_DIFF_STORE_ENABLED = "diff_store";
var FACT_NOOP_MODE = "noop_mode";
var STATE_ERROR_IN_MAIN = "ERROR_IN_MAIN";
var STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
var STATE_STARTED = "MAGIC_NIX_CACHE_STARTED";
var STARTED_HINT = "true";
@ -95483,7 +95484,6 @@ var MagicNixCacheAction = class extends DetSysAction {
requireNix: "warn",
diagnosticsSuffix: "perf"
});
this.errorInMain = false;
this.hostAndPort = inputs_exports.getString("listen");
this.diffStore = inputs_exports.getBool("diff-store");
this.addFact(FACT_DIFF_STORE_ENABLED, this.diffStore);
@ -95534,7 +95534,7 @@ var MagicNixCacheAction = class extends DetSysAction {
await this.notifyAutoCache();
}
async post() {
if (!this.strictMode && this.errorInMain) {
if (!this.strictMode && this.mainErrored) {
this.exitWithWarning(`skipping post phase due to error in main phase`);
} else {
if (this.noopMode) {
@ -95663,7 +95663,7 @@ var MagicNixCacheAction = class extends DetSysAction {
if (this.strictMode) {
reject(new Error(`error in notifyPromise: ${msg}`));
} else {
this.errorInMain = true;
this.setMainErrored();
this.exitWithWarning(`failed to start daemon: ${msg}`);
}
});
@ -95679,7 +95679,7 @@ var MagicNixCacheAction = class extends DetSysAction {
if (this.strictMode) {
reject(new Error(msg));
} else {
this.errorInMain = true;
this.setMainErrored();
this.exitWithWarning(`Failed to kill daemon: ${msg}`);
}
});
@ -95711,7 +95711,7 @@ var MagicNixCacheAction = class extends DetSysAction {
if (this.strictMode) {
core.setFailed(`Magic Nix Cache failed to start: ${(0,external_node_util_.inspect)(e)}`);
} else {
this.errorInMain = true;
this.setMainErrored();
core.warning(`Error marking the workflow as started:`);
core.warning((0,external_node_util_.inspect)(e));
core.warning(
@ -95773,6 +95773,16 @@ var MagicNixCacheAction = class extends DetSysAction {
core.warning(`strict mode not enabled; exiting`);
process.exit(0);
}
// If the main phase errors, save the state for use in post.
// This matters only when strict mode is NOT set.
setMainErrored() {
core.saveState(STATE_ERROR_IN_MAIN, "true");
}
// In the post phase, if the main phase errored, return `true` so that the
// phase can be skipped (with a warning).
get mainErrored() {
return core.getState(STATE_ERROR_IN_MAIN) === "true";
}
};
function main() {
new MagicNixCacheAction().execute();

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -18,6 +18,7 @@ const FACT_ENV_VARS_PRESENT = "required_env_vars_present";
const FACT_DIFF_STORE_ENABLED = "diff_store";
const FACT_NOOP_MODE = "noop_mode";
const STATE_ERROR_IN_MAIN = "ERROR_IN_MAIN";
const STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
const STATE_STARTED = "MAGIC_NIX_CACHE_STARTED";
const STARTED_HINT = "true";
@ -30,7 +31,6 @@ const TEXT_TRUST_UNKNOWN =
"The Nix daemon may not consider the user running this workflow to be trusted. Magic Nix Cache may not start correctly.";
class MagicNixCacheAction extends DetSysAction {
private errorInMain: boolean;
private hostAndPort: string;
private diffStore: boolean;
private httpClient: Got;
@ -47,7 +47,6 @@ class MagicNixCacheAction extends DetSysAction {
diagnosticsSuffix: "perf",
});
this.errorInMain = false;
this.hostAndPort = inputs.getString("listen");
this.diffStore = inputs.getBool("diff-store");
@ -108,7 +107,7 @@ class MagicNixCacheAction extends DetSysAction {
}
async post(): Promise<void> {
if (!this.strictMode && this.errorInMain) {
if (!this.strictMode && this.mainErrored) {
this.exitWithWarning(`skipping post phase due to error in main phase`);
} else {
if (this.noopMode) {
@ -271,7 +270,7 @@ class MagicNixCacheAction extends DetSysAction {
if (this.strictMode) {
reject(new Error(`error in notifyPromise: ${msg}`));
} else {
this.errorInMain = true;
this.setMainErrored();
this.exitWithWarning(`failed to start daemon: ${msg}`);
}
});
@ -289,7 +288,7 @@ class MagicNixCacheAction extends DetSysAction {
if (this.strictMode) {
reject(new Error(msg));
} else {
this.errorInMain = true;
this.setMainErrored();
this.exitWithWarning(`Failed to kill daemon: ${msg}`);
}
});
@ -329,7 +328,7 @@ class MagicNixCacheAction extends DetSysAction {
if (this.strictMode) {
actionsCore.setFailed(`Magic Nix Cache failed to start: ${inspect(e)}`);
} else {
this.errorInMain = true;
this.setMainErrored();
actionsCore.warning(`Error marking the workflow as started:`);
actionsCore.warning(inspect(e));
actionsCore.warning(
@ -400,6 +399,18 @@ class MagicNixCacheAction extends DetSysAction {
actionsCore.warning(`strict mode not enabled; exiting`);
process.exit(0);
}
// If the main phase errors, save the state for use in post.
// This matters only when strict mode is NOT set.
private setMainErrored(): void {
actionsCore.saveState(STATE_ERROR_IN_MAIN, "true");
}
// In the post phase, if the main phase errored, return `true` so that the
// phase can be skipped (with a warning).
private get mainErrored(): boolean {
return actionsCore.getState(STATE_ERROR_IN_MAIN) === "true";
}
}
function main(): void {