@getdial/cli 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/wait-for.js +58 -20
- package/package.json +1 -1
|
@@ -1,33 +1,23 @@
|
|
|
1
|
+
import { readAuth } from "../lib/state.js";
|
|
1
2
|
import { paths } from "../lib/paths.js";
|
|
2
3
|
import { supervisorStatus } from "../lib/supervisor/index.js";
|
|
3
4
|
import { parseFieldArg, parseRegexArg } from "../lib/event-filter.js";
|
|
4
5
|
import { currentSize, findLatestMatch, tailUntilMatch } from "../lib/log-tail.js";
|
|
6
|
+
import { apiPost } from "../lib/api.js";
|
|
7
|
+
const PER_POLL_SECONDS = 30;
|
|
5
8
|
export async function runWaitFor(opts) {
|
|
6
|
-
const status = supervisorStatus();
|
|
7
|
-
if (!status.installed || !status.running) {
|
|
8
|
-
const hint = !status.installed
|
|
9
|
-
? "Run `dial listen install` (or rerun `dial onboard`)."
|
|
10
|
-
: "It's installed but not running. Reinstall with `dial listen install` to restart it.";
|
|
11
|
-
if (opts.json) {
|
|
12
|
-
console.log(JSON.stringify({
|
|
13
|
-
ok: false,
|
|
14
|
-
reason: "listen_not_running",
|
|
15
|
-
installed: status.installed,
|
|
16
|
-
running: status.running,
|
|
17
|
-
pid: status.pid,
|
|
18
|
-
unit_path: status.unitPath,
|
|
19
|
-
}));
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
console.error(`listen daemon is not running (installed=${status.installed}, running=${status.running}). ${hint}`);
|
|
23
|
-
}
|
|
24
|
-
return 3;
|
|
25
|
-
}
|
|
26
9
|
const spec = {
|
|
27
10
|
eventType: opts.eventType,
|
|
28
11
|
fields: opts.fields.map(parseFieldArg),
|
|
29
12
|
regexes: opts.regexes.map(parseRegexArg),
|
|
30
13
|
};
|
|
14
|
+
const status = supervisorStatus();
|
|
15
|
+
if (status.installed && status.running) {
|
|
16
|
+
return waitFromLog(spec, opts);
|
|
17
|
+
}
|
|
18
|
+
return waitFromApi(spec, opts);
|
|
19
|
+
}
|
|
20
|
+
async function waitFromLog(spec, opts) {
|
|
31
21
|
const file = paths().listenLog;
|
|
32
22
|
const startOffset = currentSize(file);
|
|
33
23
|
const hit = await tailUntilMatch(file, spec, startOffset, opts.timeoutSeconds * 1000);
|
|
@@ -54,3 +44,51 @@ export async function runWaitFor(opts) {
|
|
|
54
44
|
}
|
|
55
45
|
return 2;
|
|
56
46
|
}
|
|
47
|
+
async function waitFromApi(spec, opts) {
|
|
48
|
+
const auth = readAuth();
|
|
49
|
+
if (!auth) {
|
|
50
|
+
fail(opts.json, "not_signed_in", "Not signed in. Run `dial signup` and `dial onboard` first.");
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
const filters = {};
|
|
54
|
+
for (const f of spec.fields)
|
|
55
|
+
filters[f.name] = f.value;
|
|
56
|
+
const regexFilters = {};
|
|
57
|
+
for (const r of spec.regexes)
|
|
58
|
+
regexFilters[r.name] = { pattern: r.regex.source, flags: r.regex.flags };
|
|
59
|
+
const deadline = Date.now() + opts.timeoutSeconds * 1000;
|
|
60
|
+
while (Date.now() < deadline) {
|
|
61
|
+
const remainingSec = Math.max(1, Math.ceil((deadline - Date.now()) / 1000));
|
|
62
|
+
const timeout = Math.min(PER_POLL_SECONDS, remainingSec);
|
|
63
|
+
const res = await apiPost("/api/v1/events/wait", {
|
|
64
|
+
event_type: spec.eventType,
|
|
65
|
+
filters: Object.keys(filters).length > 0 ? filters : undefined,
|
|
66
|
+
regex_filters: Object.keys(regexFilters).length > 0 ? regexFilters : undefined,
|
|
67
|
+
timeout,
|
|
68
|
+
}, auth.api_key);
|
|
69
|
+
if (res.ok && res.data?.event) {
|
|
70
|
+
process.stdout.write(JSON.stringify(res.data.event) + "\n");
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
if (res.ok === false && res.status === 408) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (res.ok === false) {
|
|
77
|
+
fail(opts.json, "api_fallback_failed", res.error, { status: res.status });
|
|
78
|
+
return 4;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (opts.json) {
|
|
82
|
+
console.log(JSON.stringify({ ok: false, timeout: true, source: "api", event: null }));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
console.error(`timed out after ${opts.timeoutSeconds}s; no matching ${opts.eventType} via API fallback.`);
|
|
86
|
+
}
|
|
87
|
+
return 2;
|
|
88
|
+
}
|
|
89
|
+
function fail(json, code, message, extra) {
|
|
90
|
+
if (json)
|
|
91
|
+
console.log(JSON.stringify({ ok: false, code, message, ...extra }));
|
|
92
|
+
else
|
|
93
|
+
console.error(message);
|
|
94
|
+
}
|