-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBackupService.cs
More file actions
67 lines (55 loc) · 1.77 KB
/
AutoBackupService.cs
File metadata and controls
67 lines (55 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using ClassIsland.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SuperAutoBackup.ConfigHandlers;
using SuperAutoBackup.Shared;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SuperAutoBackup;
public class AutoBackupService : IHostedService
{
private readonly ConfigData _config;
public AutoBackupService(IServiceProvider serviceProvider)
{
_config = GlobalConstants.Config!.Data;
}
public Task StartAsync(CancellationToken cancellationToken)
{
AppBase.Current.AppStarted += OnAppStarted;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
AppBase.Current.AppStarted -= OnAppStarted;
return Task.CompletedTask;
}
private async void OnAppStarted(object? sender, EventArgs e)
{
if (!_config.IsAutoBackupEnabled) return;
try
{
await Task.Delay(TimeSpan.FromSeconds(10));
_config.IsBackupInProgress = true;
_config.BackupProgress = 0;
var progress = new Progress<double>(value =>
_config.BackupProgress = value);
await BackupHelper.CreateBackup(
_config.BackupFolderPath,
_config.IsLogGenerationEnabled,
progress);
BackupHelper.CleanOldBackups(
_config.BackupFolderPath,
_config.BackupCountLimit);
Console.WriteLine("[SuperAutoBackup] 自动备份完成");
}
catch (Exception ex)
{
Console.WriteLine($"[SuperAutoBackup] 自动备份失败: {ex.Message}");
}
finally
{
_config.IsBackupInProgress = false;
}
}
}