fix(apis/websocket): improve logging and error handling

This commit is contained in:
PalmDevs
2024-04-02 19:10:51 +07:00
parent e25fc7d3c5
commit b6cbe9d64c
3 changed files with 41 additions and 46 deletions

View File

@@ -16,19 +16,18 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
const nextSeq = client.currentSequence++ const nextSeq = client.currentSequence++
logger.debug(`Client ${client.id} requested to parse image from URL:`, imageUrl) logger.debug(`Client ${client.id} requested to parse image from URL (${nextSeq})`, imageUrl)
logger.debug(`Queue currently has ${queue.remaining}/${config.ocrConcurrentQueues} items in it`)
if (queue.remaining < config.ocrConcurrentQueues) queue.shift() if (queue.remaining < config.ocrConcurrentQueues) queue.shift()
await queue.wait() await queue.wait()
try { logger.debug(`Process queued (${nextSeq}), queue has ${queue.remaining} items`)
logger.debug(`Recognizing image from URL for client ${client.id}`)
try {
const { data, jobId } = await tesseract.recognize(imageUrl) const { data, jobId } = await tesseract.recognize(imageUrl)
logger.debug(`Recognized image from URL for client ${client.id} (job ${jobId}):`, data.text) logger.debug(`Image parsed (job ${jobId}) (${nextSeq}):`, data.text)
await client.send( client.send(
{ {
op: ServerOperation.ParsedImage, op: ServerOperation.ParsedImage,
d: { d: {
@@ -37,20 +36,20 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
}, },
nextSeq, nextSeq,
) )
} catch { } catch (e) {
logger.error(`Failed to parse image from URL for client ${client.id}:`, imageUrl) if (!client.disconnected)
await client.send( client.send(
{ {
op: ServerOperation.ParseImageFailed, op: ServerOperation.ParseImageFailed,
d: null, d: null,
}, },
nextSeq, nextSeq,
) )
else logger.warn(`Client disconnected before the failed packet could be sent (${nextSeq})`)
logger.error(`Failed to parse image (${nextSeq}):`, e)
} finally { } finally {
queue.shift() queue.shift()
logger.debug( logger.debug(`Finished parsing image (${nextSeq}), queue has ${queue.remaining} items`)
`Finished processing image from URL for client ${client.id}, queue has ${queue.remaining}/${config.ocrConcurrentQueues} remaining items in it`,
)
} }
} }

View File

@@ -1,7 +1,5 @@
import { type ClientOperation, ServerOperation } from '@revanced/bot-shared' import { type ClientOperation, ServerOperation } from '@revanced/bot-shared'
import { inspect as inspectObject } from 'util'
import type { EventHandler } from '.' import type { EventHandler } from '.'
const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (packet, { wit, logger }) => { const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (packet, { wit, logger }) => {
@@ -19,7 +17,7 @@ const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (pa
const { intents } = await wit.message(actualText) const { intents } = await wit.message(actualText)
const intentsWithoutIds = intents.map(({ id, ...rest }) => rest) const intentsWithoutIds = intents.map(({ id, ...rest }) => rest)
await client.send( client.send(
{ {
op: ServerOperation.ParsedText, op: ServerOperation.ParsedText,
d: { d: {
@@ -29,16 +27,16 @@ const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (pa
nextSeq, nextSeq,
) )
} catch (e) { } catch (e) {
await client.send( if (!client.disconnected)
{ client.send(
op: ServerOperation.ParseTextFailed, {
d: null, op: ServerOperation.ParseTextFailed,
}, d: null,
nextSeq, },
) nextSeq,
)
if (e instanceof Error) logger.error(e.stack ?? e.message) else logger.warn(`Client disconnected before the failed packet could be sent (${nextSeq})`)
else logger.error(inspectObject(e)) logger.error(`Failed to parse text (${nextSeq}):`, e)
} }
} }

View File

@@ -1,7 +1,5 @@
import { type ClientOperation, ServerOperation } from '@revanced/bot-shared' import { type ClientOperation, ServerOperation } from '@revanced/bot-shared'
import { inspect as inspectObject } from 'util'
import type { EventHandler } from '.' import type { EventHandler } from '.'
const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = async (packet, { wit, logger }) => { const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = async (packet, { wit, logger }) => {
@@ -13,11 +11,11 @@ const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = asy
const nextSeq = client.currentSequence++ const nextSeq = client.currentSequence++
const actualText = text.slice(0, 279) const actualText = text.slice(0, 279)
logger.debug(`Client ${client.id} requested to train label ${label} with:`, actualText) logger.debug(`${client.id} requested to train label ${label} (${nextSeq}) with:`, actualText)
try { try {
await wit.train(actualText, label) await wit.train(actualText, label)
await client.send( client.send(
{ {
op: ServerOperation.TrainedMessage, op: ServerOperation.TrainedMessage,
d: null, d: null,
@@ -25,18 +23,18 @@ const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = asy
nextSeq, nextSeq,
) )
logger.debug(`Trained label ${label} with:`, actualText) logger.debug(`Trained label (${nextSeq})`)
} catch (e) { } catch (e) {
await client.send( if (!client.disconnected)
{ client.send(
op: ServerOperation.TrainMessageFailed, {
d: null, op: ServerOperation.TrainMessageFailed,
}, d: null,
nextSeq, },
) nextSeq,
)
if (e instanceof Error) logger.error(e.stack ?? e.message) else logger.warn(`Client ${client.id} disconnected before the failed packet could be sent`)
else logger.error(inspectObject(e)) logger.error(`Failed to train (${nextSeq})`, e)
} }
} }