diff --git a/commands/services/tail.ts b/commands/services/tail.ts index 28b38b2..0bbbba0 100644 --- a/commands/services/tail.ts +++ b/commands/services/tail.ts @@ -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. @@ -53,61 +53,65 @@ export const servicesTailCommand = }, } ); - + let conn: WebSocketConnection; let reader: ReadableStreamDefaultReader; let writer: WritableStreamDefaultWriter; - + 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.")