File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ using Quartz ;
2
+
3
+ namespace WebSite . Jobs ;
4
+
5
+ public class FileCleanerJob ( IWebHostEnvironment env ) : IJob
6
+ {
7
+ public Task Execute ( IJobExecutionContext context )
8
+ {
9
+ var directoryPath = Path . Combine ( env . WebRootPath , "IconFolder" ) ;
10
+ try
11
+ {
12
+ var files = Directory . GetFiles ( directoryPath ) ;
13
+ foreach ( var file in files )
14
+ {
15
+ var creationTime = File . GetCreationTime ( file ) ;
16
+ if ( ( DateTime . Now - creationTime ) . TotalMinutes > 2 )
17
+ {
18
+ File . Delete ( file ) ;
19
+ }
20
+ }
21
+ }
22
+ catch ( Exception ex )
23
+ {
24
+ // Handle exceptions, e.g., log them
25
+ Console . WriteLine ( $ "Error: { ex . Message } ") ;
26
+ }
27
+
28
+ return Task . CompletedTask ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change 3
3
using FluentValidation . AspNetCore ;
4
4
using Microsoft . AspNetCore . ResponseCompression ;
5
5
using Microsoft . Extensions . WebEncoders ;
6
+ using Quartz ;
6
7
using Scalar . AspNetCore ;
7
8
using System . IO . Compression ;
8
9
using System . Text . Encodings . Web ;
9
10
using System . Text . Unicode ;
11
+ using Quartz . AspNetCore ;
12
+ using WebSite . Jobs ;
10
13
using WebSite . Options ;
11
14
12
15
var builder = WebApplication . CreateBuilder ( args ) ;
41
44
builder . Services . Configure < OpenAIOption > ( builder . Configuration . GetSection ( "OpenAI" ) ) ;
42
45
43
46
builder . Services . AddOpenApi ( ) ;
47
+
48
+ builder . Services . AddTransient < FileCleanerJob > ( ) ;
49
+ builder . Services . AddQuartz ( q =>
50
+ {
51
+ var jobKey = new JobKey ( "fileCleanerJob" , "group1" ) ;
52
+ q . AddJob < FileCleanerJob > ( opts => opts . WithIdentity ( jobKey ) ) ;
53
+
54
+ q . AddTrigger ( opts => opts
55
+ . ForJob ( jobKey )
56
+ . WithIdentity ( "fileCleanerTrigger" , "group1" )
57
+ . StartNow ( )
58
+ . WithSimpleSchedule ( x => x
59
+ . WithIntervalInMinutes ( 2 )
60
+ . RepeatForever ( ) ) ) ;
61
+ } ) ;
62
+
63
+ builder . Services . AddQuartzHostedService ( q => q . WaitForJobsToComplete = true ) ;
64
+
44
65
var app = builder . Build ( ) ;
45
66
46
67
using ( var serviceScope = app . Services . CreateScope ( ) )
Original file line number Diff line number Diff line change 16
16
<PackageReference Include =" Known.Core" Version =" 3.1.9" />
17
17
<PackageReference Include =" Microsoft.AspNetCore.OpenApi" Version =" 10.0.0-preview.1.25120.3" />
18
18
<PackageReference Include =" Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version =" 1.21.2" />
19
+ <PackageReference Include =" Quartz.AspNetCore" Version =" 3.14.0" />
19
20
<PackageReference Include =" Scalar.AspNetCore" Version =" 2.0.26" />
20
21
<PackageReference Include =" Microsoft.SemanticKernel" Version =" 1.40.1" />
21
22
<ProjectReference Include =" ..\CodeWF\CodeWF.csproj" />
You can’t perform that action at this time.
0 commit comments