-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
403 additions
and
67 deletions.
There are no files selected for viewing
30 changes: 28 additions & 2 deletions
30
ResourceMonitor/.idea/.idea.ResourceMonitor/.idea/contentModel.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
ResourceMonitor/ResourceMonitor.Test/ResourceMonitor.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
13 changes: 13 additions & 0 deletions
13
ResourceMonitor/ResourceMonitor.Test/RulesContainerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace ResourceMonitor.Test | ||
{ | ||
[TestClass] | ||
public class RulesContainerTests | ||
{ | ||
[TestMethod] | ||
public void TestMethod1() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
ResourceMonitor/ResourceMonitor/Services/Declaration/IRulesContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Immutable; | ||
using ResourceMonitor.Models.DandanplayApi; | ||
|
||
namespace ResourceMonitor.Services.Declaration | ||
{ | ||
public interface IRulesContainer | ||
{ | ||
bool IsUpdating { get; set; } | ||
ImmutableList<AutoDownloadRule> LocalRules { get; } | ||
void SyncWithServerRules(AutoDownloadRuleListResponse serverRules); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...urceMonitor/ResourceMonitor/Services/Implementation/CheckNewResourcesBackgroundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using ResourceMonitor.Models.DandanplayApi; | ||
using ResourceMonitor.Services.Declaration; | ||
|
||
namespace ResourceMonitor.Services.Implementation | ||
{ | ||
/// <summary> | ||
/// 检查新的资源 | ||
/// </summary> | ||
public class CheckNewResourcesBackgroundService : BackgroundService | ||
{ | ||
private readonly IRulesContainer _rulesContainer; | ||
private readonly IConfiguration _configuration; | ||
private readonly ILogger<CheckNewResourcesBackgroundService> _logger; | ||
|
||
public CheckNewResourcesBackgroundService(IRulesContainer rulesContainer, | ||
IConfiguration configuration, | ||
ILogger<CheckNewResourcesBackgroundService> logger) | ||
{ | ||
_rulesContainer = rulesContainer; | ||
_configuration = configuration; | ||
_logger = logger; | ||
} | ||
|
||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
_logger.LogInformation($"{nameof(CheckNewResourcesBackgroundService)} 开始在后台运行"); | ||
|
||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
_logger.LogInformation("开始解析所有规则"); | ||
|
||
//等待规则同步完毕 | ||
while (_rulesContainer.IsUpdating) | ||
{ | ||
_logger.LogInformation("自动下载规则正在同步中,等待同步完毕..."); | ||
await Task.Delay(TimeSpan.FromSeconds(15), stoppingToken); | ||
} | ||
|
||
//按顺序解析每个规则 | ||
var ruleList = _rulesContainer.LocalRules; | ||
foreach (var rule in ruleList) | ||
{ | ||
await DownloadRule(rule); | ||
} | ||
|
||
//默认每 15 分钟检查一次 | ||
_logger.LogInformation("全部规则解析完毕,等待 15 分钟后再次执行"); | ||
await Task.Delay(TimeSpan.FromMinutes(15), stoppingToken); | ||
} | ||
} | ||
|
||
private async Task DownloadRule(AutoDownloadRule rule) | ||
{ | ||
_logger.LogInformation($"正在解析规则 {rule}"); | ||
//获得需要下载的新资源 | ||
|
||
//对比当前已有的下载任务,只下载新的资源 | ||
|
||
//解析磁力链至种子文件 | ||
|
||
//连接下载器,添加远程任务 | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
ResourceMonitor/ResourceMonitor/Services/Implementation/RulesContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections.Concurrent; | ||
using System.Collections.Immutable; | ||
using Microsoft.Extensions.Logging; | ||
using ResourceMonitor.Models.DandanplayApi; | ||
using ResourceMonitor.Services.Declaration; | ||
|
||
namespace ResourceMonitor.Services.Implementation | ||
{ | ||
public class RulesContainer : IRulesContainer | ||
{ | ||
private readonly ILogger<RulesContainer> _logger; | ||
private readonly ConcurrentDictionary<string, AutoDownloadRule> _localRules = new ConcurrentDictionary<string, AutoDownloadRule>(); | ||
private bool _isUpdating = true; | ||
|
||
public RulesContainer(ILogger<RulesContainer> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public bool IsUpdating | ||
{ | ||
get => _isUpdating; | ||
set | ||
{ | ||
_logger.LogDebug($"RulesContainer.IsUpdating 设置为 {value}"); | ||
_isUpdating = value; | ||
} | ||
} | ||
|
||
public ImmutableList<AutoDownloadRule> LocalRules => _localRules.Values.ToImmutableList(); | ||
|
||
public void SyncWithServerRules(AutoDownloadRuleListResponse serverRules) | ||
{ | ||
_logger.LogInformation("开始同步服务器规则"); | ||
//移除服务器端已经删除的规则 | ||
foreach (var removedRuleId in serverRules.removedRuleIds ?? new string[0]) | ||
{ | ||
_localRules.TryRemove(removedRuleId, out var removedRule); | ||
_logger.LogInformation($"删除规则 {removedRule}"); | ||
} | ||
|
||
//添加或更新规则 | ||
foreach (var serverRule in serverRules.rules) | ||
{ | ||
//新增规则 | ||
if (!_localRules.ContainsKey(serverRule.id)) | ||
{ | ||
_localRules.TryAdd(serverRule.id, serverRule); | ||
_logger.LogInformation($"新增规则 {serverRule}"); | ||
} | ||
else //更新规则 | ||
{ | ||
var localRule = _localRules[serverRule.id]; | ||
if (serverRule.version > localRule.version) | ||
{ | ||
_localRules[serverRule.id] = serverRule; | ||
_logger.LogInformation($"更新规则-原规则 {localRule}"); | ||
_logger.LogInformation($"更新规则-替换为 {serverRule}"); | ||
} | ||
} | ||
} | ||
|
||
_logger.LogInformation($"规则同步完毕,当前本地规则共有 {_localRules.Count} 个"); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.