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..07fe6872 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,22 @@ 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") - throw error + this.log(ERROR, "nostr subprocess error", error) }) + + 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}`) + }) + 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..941d9533 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,14 +57,17 @@ 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; + if (!code) { + return + } + 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 6a7c9bfd..6f8bbb51 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,13 +53,17 @@ 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; + if (!code) { + return + } + throw new Error(`Tlv Storage processor exited with code ${code}`) }); this.isConnected = true;