relayrunner 0.9.1 → 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 +151 -8
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -54,7 +54,11 @@ const HelloMessage = z.object({
54
54
  isolated: z.object({
55
55
  os,
56
56
  arch: arch$1
57
- }).optional()
57
+ }).optional(),
58
+ vm: z.object({
59
+ cpus: z.number().int().positive().max(1e4),
60
+ memoryGb: z.number().int().positive().max(1e5)
61
+ }).optional().catch(void 0)
58
62
  });
59
63
  const StatusMessage = z.object({
60
64
  v: version$1,
@@ -62,6 +66,15 @@ const StatusMessage = z.object({
62
66
  state: z.enum(["idle", "busy"]),
63
67
  running: z.number().int().nonnegative().max(1e3).optional()
64
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
+ });
65
78
  const JobMessage = z.object({
66
79
  v: version$1,
67
80
  type: z.literal("job"),
@@ -71,7 +84,8 @@ const JobMessage = z.object({
71
84
  "finished",
72
85
  "failed"
73
86
  ]),
74
- message: z.string().optional()
87
+ message: z.string().optional(),
88
+ usage: JobUsage.optional().catch(void 0)
75
89
  });
76
90
  const TokenSavedMessage = z.object({
77
91
  v: version$1,
@@ -170,7 +184,7 @@ function parse(version) {
170
184
  ] : null;
171
185
  }
172
186
  // version.ts
173
- const cliVersion = "0.9.1";
187
+ const cliVersion = "0.10.0";
174
188
  // paths.ts
175
189
  function relayrunnerHome() {
176
190
  return resolve(process.env.RELAYRUNNER_HOME || join(homedir(), ".relayrunner"));
@@ -1013,6 +1027,121 @@ function duplicateKey(value) {
1013
1027
  function decodeJson(base64) {
1014
1028
  return JSON.parse(Buffer.from(base64, "base64").toString("utf8").replace(/^/, ""));
1015
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
+ }
1016
1145
  // job.ts
1017
1146
  const runnerEnv = () => Object.fromEntries([
1018
1147
  "HOME",
@@ -1032,15 +1161,17 @@ async function runJob(run, options) {
1032
1161
  report(update);
1033
1162
  return update;
1034
1163
  };
1035
- const failed = (message) => finish({
1164
+ const failed = (message, usage) => finish({
1036
1165
  routeId,
1037
1166
  state: "failed",
1038
- message
1167
+ message,
1168
+ usage
1039
1169
  });
1040
1170
  const checked = checkJitConfig(run.jitConfig, routeId);
1041
1171
  if (!checked.ok) return failed(`Refused the job's runner config: ${checked.reason}.`);
1042
1172
  options.onChecked?.(checked.target);
1043
1173
  let clean = false;
1174
+ let sampler;
1044
1175
  try {
1045
1176
  const child = launcher.start(run);
1046
1177
  child.stdout.on("data", (chunk) => output(chunk, "stdout"));
@@ -1054,16 +1185,20 @@ async function runJob(run, options) {
1054
1185
  routeId,
1055
1186
  state: "started"
1056
1187
  });
1188
+ if (launcher.usage) sampler = sampleUsage(launcher.usage(run, child));
1057
1189
  const [code, signal] = await new Promise((resolve) => child.once("close", (code, signal) => resolve([code, signal])));
1190
+ const usage = await sampler?.stop();
1058
1191
  clean = code === 0;
1059
1192
  if (clean) return finish({
1060
1193
  routeId,
1061
- state: "finished"
1194
+ state: "finished",
1195
+ usage
1062
1196
  });
1063
- 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);
1064
1198
  } catch (error) {
1065
1199
  return failed(error instanceof Error ? error.message : String(error));
1066
1200
  } finally {
1201
+ sampler?.stop();
1067
1202
  await launcher.cleanup(run, clean);
1068
1203
  }
1069
1204
  }
@@ -1091,6 +1226,7 @@ function hostLauncher(dir) {
1091
1226
  ]
1092
1227
  });
1093
1228
  },
1229
+ usage: (_run, child) => processTreeUsage(child.pid),
1094
1230
  cleanup() {
1095
1231
  if (!locked) return;
1096
1232
  clean(dir);
@@ -1468,6 +1604,7 @@ function isolatedLauncher(isolation) {
1468
1604
  child.once("close", (code, signal) => debug.write(`${name}: exit ${signal ?? code}`));
1469
1605
  return child;
1470
1606
  },
1607
+ usage: (run) => vmUsage(containerName(run.routeId), isolation, command),
1471
1608
  async cleanup(run, clean) {
1472
1609
  if (clean) return;
1473
1610
  const args = [
@@ -3323,6 +3460,7 @@ async function connectMachine(args) {
3323
3460
  let launchers = [];
3324
3461
  let vmLauncher;
3325
3462
  let isolatedOnly;
3463
+ let vmSize;
3326
3464
  if (isolated) {
3327
3465
  const firewall = firewallInstalled();
3328
3466
  if (firewall) log(args["local-network"] ? "The firewall on this Mac is on, so --local-network has no effect. Turn it off with `relayrunner firewall off`." : "The firewall on this Mac is on.");
@@ -3335,6 +3473,10 @@ async function connectMachine(args) {
3335
3473
  debug: debugLog()
3336
3474
  });
3337
3475
  runnerVersion = isolation.runnerVersion;
3476
+ vmSize = {
3477
+ cpus: isolation.cpus,
3478
+ memoryGb: isolation.memoryGb
3479
+ };
3338
3480
  const launcher = isolatedLauncher(isolation);
3339
3481
  if (args.macos) vmLauncher = launcher;
3340
3482
  else {
@@ -3385,7 +3527,8 @@ async function connectMachine(args) {
3385
3527
  ...platform,
3386
3528
  runnerVersion,
3387
3529
  cliVersion,
3388
- device
3530
+ device,
3531
+ ...vmSize && { vm: vmSize }
3389
3532
  },
3390
3533
  slots,
3391
3534
  runJob: async (run, slot, report) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayrunner",
3
- "version": "0.9.1",
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": {