Skip to content

Commit eca4570

Browse files
committed
feat: support auto delete plugin inactivated in 10mins
1 parent 7118bcc commit eca4570

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

lib/mongo/models/plugins.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const pluginTypeEnum = z.enum(['tool']);
77
export const PluginZodSchema = z.object({
88
type: z.literal('tool'),
99
toolId: z.string(),
10-
status: z.enum(['active', 'pending'])
10+
status: z.enum(['active', 'pending']),
11+
ttl: z.date().optional()
1112
});
1213

1314
export type MongoPluginSchemaType = z.infer<typeof PluginZodSchema>;
@@ -19,10 +20,19 @@ const pluginMongooseSchema = new Schema({
1920
type: String,
2021
required: true,
2122
enum: Object.values(PluginZodSchema.shape.status.enum)
23+
},
24+
ttl: {
25+
type: Date
2226
}
2327
});
2428

2529
pluginMongooseSchema.index({ toolId: 1 }, { unique: true, sparse: true });
2630
pluginMongooseSchema.index({ type: 1 });
31+
pluginMongooseSchema.index(
32+
{ ttl: 1 },
33+
{
34+
expireAfterSeconds: 0
35+
}
36+
);
2737

2838
export const MongoPlugin = getMongoModel('fastgpt_plugins', pluginMongooseSchema);

modules/tool/api/upload/confirmUpload.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export default s.route(contract.tool.upload.confirmUpload, async ({ body }) => {
3636
{
3737
$set: {
3838
status: PluginZodSchema.shape.status.Enum.active
39+
},
40+
$unset: {
41+
ttl: 1
3942
}
4043
},
4144
{

modules/tool/api/upload/install.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export default s.route(contract.tool.upload.install, async ({ body }) => {
3535
},
3636
type: 'tool'
3737
},
38-
{},
38+
{
39+
status: 'active'
40+
},
3941
{
4042
upsert: true
4143
}

modules/tool/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ToolDetailSchema } from './type/api';
1111
import { catchError } from '@/utils/catch';
1212
import { mimeMap } from '@/s3/const';
1313
import { MongoPlugin } from '@/mongo/models/plugins';
14+
import { addHours, addMinutes } from 'date-fns';
1415

1516
/**
1617
* Move files from unzipped structure to dist directory
@@ -227,7 +228,8 @@ export const parsePkg = async (filepath: string, setTTL: boolean = true) => {
227228
type: 'tool'
228229
},
229230
{
230-
status: 'pending'
231+
status: 'pending',
232+
ttl: addMinutes(new Date(), 10)
231233
},
232234
{
233235
upsert: true

0 commit comments

Comments
 (0)