FastGPT/packages/service/common/s3/controller.ts
heheer 4f95f6867e
Some checks failed
Document deploy / sync-images (push) Has been cancelled
Build FastGPT images in Personal warehouse / get-vars (push) Has been cancelled
Document deploy / generate-timestamp (push) Has been cancelled
Document deploy / build-images (map[domain:https://fastgpt.cn suffix:cn]) (push) Has been cancelled
Document deploy / build-images (map[domain:https://fastgpt.io suffix:io]) (push) Has been cancelled
Document deploy / update-images (map[deployment:fastgpt-docs domain:https://fastgpt.cn kube_config:KUBE_CONFIG_CN suffix:cn]) (push) Has been cancelled
Document deploy / update-images (map[deployment:fastgpt-docs domain:https://fastgpt.io kube_config:KUBE_CONFIG_IO suffix:io]) (push) Has been cancelled
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:amd64 runs-on:ubuntu-24.04]) (push) Has been cancelled
Build FastGPT images in Personal warehouse / build-fastgpt-images (map[arch:arm64 runs-on:ubuntu-24.04-arm]) (push) Has been cancelled
Build FastGPT images in Personal warehouse / release-fastgpt-images (push) Has been cancelled
app delete queue (#6122)
* app delete queue

* test

* perf: del app queue

* perf: log

* perf: query

* perf: retry del s3

* fix: ts

* perf: add job

* redis retry

* perf: mq check

* update log

* perf: mq concurrency

* perf: error check

* perf: mq

* perf: init model

---------

Co-authored-by: archer <545436317@qq.com>
2025-12-20 13:11:02 +08:00

65 lines
1.8 KiB
TypeScript

import { MongoS3TTL } from './schema';
import { addLog } from '../system/log';
import { setCron } from '../system/cron';
import { checkTimerLock } from '../system/timerLock/utils';
import { TimerIdEnum } from '../system/timerLock/constants';
import path from 'node:path';
import { S3Error } from 'minio';
export async function clearExpiredMinioFiles() {
try {
const expiredFiles = await MongoS3TTL.find({
expiredTime: { $lte: new Date() }
}).lean();
if (expiredFiles.length === 0) {
addLog.info('No expired minio files to clean');
return;
}
addLog.info(`Found ${expiredFiles.length} expired minio files to clean`);
let success = 0;
let fail = 0;
for (const file of expiredFiles) {
try {
const bucketName = file.bucketName;
const bucket = global.s3BucketMap[bucketName];
if (bucket) {
await bucket.addDeleteJob({ key: file.minioKey });
await MongoS3TTL.deleteOne({ _id: file._id });
success++;
addLog.info(
`Deleted expired minio file: ${file.minioKey} from bucket: ${file.bucketName}`
);
} else {
addLog.warn(`Bucket not found: ${file.bucketName}`);
}
} catch (error) {
fail++;
addLog.error(`Failed to delete minio file: ${file.minioKey}`, error);
}
}
addLog.info(`Minio TTL cleanup completed. Success: ${success}, Failed: ${fail}`);
} catch (error) {
addLog.error('Error in clearExpiredMinioFiles', error);
}
}
export function clearExpiredS3FilesCron() {
// 每小时执行一次
setCron('0 */1 * * *', async () => {
if (
await checkTimerLock({
timerId: TimerIdEnum.clearExpiredMinioFiles,
lockMinuted: 59
})
) {
await clearExpiredMinioFiles();
}
});
}