Optimizing Task Code, add unified processing of scheduled tasks

This commit is contained in:
2dust 2025-03-03 12:20:58 +08:00
parent 438eaba4d5
commit 67fe6ac3d8
4 changed files with 72 additions and 68 deletions

View file

@ -80,8 +80,6 @@ namespace ServiceLib.Handler
Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}"); Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}");
Logging.LoggingEnabled(_config.GuiItem.EnableLog); Logging.LoggingEnabled(_config.GuiItem.EnableLog);
ClearExpiredFiles();
return true; return true;
} }
@ -92,16 +90,6 @@ namespace ServiceLib.Handler
return true; return true;
} }
private void ClearExpiredFiles()
{
Task.Run(() =>
{
FileManager.DeleteExpiredFiles(Utils.GetLogPath(), DateTime.Now.AddMonths(-1));
FileManager.DeleteExpiredFiles(Utils.GetTempPath(), DateTime.Now.AddMonths(-1));
FileManager.DeleteExpiredFiles(Utils.GetBinConfigPath(), DateTime.Now.AddHours(-1));
});
}
#endregion Init #endregion Init
#region Config #region Config

View file

@ -20,14 +20,6 @@ namespace ServiceLib.Handler
public async Task Init() public async Task Init()
{ {
await InitData(); await InitData();
_ = Task.Run(async () =>
{
while (true)
{
await Task.Delay(1000 * 600);
await SaveQueueIndexIds();
}
});
} }
public async Task<ConcurrentBag<ProfileExItem>> GetProfileExs() public async Task<ConcurrentBag<ProfileExItem>> GetProfileExs()

View file

@ -1,4 +1,4 @@
namespace ServiceLib.Handler namespace ServiceLib.Handler
{ {
public class TaskHandler public class TaskHandler
{ {
@ -7,65 +7,91 @@
public void RegUpdateTask(Config config, Action<bool, string> updateFunc) public void RegUpdateTask(Config config, Action<bool, string> updateFunc)
{ {
Task.Run(() => UpdateTaskRunSubscription(config, updateFunc)); Task.Run(() => ScheduledTasks(config, updateFunc));
Task.Run(() => UpdateTaskRunGeo(config, updateFunc)); }
private async Task ScheduledTasks(Config config, Action<bool, string> updateFunc)
{
Logging.SaveLog("Setup Scheduled Tasks");
var numOfExecuted = 1;
while (true)
{
//1 minute
await Task.Delay(1000 * 60);
//Execute once 1 minute
await UpdateTaskRunSubscription(config, updateFunc);
//Execute once 20 minute
if (numOfExecuted % 20 == 0)
{
Logging.SaveLog("Execute save config");
await ConfigHandler.SaveConfig(config);
await ProfileExHandler.Instance.SaveTo();
}
//Execute once 1 hour
if (numOfExecuted % 60 == 0)
{
Logging.SaveLog("Execute delete expired files");
FileManager.DeleteExpiredFiles(Utils.GetBinConfigPath(), DateTime.Now.AddHours(-1));
FileManager.DeleteExpiredFiles(Utils.GetLogPath(), DateTime.Now.AddMonths(-1));
FileManager.DeleteExpiredFiles(Utils.GetTempPath(), DateTime.Now.AddMonths(-1));
//Check once 1 hour
await UpdateTaskRunGeo(config, numOfExecuted / 60, updateFunc);
}
numOfExecuted++;
}
} }
private async Task UpdateTaskRunSubscription(Config config, Action<bool, string> updateFunc) private async Task UpdateTaskRunSubscription(Config config, Action<bool, string> updateFunc)
{
await Task.Delay(60000);
Logging.SaveLog("UpdateTaskRunSubscription");
var updateHandle = new UpdateService();
while (true)
{ {
var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(); var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds();
var lstSubs = (await AppHandler.Instance.SubItems()) var lstSubs = (await AppHandler.Instance.SubItems())?
.Where(t => t.AutoUpdateInterval > 0) .Where(t => t.AutoUpdateInterval > 0)
.Where(t => updateTime - t.UpdateTime >= t.AutoUpdateInterval * 60) .Where(t => updateTime - t.UpdateTime >= t.AutoUpdateInterval * 60)
.ToList(); .ToList();
if (lstSubs is not { Count: > 0 })
{
return;
}
Logging.SaveLog("Execute update subscription");
var updateHandle = new UpdateService();
foreach (var item in lstSubs) foreach (var item in lstSubs)
{ {
await updateHandle.UpdateSubscriptionProcess(config, item.Id, true, (bool success, string msg) => await updateHandle.UpdateSubscriptionProcess(config, item.Id, true, (bool success, string msg) =>
{ {
updateFunc?.Invoke(success, msg); updateFunc?.Invoke(success, msg);
if (success) if (success)
Logging.SaveLog("subscription" + msg); {
Logging.SaveLog("Update subscription end" + msg);
}
}); });
item.UpdateTime = updateTime; item.UpdateTime = updateTime;
await ConfigHandler.AddSubItem(config, item); await ConfigHandler.AddSubItem(config, item);
await Task.Delay(1000);
await Task.Delay(5000);
}
await Task.Delay(60000);
} }
} }
private async Task UpdateTaskRunGeo(Config config, Action<bool, string> updateFunc) private async Task UpdateTaskRunGeo(Config config, int hours, Action<bool, string> updateFunc)
{ {
var autoUpdateGeoTime = DateTime.Now; if (config.GuiItem.AutoUpdateInterval > 0 && hours > 0 && hours % config.GuiItem.AutoUpdateInterval == 0)
{
//await Task.Delay(1000 * 120); Logging.SaveLog("Execute update geo files");
Logging.SaveLog("UpdateTaskRunGeo");
var updateHandle = new UpdateService(); var updateHandle = new UpdateService();
while (true)
{
await Task.Delay(1000 * 3600);
var dtNow = DateTime.Now;
if (config.GuiItem.AutoUpdateInterval > 0)
{
if ((dtNow - autoUpdateGeoTime).Hours % config.GuiItem.AutoUpdateInterval == 0)
{
await updateHandle.UpdateGeoFileAll(config, (bool success, string msg) => await updateHandle.UpdateGeoFileAll(config, (bool success, string msg) =>
{ {
updateFunc?.Invoke(false, msg); updateFunc?.Invoke(false, msg);
}); });
autoUpdateGeoTime = dtNow;
}
}
} }
} }
} }

View file

@ -25,8 +25,6 @@ namespace ServiceLib.Services
await RunAsync(actionType, selecteds); await RunAsync(actionType, selecteds);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
UpdateFunc("", ResUI.SpeedtestingCompleted); UpdateFunc("", ResUI.SpeedtestingCompleted);
FileManager.DeleteExpiredFiles(Utils.GetBinConfigPath(), DateTime.Now.AddHours(-1));
}); });
} }