Skip to content

Commit 7b62a4c

Browse files
committed
feat: 增量更新
1 parent fe6b212 commit 7b62a4c

File tree

5 files changed

+85
-33
lines changed

5 files changed

+85
-33
lines changed

CSharp-OpenBMCLAPI/Modules/Cluster.cs

+70-30
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace CSharpOpenBMCLAPI.Modules
2525
/// <summary>
2626
/// Cluster 实例,进行最基本的节点服务
2727
/// </summary>
28-
public class Cluster : RunnableBase
28+
public class Cluster
2929
{
3030
internal ClusterInfo clusterInfo;
3131
private TokenManager token;
@@ -34,11 +34,12 @@ public class Cluster : RunnableBase
3434
private SocketIOClient.SocketIO socket;
3535
public bool IsEnabled { get; set; }
3636
public bool WantEnable { get; set; }
37-
private Task? _keepAlive;
3837
internal IStorage storage;
3938
protected AccessCounter counter;
4039
public CancellationTokenSource cancellationSrc = new CancellationTokenSource();
4140
internal ClusterRequiredData requiredData;
41+
internal List<ApiFileInfo> files;
42+
4243
//List<Task> tasks = new List<Task>();
4344

4445
/// <summary>
@@ -58,7 +59,7 @@ public Cluster(ClusterRequiredData requiredData) : base()
5859
client.DefaultRequestHeaders.Authorization = new("Bearer", requiredData.Token?.Token.token);
5960

6061
this.storage = new FileStorage(ClusterRequiredData.Config.clusterFileDirectory);
61-
62+
this.files = new List<ApiFileInfo>();
6263
this.counter = new();
6364
InitializeSocket();
6465

@@ -95,7 +96,7 @@ protected void InitializeSocket()
9596
/// </summary>
9697
/// <param name="args"></param>
9798
/// <returns></returns>
98-
protected override int Run(string[] args)
99+
public int Start()
99100
{
100101
requiredData.PluginManager.TriggerEvent(this, ProgramEventType.ClusterStarted);
101102
// 工作进程启动
@@ -144,9 +145,18 @@ protected async Task<int> AsyncRun()
144145

145146
Logger.Instance.LogSystem($"工作进程 {guid} 在 <{ClusterRequiredData.Config.HOST}:{ClusterRequiredData.Config.PORT}> 提供服务");
146147

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);
148158

149-
_keepAlive.Wait();
159+
Tasks.KeepAlive.Wait();
150160

151161
return returns;
152162
}
@@ -412,32 +422,16 @@ protected async Task CheckFiles()
412422
{
413423
return;
414424
}
415-
var resp = await this.client.GetAsync("openbmclapi/files");
416425
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+
}
425431

426432
object countLock = new();
427433
int count = 0;
428434

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-
441435
Parallel.ForEach(files, file =>
442436
//foreach (var file in files)
443437
{
@@ -449,11 +443,57 @@ protected async Task CheckFiles()
449443
Logger.Instance.LogInfoNoNewLine($"\r{count}/{files.Count}");
450444
});
451445

452-
t.Cancel();
453-
454446
files = null!;
455447
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;
457497
}
458498

459499
void CheckSingleFile(ApiFileInfo file)

CSharp-OpenBMCLAPI/Modules/Tasks.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CSharpOpenBMCLAPI.Modules
8+
{
9+
internal class Tasks
10+
{
11+
public static Task? KeepAlive { get; set; }
12+
public static Task? CheckFile { get; set; }
13+
}
14+
}

CSharp-OpenBMCLAPI/Modules/Utils.cs

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public static async Task ExitCluster(Cluster cluster)
7676
await cluster.KeepAlive();
7777
Thread.Sleep(1000);
7878
await cluster.Disable();
79-
cluster.Stop();
8079
cluster.IsEnabled = false;
8180
}
8281

CSharp-OpenBMCLAPI/Modules/WebServer/SimpleWebServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected void HandleRequest(TcpClient tcpClient)
7777
tcpClient.Close();
7878
}
7979
}
80-
catch (Exception ex)
80+
catch
8181
{
8282
//Logger.Instance.LogError(ex.ExceptionToDetail());
8383
if (tcpClient.Connected) tcpClient.Close();

CSharp-OpenBMCLAPI/Program.cs

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ protected override int Run(string[] args)
137137
Console.CancelKeyPress += (sender, e) => Utils.ExitCluster(cluster).Wait();
138138

139139
cluster.Start();
140-
cluster.WaitForStop();
141140

142141
requiredData.PluginManager.TriggerEvent(this, ProgramEventType.ProgramStopped);
143142
return returns;

0 commit comments

Comments
 (0)