mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-04-26 00:46:23 +02:00
fix run-id-gen
This commit is contained in:
parent
88fc585cc2
commit
80ceba4b11
1 changed files with 32 additions and 0 deletions
32
apps/cli/src/application/lib/run-id-gen.ts
Normal file
32
apps/cli/src/application/lib/run-id-gen.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
class RunIdGenerator {
|
||||
private lastMs = 0;
|
||||
private seq = 0;
|
||||
private readonly pid: string;
|
||||
private readonly hostTag: string;
|
||||
|
||||
constructor(hostTag: string = "") {
|
||||
this.pid = String(process.pid).padStart(7, "0");
|
||||
this.hostTag = hostTag ? `-${hostTag}` : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ISO8601-based, lexicographically sortable id string.
|
||||
* Example: 2025-11-11T04-36-29Z-0001234-h1-000
|
||||
*/
|
||||
next(): string {
|
||||
const now = Date.now();
|
||||
const ms = now >= this.lastMs ? now : this.lastMs; // monotonic clamp
|
||||
this.seq = ms === this.lastMs ? this.seq + 1 : 0;
|
||||
this.lastMs = ms;
|
||||
|
||||
// Build ISO string (UTC) and remove milliseconds for cleaner filenames
|
||||
const iso = new Date(ms).toISOString() // e.g. 2025-11-11T04:36:29.123Z
|
||||
.replace(/\.\d{3}Z$/, "Z") // drop .123 part
|
||||
.replace(/:/g, "-"); // safe for files: 2025-11-11T04-36-29Z
|
||||
|
||||
const seqStr = String(this.seq).padStart(3, "0");
|
||||
return `${iso}-${this.pid}${this.hostTag}-${seqStr}`;
|
||||
}
|
||||
}
|
||||
|
||||
export const runIdGenerator = new RunIdGenerator();
|
||||
Loading…
Add table
Add a link
Reference in a new issue