-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICronScheduledTask.cs
More file actions
101 lines (84 loc) · 3.18 KB
/
ICronScheduledTask.cs
File metadata and controls
101 lines (84 loc) · 3.18 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/// <summary>
/// Contract for a scheduled background task that includes its execution logic and its specific schedule.
/// </summary>
public interface ICronScheduledTask
{
/// <summary>
/// A unique name for logging and identification.
/// </summary>
string Name { get; }
/// <summary>
/// The schedule definition for the task.
/// Format: [DayOfWeek|Daily]@[HH:MM]
/// Examples: "Daily@08:30", "Monday@08:30", "Sunday@00:00"
/// </summary>
string Schedule { get; }
/// <summary>
/// Executes the scheduled job logic.
/// </summary>
Task ExecuteAsync();
}
/// <summary>
/// A daily scheduled task for routine maintenance (e.g., database cleanup, log rotation).
/// Runs every day at 04:00 AM UTC.
/// </summary>
public class DailyMaintenanceTask : ICronScheduledTask
{
private readonly ILogger<DailyMaintenanceTask> _logger;
public DailyMaintenanceTask(ILogger<DailyMaintenanceTask> logger)
{
_logger = logger;
}
// --- ICronScheduledTask Implementation ---
public string Name => "Daily Maintenance & Cleanup";
public string Schedule => "Daily@04:00"; // Run every day at 4:00 AM UTC
// ----------------------------------------
public async Task ExecuteAsync()
{
_logger.LogInformation("Executing Daily Maintenance Task: Running routine cleanup.");
// --- Actual Scheduled Logic ---
await Task.Delay(100); // Simulate asynchronous database cleanup or file deletion
// Note: For a real cleanup task, you would inject and use a repository here.
// ------------------------------
_logger.LogInformation("Daily Maintenance Task completed successfully.");
}
}
/// <summary>
/// Scheduled task for generating a report every Monday morning.
/// </summary>
public class BiWeeklyReportTaskMonday : ICronScheduledTask
{
private readonly ILogger<BiWeeklyReportTaskMonday> _logger;
public BiWeeklyReportTaskMonday(ILogger<BiWeeklyReportTaskMonday> logger)
{
_logger = logger;
}
public string Name => "Bi-Weekly Report (Monday)";
public string Schedule => "Monday@08:30"; // Run every Monday at 8:30 AM UTC
public async Task ExecuteAsync()
{
_logger.LogInformation("Executing Monday Report Task: Compiling weekly progress report.");
await Task.Delay(100); // Simulate report generation
_logger.LogInformation("Monday Report Task completed successfully.");
}
}
/// <summary>
/// Scheduled task for generating a report every Friday morning.
/// </summary>
public class BiWeeklyReportTaskFriday : ICronScheduledTask
{
private readonly ILogger<BiWeeklyReportTaskFriday> _logger;
public BiWeeklyReportTaskFriday(ILogger<BiWeeklyReportTaskFriday> logger)
{
_logger = logger;
}
public string Name => "Bi-Weekly Report (Friday)";
public string Schedule => "Friday@08:30"; // Run every Friday at 8:30 AM UTC
public async Task ExecuteAsync()
{
_logger.LogInformation("Executing Friday Report Task: Compiling end-of-week summary report.");
await Task.Delay(100); // Simulate report generation
_logger.LogInformation("Friday Report Task completed successfully.");
}
}