Skip to content

Commit 0b84a32

Browse files
committed
Simplify loging and add more docs
1 parent 0440e39 commit 0b84a32

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/resource_clients/actor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,15 @@ export interface ActorStartOptions {
438438
}
439439

440440
export interface ActorCallOptions extends Omit<ActorStartOptions, 'waitForFinish'> {
441+
/**
442+
* Wait time in seconds for the actor run to finish.
443+
*/
441444
waitSecs?: number;
445+
/**
446+
* `Log` instance that should be used to redirect actor run logs to.
447+
* If `undefined` default `Log` will be created and used.
448+
* If `null`, no log redirection will occur.
449+
*/
442450
log?: Log | null;
443451
}
444452

src/resource_clients/log.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,38 +188,29 @@ export class StreamedLog {
188188

189189
// Push complete part of the chunk to the buffer
190190
this.streamBuffer.push(Buffer.from(chunkWithPreviousRemainder.slice(0, lastCompleteMessageIndex)));
191-
this.logBufferContent(false);
191+
this.logBufferContent();
192192

193193
// Keep processing the new data until stopped
194194
if (this.stopLogging) {
195195
break;
196196
}
197197
}
198-
199-
// Process the remaining buffer
200-
this.logBufferContent(true);
198+
// Process whatever is left when exiting. Maybe it is incomplete, maybe it is last line without EOL.
199+
const lastMessage = Buffer.from(previousChunkRemainder).toString().trim();
200+
if (lastMessage.length) {
201+
this.toLog.info(lastMessage);
202+
}
201203
}
202204

203205
/**
204206
* Parse the buffer and log complete messages.
205207
*/
206-
private logBufferContent(includeLastPart = false): void {
208+
private logBufferContent(): void {
207209
const allParts = Buffer.concat(this.streamBuffer).toString().split(this.splitMarker).slice(1);
208-
let messageMarkers;
209-
let messageContents;
210210
// Parse the buffer parts into complete messages
211-
if (includeLastPart) {
212-
// This is final call, so log everything. Do not keep anything in the buffer.
213-
messageMarkers = allParts.filter((_, i) => i % 2 === 0);
214-
messageContents = allParts.filter((_, i) => i % 2 !== 0);
215-
this.streamBuffer = [];
216-
} else {
217-
messageMarkers = allParts.filter((_, i) => i % 2 === 0).slice(0, -1);
218-
messageContents = allParts.filter((_, i) => i % 2 !== 0).slice(0, -1);
219-
220-
// The last two parts (marker and message) are possibly not complete and will be left in the buffer.
221-
this.streamBuffer = [Buffer.from(allParts.slice(-2).join(''))];
222-
}
211+
const messageMarkers = allParts.filter((_, i) => i % 2 === 0);
212+
const messageContents = allParts.filter((_, i) => i % 2 !== 0);
213+
this.streamBuffer = [];
223214

224215
messageMarkers.forEach((marker, index) => {
225216
const decodedMarker = marker;

0 commit comments

Comments
 (0)