@isentinel/machine-budget 0.1.0 → 0.2.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.
@@ -0,0 +1,51 @@
1
+ import { a as readSharedLedger, n as formatGb, t as describeRun } from "./run-text-1kRifnCt.mjs";
2
+ import process from "node:process";
3
+ // src/status.ts
4
+ /**
5
+ * Writes the report of the machine budget: each session with the age of its
6
+ * heartbeat, each lease with its holder and lane, and each waiter with its
7
+ * request and how long it has waited.
8
+ *
9
+ * @param input - The ledger path, the time and the rows.
10
+ * @returns The lines of the report.
11
+ */
12
+ function formatStatus({ file, now, snapshot }) {
13
+ if (snapshot === void 0) return [`machine budget ledger: ${file}`, "the ledger is empty: no run has opened it"];
14
+ const { leases, sessions, waiters } = snapshot;
15
+ const sessionsById = new Map(sessions.map((session) => [session.id, session]));
16
+ function holder(id) {
17
+ return describeRun(sessionsById.get(id));
18
+ }
19
+ return [
20
+ `machine budget ledger: ${file}`,
21
+ `runs (${sessions.length}):`,
22
+ ...sessions.map((session) => {
23
+ return ` ${describeRun(session)}, heartbeat ${formatSeconds(now - session.heartbeatAt)} ago`;
24
+ }),
25
+ `leases (${leases.length}):`,
26
+ ...leases.map((lease) => {
27
+ return ` ${holder(lease.session)}: ${lease.lane} lane, ${formatGb(lease.memory)}, ${lease.threads} threads`;
28
+ }),
29
+ `waiters (${waiters.length}):`,
30
+ ...waiters.map((waiter) => {
31
+ return ` ${holder(waiter.session)}: ${formatGb(waiter.memory)}, ${waiter.threads} threads, waiting ${formatSeconds(now - waiter.since)}`;
32
+ })
33
+ ];
34
+ }
35
+ function formatSeconds(milliseconds) {
36
+ return `${Math.max(0, Math.round(milliseconds / 1e3))} s`;
37
+ }
38
+ // src/status-cli.ts
39
+ /**
40
+ * Prints the machine budget ledger of this machine and writes nothing.
41
+ *
42
+ * @param read - Reads the ledger path and its rows.
43
+ */
44
+ function main(read = readSharedLedger) {
45
+ const lines = formatStatus({
46
+ ...read(),
47
+ now: Date.now()
48
+ });
49
+ process.stdout.write(`${lines.join("\n")}\n`);
50
+ }
51
+ export { main };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isentinel/machine-budget",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Leases memory and threads to Node dev tools from one budget shared by the whole machine.",
5
5
  "keywords": [
6
6
  "memory",
@@ -21,7 +21,11 @@
21
21
  "./package.json": "./package.json"
22
22
  },
23
23
  "types": "./dist/index.d.mts",
24
+ "bin": {
25
+ "machine-budget-status": "./bin/machine-budget-status.js"
26
+ },
24
27
  "files": [
28
+ "bin",
25
29
  "dist"
26
30
  ],
27
31
  "dependencies": {