@nledford/omp-coord 0.4.0 → 0.4.2

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.
@@ -47,12 +47,38 @@ export interface JobRequest {
47
47
  lane?: string;
48
48
  priority: number;
49
49
  key?: string;
50
+ meta?: Record<string, string>;
50
51
  command: string;
51
52
  socket: string;
52
53
  pid: number;
53
54
  now: number;
54
55
  }
55
56
 
57
+ export const MAX_META = 8;
58
+ const META_KEY = /^[a-z][a-z0-9_-]{0,31}$/;
59
+ const MAX_META_VALUE = 200;
60
+
61
+ /**
62
+ * Parses repeated `--meta k=v` values into tags (a repeated key: the last wins). Throws an Error naming the problem:
63
+ * more than `MAX_META` pairs, no `=`, a key outside `[a-z][a-z0-9_-]{0,31}`, a value over 200 characters or with a
64
+ * newline. Returns undefined for no pairs.
65
+ */
66
+ export function parseMeta(pairs: string[]): Record<string, string> | undefined {
67
+ if (pairs.length > MAX_META) throw new Error(`--meta may be given at most ${MAX_META} times`);
68
+ const meta: Record<string, string> = {};
69
+ for (const pair of pairs) {
70
+ const eq = pair.indexOf("=");
71
+ if (eq < 0) throw new Error(`--meta takes k=v, not "${pair}"`);
72
+ const k = pair.slice(0, eq);
73
+ const v = pair.slice(eq + 1);
74
+ if (!META_KEY.test(k)) throw new Error(`--meta key "${k}" must match [a-z][a-z0-9_-]{0,31}`);
75
+ if (v.length > MAX_META_VALUE) throw new Error(`--meta ${k}: the value is over ${MAX_META_VALUE} characters`);
76
+ if (/[\r\n]/.test(v)) throw new Error(`--meta ${k}: the value must not contain a newline`);
77
+ meta[k] = v;
78
+ }
79
+ return Object.keys(meta).length ? meta : undefined;
80
+ }
81
+
56
82
  export type EnqueueResult = { ok: true } | { ok: false; reason: string };
57
83
 
58
84
  /** Puts a job in line (or starts it at once when a slot is free). */
@@ -86,6 +112,7 @@ export function enqueueJob(board: Board, queues: Queues, req: JobRequest): Enque
86
112
  lane,
87
113
  priority: req.priority,
88
114
  key: req.key,
115
+ meta: req.meta,
89
116
  command: req.command,
90
117
  socket: req.socket,
91
118
  pid: req.pid,
@@ -100,6 +127,7 @@ export function enqueueJob(board: Board, queues: Queues, req: JobRequest): Enque
100
127
  priority: req.priority,
101
128
  ...(req.key === undefined ? {} : { key: req.key }),
102
129
  ...(req.agent === undefined ? {} : { agent: req.agent }),
130
+ ...(req.meta === undefined ? {} : { meta: req.meta }),
103
131
  });
104
132
  schedule(board, queues, req.now);
105
133
  return { ok: true };
@@ -128,6 +156,8 @@ export function jobEnded(
128
156
  queue: job.queue,
129
157
  exitCode: j.exitCode,
130
158
  ranMs: j.now - (job.startedAt ?? j.now),
159
+ ...(job.key === undefined ? {} : { key: job.key }),
160
+ ...(job.meta === undefined ? {} : { meta: job.meta }),
131
161
  ...(j.timedOut ? { timedOut: true } : {}),
132
162
  });
133
163
  } else if (job.state === "waiting") {