-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.mjs
More file actions
392 lines (362 loc) · 14.3 KB
/
Copy pathworkflow.mjs
File metadata and controls
392 lines (362 loc) · 14.3 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { spawn } from "node:child_process";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
import { readJsonFile, writeJsonAtomic } from "./lib/atomic-json.mjs";
import { markLedgerPathsUploaded } from "./lib/media-ledger.mjs";
import { MEDIA_EXTENSIONS, directorySize } from "./download-media.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const GIB = 1024 ** 3;
export async function runWorkflowAttempt({
mediaRoot = process.env.OUTDIR || "/data/staging",
authPath = process.env.AUTH_PATH || "/data/auth.storage.json",
statePath = process.env.STATE_PATH || "/data/sync-state.json",
outfile = process.env.OUTFILE || "/tmp/input.json",
syncReportPath = process.env.SYNC_REPORT_PATH || "/data/last-sync-report.json",
commandRunner = defaultCommandRunner,
logger = console,
alert = sendAlert,
immichUrl = process.env.IMMICH_INSTANCE_URL,
immichApiKey = process.env.IMMICH_API_KEY,
immichCli = process.env.IMMICH_CLI_PATH || path.join(__dirname, "node_modules", ".bin", "immich"),
syncArgs = splitShellWords(process.env.SYNC_ARGS || ""),
deleteAfterUpload = booleanSetting("DELETE_AFTER_UPLOAD", true),
autoCreateAlbum = booleanSetting("IMMICH_AUTO_CREATE_ALBUM", false)
} = {}) {
fs.mkdirSync(mediaRoot, { recursive: true });
const beforeSync = summarizeMedia(mediaRoot);
logger.log(`[workflow] stage=download existing=${beforeSync.count} existing_bytes=${beforeSync.bytes}`);
try { fs.unlinkSync(syncReportPath); } catch (error) {
if (error?.code !== "ENOENT") throw error;
}
await commandRunner(process.execPath, [
path.join(__dirname, "lg.mjs"), "sync",
"--auth", authPath,
"--state", statePath,
"--outfile", outfile,
"--outdir", mediaRoot,
"--report", syncReportPath,
...syncArgs
], { env: process.env, logger });
const syncReport = readJsonFile(syncReportPath, emptySyncReport());
await notifyUnavailableMedia(syncReport, { alert, logger });
const staged = summarizeMedia(mediaRoot);
verifyStaging(staged);
logger.log(
`[workflow] stage=verify files=${staged.count} bytes=${staged.bytes} `
+ `downloaded=${syncReport.downloaded || 0} downloaded_bytes=${syncReport.downloadedBytes || 0} `
+ `download_failures=${syncReport.failed || 0}`
);
if (!immichUrl && !immichApiKey) {
logger.log("[workflow] Immich is not configured; leaving verified media in staging");
return workflowResult({ syncReport, staged, uploaded: 0, uploadedBytes: 0, deleted: 0, deletedBytes: 0 });
}
if (!immichUrl || !immichApiKey) {
throw new Error("IMMICH_INSTANCE_URL and IMMICH_API_KEY must both be set");
}
if (staged.count === 0) {
logger.log("[workflow] stage=upload no staged media");
return workflowResult({ syncReport, staged, uploaded: 0, uploadedBytes: 0, deleted: 0, deletedBytes: 0 });
}
logger.log(`[workflow] stage=upload files=${staged.count} bytes=${staged.bytes}`);
const uploadArguments = ["upload", "--recursive", "--no-progress"];
if (autoCreateAlbum) uploadArguments.push("--album");
if (deleteAfterUpload) uploadArguments.push("--delete", "--delete-duplicates");
uploadArguments.push(mediaRoot);
await commandRunner(immichCli, uploadArguments, {
env: { ...process.env, IMMICH_INSTANCE_URL: immichUrl, IMMICH_API_KEY: immichApiKey },
logger
});
const deletedPaths = new Set(staged.files.filter(file => !fs.existsSync(file.path)).map(file => file.path));
let deletedBytes = staged.files
.filter(file => deletedPaths.has(file.path))
.reduce((sum, file) => sum + file.bytes, 0);
if (deleteAfterUpload) {
const remaining = staged.files.filter(file => fs.existsSync(file.path));
if (remaining.length > 0) {
throw new Error(`Immich returned success but ${remaining.length} staged media file(s) were not deleted`);
}
for (const file of staged.files) {
const sidecar = file.path.slice(0, -path.extname(file.path).length) + ".xmp";
try {
const bytes = fs.statSync(sidecar).size;
fs.unlinkSync(sidecar);
deletedBytes += bytes;
} catch (error) {
if (error?.code !== "ENOENT") throw error;
}
}
markLedgerPathsUploaded(mediaRoot, deletedPaths);
}
const afterUpload = summarizeMedia(mediaRoot);
logger.log(
`[workflow] stage=complete uploaded=${staged.count} uploaded_bytes=${staged.bytes} `
+ `deleted=${deletedPaths.size} deleted_bytes=${deletedBytes} remaining=${afterUpload.count}`
);
return workflowResult({
syncReport,
staged: afterUpload,
uploaded: staged.count,
uploadedBytes: staged.bytes,
deleted: deletedPaths.size,
deletedBytes
});
}
export async function runWorkflowWithRetries({
attempt = runWorkflowAttempt,
maxRetries = numberSetting("WORKFLOW_MAX_RETRIES", 3),
retryBaseMs = numberSetting("WORKFLOW_RETRY_BASE_MS", 5000),
retryMaxMs = numberSetting("WORKFLOW_RETRY_MAX_MS", 60000),
sleep = delay,
logger = console,
statePath = process.env.WORKFLOW_STATE_PATH || "/data/workflow-state.json",
mediaRoot = process.env.OUTDIR || "/data/staging",
alert = sendAlert,
now = () => new Date()
} = {}) {
const state = loadWorkflowState(statePath);
await runPreflightAlerts({ state, mediaRoot, alert, now, logger });
let lastError;
for (let index = 0; index < maxRetries; index += 1) {
try {
const result = await attempt({ alert });
const degraded = Number(result.download?.failed || 0) > 0;
const timestamp = now().toISOString();
state.lastRunAt = timestamp;
state.lastStatus = degraded ? "degraded" : "success";
state.lastSummary = result;
state.lastError = null;
if (degraded) {
state.consecutiveFailures = Number(state.consecutiveFailures || 0) + 1;
const repeated = state.consecutiveFailures >= numberSetting("ALERT_FAILURE_THRESHOLD", 3);
await alert(
repeated
? "Learning Genie sync repeatedly has media failures"
: "Learning Genie sync completed with media failures",
`${result.download.failed} media item(s) remain queued for retry; `
+ `consecutive degraded/failed runs: ${state.consecutiveFailures}.`,
{ priority: repeated ? "urgent" : "high" }
);
} else {
state.consecutiveFailures = 0;
state.lastSuccessfulRunAt = timestamp;
}
writeJsonAtomic(statePath, state);
return result;
} catch (error) {
lastError = error;
if (index + 1 < maxRetries) {
const waitMs = Math.min(retryBaseMs * 2 ** index, retryMaxMs);
logger.error(`[workflow] attempt=${index + 1}/${maxRetries} failed; retrying in ${waitMs}ms: ${error?.message || error}`);
await sleep(waitMs);
}
}
}
const timestamp = now().toISOString();
state.lastRunAt = timestamp;
state.lastStatus = "failed";
state.lastError = lastError?.message || String(lastError);
state.consecutiveFailures = Number(state.consecutiveFailures || 0) + 1;
writeJsonAtomic(statePath, state);
if (state.consecutiveFailures >= numberSetting("ALERT_FAILURE_THRESHOLD", 3)) {
await alert(
"Learning Genie sync repeatedly failing",
`${state.consecutiveFailures} consecutive runs failed. Latest error: ${state.lastError}`,
{ priority: "urgent" }
);
}
throw lastError;
}
export function summarizeMedia(root) {
const files = [];
if (!fs.existsSync(root)) return { count: 0, bytes: 0, zeroByte: [], incomplete: [], files };
const stack = [root];
while (stack.length) {
const directory = stack.pop();
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
if (entry.name !== ".lg-sync-tmp") stack.push(fullPath);
continue;
}
const extension = path.extname(entry.name).toLowerCase();
if (!entry.isFile() || !MEDIA_EXTENSIONS.has(extension)) continue;
const bytes = fs.statSync(fullPath).size;
files.push({ path: fullPath, bytes });
}
}
return {
count: files.length,
bytes: files.reduce((sum, file) => sum + file.bytes, 0),
zeroByte: files.filter(file => file.bytes === 0).map(file => file.path),
incomplete: files.filter(file => fs.existsSync(`${file.path}.aria2`)).map(file => file.path),
files
};
}
export function verifyStaging(summary) {
if (summary.zeroByte.length > 0) {
throw new Error(`Staging verification found ${summary.zeroByte.length} zero-byte media file(s)`);
}
if (summary.incomplete.length > 0) {
throw new Error(`Staging verification found ${summary.incomplete.length} media file(s) with aria2 control files`);
}
}
export async function notifyUnavailableMedia(syncReport, { alert = sendAlert, logger = console } = {}) {
const count = Number(syncReport?.unavailable || 0);
if (count <= 0) return false;
const items = (syncReport.unavailableItems || []).slice(0, 10);
const details = items.map(item => {
const identifier = item.mediaId || item.key || "unknown media";
return `${identifier} -> ${item.path || "unknown path"}: ${item.error || "source unavailable"}`;
});
const remaining = Math.max(0, count - items.length);
if (remaining > 0) details.push(`...and ${remaining} more unavailable item(s)`);
const body = [
`${count} media item(s) were confirmed permanently unavailable and removed from the retry queue.`,
...details
].join("\n");
logger.warn(`[workflow] newly_unavailable=${count}; the durable ledger retains audit details`);
await alert("Learning Genie media permanently unavailable", body, { priority: "high" });
return true;
}
export function loadWorkflowState(statePath) {
const state = readJsonFile(statePath, { version: 1, consecutiveFailures: 0 });
state.version = 1;
state.consecutiveFailures ||= 0;
return state;
}
export async function sendAlert(title, body, { priority = "default" } = {}) {
const directUrl = process.env.ALERT_WEBHOOK_URL;
const ntfyBase = process.env.NTFY_URL;
const ntfyTopic = process.env.NTFY_TOPIC;
const target = directUrl || (ntfyBase && ntfyTopic
? `${ntfyBase.replace(/\/$/, "")}/${encodeURIComponent(ntfyTopic)}`
: null);
if (!target) return false;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10000);
try {
const response = await fetch(target, {
method: "POST",
headers: { Title: title, Priority: priority },
body,
signal: controller.signal
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return true;
} catch (error) {
console.error(`[alert] failed to send notification: ${error?.message || error}`);
return false;
} finally {
clearTimeout(timeout);
}
}
async function runPreflightAlerts({ state, mediaRoot, alert, now, logger }) {
const staleAfterHours = numberSetting("STALE_AFTER_HOURS", 48);
if (state.lastSuccessfulRunAt && staleAfterHours > 0) {
const ageMs = now().getTime() - Date.parse(state.lastSuccessfulRunAt);
if (ageMs > staleAfterHours * 60 * 60 * 1000) {
logger.warn(`[workflow] last successful run is ${Math.floor(ageMs / 3_600_000)} hours old`);
await alert(
"Learning Genie sync is stale",
`Last fully successful run: ${state.lastSuccessfulRunAt}`,
{ priority: "high" }
);
}
}
const stagingBytes = directorySize(mediaRoot);
const alertBytes = numberSetting("STAGING_ALERT_BYTES", 10 * GIB);
if (alertBytes > 0 && stagingBytes >= alertBytes) {
logger.warn(`[workflow] staging directory is unexpectedly large: ${stagingBytes} bytes`);
await alert(
"Learning Genie staging directory is large",
`Staging contains ${stagingBytes} bytes; configured alert threshold is ${alertBytes} bytes.`,
{ priority: "high" }
);
}
}
function workflowResult({ syncReport, staged, uploaded, uploadedBytes, deleted, deletedBytes }) {
return {
download: syncReport,
uploaded,
uploadedBytes,
deleted,
deletedBytes,
remaining: staged.count,
remainingBytes: staged.bytes
};
}
function emptySyncReport() {
return {
downloaded: 0,
downloadedBytes: 0,
skipped: 0,
skippedBytes: 0,
failed: 0,
unavailable: 0,
unavailableItems: [],
warnings: 0
};
}
function splitShellWords(value) {
if (!value.trim()) return [];
const words = [];
let current = "";
let quote = null;
for (const character of value) {
if (quote) {
if (character === quote) quote = null;
else current += character;
} else if (character === "'" || character === '"') {
quote = character;
} else if (/\s/.test(character)) {
if (current) { words.push(current); current = ""; }
} else current += character;
}
if (quote) throw new Error("Unterminated quote in SYNC_ARGS");
if (current) words.push(current);
return words;
}
function booleanSetting(name, fallback) {
if (process.env[name] == null) return fallback;
return !["0", "false", "no", "off"].includes(process.env[name].toLowerCase());
}
function numberSetting(name, fallback) {
const parsed = Number(process.env[name]);
return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback;
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function defaultCommandRunner(command, args, { env = process.env, logger = console } = {}) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { env, stdio: ["ignore", "pipe", "pipe"] });
let stderrTail = "";
child.stdout.on("data", chunk => {
process.stdout.write(chunk);
});
child.stderr.on("data", chunk => {
process.stderr.write(chunk);
stderrTail = `${stderrTail}${chunk}`.slice(-32_768);
});
child.on("error", reject);
child.on("exit", (code, signal) => {
if (code === 0) resolve({ code });
else reject(new Error(`${command} exited with ${code ?? signal}: ${stderrTail.trim()}`));
});
logger.log(`[workflow] exec=${path.basename(command)} args=${args.slice(0, -1).join(" ")}`);
});
}
async function main() {
try {
await runWorkflowWithRetries();
} catch (error) {
console.error(`[workflow] failed after bounded retries: ${error?.stack || error}`);
process.exitCode = 1;
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
await main();
}