Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
use stream.opened instead of stream.connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadwork committed Nov 5, 2023
1 parent 79dad1b commit 89abf36
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions commands/services/tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ajv, logAjvErrors } from "../../util/ajv.ts";
import { getLogger } from "../../util/logging.ts";
import { Subcommand, withConfig } from "../_helpers.ts";

const desc =
const desc =
`Tails logs for a given service.
TODO: support specifying a particular deploy ID from which to source logs.
Expand Down Expand Up @@ -53,61 +53,65 @@ export const servicesTailCommand =
},
}
);

let conn: WebSocketConnection;
let reader: ReadableStreamDefaultReader<string | Uint8Array>;
let writer: WritableStreamDefaultWriter<string | Uint8Array>;

try {
conn = await stream.connection;
if(stream.connection) {
conn = await stream.connection;
} else {
conn = await stream.opened;
}
reader = await conn.readable.getReader();
writer = await conn.writable.getWriter();
} catch (err) {
throw err;
}

const logEntryValidator = ajv.compile(LogTailEntry);


setInterval(() => {
writer.write('{ "ping": true }');
}, 15000);

while (true) {
const input = (await reader.read()).value;
if (!input) {
logger.debug("empty packet received from web socket?");
continue;
}

let json: string;
if (typeof(input) === 'string') {
json = input;
} else {
json = (new TextDecoder()).decode(input);
}

const rawMsg = JSON.parse(json);

if (logEntryValidator(rawMsg)) {
const msg = rawMsg as {deployID: string; text: string}
if (deployIds && !deployIds.has(msg.deployID)) {
continue;
}

let output;
if (opts.json) {
output = json.trim();
} else {
output = "";

if (!opts.raw) {
output += `${msg.deployID}: `;
}

output += msg.text;
}

console.log(output);
} else {
logger.error("Unparseable entry from tail socket.")
Expand Down

0 comments on commit 89abf36

Please sign in to comment.