Skip to content

Commit

Permalink
log-client returns log after which to resume
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinRoy committed Nov 21, 2023
1 parent 2208cdb commit 3f84c2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-moles-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lightmill/log-client': minor
---

resumeRun returns the log after which the run has been resumed
27 changes: 19 additions & 8 deletions packages/log-client/src/log-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { throttle } from 'throttle-debounce';
import type { JsonValue } from 'type-fest';
import * as Interface from './log-server-interface.js';
import { RequestError } from './fetch.js';

interface Typed<Type extends string = string> {
type: Type;
Expand Down Expand Up @@ -80,6 +81,11 @@ export class LogClient<ClientLog extends Typed & OptionallyDated = AnyLog> {
async getResumableRuns() {
let sessionInfo = await Interface.getSessionInfo({
apiRoot: this.#apiRoot,
}).catch((error) => {
if (error instanceof RequestError && error.status === 404) {
return { runs: [] };
}
throw error;
});

let matchingRuns = (sessionInfo?.runs ?? []).filter(
Expand All @@ -103,14 +109,14 @@ export class LogClient<ClientLog extends Typed & OptionallyDated = AnyLog> {
}));
}

async resumeRun({
async resumeRun<T extends ClientLog['type']>({
runId,
experimentId,
resumeAfterLast,
}: {
runId?: string;
experimentId?: string;
resumeAfterLast: ClientLog['type'] | ClientLog['type'][];
resumeAfterLast: T | T[];
}) {
if (this.#runStatus !== 'idle') {
throw new Error(
Expand Down Expand Up @@ -159,22 +165,27 @@ export class LogClient<ClientLog extends Typed & OptionallyDated = AnyLog> {
runId: runIdToResume,
experimentId: experimentIdToResume,
});
let resumeAfterLastSet = new Set(
let resumeAfterLastSet = new Set<string>(
Array.isArray(resumeAfterLast) ? resumeAfterLast : [resumeAfterLast],
);
let lastNumber = runInfo.logs
.filter((l) => resumeAfterLastSet.has(l.type))
.reduce((acc, log) => Math.max(acc, log.lastNumber), 0);
let lastLog = runInfo.logs
.filter((l): l is { type: T } & typeof l =>
resumeAfterLastSet.has(l.type),
)
.reduce((maxLog, log) =>
maxLog.lastNumber > log.lastNumber ? maxLog : log,
);
await Interface.resumeRun({
apiRoot: this.#apiRoot,
runId: runIdToResume,
experimentId: experimentIdToResume,
resumeFrom: lastNumber + 1,
resumeFrom: lastLog.lastNumber + 1,
});
this.#runId = runId;
this.#experimentId = experimentId;
this.#runStatus = 'running';
this.#logCount = lastNumber;
this.#logCount = lastLog.lastNumber;
return { type: lastLog.type, number: lastLog.count };
} catch (err) {
this.#runStatus = 'error';
throw err;
Expand Down

0 comments on commit 3f84c2c

Please sign in to comment.