diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 4db387a2..ca3dfbb1 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -54,6 +54,7 @@ jobs: with: output: swagger-ui spec-file: static/api.json + github_token: ${{ secrets.GITHUB_TOKEN }} - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 diff --git a/src/http/plugins/db.ts b/src/http/plugins/db.ts index 1f1391a5..369ee12f 100644 --- a/src/http/plugins/db.ts +++ b/src/http/plugins/db.ts @@ -67,27 +67,23 @@ export const db = fastifyPlugin( fastify.addHook('onTimeout', async (request) => { if (request.db) { - try { - await request.db.dispose() - } catch (e) { + request.db.dispose().catch((e) => { logSchema.error(request.log, 'Error disposing db connection', { type: 'db-connection', error: e, }) - } + }) } }) fastify.addHook('onRequestAbort', async (request) => { if (request.db) { - try { - await request.db.dispose() - } catch (e) { + request.db.dispose().catch((e) => { logSchema.error(request.log, 'Error disposing db connection', { type: 'db-connection', error: e, }) - } + }) } }) }, @@ -133,27 +129,23 @@ export const dbSuperUser = fastifyPlugin( fastify.addHook('onTimeout', async (request) => { if (request.db) { - try { - await request.db.dispose() - } catch (e) { + request.db.dispose().catch((e) => { logSchema.error(request.log, 'Error disposing db connection', { type: 'db-connection', error: e, }) - } + }) } }) fastify.addHook('onRequestAbort', async (request) => { if (request.db) { - try { - await request.db.dispose() - } catch (e) { + request.db.dispose().catch((e) => { logSchema.error(request.log, 'Error disposing db connection', { type: 'db-connection', error: e, }) - } + }) } }) }, diff --git a/src/internal/queue/queue.ts b/src/internal/queue/queue.ts index e687dfb4..541f9053 100644 --- a/src/internal/queue/queue.ts +++ b/src/internal/queue/queue.ts @@ -98,7 +98,6 @@ export abstract class Queue { }) return Queue.stop() .then(async () => { - await Queue.callClose() logSchema.info(logger, '[Queue] Exited', { type: 'queue', }) @@ -109,6 +108,11 @@ export abstract class Queue { type: 'queue', }) }) + .finally(async () => { + await Queue.callClose().catch(() => { + // no-op + }) + }) }, { once: true } ) diff --git a/src/storage/events/base-event.ts b/src/storage/events/base-event.ts index bd228578..26bb691a 100644 --- a/src/storage/events/base-event.ts +++ b/src/storage/events/base-event.ts @@ -1,7 +1,7 @@ import { Event as QueueBaseEvent, BasePayload, StaticThis, Event } from '@internal/queue' import { getPostgresConnection, getServiceKeyUser } from '@internal/database' import { StorageKnexDB } from '../database' -import { createStorageBackend } from '../backend' +import { createStorageBackend, StorageBackendAdapter } from '../backend' import { Storage } from '../storage' import { getConfig } from '../../config' import { logger } from '@internal/monitoring' @@ -9,20 +9,15 @@ import { createAgent } from '@internal/http' const { storageS3MaxSockets, storageBackendType, region } = getConfig() -const httpAgent = createAgent('s3_worker', { - maxSockets: storageS3MaxSockets, -}) -const storageBackend = createStorageBackend(storageBackendType, { - httpAgent: httpAgent, -}) +let storageBackend: StorageBackendAdapter | undefined = undefined export abstract class BaseEvent> extends QueueBaseEvent { static onStart() { - httpAgent.monitor() + this.getOrCreateStorageBackend() } static onClose() { - storageBackend.close() + storageBackend?.close() } /** @@ -81,6 +76,26 @@ export abstract class BaseEvent> extends host: payload.tenant.host, }) - return new Storage(storageBackend, db) + return new Storage(this.getOrCreateStorageBackend(), db) + } + + protected static getOrCreateStorageBackend(monitor = false) { + if (storageBackend) { + return storageBackend + } + + const httpAgent = createAgent('s3_worker', { + maxSockets: storageS3MaxSockets, + }) + + storageBackend = createStorageBackend(storageBackendType, { + httpAgent: httpAgent, + }) + + if (monitor) { + httpAgent.monitor() + } + + return storageBackend } }