diff --git a/v2rayN/ServiceLib/Handler/AppHandler.cs b/v2rayN/ServiceLib/Handler/AppHandler.cs index cf0f73af..8a17932f 100644 --- a/v2rayN/ServiceLib/Handler/AppHandler.cs +++ b/v2rayN/ServiceLib/Handler/AppHandler.cs @@ -80,8 +80,6 @@ namespace ServiceLib.Handler Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}"); Logging.LoggingEnabled(_config.GuiItem.EnableLog); - ClearExpiredFiles(); - return true; } @@ -92,16 +90,6 @@ namespace ServiceLib.Handler 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 #region Config diff --git a/v2rayN/ServiceLib/Handler/ProfileExHandler.cs b/v2rayN/ServiceLib/Handler/ProfileExHandler.cs index 4743696d..31a6b56c 100644 --- a/v2rayN/ServiceLib/Handler/ProfileExHandler.cs +++ b/v2rayN/ServiceLib/Handler/ProfileExHandler.cs @@ -20,14 +20,6 @@ namespace ServiceLib.Handler public async Task Init() { await InitData(); - _ = Task.Run(async () => - { - while (true) - { - await Task.Delay(1000 * 600); - await SaveQueueIndexIds(); - } - }); } public async Task> GetProfileExs() diff --git a/v2rayN/ServiceLib/Handler/TaskHandler.cs b/v2rayN/ServiceLib/Handler/TaskHandler.cs index 3d04a33d..88657f0c 100644 --- a/v2rayN/ServiceLib/Handler/TaskHandler.cs +++ b/v2rayN/ServiceLib/Handler/TaskHandler.cs @@ -1,4 +1,4 @@ -namespace ServiceLib.Handler +namespace ServiceLib.Handler { public class TaskHandler { @@ -7,66 +7,92 @@ public void RegUpdateTask(Config config, Action updateFunc) { - Task.Run(() => UpdateTaskRunSubscription(config, updateFunc)); - Task.Run(() => UpdateTaskRunGeo(config, updateFunc)); + Task.Run(() => ScheduledTasks(config, updateFunc)); + } + + private async Task ScheduledTasks(Config config, Action 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 updateFunc) { - await Task.Delay(60000); - Logging.SaveLog("UpdateTaskRunSubscription"); + var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(); + var lstSubs = (await AppHandler.Instance.SubItems())? + .Where(t => t.AutoUpdateInterval > 0) + .Where(t => updateTime - t.UpdateTime >= t.AutoUpdateInterval * 60) + .ToList(); - var updateHandle = new UpdateService(); - while (true) + if (lstSubs is not { Count: > 0 }) { - var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(); - var lstSubs = (await AppHandler.Instance.SubItems()) - .Where(t => t.AutoUpdateInterval > 0) - .Where(t => updateTime - t.UpdateTime >= t.AutoUpdateInterval * 60) - .ToList(); + return; + } - foreach (var item in lstSubs) + Logging.SaveLog("Execute update subscription"); + var updateHandle = new UpdateService(); + + 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); - if (success) - Logging.SaveLog("subscription" + msg); - }); - item.UpdateTime = updateTime; - await ConfigHandler.AddSubItem(config, item); - - await Task.Delay(5000); - } - await Task.Delay(60000); + updateFunc?.Invoke(success, msg); + if (success) + { + Logging.SaveLog("Update subscription end" + msg); + } + }); + item.UpdateTime = updateTime; + await ConfigHandler.AddSubItem(config, item); + await Task.Delay(1000); } } - private async Task UpdateTaskRunGeo(Config config, Action updateFunc) + private async Task UpdateTaskRunGeo(Config config, int hours, Action updateFunc) { - var autoUpdateGeoTime = DateTime.Now; - - //await Task.Delay(1000 * 120); - Logging.SaveLog("UpdateTaskRunGeo"); - - var updateHandle = new UpdateService(); - while (true) + if (config.GuiItem.AutoUpdateInterval > 0 && hours > 0 && hours % config.GuiItem.AutoUpdateInterval == 0) { - await Task.Delay(1000 * 3600); + Logging.SaveLog("Execute update geo files"); - var dtNow = DateTime.Now; - if (config.GuiItem.AutoUpdateInterval > 0) + var updateHandle = new UpdateService(); + await updateHandle.UpdateGeoFileAll(config, (bool success, string msg) => { - if ((dtNow - autoUpdateGeoTime).Hours % config.GuiItem.AutoUpdateInterval == 0) - { - await updateHandle.UpdateGeoFileAll(config, (bool success, string msg) => - { - updateFunc?.Invoke(false, msg); - }); - autoUpdateGeoTime = dtNow; - } - } + updateFunc?.Invoke(false, msg); + }); } } } -} \ No newline at end of file +} diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 37cb83b3..05de5b02 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -25,8 +25,6 @@ namespace ServiceLib.Services await RunAsync(actionType, selecteds); await ProfileExHandler.Instance.SaveTo(); UpdateFunc("", ResUI.SpeedtestingCompleted); - - FileManager.DeleteExpiredFiles(Utils.GetBinConfigPath(), DateTime.Now.AddHours(-1)); }); }