Skip to content

Commit 689d255

Browse files
authored
fix: base event storage initialisation (#567)
1 parent c8f93e5 commit 689d255

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

.github/workflows/docs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
with:
5555
output: swagger-ui
5656
spec-file: static/api.json
57+
github_token: ${{ secrets.GITHUB_TOKEN }}
5758

5859
- name: Deploy to GitHub Pages
5960
uses: peaceiris/actions-gh-pages@v3

src/http/plugins/db.ts

+8-16
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,23 @@ export const db = fastifyPlugin(
6767

6868
fastify.addHook('onTimeout', async (request) => {
6969
if (request.db) {
70-
try {
71-
await request.db.dispose()
72-
} catch (e) {
70+
request.db.dispose().catch((e) => {
7371
logSchema.error(request.log, 'Error disposing db connection', {
7472
type: 'db-connection',
7573
error: e,
7674
})
77-
}
75+
})
7876
}
7977
})
8078

8179
fastify.addHook('onRequestAbort', async (request) => {
8280
if (request.db) {
83-
try {
84-
await request.db.dispose()
85-
} catch (e) {
81+
request.db.dispose().catch((e) => {
8682
logSchema.error(request.log, 'Error disposing db connection', {
8783
type: 'db-connection',
8884
error: e,
8985
})
90-
}
86+
})
9187
}
9288
})
9389
},
@@ -133,27 +129,23 @@ export const dbSuperUser = fastifyPlugin<DbSuperUserPluginOptions>(
133129

134130
fastify.addHook('onTimeout', async (request) => {
135131
if (request.db) {
136-
try {
137-
await request.db.dispose()
138-
} catch (e) {
132+
request.db.dispose().catch((e) => {
139133
logSchema.error(request.log, 'Error disposing db connection', {
140134
type: 'db-connection',
141135
error: e,
142136
})
143-
}
137+
})
144138
}
145139
})
146140

147141
fastify.addHook('onRequestAbort', async (request) => {
148142
if (request.db) {
149-
try {
150-
await request.db.dispose()
151-
} catch (e) {
143+
request.db.dispose().catch((e) => {
152144
logSchema.error(request.log, 'Error disposing db connection', {
153145
type: 'db-connection',
154146
error: e,
155147
})
156-
}
148+
})
157149
}
158150
})
159151
},

src/internal/queue/queue.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export abstract class Queue {
9898
})
9999
return Queue.stop()
100100
.then(async () => {
101-
await Queue.callClose()
102101
logSchema.info(logger, '[Queue] Exited', {
103102
type: 'queue',
104103
})
@@ -109,6 +108,11 @@ export abstract class Queue {
109108
type: 'queue',
110109
})
111110
})
111+
.finally(async () => {
112+
await Queue.callClose().catch(() => {
113+
// no-op
114+
})
115+
})
112116
},
113117
{ once: true }
114118
)

src/storage/events/base-event.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
import { Event as QueueBaseEvent, BasePayload, StaticThis, Event } from '@internal/queue'
22
import { getPostgresConnection, getServiceKeyUser } from '@internal/database'
33
import { StorageKnexDB } from '../database'
4-
import { createStorageBackend } from '../backend'
4+
import { createStorageBackend, StorageBackendAdapter } from '../backend'
55
import { Storage } from '../storage'
66
import { getConfig } from '../../config'
77
import { logger } from '@internal/monitoring'
88
import { createAgent } from '@internal/http'
99

1010
const { storageS3MaxSockets, storageBackendType, region } = getConfig()
1111

12-
const httpAgent = createAgent('s3_worker', {
13-
maxSockets: storageS3MaxSockets,
14-
})
15-
const storageBackend = createStorageBackend(storageBackendType, {
16-
httpAgent: httpAgent,
17-
})
12+
let storageBackend: StorageBackendAdapter | undefined = undefined
1813

1914
export abstract class BaseEvent<T extends Omit<BasePayload, '$version'>> extends QueueBaseEvent<T> {
2015
static onStart() {
21-
httpAgent.monitor()
16+
this.getOrCreateStorageBackend()
2217
}
2318

2419
static onClose() {
25-
storageBackend.close()
20+
storageBackend?.close()
2621
}
2722

2823
/**
@@ -81,6 +76,26 @@ export abstract class BaseEvent<T extends Omit<BasePayload, '$version'>> extends
8176
host: payload.tenant.host,
8277
})
8378

84-
return new Storage(storageBackend, db)
79+
return new Storage(this.getOrCreateStorageBackend(), db)
80+
}
81+
82+
protected static getOrCreateStorageBackend(monitor = false) {
83+
if (storageBackend) {
84+
return storageBackend
85+
}
86+
87+
const httpAgent = createAgent('s3_worker', {
88+
maxSockets: storageS3MaxSockets,
89+
})
90+
91+
storageBackend = createStorageBackend(storageBackendType, {
92+
httpAgent: httpAgent,
93+
})
94+
95+
if (monitor) {
96+
httpAgent.monitor()
97+
}
98+
99+
return storageBackend
85100
}
86101
}

src/storage/object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export class ObjectStorage {
384384
} catch (e) {
385385
await ObjectAdminDelete.send({
386386
name: destinationKey,
387-
bucketId: this.bucketId,
387+
bucketId: destinationBucket,
388388
tenant: this.db.tenant(),
389389
version: newVersion,
390390
reqId: this.db.reqId,

0 commit comments

Comments
 (0)