@@ -25,7 +25,7 @@ namespace CSharpOpenBMCLAPI.Modules
25
25
/// <summary>
26
26
/// Cluster 实例,进行最基本的节点服务
27
27
/// </summary>
28
- public class Cluster : RunnableBase
28
+ public class Cluster
29
29
{
30
30
internal ClusterInfo clusterInfo ;
31
31
private TokenManager token ;
@@ -34,11 +34,12 @@ public class Cluster : RunnableBase
34
34
private SocketIOClient . SocketIO socket ;
35
35
public bool IsEnabled { get ; set ; }
36
36
public bool WantEnable { get ; set ; }
37
- private Task ? _keepAlive ;
38
37
internal IStorage storage ;
39
38
protected AccessCounter counter ;
40
39
public CancellationTokenSource cancellationSrc = new CancellationTokenSource ( ) ;
41
40
internal ClusterRequiredData requiredData ;
41
+ internal List < ApiFileInfo > files ;
42
+
42
43
//List<Task> tasks = new List<Task>();
43
44
44
45
/// <summary>
@@ -58,7 +59,7 @@ public Cluster(ClusterRequiredData requiredData) : base()
58
59
client . DefaultRequestHeaders . Authorization = new ( "Bearer" , requiredData . Token ? . Token . token ) ;
59
60
60
61
this . storage = new FileStorage ( ClusterRequiredData . Config . clusterFileDirectory ) ;
61
-
62
+ this . files = new List < ApiFileInfo > ( ) ;
62
63
this . counter = new ( ) ;
63
64
InitializeSocket ( ) ;
64
65
@@ -95,7 +96,7 @@ protected void InitializeSocket()
95
96
/// </summary>
96
97
/// <param name="args"></param>
97
98
/// <returns></returns>
98
- protected override int Run ( string [ ] args )
99
+ public int Start ( )
99
100
{
100
101
requiredData . PluginManager . TriggerEvent ( this , ProgramEventType . ClusterStarted ) ;
101
102
// 工作进程启动
@@ -144,9 +145,18 @@ protected async Task<int> AsyncRun()
144
145
145
146
Logger . Instance . LogSystem ( $ "工作进程 { guid } 在 <{ ClusterRequiredData . Config . HOST } :{ ClusterRequiredData . Config . PORT } > 提供服务") ;
146
147
147
- _keepAlive = Task . Run ( _KeepAlive , cancellationSrc . Token ) ;
148
+ Tasks . CheckFile = Task . Run ( ( ) =>
149
+ {
150
+ while ( true )
151
+ {
152
+ CheckFiles ( ) . Wait ( ) ;
153
+ Thread . Sleep ( 10 * 60 * 1000 ) ;
154
+ }
155
+ } ) ;
156
+
157
+ Tasks . KeepAlive = Task . Run ( _KeepAlive , cancellationSrc . Token ) ;
148
158
149
- _keepAlive . Wait ( ) ;
159
+ Tasks . KeepAlive . Wait ( ) ;
150
160
151
161
return returns ;
152
162
}
@@ -412,32 +422,16 @@ protected async Task CheckFiles()
412
422
{
413
423
return ;
414
424
}
415
- var resp = await this . client . GetAsync ( "openbmclapi/files" ) ;
416
425
Logger . Instance . LogDebug ( $ "文件检查策略:{ ClusterRequiredData . Config . startupCheckMode } ") ;
417
- byte [ ] bytes = await resp . Content . ReadAsByteArrayAsync ( ) ;
418
-
419
- UnpackBytes ( ref bytes ) ;
420
-
421
- AvroParser avro = new AvroParser ( bytes ) ;
422
- List < ApiFileInfo > files ;
423
-
424
- files = avro . Parse ( ) ;
426
+ var updatedFiles = await GetFileList ( this . files ) ;
427
+ if ( updatedFiles != null && updatedFiles . Count != 0 )
428
+ {
429
+ this . files = updatedFiles ;
430
+ }
425
431
426
432
object countLock = new ( ) ;
427
433
int count = 0 ;
428
434
429
- CancellationTokenSource t = new ( ) ;
430
-
431
- _ = Task . Run ( ( ) =>
432
- {
433
- while ( true )
434
- {
435
- GC . Collect ( ) ;
436
- Thread . Sleep ( 1000 ) ;
437
- t . Token . ThrowIfCancellationRequested ( ) ;
438
- }
439
- } , t . Token ) ;
440
-
441
435
Parallel . ForEach ( files , file =>
442
436
//foreach (var file in files)
443
437
{
@@ -449,11 +443,57 @@ protected async Task CheckFiles()
449
443
Logger . Instance . LogInfoNoNewLine ( $ "\r { count } /{ files . Count } ") ;
450
444
} ) ;
451
445
452
- t . Cancel ( ) ;
453
-
454
446
files = null ! ;
455
447
countLock = null ! ;
456
- bytes = null ! ;
448
+ }
449
+
450
+ /// <summary>
451
+ /// 获取完整的文件列表
452
+ /// </summary>
453
+ /// <returns></returns>
454
+ public async Task < List < ApiFileInfo > > GetFileList ( )
455
+ {
456
+ var resp = await this . client . GetAsync ( "openbmclapi/files" ) ;
457
+ Logger . Instance . LogDebug ( $ "检查文件结果:{ resp } ") ;
458
+
459
+ List < ApiFileInfo > files ;
460
+
461
+ byte [ ] bytes = await resp . Content . ReadAsByteArrayAsync ( ) ;
462
+ UnpackBytes ( ref bytes ) ;
463
+
464
+ AvroParser avro = new AvroParser ( bytes ) ;
465
+
466
+ files = avro . Parse ( ) ;
467
+ return files ;
468
+ }
469
+
470
+ public async Task < List < ApiFileInfo > > GetFileList ( List < ApiFileInfo > ? files )
471
+ {
472
+ if ( files == null ) return await GetFileList ( ) ;
473
+
474
+ List < ApiFileInfo > updatedFiles ;
475
+
476
+ if ( ( files == null ) || ( files . Count == 0 ) )
477
+ {
478
+ return await GetFileList ( ) ;
479
+ }
480
+ long lastModified = files . Select ( f => f . mtime ) . Max ( ) ;
481
+
482
+ var resp = await this . client . GetAsync ( $ "openbmclapi/files?lastModified={ lastModified } ") ;
483
+ Logger . Instance . LogDebug ( $ "检查文件结果:{ resp } ") ;
484
+ if ( resp . StatusCode == HttpStatusCode . NotModified )
485
+ {
486
+ updatedFiles = new List < ApiFileInfo > ( ) ;
487
+ return updatedFiles ;
488
+ }
489
+
490
+ byte [ ] bytes = await resp . Content . ReadAsByteArrayAsync ( ) ;
491
+ UnpackBytes ( ref bytes ) ;
492
+
493
+ AvroParser avro = new AvroParser ( bytes ) ;
494
+
495
+ updatedFiles = avro . Parse ( ) ;
496
+ return updatedFiles ;
457
497
}
458
498
459
499
void CheckSingleFile ( ApiFileInfo file )
0 commit comments