From 1f42cfbc5e545243569f6398a683a44bbe4b3670 Mon Sep 17 00:00:00 2001 From: shafi54 <108669266+shafi-aviz@users.noreply.github.com> Date: Fri, 3 Apr 2026 21:00:02 +0530 Subject: [PATCH] enh --- apps/backend/src/jobs/cache-creator.ts | 9 +++------ apps/backend/src/lib/post-order-handler.ts | 1 + apps/backend/src/lib/queue-consumer.ts | 1 + apps/backend/src/lib/worker-init.ts | 8 ++++++++ apps/backend/worker.ts | 19 +++++++++++-------- apps/backend/wrangler.toml | 3 +++ 6 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 apps/backend/src/lib/worker-init.ts diff --git a/apps/backend/src/jobs/cache-creator.ts b/apps/backend/src/jobs/cache-creator.ts index c8a6dfb..9a954af 100644 --- a/apps/backend/src/jobs/cache-creator.ts +++ b/apps/backend/src/jobs/cache-creator.ts @@ -1,6 +1,6 @@ import dayjs from 'dayjs' import { initializeAllStores } from '@/src/stores/store-initializer' -import { initDb } from '@/src/dbService' +import { ensureWorkerInit } from '@/src/lib/worker-init' const LAST_TRIGGER_KEY = 'lastTrigger' const ALARM_DELAY_MINUTES = 0.5 @@ -13,7 +13,7 @@ export class CacheCreator { constructor(state: any, env: any) { this.state = state this.env = env - ;(globalThis as any).ENV = env + ensureWorkerInit(env) } async schedule(): Promise { @@ -52,10 +52,7 @@ export class CacheCreator { } async alarm(): Promise { - ;(globalThis as any).ENV = this.env - if (this.env?.DB) { - initDb(this.env.DB) - } + ensureWorkerInit(this.env) const lastTrigger = await this.state.storage.get(LAST_TRIGGER_KEY) if (!lastTrigger) { return diff --git a/apps/backend/src/lib/post-order-handler.ts b/apps/backend/src/lib/post-order-handler.ts index f98f59f..eb0820c 100644 --- a/apps/backend/src/lib/post-order-handler.ts +++ b/apps/backend/src/lib/post-order-handler.ts @@ -4,6 +4,7 @@ import { } from '@/src/dbService' import { sendTelegramMessage } from '@/src/lib/telegram-service' import { queueDataPusher } from '@/src/lib/queue-data-pusher' +import { ensureWorkerInit } from './worker-init'; interface OrderIdMessage { orderIds: number[]; diff --git a/apps/backend/src/lib/queue-consumer.ts b/apps/backend/src/lib/queue-consumer.ts index f60360d..951a757 100644 --- a/apps/backend/src/lib/queue-consumer.ts +++ b/apps/backend/src/lib/queue-consumer.ts @@ -24,6 +24,7 @@ export const handleNotifQueue = (batch: any) => { } export const handleOrderPlacedQueue = async (batch: any) => { + console.log('from the order placed queue handler') for (const message of batch.messages || []) { const body = message?.body if (!body || !Array.isArray(body.orderIds)) { diff --git a/apps/backend/src/lib/worker-init.ts b/apps/backend/src/lib/worker-init.ts new file mode 100644 index 0000000..da081d7 --- /dev/null +++ b/apps/backend/src/lib/worker-init.ts @@ -0,0 +1,8 @@ +import { initDb } from '@/src/dbService' + +export const ensureWorkerInit = (env: any) => { + ;(globalThis as any).ENV = env + if (env?.DB) { + initDb(env.DB) + } +} diff --git a/apps/backend/worker.ts b/apps/backend/worker.ts index 51b3166..d50bc85 100644 --- a/apps/backend/worker.ts +++ b/apps/backend/worker.ts @@ -5,7 +5,7 @@ import type { } from '@cloudflare/workers-types' import { CacheCreator } from './src/jobs/cache-creator' import { createApp } from './src/app' -import { initDb } from './src/dbService' +import { ensureWorkerInit } from './src/lib/worker-init' import { handleNotifQueue, handleOrderPlacedQueue, @@ -32,10 +32,7 @@ export default { }, ctx: ExecutionContext ) { - ;(globalThis as any).ENV = env - if (env.DB) { - initDb(env.DB) - } + ensureWorkerInit(env) const app = createApp() return app.fetch(request, env, ctx) }, @@ -51,19 +48,25 @@ export default { ORDER_CANCELLED_QUEUE: { send: (message: unknown) => Promise } + DB?: D1Database + NOTIF_QUEUE_NAME: string + ORDER_PLACED_QUEUE_NAME: string + ORDER_CANCELLED_QUEUE_NAME: string } ) { - if (batch?.queue === 'notif_queue') { + ensureWorkerInit(env) + console.log('from the queue handler') + if (batch?.queue === env.NOTIF_QUEUE_NAME) { handleNotifQueue(batch) return } - if (batch?.queue === 'order_placed_queue') { + if (batch?.queue === env.ORDER_PLACED_QUEUE_NAME) { await handleOrderPlacedQueue(batch) return } - if (batch?.queue === 'order_cancelled_queue') { + if (batch?.queue === env.ORDER_CANCELLED_QUEUE_NAME) { await handleOrderCancelledQueue(batch) return } diff --git a/apps/backend/wrangler.toml b/apps/backend/wrangler.toml index c192f63..2858bc5 100644 --- a/apps/backend/wrangler.toml +++ b/apps/backend/wrangler.toml @@ -57,6 +57,9 @@ head_sampling_rate = 1 [vars] ENV_MODE = "PROD" +NOTIF_QUEUE_NAME = "notif-queue-dev" +ORDER_PLACED_QUEUE_NAME = "order-placed-queue-dev" +ORDER_CANCELLED_QUEUE_NAME = "order-cancelled-queue-dev" DATABASE_URL = "postgresql://postgres:meatfarmer_master_password@57.128.212.174:7447/meatfarmer" PHONE_PE_BASE_URL = "https://api-preprod.phonepe.com/" PHONE_PE_CLIENT_ID = "TEST-M23F2IGP34ZAR_25090"