relayrunner 0.9.2 → 0.10.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.
Files changed (2) hide show
  1. package/dist/cli.js +139 -6
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -66,6 +66,15 @@ const StatusMessage = z.object({
66
66
  state: z.enum(["idle", "busy"]),
67
67
  running: z.number().int().nonnegative().max(1e3).optional()
68
68
  });
69
+ const JobUsage = z.object({
70
+ cpuP95: z.number().min(0).max(1),
71
+ cpuPeak: z.number().min(0).max(1),
72
+ memoryP95Mb: z.number().int().nonnegative(),
73
+ memoryPeakMb: z.number().int().nonnegative(),
74
+ cpus: z.number().int().positive().max(1e5),
75
+ memoryMb: z.number().int().positive().max(1e9),
76
+ samples: z.number().int().positive()
77
+ });
69
78
  const JobMessage = z.object({
70
79
  v: version$1,
71
80
  type: z.literal("job"),
@@ -75,7 +84,8 @@ const JobMessage = z.object({
75
84
  "finished",
76
85
  "failed"
77
86
  ]),
78
- message: z.string().optional()
87
+ message: z.string().optional(),
88
+ usage: JobUsage.optional().catch(void 0)
79
89
  });
80
90
  const TokenSavedMessage = z.object({
81
91
  v: version$1,
@@ -174,7 +184,7 @@ function parse(version) {
174
184
  ] : null;
175
185
  }
176
186
  // version.ts
177
- const cliVersion = "0.9.2";
187
+ const cliVersion = "0.10.0";
178
188
  // paths.ts
179
189
  function relayrunnerHome() {
180
190
  return resolve(process.env.RELAYRUNNER_HOME || join(homedir(), ".relayrunner"));
@@ -1017,6 +1027,121 @@ function duplicateKey(value) {
1017
1027
  function decodeJson(base64) {
1018
1028
  return JSON.parse(Buffer.from(base64, "base64").toString("utf8").replace(/^/, ""));
1019
1029
  }
1030
+ // usage.ts
1031
+ const SAMPLE_MS = 5e3;
1032
+ const MB = 1048576;
1033
+ /**
1034
+ * Reads `source` every few seconds until stop(), which resolves with the job's usage, or
1035
+ * undefined when nothing was read (a job too short to sample, or a source that never answered).
1036
+ * Calling stop() again resolves with the same answer.
1037
+ */
1038
+ function sampleUsage(source, intervalMs = SAMPLE_MS) {
1039
+ const readings = [];
1040
+ let pending = Promise.resolve();
1041
+ let stopped;
1042
+ const timer = setInterval(() => {
1043
+ pending = pending.then(() => source.read().then((reading) => void (reading && readings.push(reading)), () => void 0));
1044
+ }, intervalMs);
1045
+ return { stop() {
1046
+ clearInterval(timer);
1047
+ return stopped ??= pending.then(() => summarize(readings, source));
1048
+ } };
1049
+ }
1050
+ function summarize(readings, capacity) {
1051
+ if (readings.length === 0) return void 0;
1052
+ const cpu = readings.map((reading) => Math.min(Math.max(reading.cpu, 0), 1));
1053
+ const memory = readings.map((reading) => Math.round(reading.memoryBytes / MB));
1054
+ return {
1055
+ cpuP95: round(p95(cpu)),
1056
+ cpuPeak: round(Math.max(...cpu)),
1057
+ memoryP95Mb: p95(memory),
1058
+ memoryPeakMb: Math.max(...memory),
1059
+ cpus: capacity.cpus,
1060
+ memoryMb: capacity.memoryMb,
1061
+ samples: readings.length
1062
+ };
1063
+ }
1064
+ function p95(values) {
1065
+ const sorted = [...values].sort((a, b) => a - b);
1066
+ return sorted[Math.ceil(sorted.length * .95) - 1];
1067
+ }
1068
+ const round = (share) => Math.round(share * 1e3) / 1e3;
1069
+ /**
1070
+ * A job in the VM `name`, read with `container stats`, which reports the cores the VM is busy
1071
+ * on right now (it samples for about two seconds per call).
1072
+ */
1073
+ function vmUsage(name, size, command = "container") {
1074
+ return {
1075
+ cpus: size.cpus,
1076
+ memoryMb: size.memoryGb * 1024,
1077
+ async read() {
1078
+ const output = await run(command, [
1079
+ "stats",
1080
+ "--format",
1081
+ "json",
1082
+ "--no-stream",
1083
+ name
1084
+ ]);
1085
+ if (!output) return null;
1086
+ const [stats] = JSON.parse(output);
1087
+ if (typeof stats?.cpuUtilization !== "number" || typeof stats.memoryUsageBytes !== "number") return null;
1088
+ return {
1089
+ cpu: stats.cpuUtilization / size.cpus,
1090
+ memoryBytes: stats.memoryUsageBytes
1091
+ };
1092
+ }
1093
+ };
1094
+ }
1095
+ /**
1096
+ * A job running on this machine, read from `ps` as the runner process `pid` and everything under
1097
+ * it. Rougher than a VM's reading: a process the job hands work to outside its tree (Docker
1098
+ * Desktop, a simulator) isn't counted, and `ps` reports each process's recent CPU, not exact use.
1099
+ */
1100
+ function processTreeUsage(pid) {
1101
+ const cores = cpus().length;
1102
+ return {
1103
+ cpus: cores,
1104
+ memoryMb: Math.round(totalmem() / MB),
1105
+ async read() {
1106
+ const output = await run("ps", [
1107
+ "-A",
1108
+ "-o",
1109
+ "pid=,ppid=,%cpu=,rss="
1110
+ ]);
1111
+ return output ? treeReading(output, pid, cores) : null;
1112
+ }
1113
+ };
1114
+ }
1115
+ /** Sums `ps -o pid=,ppid=,%cpu=,rss=` output over `root` and its descendants. */
1116
+ function treeReading(output, root, cores) {
1117
+ const children = /* @__PURE__ */ new Map();
1118
+ const usage = /* @__PURE__ */ new Map();
1119
+ for (const line of output.split("\n")) {
1120
+ const [pid, ppid, cpu, rss] = line.trim().split(/\s+/).map(Number);
1121
+ if (!Number.isFinite(pid) || !Number.isFinite(ppid)) continue;
1122
+ usage.set(pid, {
1123
+ cpu: cpu || 0,
1124
+ rssKb: rss || 0
1125
+ });
1126
+ const siblings = children.get(ppid);
1127
+ if (siblings) siblings.push(pid);
1128
+ else children.set(ppid, [pid]);
1129
+ }
1130
+ if (!usage.has(root)) return null;
1131
+ let percent = 0;
1132
+ let rssKb = 0;
1133
+ const stack = [root];
1134
+ for (let pid = stack.pop(); pid !== void 0; pid = stack.pop()) {
1135
+ const own = usage.get(pid);
1136
+ percent += own.cpu;
1137
+ rssKb += own.rssKb;
1138
+ stack.push(...children.get(pid) ?? []);
1139
+ }
1140
+ return {
1141
+ cpu: percent / 100 / cores,
1142
+ memoryBytes: rssKb * 1024
1143
+ };
1144
+ }
1020
1145
  // job.ts
1021
1146
  const runnerEnv = () => Object.fromEntries([
1022
1147
  "HOME",
@@ -1036,15 +1161,17 @@ async function runJob(run, options) {
1036
1161
  report(update);
1037
1162
  return update;
1038
1163
  };
1039
- const failed = (message) => finish({
1164
+ const failed = (message, usage) => finish({
1040
1165
  routeId,
1041
1166
  state: "failed",
1042
- message
1167
+ message,
1168
+ usage
1043
1169
  });
1044
1170
  const checked = checkJitConfig(run.jitConfig, routeId);
1045
1171
  if (!checked.ok) return failed(`Refused the job's runner config: ${checked.reason}.`);
1046
1172
  options.onChecked?.(checked.target);
1047
1173
  let clean = false;
1174
+ let sampler;
1048
1175
  try {
1049
1176
  const child = launcher.start(run);
1050
1177
  child.stdout.on("data", (chunk) => output(chunk, "stdout"));
@@ -1058,16 +1185,20 @@ async function runJob(run, options) {
1058
1185
  routeId,
1059
1186
  state: "started"
1060
1187
  });
1188
+ if (launcher.usage) sampler = sampleUsage(launcher.usage(run, child));
1061
1189
  const [code, signal] = await new Promise((resolve) => child.once("close", (code, signal) => resolve([code, signal])));
1190
+ const usage = await sampler?.stop();
1062
1191
  clean = code === 0;
1063
1192
  if (clean) return finish({
1064
1193
  routeId,
1065
- state: "finished"
1194
+ state: "finished",
1195
+ usage
1066
1196
  });
1067
- return failed(signal ? `The runner was stopped by ${signal}` : `The runner exited with ${code}`);
1197
+ return failed(signal ? `The runner was stopped by ${signal}` : `The runner exited with ${code}`, usage);
1068
1198
  } catch (error) {
1069
1199
  return failed(error instanceof Error ? error.message : String(error));
1070
1200
  } finally {
1201
+ sampler?.stop();
1071
1202
  await launcher.cleanup(run, clean);
1072
1203
  }
1073
1204
  }
@@ -1095,6 +1226,7 @@ function hostLauncher(dir) {
1095
1226
  ]
1096
1227
  });
1097
1228
  },
1229
+ usage: (_run, child) => processTreeUsage(child.pid),
1098
1230
  cleanup() {
1099
1231
  if (!locked) return;
1100
1232
  clean(dir);
@@ -1472,6 +1604,7 @@ function isolatedLauncher(isolation) {
1472
1604
  child.once("close", (code, signal) => debug.write(`${name}: exit ${signal ?? code}`));
1473
1605
  return child;
1474
1606
  },
1607
+ usage: (run) => vmUsage(containerName(run.routeId), isolation, command),
1475
1608
  async cleanup(run, clean) {
1476
1609
  if (clean) return;
1477
1610
  const args = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayrunner",
3
- "version": "0.9.2",
3
+ "version": "0.10.0",
4
4
  "description": "Run GitHub Actions jobs on your machine through the RelayRunner gateway.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {