69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
using System.Diagnostics;
|
|
using System.Net.Http.Json;
|
|
using Core;
|
|
using FluentScheduler;
|
|
using Interface.Jobs;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Service.Jobs;
|
|
|
|
public class RSyncTaskRegistry : Registry, IRSyncTaskRegistry
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public RSyncTaskRegistry(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
Schedule(Job).ToRunEvery(1).Days().At(10, 0);
|
|
}
|
|
|
|
public async void Job()
|
|
{
|
|
try
|
|
{
|
|
// await JobExecute();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
await WxNotify.SendCommonAsync($"RSyncTaskRegistry.Job() error: {e}");
|
|
}
|
|
}
|
|
|
|
private async Task JobExecute()
|
|
{
|
|
var config = _configuration.GetSection("SyncTask");
|
|
|
|
foreach (var item in config.GetSection("SyncPaths").GetChildren())
|
|
{
|
|
var source = Path.Combine(config["SourceRoot"]!, item["Source"]!);
|
|
var isDeleteAfter = item["DeleteAfter"] == "true";
|
|
await ExecuteItem(source, config["TargetRoot"]!, item["Target"]!, isDeleteAfter);
|
|
}
|
|
}
|
|
|
|
private async Task ExecuteItem(string source, string remote, string destination, bool isDeleteAfter)
|
|
{
|
|
var logName = $"rclone_output_{destination.Replace("/", "_")}{DateTime.Now:yyMMddHHmm}.log";
|
|
|
|
var commands = new[]
|
|
{
|
|
$"rclone sync " +
|
|
$"{source} " +
|
|
$"{remote}:{destination} " +
|
|
$"--fast-list " +
|
|
$"--size-only " +
|
|
$"{(isDeleteAfter ? "--delete-after" : "--delete-excluded")} " +
|
|
$"> /wd/logs/{logName} 2>&1"
|
|
};
|
|
|
|
await WxNotify.SendCommonAsync($@"RSyncTaskRegistry.ExecuteItem()
|
|
|
|
`{commands[0]}`
|
|
|
|
> {DateTime.Now:yyyy-MM-dd HH:mm:ss}
|
|
");
|
|
|
|
_ = Command.ExecAsync(commands);
|
|
}
|
|
} |