From ae38963b7a346b88e33bdac11ad7e3a7f8da77de Mon Sep 17 00:00:00 2001 From: boufni95 Date: Wed, 23 Apr 2025 17:14:50 +0000 Subject: [PATCH] fix db date + fix zombie sub process --- .gitignore | 1 + datasource.js | 2 +- metricsDatasource.js | 2 +- src/services/metrics/index.ts | 2 +- src/services/nostr/handler.ts | 2 +- src/services/storage/db/storageProcessor.ts | 10 ++++++++-- src/services/storage/entity/RootOperation.ts | 3 +++ src/services/storage/metricsStorage.ts | 2 +- .../migrations/1745428134124-root_ops_time.ts | 20 +++++++++++++++++++ src/services/storage/migrations/runner.ts | 5 +++-- .../storage/tlv/tlvFilesStorageProcessor.ts | 9 +++++++-- 11 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/services/storage/migrations/1745428134124-root_ops_time.ts diff --git a/.gitignore b/.gitignore index 32399854..3dfafe60 100644 --- a/.gitignore +++ b/.gitignore @@ -23,5 +23,6 @@ admin.connect debug.txt proto/autogenerated/debug.txt metrics_cache/ +metric_cache/ metrics_events/ bundler_events/ \ No newline at end of file diff --git a/datasource.js b/datasource.js index 402553a1..8b250b72 100644 --- a/datasource.js +++ b/datasource.js @@ -38,4 +38,4 @@ export default new DataSource({ UserBasicAuth, UserEphemeralKey, Product, UserToUserPayment, Application, ApplicationUser, UserToUserPayment, LspOrder, LndNodeInfo, TrackedProvider, InviteToken, DebitAccess, UserOffer], // synchronize: true, }) -//npx typeorm migration:generate ./src/services/storage/migrations/user_offer -d ./datasource.js \ No newline at end of file +//npx typeorm migration:generate ./src/services/storage/migrations/ops_time -d ./datasource.js \ No newline at end of file diff --git a/metricsDatasource.js b/metricsDatasource.js index 02103483..5e1bdffa 100644 --- a/metricsDatasource.js +++ b/metricsDatasource.js @@ -15,4 +15,4 @@ export default new DataSource({ migrations: [LndMetrics1703170330183, ChannelRouting1709316653538, HtlcCount1724266887195, BalanceEvents1724860966825] }); -//npx typeorm migration:generate ./src/services/storage/migrations/root_ops -d ./metricsDatasource.js \ No newline at end of file +//npx typeorm migration:generate ./src/services/storage/migrations/root_ops_time -d ./metricsDatasource.js \ No newline at end of file diff --git a/src/services/metrics/index.ts b/src/services/metrics/index.ts index b4e1a23b..8321c2dc 100644 --- a/src/services/metrics/index.ts +++ b/src/services/metrics/index.ts @@ -348,7 +348,7 @@ export default class Handler { open_channels: openChannels.map(c => ({ channel_point: c.channelPoint, active: c.active, capacity: Number(c.capacity), channel_id: c.chanId, lifetime: Number(c.lifetime), local_balance: Number(c.localBalance), remote_balance: Number(c.remoteBalance), label: c.peerAlias })), forwarding_events: totalEvents, forwarding_fees: totalFees, - root_ops: rootOps.map(r => ({ amount: r.operation_amount, created_at_unix: r.created_at.getTime(), op_id: r.operation_identifier, op_type: mapRootOpType(r.operation_type) })), + root_ops: rootOps.map(r => ({ amount: r.operation_amount, created_at_unix: r.at_unix || 0, op_id: r.operation_identifier, op_type: mapRootOpType(r.operation_type) })), }], } } diff --git a/src/services/nostr/handler.ts b/src/services/nostr/handler.ts index 34050167..6174deb1 100644 --- a/src/services/nostr/handler.ts +++ b/src/services/nostr/handler.ts @@ -62,7 +62,7 @@ const send = (message: ChildProcessResponse) => { if (process.send) { process.send(message, undefined, undefined, err => { if (err) { - getLogger({ component: "nostrMiddleware" })(ERROR, "failed to send message to parent process", err, "message:", message) + getLogger({ component: "nostrMiddleware" })(ERROR, "failed to send message to parent process from nostr handler", err, "message:", message) process.exit(1) } }) diff --git a/src/services/storage/db/storageProcessor.ts b/src/services/storage/db/storageProcessor.ts index 549b9b0d..91455178 100644 --- a/src/services/storage/db/storageProcessor.ts +++ b/src/services/storage/db/storageProcessor.ts @@ -493,8 +493,14 @@ class StorageProcessor { } private sendResponse(response: OperationResponse) { - if (process.send) { - process.send(response); + try { + + if (process.send) { + process.send(response); + } + } catch (error) { + console.error("failed to send response to main process from storage processor, killing sub process") + process.exit(1) } } } diff --git a/src/services/storage/entity/RootOperation.ts b/src/services/storage/entity/RootOperation.ts index 96e54186..3a2bd1f9 100644 --- a/src/services/storage/entity/RootOperation.ts +++ b/src/services/storage/entity/RootOperation.ts @@ -14,6 +14,9 @@ export class RootOperation { @Column() operation_identifier: string + @Column({ default: 0 }) + at_unix: number + @CreateDateColumn() created_at: Date diff --git a/src/services/storage/metricsStorage.ts b/src/services/storage/metricsStorage.ts index 3dd36e2f..8061b1b7 100644 --- a/src/services/storage/metricsStorage.ts +++ b/src/services/storage/metricsStorage.ts @@ -109,7 +109,7 @@ export default class { } async AddRootOperation(opType: string, id: string, amount: number, txId?: string) { - return this.dbs.CreateAndSave('RootOperation', { operation_type: opType, operation_amount: amount, operation_identifier: id }, txId) + return this.dbs.CreateAndSave('RootOperation', { operation_type: opType, operation_amount: amount, operation_identifier: id, at_unix: Math.floor(Date.now() / 1000) }, txId) } async GetRootOperations({ from, to }: { from?: number, to?: number }, txId?: string) { diff --git a/src/services/storage/migrations/1745428134124-root_ops_time.ts b/src/services/storage/migrations/1745428134124-root_ops_time.ts new file mode 100644 index 00000000..2c4e6bea --- /dev/null +++ b/src/services/storage/migrations/1745428134124-root_ops_time.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class RootOpsTime1745428134124 implements MigrationInterface { + name = 'RootOpsTime1745428134124' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "temporary_root_operation" ("serial_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "operation_type" varchar NOT NULL, "operation_amount" integer NOT NULL, "operation_identifier" varchar NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')), "at_unix" integer NOT NULL DEFAULT (0))`); + await queryRunner.query(`INSERT INTO "temporary_root_operation"("serial_id", "operation_type", "operation_amount", "operation_identifier", "created_at", "updated_at") SELECT "serial_id", "operation_type", "operation_amount", "operation_identifier", "created_at", "updated_at" FROM "root_operation"`); + await queryRunner.query(`DROP TABLE "root_operation"`); + await queryRunner.query(`ALTER TABLE "temporary_root_operation" RENAME TO "root_operation"`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "root_operation" RENAME TO "temporary_root_operation"`); + await queryRunner.query(`CREATE TABLE "root_operation" ("serial_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "operation_type" varchar NOT NULL, "operation_amount" integer NOT NULL, "operation_identifier" varchar NOT NULL, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`); + await queryRunner.query(`INSERT INTO "root_operation"("serial_id", "operation_type", "operation_amount", "operation_identifier", "created_at", "updated_at") SELECT "serial_id", "operation_type", "operation_amount", "operation_identifier", "created_at", "updated_at" FROM "temporary_root_operation"`); + await queryRunner.query(`DROP TABLE "temporary_root_operation"`); + } + +} diff --git a/src/services/storage/migrations/runner.ts b/src/services/storage/migrations/runner.ts index 06be66d2..99e69c3a 100644 --- a/src/services/storage/migrations/runner.ts +++ b/src/services/storage/migrations/runner.ts @@ -15,8 +15,9 @@ import { DebitToPub1727105758354 } from './1727105758354-debit_to_pub.js' import { UserCbUrl1727112281043 } from './1727112281043-user_cb_url.js' import { RootOps1732566440447 } from './1732566440447-root_ops.js' import { UserOffer1733502626042 } from './1733502626042-user_offer.js' -export const allMigrations = [Initial1703170309875, LspOrder1718387847693, LiquidityProvider1719335699480, LndNodeInfo1720187506189, TrackedProvider1720814323679, CreateInviteTokenTable1721751414878, PaymentIndex1721760297610, DebitAccess1726496225078, DebitAccessFixes1726685229264, DebitToPub1727105758354, UserCbUrl1727112281043, UserOffer1733502626042] -export const allMetricsMigrations = [LndMetrics1703170330183, ChannelRouting1709316653538, HtlcCount1724266887195, BalanceEvents1724860966825, RootOps1732566440447] +import { RootOpsTime1745428134124 } from './1745428134124-root_ops_time.js' +export const allMigrations = [Initial1703170309875, LspOrder1718387847693, LiquidityProvider1719335699480, LndNodeInfo1720187506189, TrackedProvider1720814323679, CreateInviteTokenTable1721751414878, PaymentIndex1721760297610, DebitAccess1726496225078, DebitAccessFixes1726685229264, DebitToPub1727105758354, UserCbUrl1727112281043, UserOffer1733502626042, RootOpsTime1745428134124] +export const allMetricsMigrations = [LndMetrics1703170330183, ChannelRouting1709316653538, HtlcCount1724266887195, BalanceEvents1724860966825, RootOps1732566440447, RootOpsTime1745428134124] /* export const TypeOrmMigrationRunner = async (log: PubLogger, storageManager: Storage, settings: DbSettings, arg: string | undefined): Promise => { await connectAndMigrate(log, storageManager, allMigrations, allMetricsMigrations) return false diff --git a/src/services/storage/tlv/tlvFilesStorageProcessor.ts b/src/services/storage/tlv/tlvFilesStorageProcessor.ts index 39e071a8..caf9c14c 100644 --- a/src/services/storage/tlv/tlvFilesStorageProcessor.ts +++ b/src/services/storage/tlv/tlvFilesStorageProcessor.ts @@ -356,8 +356,13 @@ class TlvFilesStorageProcessor { } private sendResponse(response: TlvOperationResponse) { - if (process.send) { - process.send(response); + try { + if (process.send) { + process.send(response); + } + } catch (error) { + console.error("failed to send response to main process from tlv files storage processor, killing sub process") + process.exit(1) } } }