From d4c1e980f3599515dbfb59610a38319330c3d891 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 10 Mar 2023 14:08:44 +0000 Subject: [PATCH] debug --- .github/workflows/deploy.yml | 2 +- backend/src/activitypub/objects/index.ts | 22 ++++++++++++++++++++-- backend/src/database/d1.ts | 1 + backend/src/database/index.ts | 1 + backend/src/database/neon.ts | 8 ++++++++ consumer/src/inbox.ts | 1 - consumer/src/index.ts | 23 ++++++++++++++--------- tf/main.tf | 2 +- 8 files changed, 46 insertions(+), 14 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e3c45ed..d6d4077 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,7 +2,7 @@ name: Deploy on: push: branches: - - main + - sven/debug5 repository_dispatch: jobs: deploy: diff --git a/backend/src/activitypub/objects/index.ts b/backend/src/activitypub/objects/index.ts index 70b3e36..90514c0 100644 --- a/backend/src/activitypub/objects/index.ts +++ b/backend/src/activitypub/objects/index.ts @@ -128,7 +128,16 @@ export async function cacheObject( } { - const properties = JSON.parse(row.properties) + let properties + if (typeof row.properties === 'object') { + // neon uses JSONB for properties which is returned as a deserialized + // object. + properties = row.properties + } else { + // D1 uses a string for JSON properties + properties = JSON.parse(row.properties) + } + const object = { published: new Date(row.cdate).toISOString(), ...properties, @@ -206,7 +215,16 @@ export async function getObjectBy(db: Database, key: ObjectByKey, value: string) } const result: any = results[0] - const properties = JSON.parse(result.properties) + + let properties + if (typeof row.properties === 'object') { + // neon uses JSONB for properties which is returned as a deserialized + // object. + properties = row.properties + } else { + // D1 uses a string for JSON properties + properties = JSON.parse(row.properties) + } return { published: new Date(result.cdate).toISOString(), diff --git a/backend/src/database/d1.ts b/backend/src/database/d1.ts index f13b433..9a8ce2f 100644 --- a/backend/src/database/d1.ts +++ b/backend/src/database/d1.ts @@ -35,6 +35,7 @@ export default function make({ DATABASE }: Pick): Database { const db = DATABASE as any db.qb = qb db.client = 'd1' + db.closeConnection = async () => {} return db as Database } diff --git a/backend/src/database/index.ts b/backend/src/database/index.ts index 9e17829..2a0992c 100644 --- a/backend/src/database/index.ts +++ b/backend/src/database/index.ts @@ -16,6 +16,7 @@ export interface Database { exec(query: string): Promise> qb: QueryBuilder client: string + closeConnection(): Promise } export interface PreparedStatement { diff --git a/backend/src/database/neon.ts b/backend/src/database/neon.ts index 0dde858..de42a9c 100644 --- a/backend/src/database/neon.ts +++ b/backend/src/database/neon.ts @@ -48,6 +48,12 @@ export default async function make(env: Pick): Promise client: 'neon', qb, + async closeConnection() { + console.log("closeConnection"); + await client.end() + console.log("did closeConnection"); + }, + prepare(query: string) { return new PreparedStatement(env, query, [], client) }, @@ -102,6 +108,7 @@ export class PreparedStatement { throw new Error('not implemented') } const query = sqliteToPsql(this.query) + console.log({ query }) const results = await this.client.query(query, this.values) if (results.rows.length !== 1) { @@ -117,6 +124,7 @@ export class PreparedStatement { async all(): Promise> { const query = sqliteToPsql(this.query) + console.log({ query }) const results = await this.client.query(query, this.values) return { diff --git a/consumer/src/inbox.ts b/consumer/src/inbox.ts index 5ef3227..bc962bd 100644 --- a/consumer/src/inbox.ts +++ b/consumer/src/inbox.ts @@ -13,7 +13,6 @@ export async function handleInboxMessage(env: Env, actor: Actor, message: InboxM const adminEmail = env.ADMIN_EMAIL const cache = cacheFromEnv(env) const activity = message.activity - console.log(JSON.stringify(activity)) await activityHandler.handle(domain, activity, db, message.userKEK, adminEmail, message.vapidKeys) diff --git a/consumer/src/index.ts b/consumer/src/index.ts index ada414d..93c2b81 100644 --- a/consumer/src/index.ts +++ b/consumer/src/index.ts @@ -22,10 +22,12 @@ export type Env = { export default { async queue(batch: MessageBatch, env: Env, ctx: ExecutionContext) { const sentry = initSentryQueue(env, ctx) - const db = await getDatabase(env) - try { - for (const message of batch.messages) { + for (const message of batch.messages) { + console.log(JSON.stringify(message)) + const db = await getDatabase(env) + + try { const actor = await actors.getActorById(db, new URL(message.body.actorId)) if (actor === null) { console.warn(`actor ${message.body.actorId} is missing`) @@ -42,14 +44,17 @@ export default { break } default: - throw new Error('unsupported message type: ' + message.body.type) + console.warn('unsupported message type: ' + message.body.type) } + } catch (err: any) { + if (sentry !== null) { + sentry.captureException(err) + } + console.error(err.stack, err.cause) + throw err + } finally { + await db.closeConnection() } - } catch (err: any) { - if (sentry !== null) { - sentry.captureException(err) - } - console.error(err.stack, err.cause) } }, } diff --git a/tf/main.tf b/tf/main.tf index f5384da..cde8638 100644 --- a/tf/main.tf +++ b/tf/main.tf @@ -101,7 +101,7 @@ resource "random_password" "user_key" { resource "cloudflare_pages_project" "wildebeest_pages_project" { account_id = var.cloudflare_account_id name = "wildebeest-${lower(var.name_suffix)}" - production_branch = "main" + production_branch = "sven/debug5" deployment_configs { production {