@@ -2,6 +2,9 @@ import { retryFn } from '@fastgpt/global/common/system/utils';
2
2
import { connectionMongo } from '../../mongo' ;
3
3
import { MongoRawTextBufferSchema , bucketName } from './schema' ;
4
4
import { addLog } from '../../system/log' ;
5
+ import { setCron } from '../../system/cron' ;
6
+ import { checkTimerLock } from '../../system/timerLock/utils' ;
7
+ import { TimerIdEnum } from '../../system/timerLock/constants' ;
5
8
6
9
const getGridBucket = ( ) => {
7
10
return new connectionMongo . mongo . GridFSBucket ( connectionMongo . connection . db ! , {
@@ -137,3 +140,40 @@ export const updateRawTextBufferExpiredTime = async ({
137
140
) ;
138
141
} ) ;
139
142
} ;
143
+
144
+ export const clearExpiredRawTextBufferCron = async ( ) => {
145
+ const clearExpiredRawTextBuffer = async ( ) => {
146
+ addLog . debug ( 'Clear expired raw text buffer start' ) ;
147
+ const gridBucket = getGridBucket ( ) ;
148
+
149
+ return retryFn ( async ( ) => {
150
+ const data = await MongoRawTextBufferSchema . find (
151
+ {
152
+ 'metadata.expiredTime' : { $lt : new Date ( ) }
153
+ } ,
154
+ '_id'
155
+ ) . lean ( ) ;
156
+
157
+ for ( const item of data ) {
158
+ await gridBucket . delete ( item . _id ) ;
159
+ }
160
+ addLog . debug ( 'Clear expired raw text buffer end' ) ;
161
+ } ) ;
162
+ } ;
163
+
164
+ setCron ( '*/10 * * * *' , async ( ) => {
165
+ if (
166
+ await checkTimerLock ( {
167
+ timerId : TimerIdEnum . clearExpiredRawTextBuffer ,
168
+ lockMinuted : 9
169
+ } )
170
+ ) {
171
+ try {
172
+ await clearExpiredRawTextBuffer ( ) ;
173
+ } catch ( error ) {
174
+ addLog . error ( 'clearExpiredRawTextBufferCron error' , error ) ;
175
+ }
176
+ }
177
+ } ) ;
178
+ clearExpiredRawTextBuffer ( ) ;
179
+ } ;
0 commit comments