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