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_ENV_VARS_PRESENT = "required_env_vars_present";
var FACT_DIFF_STORE_ENABLED = "diff_store"; var FACT_DIFF_STORE_ENABLED = "diff_store";
var FACT_NOOP_MODE = "noop_mode"; var FACT_NOOP_MODE = "noop_mode";
var STATE_ERROR_IN_MAIN = "ERROR_IN_MAIN";
var STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR"; var STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
var STATE_STARTED = "MAGIC_NIX_CACHE_STARTED"; var STATE_STARTED = "MAGIC_NIX_CACHE_STARTED";
var STARTED_HINT = "true"; var STARTED_HINT = "true";
@ -95483,7 +95484,6 @@ var MagicNixCacheAction = class extends DetSysAction {
requireNix: "warn", requireNix: "warn",
diagnosticsSuffix: "perf" diagnosticsSuffix: "perf"
}); });
this.errorInMain = false;
this.hostAndPort = inputs_exports.getString("listen"); this.hostAndPort = inputs_exports.getString("listen");
this.diffStore = inputs_exports.getBool("diff-store"); this.diffStore = inputs_exports.getBool("diff-store");
this.addFact(FACT_DIFF_STORE_ENABLED, this.diffStore); this.addFact(FACT_DIFF_STORE_ENABLED, this.diffStore);
@ -95534,7 +95534,7 @@ var MagicNixCacheAction = class extends DetSysAction {
await this.notifyAutoCache(); await this.notifyAutoCache();
} }
async post() { async post() {
if (!this.strictMode && this.errorInMain) { if (!this.strictMode && this.mainErrored) {
this.exitWithWarning(`skipping post phase due to error in main phase`); this.exitWithWarning(`skipping post phase due to error in main phase`);
} else { } else {
if (this.noopMode) { if (this.noopMode) {
@ -95663,7 +95663,7 @@ var MagicNixCacheAction = class extends DetSysAction {
if (this.strictMode) { if (this.strictMode) {
reject(new Error(`error in notifyPromise: ${msg}`)); reject(new Error(`error in notifyPromise: ${msg}`));
} else { } else {
this.errorInMain = true; this.setMainErrored();
this.exitWithWarning(`failed to start daemon: ${msg}`); this.exitWithWarning(`failed to start daemon: ${msg}`);
} }
}); });
@ -95679,7 +95679,7 @@ var MagicNixCacheAction = class extends DetSysAction {
if (this.strictMode) { if (this.strictMode) {
reject(new Error(msg)); reject(new Error(msg));
} else { } else {
this.errorInMain = true; this.setMainErrored();
this.exitWithWarning(`Failed to kill daemon: ${msg}`); this.exitWithWarning(`Failed to kill daemon: ${msg}`);
} }
}); });
@ -95711,7 +95711,7 @@ var MagicNixCacheAction = class extends DetSysAction {
if (this.strictMode) { if (this.strictMode) {
core.setFailed(`Magic Nix Cache failed to start: ${(0,external_node_util_.inspect)(e)}`); core.setFailed(`Magic Nix Cache failed to start: ${(0,external_node_util_.inspect)(e)}`);
} else { } else {
this.errorInMain = true; this.setMainErrored();
core.warning(`Error marking the workflow as started:`); core.warning(`Error marking the workflow as started:`);
core.warning((0,external_node_util_.inspect)(e)); core.warning((0,external_node_util_.inspect)(e));
core.warning( core.warning(
@ -95773,6 +95773,16 @@ var MagicNixCacheAction = class extends DetSysAction {
core.warning(`strict mode not enabled; exiting`); core.warning(`strict mode not enabled; exiting`);
process.exit(0); 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() { function main() {
new MagicNixCacheAction().execute(); 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_DIFF_STORE_ENABLED = "diff_store";
const FACT_NOOP_MODE = "noop_mode"; const FACT_NOOP_MODE = "noop_mode";
const STATE_ERROR_IN_MAIN = "ERROR_IN_MAIN";
const STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR"; const STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
const STATE_STARTED = "MAGIC_NIX_CACHE_STARTED"; const STATE_STARTED = "MAGIC_NIX_CACHE_STARTED";
const STARTED_HINT = "true"; 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."; "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 { class MagicNixCacheAction extends DetSysAction {
private errorInMain: boolean;
private hostAndPort: string; private hostAndPort: string;
private diffStore: boolean; private diffStore: boolean;
private httpClient: Got; private httpClient: Got;
@ -47,7 +47,6 @@ class MagicNixCacheAction extends DetSysAction {
diagnosticsSuffix: "perf", diagnosticsSuffix: "perf",
}); });
this.errorInMain = false;
this.hostAndPort = inputs.getString("listen"); this.hostAndPort = inputs.getString("listen");
this.diffStore = inputs.getBool("diff-store"); this.diffStore = inputs.getBool("diff-store");
@ -108,7 +107,7 @@ class MagicNixCacheAction extends DetSysAction {
} }
async post(): Promise<void> { 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`); this.exitWithWarning(`skipping post phase due to error in main phase`);
} else { } else {
if (this.noopMode) { if (this.noopMode) {
@ -271,7 +270,7 @@ class MagicNixCacheAction extends DetSysAction {
if (this.strictMode) { if (this.strictMode) {
reject(new Error(`error in notifyPromise: ${msg}`)); reject(new Error(`error in notifyPromise: ${msg}`));
} else { } else {
this.errorInMain = true; this.setMainErrored();
this.exitWithWarning(`failed to start daemon: ${msg}`); this.exitWithWarning(`failed to start daemon: ${msg}`);
} }
}); });
@ -289,7 +288,7 @@ class MagicNixCacheAction extends DetSysAction {
if (this.strictMode) { if (this.strictMode) {
reject(new Error(msg)); reject(new Error(msg));
} else { } else {
this.errorInMain = true; this.setMainErrored();
this.exitWithWarning(`Failed to kill daemon: ${msg}`); this.exitWithWarning(`Failed to kill daemon: ${msg}`);
} }
}); });
@ -329,7 +328,7 @@ class MagicNixCacheAction extends DetSysAction {
if (this.strictMode) { if (this.strictMode) {
actionsCore.setFailed(`Magic Nix Cache failed to start: ${inspect(e)}`); actionsCore.setFailed(`Magic Nix Cache failed to start: ${inspect(e)}`);
} else { } else {
this.errorInMain = true; this.setMainErrored();
actionsCore.warning(`Error marking the workflow as started:`); actionsCore.warning(`Error marking the workflow as started:`);
actionsCore.warning(inspect(e)); actionsCore.warning(inspect(e));
actionsCore.warning( actionsCore.warning(
@ -400,6 +399,18 @@ class MagicNixCacheAction extends DetSysAction {
actionsCore.warning(`strict mode not enabled; exiting`); actionsCore.warning(`strict mode not enabled; exiting`);
process.exit(0); 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 { function main(): void {