-
-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathobjects.ts
More file actions
207 lines (187 loc) · 5.09 KB
/
objects.ts
File metadata and controls
207 lines (187 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { FastifyInstance, RequestGenericInterface } from 'fastify'
import apiKey from '../../plugins/apikey'
import { dbSuperUser, storage } from '../../plugins'
import { ObjectScanner } from '@storage/scanner/scanner'
import { FastifyReply } from 'fastify/types/reply'
const listOrphanedObjects = {
description: 'List Orphaned Objects',
params: {
type: 'object',
properties: {
tenantId: { type: 'string' },
bucketId: { type: 'string' },
},
required: ['tenantId', 'bucketId'],
},
query: {
type: 'object',
properties: {
before: { type: 'string' },
keepTmpTable: { type: 'boolean' },
},
},
} as const
const syncOrphanedObjects = {
description: 'Sync Orphaned Objects',
params: {
type: 'object',
properties: {
tenantId: { type: 'string' },
bucketId: { type: 'string' },
},
required: ['tenantId', 'bucketId'],
},
body: {
type: 'object',
properties: {
deleteDbKeys: { type: 'boolean' },
deleteS3Keys: { type: 'boolean' },
tmpTable: { type: 'string' },
},
},
optional: ['deleteDbKeys', 'deleteS3Keys'],
} as const
interface ListOrphanObjectsRequest extends RequestGenericInterface {
Params: {
tenantId: string
bucketId: string
}
Querystring: {
before?: string
keepTmpTable?: boolean
}
}
interface SyncOrphanObjectsRequest extends RequestGenericInterface {
Params: {
tenantId: string
bucketId: string
}
Body: {
deleteDbKeys?: boolean
deleteS3Keys?: boolean
before?: string
tmpTable?: string
keepTmpTable?: boolean
}
}
export default async function routes(fastify: FastifyInstance) {
fastify.register(apiKey)
fastify.register(dbSuperUser, {
disableHostCheck: true,
maxConnections: 5,
})
fastify.register(storage)
fastify.get<ListOrphanObjectsRequest>(
'/:tenantId/buckets/:bucketId/orphan-objects',
{
schema: listOrphanedObjects,
},
async (req, reply) => {
const bucket = req.params.bucketId
let before = req.query.before ? new Date(req.query.before as string) : undefined
if (before && isNaN(before.getTime())) {
return reply.status(400).send({
error: 'Invalid date format',
})
}
if (!before) {
before = new Date()
before.setHours(before.getHours() - 1)
}
const scanner = new ObjectScanner(req.storage)
const orphanObjects = scanner.listOrphaned(bucket, {
signal: req.signals.disconnect.signal,
before: before,
keepTmpTable: Boolean(req.query.keepTmpTable),
})
reply.header('Content-Type', 'application/json; charset=utf-8')
// Do not let the connection time out, periodically send
// a ping message to keep the connection alive
const respPing = ping(reply)
try {
for await (const result of orphanObjects) {
if (result.value.length > 0) {
respPing.update()
reply.raw.write(
JSON.stringify({
...result,
event: 'data',
})
)
}
}
} catch (e) {
throw e
} finally {
respPing.clear()
reply.raw.end()
}
}
)
fastify.delete<SyncOrphanObjectsRequest>(
'/:tenantId/buckets/:bucketId/orphan-objects',
{
schema: syncOrphanedObjects,
},
async (req, reply) => {
if (!req.body.deleteDbKeys && !req.body.deleteS3Keys) {
return reply.status(400).send({
error: 'At least one of deleteDbKeys or deleteS3Keys must be set to true',
})
}
const bucket = `${req.params.bucketId}`
let before = req.body.before ? new Date(req.body.before as string) : undefined
if (!before) {
before = new Date()
before.setHours(before.getHours() - 1)
}
const respPing = ping(reply)
try {
const scanner = new ObjectScanner(req.storage)
const result = scanner.deleteOrphans(bucket, {
deleteDbKeys: req.body.deleteDbKeys,
deleteS3Keys: req.body.deleteS3Keys,
signal: req.signals.disconnect.signal,
before,
tmpTable: req.body.tmpTable,
})
for await (const deleted of result) {
respPing.update()
reply.raw.write(
JSON.stringify({
...deleted,
event: 'data',
})
)
}
} catch (e) {
throw e
} finally {
respPing.clear()
reply.raw.end()
}
}
)
}
// Occasionally write a ping message to the response stream
function ping(reply: FastifyReply) {
let lastSend = undefined as Date | undefined
const clearPing = setInterval(() => {
const fiveSecondsEarly = new Date()
fiveSecondsEarly.setSeconds(fiveSecondsEarly.getSeconds() - 5)
if (!lastSend || (lastSend && lastSend < fiveSecondsEarly)) {
lastSend = new Date()
reply.raw.write(
JSON.stringify({
event: 'ping',
})
)
}
}, 1000 * 10)
return {
clear: () => clearInterval(clearPing),
update: () => {
lastSend = new Date()
},
}
}