File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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 33using FluentValidation . AspNetCore ;
44using Microsoft . AspNetCore . ResponseCompression ;
55using Microsoft . Extensions . WebEncoders ;
6+ using Quartz ;
67using Scalar . AspNetCore ;
78using System . IO . Compression ;
89using System . Text . Encodings . Web ;
910using System . Text . Unicode ;
11+ using Quartz . AspNetCore ;
12+ using WebSite . Jobs ;
1013using WebSite . Options ;
1114
1215var builder = WebApplication . CreateBuilder ( args ) ;
4144builder . Services . Configure < OpenAIOption > ( builder . Configuration . GetSection ( "OpenAI" ) ) ;
4245
4346builder . 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+
4465var app = builder . Build ( ) ;
4566
4667using ( var serviceScope = app . Services . CreateScope ( ) )
Original file line number Diff line number Diff line change 1616 <PackageReference Include =" Known.Core" Version =" 3.1.9" />
1717 <PackageReference Include =" Microsoft.AspNetCore.OpenApi" Version =" 10.0.0-preview.1.25120.3" />
1818 <PackageReference Include =" Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version =" 1.21.2" />
19+ <PackageReference Include =" Quartz.AspNetCore" Version =" 3.14.0" />
1920 <PackageReference Include =" Scalar.AspNetCore" Version =" 2.0.26" />
2021 <PackageReference Include =" Microsoft.SemanticKernel" Version =" 1.40.1" />
2122 <ProjectReference Include =" ..\CodeWF\CodeWF.csproj" />
You can’t perform that action at this time.
0 commit comments