forked from nix-community/cache-nix-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveImpl.ts
163 lines (142 loc) · 5.03 KB
/
saveImpl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as cache from "@cache-nix-action/cache";
import * as core from "@actions/core";
import { Events, Inputs, State } from "./constants";
import * as inputs from "./inputs";
import {
type IStateProvider,
NullStateProvider,
StateProvider
} from "./stateProvider";
import * as utils from "./utils/action";
import { removeGarbage } from "./utils/collectGarbage";
import { purgeCacheByKey, purgeCachesByTime } from "./utils/purge";
import * as fs from "fs";
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn.
process.on("uncaughtException", e => utils.logError(e.message));
export async function saveImpl(
stateProvider: IStateProvider
): Promise<number | void> {
const cacheId = -1;
const time = Date.now();
try {
if (!utils.isCacheFeatureAvailable()) {
return;
}
if (!utils.isValidEvent()) {
throw new Error(
`Event Validation Error: The event type ${
process.env[Events.Key]
} is not supported because it's not tied to a branch or tag ref.`
);
}
// If restore has stored a primary key in state, reuse that
// Else re-evaluate from inputs
const primaryKey =
stateProvider.getState(State.CachePrimaryKey) || inputs.primaryKey;
if (inputs.save) {
utils.info(
`Trying to save a new cache with the key "${primaryKey}".`
);
}
if (inputs.purge) {
if (inputs.purgePrimaryKey == "always") {
await purgeCacheByKey(
primaryKey,
`Purging the cache with the key "${primaryKey}" because of "${Inputs.PurgePrimaryKey}: always".`
);
} else {
await purgeCachesByTime({
primaryKey,
time,
prefixes: []
});
}
}
// Save a cache using the primary key
if (!inputs.save) {
`Not saving a new cache because of "${Inputs.Save}: false"`;
} else {
utils.info(`Searching for a cache with the key "${primaryKey}".`);
const foundKey = await utils.restoreCache({
primaryKey,
restoreKeys: [],
lookupOnly: true
});
if (utils.isExactKeyMatch(primaryKey, foundKey)) {
utils.info(
`
Cache hit occurred on the "${Inputs.PrimaryKey}".
Not saving a new cache.
`
);
} else {
utils.info(`Found no cache with this key.`);
await removeGarbage();
utils.info(`Saving a new cache with the key "${primaryKey}".`);
// can throw
await cache.saveCache(inputs.paths, primaryKey, {
uploadChunkSize: inputs.uploadChunkSize
});
utils.info(`Saved a new cache.`);
core.debug("\n\nNix store paths:\n\n");
fs.readdirSync("/nix/store").forEach(file => {
core.debug(file);
});
}
}
// Purge other caches
if (inputs.purge) {
await purgeCachesByTime({
primaryKey,
time,
prefixes: inputs.purgePrefixes
});
}
} catch (error: unknown) {
core.setFailed((error as Error).message);
}
return cacheId;
}
export async function saveOnlyRun(
earlyExit?: boolean | undefined
): Promise<void> {
try {
const cacheId = await saveImpl(new NullStateProvider());
if (cacheId === -1) {
core.warning(`Cache save failed.`);
}
} catch (err) {
console.error(err);
if (earlyExit) {
process.exit(1);
}
}
// node will stay alive if any promises are not resolved,
// which is a possibility if HTTP requests are dangling
// due to retries or timeouts. We know that if we got here
// that all promises that we care about have successfully
// resolved, so simply exit with success.
if (earlyExit) {
process.exit(0);
}
}
export async function saveRun(earlyExit?: boolean | undefined): Promise<void> {
try {
await saveImpl(new StateProvider());
} catch (err) {
console.error(err);
if (earlyExit) {
process.exit(1);
}
}
// node will stay alive if any promises are not resolved,
// which is a possibility if HTTP requests are dangling
// due to retries or timeouts. We know that if we got here
// that all promises that we care about have successfully
// resolved, so simply exit with success.
if (earlyExit) {
process.exit(0);
}
}