diff --git a/plugins/MemosPlus/MemosPlus.cs b/plugins/MemosPlus/MemosPlus.cs index 50cec63e2..b2bf7379e 100644 --- a/plugins/MemosPlus/MemosPlus.cs +++ b/plugins/MemosPlus/MemosPlus.cs @@ -11,11 +11,15 @@ using System.IO; using PluginCore; using Scriban; +using Microsoft.Extensions.Logging; namespace MemosPlus { public class MemosPlus : BasePlugin, IWidgetPlugin, IStartupXPlugin, ITimeJobPlugin { + #region Fields + private readonly ILogger _logger; + #endregion #region Props public long SecondsPeriod @@ -27,8 +31,36 @@ public long SecondsPeriod return settings.SecondsPeriod; } } + + public static bool AllowedExecute + { + get + { + bool isAllowed = false; + var settings = PluginSettingsModelFactory.Create(nameof(MemosPlus)); + DateTime currentTime = DateTime.Now; + if (settings?.Backup?.AllowedExecute != null) + { + // 凌晨1点到5点的操作 + isAllowed = (currentTime.Hour >= (settings?.Backup?.AllowedExecute?.TimeHourFrom ?? 1)) + && + (currentTime.Hour < (settings?.Backup?.AllowedExecute?.TimeHourTo ?? 5)); + } + else + { + isAllowed = true; + } + + return isAllowed; + } + } #endregion + public MemosPlus(ILogger logger) + { + this._logger = logger; + } + public override (bool IsSuccess, string Message) AfterEnable() { Console.WriteLine($"{nameof(MemosPlus)}: {nameof(AfterEnable)}"); @@ -50,8 +82,10 @@ public async Task ExecuteAsync() #region 备份到 GitHub // 备份到 GitHub - if (settings.Backup.EnableBackupToGitHub) + if (settings.Backup.EnableBackupToGitHub && AllowedExecute) { + this._logger.LogWarning($"开始执行定时任务: 备份到 GitHub"); + MemosUtil memosUtil = new MemosUtil(settings.Memos.BaseUrl); int offset = 0; bool isErrorMemosApi = false; @@ -76,7 +110,7 @@ public async Task ExecuteAsync() // https://github.com/scriban/scriban var githubTemplate = Template.Parse(githubTemplateContent); List memoFilePaths = new List(); - while (list != null && list.Count >= 1) + while (list != null && list.Count >= 1 && AllowedExecute) { foreach (Utils.MemoItemModel item in list) { @@ -172,7 +206,7 @@ public async Task ExecuteAsync() } #region 清理不存在的文件 - if (!isErrorMemosApi && !isErrorMemosApiResource) + if (!isErrorMemosApi && !isErrorMemosApiResource && AllowedExecute) { // 清理不存在的文件: 对于存放 memos 的文件夹, 清理 memos 中已删除的对应文件 try @@ -211,13 +245,14 @@ public async Task ExecuteAsync() } #endregion + this._logger.LogWarning($"完成执行定时任务: 备份到 GitHub"); } #endregion } catch (Exception ex) { - Console.WriteLine($"执行定时任务失败: {ex.ToString()}"); + this._logger.LogError(ex, $"执行定时任务失败: {ex.ToString()}"); } await Task.CompletedTask; diff --git a/plugins/MemosPlus/MemosPlus.csproj b/plugins/MemosPlus/MemosPlus.csproj index c30bafd80..d3fa7ee88 100644 --- a/plugins/MemosPlus/MemosPlus.csproj +++ b/plugins/MemosPlus/MemosPlus.csproj @@ -9,8 +9,8 @@ enable enable PluginCore.MemosPlus - 0.1.15 - 0.1.15.0 + 0.1.16 + 0.1.16.0 1.0.0.0 yiyun yiyun diff --git a/plugins/MemosPlus/README.md b/plugins/MemosPlus/README.md index d279234f3..d68d644e4 100644 --- a/plugins/MemosPlus/README.md +++ b/plugins/MemosPlus/README.md @@ -61,7 +61,7 @@ - + diff --git a/plugins/MemosPlus/SettingsModel.cs b/plugins/MemosPlus/SettingsModel.cs index 5ca710834..c572db5b0 100644 --- a/plugins/MemosPlus/SettingsModel.cs +++ b/plugins/MemosPlus/SettingsModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; @@ -48,6 +48,15 @@ public class GitHubModel public class BackupModel { public bool EnableBackupToGitHub { get;set; } + + public AllowedExecuteModel AllowedExecute { get; set; } + + public class AllowedExecuteModel + { + public int TimeHourFrom { get; set; } + + public int TimeHourTo { get; set; } + } } } } diff --git a/plugins/MemosPlus/info.json b/plugins/MemosPlus/info.json index 23245f104..b31336275 100644 --- a/plugins/MemosPlus/info.json +++ b/plugins/MemosPlus/info.json @@ -3,6 +3,6 @@ "DisplayName": "memos+", "Description": "提升 memos 体验", "Author": "yiyun", - "Version": "0.1.15", + "Version": "0.1.16", "SupportedVersions": [ "0.0.1" ] } \ No newline at end of file diff --git a/plugins/MemosPlus/settings.json b/plugins/MemosPlus/settings.json index 439274ac1..f17c48772 100644 --- a/plugins/MemosPlus/settings.json +++ b/plugins/MemosPlus/settings.json @@ -14,6 +14,10 @@ "MemoFileName": "memo-{{date}}.md" }, "Backup": { - "EnableBackupToGitHub": false + "EnableBackupToGitHub": false, + "AllowedExecute": { + "TimeHourFrom": 1, + "TimeHourTo": 5 + } } } \ No newline at end of file