Skip to content

Commit

Permalink
feat(plugins/memosplus): 支持设置备份时段: Backup.AllowedExecute: TimeHourFro…
Browse files Browse the repository at this point in the history
…m->TimeHourTo

凌晨1点到凌晨5点执行备份
  • Loading branch information
yiyungent committed Apr 8, 2024
1 parent 1e0d9a7 commit 6e6941f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
43 changes: 39 additions & 4 deletions plugins/MemosPlus/MemosPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemosPlus> _logger;
#endregion

#region Props
public long SecondsPeriod
Expand All @@ -27,8 +31,36 @@ public long SecondsPeriod
return settings.SecondsPeriod;
}
}

public static bool AllowedExecute
{
get
{
bool isAllowed = false;
var settings = PluginSettingsModelFactory.Create<SettingsModel>(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<MemosPlus> logger)
{
this._logger = logger;
}

public override (bool IsSuccess, string Message) AfterEnable()
{
Console.WriteLine($"{nameof(MemosPlus)}: {nameof(AfterEnable)}");
Expand All @@ -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;
Expand All @@ -76,7 +110,7 @@ public async Task ExecuteAsync()
// https://github.com/scriban/scriban
var githubTemplate = Template.Parse(githubTemplateContent);
List<string> memoFilePaths = new List<string>();
while (list != null && list.Count >= 1)
while (list != null && list.Count >= 1 && AllowedExecute)
{
foreach (Utils.MemoItemModel item in list)
{
Expand Down Expand Up @@ -172,7 +206,7 @@ public async Task ExecuteAsync()
}

#region 清理不存在的文件
if (!isErrorMemosApi && !isErrorMemosApiResource)
if (!isErrorMemosApi && !isErrorMemosApiResource && AllowedExecute)
{
// 清理不存在的文件: 对于存放 memos 的文件夹, 清理 memos 中已删除的对应文件
try
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions plugins/MemosPlus/MemosPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>PluginCore.MemosPlus</PackageId>
<Version>0.1.15</Version>
<FileVersion>0.1.15.0</FileVersion>
<Version>0.1.16</Version>
<FileVersion>0.1.16.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Company>yiyun</Company>
<Authors>yiyun</Authors>
Expand Down
2 changes: 1 addition & 1 deletion plugins/MemosPlus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@


<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.MemosPlus-v0.1.15.README" style="border:0" alt="" />
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.MemosPlus-v0.1.16.README" style="border:0" alt="" />
<!-- End Matomo -->


Expand Down
11 changes: 10 additions & 1 deletion plugins/MemosPlus/SettingsModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
Expand Down Expand Up @@ -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; }
}
}
}
}
2 changes: 1 addition & 1 deletion plugins/MemosPlus/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"DisplayName": "memos+",
"Description": "提升 memos 体验",
"Author": "yiyun",
"Version": "0.1.15",
"Version": "0.1.16",
"SupportedVersions": [ "0.0.1" ]
}
6 changes: 5 additions & 1 deletion plugins/MemosPlus/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"MemoFileName": "memo-{{date}}.md"
},
"Backup": {
"EnableBackupToGitHub": false
"EnableBackupToGitHub": false,
"AllowedExecute": {
"TimeHourFrom": 1,
"TimeHourTo": 5
}
}
}

0 comments on commit 6e6941f

Please sign in to comment.