From b1e9dd80a7f33460f294ff0e93b28b246a6f33cf Mon Sep 17 00:00:00 2001 From: boufni95 Date: Fri, 26 Sep 2025 18:00:42 +0000 Subject: [PATCH 1/3] better logging --- src/services/nostr/handler.ts | 19 +++++++++++++------ src/services/nostr/index.ts | 9 ++++++++- src/services/storage/db/storageInterface.ts | 7 ++++--- .../storage/tlv/tlvFilesStorageFactory.ts | 6 ++++-- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/services/nostr/handler.ts b/src/services/nostr/handler.ts index 9f8c99e2..8833f6eb 100644 --- a/src/services/nostr/handler.ts +++ b/src/services/nostr/handler.ts @@ -143,6 +143,7 @@ const sendToNostr: NostrSend = (initiator, data, relays) => { } subProcessHandler.Send(initiator, data, relays) } + send({ type: 'ready' }) const supportedKinds = [21000, 21001, 21002, 21003] export default class Handler { @@ -174,8 +175,8 @@ export default class Handler { if (!relay.connected) { throw new Error("failed to connect to relay") } - } catch (err) { - log("failed to connect to relay, will try again in 2 seconds") + } catch (err:any) { + log("failed to connect to relay, will try again in 2 seconds", err.message || err) setTimeout(() => { this.Connect() }, 2000) @@ -251,10 +252,16 @@ export default class Handler { } async Send(initiator: SendInitiator, data: SendData, relays?: string[]) { - const keys = this.GetSendKeys(initiator) - const privateKey = Buffer.from(keys.privateKey, 'hex') - const toSign = await this.handleSend(data, keys) - await Promise.all(toSign.map(ue => this.sendEvent(ue, keys, relays))) + try { + const keys = this.GetSendKeys(initiator) + const privateKey = Buffer.from(keys.privateKey, 'hex') + const toSign = await this.handleSend(data, keys) + await Promise.all(toSign.map(ue => this.sendEvent(ue, keys, relays))) + } catch (e: any) { + this.log(ERROR, "failed to send event", e.message || e) + throw e + } + } async handleSend(data: SendData, keys: { name: string, privateKey: string, publicKey: string }): Promise { diff --git a/src/services/nostr/index.ts b/src/services/nostr/index.ts index 47090e64..45e041f3 100644 --- a/src/services/nostr/index.ts +++ b/src/services/nostr/index.ts @@ -2,6 +2,7 @@ import { ChildProcess, fork } from 'child_process' import { EnvCanBeInteger, EnvMustBeNonEmptyString } from "../helpers/envParser.js" import { NostrSettings, NostrEvent, ChildProcessRequest, ChildProcessResponse, SendData, SendInitiator } from "./handler.js" import { Utils } from '../helpers/utilsWrapper.js' +import {getLogger, ERROR} from '../helpers/logger.js' type EventCallback = (event: NostrEvent) => void @@ -13,13 +14,19 @@ export default class NostrSubprocess { childProcess: ChildProcess utils: Utils awaitingPongs: (() => void)[] = [] + log = getLogger({}) constructor(settings: NostrSettings, utils: Utils, eventCallback: EventCallback) { this.utils = utils this.childProcess = fork("./build/src/services/nostr/handler") this.childProcess.on("error", (error) => { - console.error("nostr subprocess error") + this.log(ERROR, "nostr subprocess error", error) throw error }) + + this.childProcess.on("exit", (code) => { + this.log(ERROR, "nostr subprocess exited with code", `nostr subprocess exited with code ${code}`) + }) + this.childProcess.on("message", (message: ChildProcessResponse) => { switch (message.type) { case 'ready': diff --git a/src/services/storage/db/storageInterface.ts b/src/services/storage/db/storageInterface.ts index e635e9cd..62935e6a 100644 --- a/src/services/storage/db/storageInterface.ts +++ b/src/services/storage/db/storageInterface.ts @@ -18,6 +18,7 @@ import { PickKeysByType } from 'typeorm/common/PickKeysByType.js'; import { deserializeResponseData, serializeRequest, WhereCondition } from './serializationHelpers.js'; import { Utils } from '../../helpers/utilsWrapper.js'; import { ProcessMetrics } from '../tlv/processMetricsCollector.js'; +import { getLogger, ERROR } from '../../helpers/logger.js'; export type TX = (txId: string) => Promise @@ -28,7 +29,7 @@ export class StorageInterface extends EventEmitter { private debug: boolean = false; private utils: Utils private dbType: 'main' | 'metrics' - + private log = getLogger({component: 'StorageInterface'}) constructor(utils: Utils) { super(); this.initializeSubprocess(); @@ -56,13 +57,13 @@ export class StorageInterface extends EventEmitter { }); this.process.on('error', (error: Error) => { - console.error('Storage processor error:', error); + this.log(ERROR, 'Storage processor error:', error); this.isConnected = false; throw error }); this.process.on('exit', (code: number) => { - console.log(`Storage processor exited with code ${code}`); + this.log(ERROR, `Storage processor exited with code ${code}`); this.isConnected = false; }); diff --git a/src/services/storage/tlv/tlvFilesStorageFactory.ts b/src/services/storage/tlv/tlvFilesStorageFactory.ts index 6a7c9bfd..0b1b5ecf 100644 --- a/src/services/storage/tlv/tlvFilesStorageFactory.ts +++ b/src/services/storage/tlv/tlvFilesStorageFactory.ts @@ -6,6 +6,7 @@ import { NostrSend, SendData, SendInitiator } from '../../nostr/handler'; import { WebRtcUserInfo } from '../../webRTC'; import * as Types from '../../../../proto/autogenerated/ts/types.js' import { ProcessMetrics } from './processMetricsCollector'; +import { getLogger, ERROR } from '../../helpers/logger.js'; export type TlvStorageInterface = { AddTlv: (appId: string, dataName: string, tlv: Uint8Array) => Promise LoadLatest: (limit?: number) => Promise @@ -18,6 +19,7 @@ export class TlvStorageFactory extends EventEmitter { private debug: boolean = false; private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') } private allowResetMetricsStorages: boolean + log = getLogger({component: 'TlvStorageFactory'}) constructor(allowResetMetricsStorages: boolean) { super(); this.allowResetMetricsStorages = allowResetMetricsStorages @@ -51,12 +53,12 @@ export class TlvStorageFactory extends EventEmitter { }); this.process.on('error', (error: Error) => { - console.error('Tlv Storage processor error:', error); + this.log(ERROR, 'Tlv Storage processor error:', error); this.isConnected = false; }); this.process.on('exit', (code: number) => { - console.log(`Tlv Storage processor exited with code ${code}`); + this.log(ERROR, `Tlv Storage processor exited with code ${code}`); this.isConnected = false; }); From eae62c4f4e27774d3c9e2eb422370b256492660c Mon Sep 17 00:00:00 2001 From: boufni95 Date: Fri, 26 Sep 2025 18:12:08 +0000 Subject: [PATCH 2/3] throw on exit --- src/services/nostr/index.ts | 6 +++--- src/services/storage/db/storageInterface.ts | 2 +- src/services/storage/tlv/tlvFilesStorageFactory.ts | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/nostr/index.ts b/src/services/nostr/index.ts index 45e041f3..fb66bc34 100644 --- a/src/services/nostr/index.ts +++ b/src/services/nostr/index.ts @@ -20,11 +20,11 @@ export default class NostrSubprocess { this.childProcess = fork("./build/src/services/nostr/handler") this.childProcess.on("error", (error) => { this.log(ERROR, "nostr subprocess error", error) - throw error }) - + this.childProcess.on("exit", (code) => { - this.log(ERROR, "nostr subprocess exited with code", `nostr subprocess exited with code ${code}`) + this.log(ERROR, `nostr subprocess exited with code ${code}`) + throw new Error(`nostr subprocess exited with code ${code}`) }) this.childProcess.on("message", (message: ChildProcessResponse) => { diff --git a/src/services/storage/db/storageInterface.ts b/src/services/storage/db/storageInterface.ts index 62935e6a..5d279992 100644 --- a/src/services/storage/db/storageInterface.ts +++ b/src/services/storage/db/storageInterface.ts @@ -59,12 +59,12 @@ export class StorageInterface extends EventEmitter { this.process.on('error', (error: Error) => { this.log(ERROR, 'Storage processor error:', error); this.isConnected = false; - throw error }); this.process.on('exit', (code: number) => { this.log(ERROR, `Storage processor exited with code ${code}`); this.isConnected = false; + throw new Error(`Storage processor exited with code ${code}`) }); this.isConnected = true; diff --git a/src/services/storage/tlv/tlvFilesStorageFactory.ts b/src/services/storage/tlv/tlvFilesStorageFactory.ts index 0b1b5ecf..4825aef1 100644 --- a/src/services/storage/tlv/tlvFilesStorageFactory.ts +++ b/src/services/storage/tlv/tlvFilesStorageFactory.ts @@ -60,6 +60,7 @@ export class TlvStorageFactory extends EventEmitter { this.process.on('exit', (code: number) => { this.log(ERROR, `Tlv Storage processor exited with code ${code}`); this.isConnected = false; + throw new Error(`Tlv Storage processor exited with code ${code}`) }); this.isConnected = true; From 9485af19623e12178cedeb8a59214d5e6d9c911e Mon Sep 17 00:00:00 2001 From: boufni95 Date: Fri, 26 Sep 2025 18:17:41 +0000 Subject: [PATCH 3/3] throw only on failure --- src/services/nostr/index.ts | 3 +++ src/services/storage/db/storageInterface.ts | 3 +++ src/services/storage/tlv/tlvFilesStorageFactory.ts | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/services/nostr/index.ts b/src/services/nostr/index.ts index fb66bc34..07fe6872 100644 --- a/src/services/nostr/index.ts +++ b/src/services/nostr/index.ts @@ -24,6 +24,9 @@ export default class NostrSubprocess { this.childProcess.on("exit", (code) => { this.log(ERROR, `nostr subprocess exited with code ${code}`) + if (!code) { + return + } throw new Error(`nostr subprocess exited with code ${code}`) }) diff --git a/src/services/storage/db/storageInterface.ts b/src/services/storage/db/storageInterface.ts index 5d279992..941d9533 100644 --- a/src/services/storage/db/storageInterface.ts +++ b/src/services/storage/db/storageInterface.ts @@ -64,6 +64,9 @@ export class StorageInterface extends EventEmitter { this.process.on('exit', (code: number) => { this.log(ERROR, `Storage processor exited with code ${code}`); this.isConnected = false; + if (!code) { + return + } throw new Error(`Storage processor exited with code ${code}`) }); diff --git a/src/services/storage/tlv/tlvFilesStorageFactory.ts b/src/services/storage/tlv/tlvFilesStorageFactory.ts index 4825aef1..6f8bbb51 100644 --- a/src/services/storage/tlv/tlvFilesStorageFactory.ts +++ b/src/services/storage/tlv/tlvFilesStorageFactory.ts @@ -60,6 +60,9 @@ export class TlvStorageFactory extends EventEmitter { this.process.on('exit', (code: number) => { this.log(ERROR, `Tlv Storage processor exited with code ${code}`); this.isConnected = false; + if (!code) { + return + } throw new Error(`Tlv Storage processor exited with code ${code}`) });