๐ Documentation: IrbyImaging.BusinessLogic/Jobs/Reminders.cs
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- UML Class Diagram
- Key Functional Areas
- Class, Constructors & Methods
- Helper Methods & Utilities
- Error Handling & Return Types
- Integration with Quartz.NET
- Sample Usage & Code Snippets
- Conclusion
1. Overview & Purpose
The Reminders class is part of the IrbyImaging.BusinessLogic.Jobs namespace and implements the Quartz.IJob interface. Its primary purpose is to:
- Act as a scheduled job using Quartz.NET
- Log execution details into a text file located under
wwwroot/Quartz/Logs.txt - Serve as a template for any future reminder-based background tasks
๐ฏ Use Case: You might schedule this job to run at fixed intervals (e.g., daily at 8ย AM) to trigger reminder notifications or housekeeping tasks.
2. Dependencies & Technologies
| Layer / Component | Technology / Package |
|---|---|
| Background Jobs | Quartz.NET |
| .NET Core Hosting | IServiceProvider (for dependency injection) |
| File I/O | System.IO (Directory, FileStream, StreamWriter) |
| Asynchronous Tasks | System.Threading.Tasks |
3. UML Class Diagram
classDiagram
direction TB
class Reminders {
- IServiceProvider _provider
+ Reminders(IServiceProvider provider)
+ Task Execute(IJobExecutionContext context)
+ void Logs(string message)
}
Reminders ..|> IJob
4. Key Functional Areas
- Job Initialization
- Injects an
IServiceProviderfor future service resolutions
- Injects an
- Job Execution
- Implements
Execute(IJobExecutionContext) - Writes a timestamped log entry to disk
- Implements
- Logging Utility
- Creates a folder
wwwroot/Quartzif missing - Overwrites
Logs.txtwith the latest message
- Creates a folder
5. Class, Constructors & Methods
Class: Reminders
public class Reminders : IJob
{
private readonly IServiceProvider _provider;
public Reminders(IServiceProvider provider) { /*...*/ }
public Task Execute(IJobExecutionContext context) { /*...*/ }
public void Logs(string message) { /*...*/ }
}
| Member | Type | Description |
|---|---|---|
_provider |
IServiceProvider |
A DI container reference (reserved for future expansions). |
Reminders(...) |
Constructor | Sets the _provider field. |
Execute(context) |
Task |
Entry-point invoked by Quartzโwrites a log and returns a completed task. |
Logs(message) |
void |
Utility to write message into wwwroot/Quartz/Logs.txt. |
6. Helper Methods & Utilities
๐ Logs(string message)
Writes timestamped or custom messages to a static file:
public void Logs(string message)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Quartz");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path = Path.Combine(path, "Logs.txt");
using FileStream fstream = new FileStream(path, FileMode.Create);
using TextWriter writer = new StreamWriter(fstream);
writer.WriteLine(message);
}
- Directory.CreateDirectory: Ensures the
Quartzfolder exists underwwwroot. - FileMode.Create: Creates or overwrites
Logs.txton each run.
7. Error Handling & Return Types
| Method | Return Type | Error Handling |
|---|---|---|
Execute(...) |
Task |
Relies on Quartz job exception propagation. |
Logs(...) |
void |
No explicit try/catchโexceptions bubble up. |
Note: For production readiness, consider wrapping file I/O in
try/catchand log failures to a fallback or external monitoring service.
8. Integration with Quartz.NET
8.1. Registering the Job
services.AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionJobFactory();
q.ScheduleJob<Reminders>(trigger => trigger
.WithIdentity("RemindersTrigger")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInHours(24)
.RepeatForever()
)
);
});
services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
8.2. Trigger Definition
- Trigger Identity:
RemindersTrigger - Schedule: Runs immediately and repeats every 24ย hours indefinitely
9. Sample Usage & Code Snippets
9.1. Scheduling Reminders Job
// In Startup.cs or Program.cs
public void ConfigureServices(IServiceCollection services)
{
// Quartz & DI
services.AddQuartz(...);
services.AddQuartzHostedService(...);
// Register Reminders
services.AddTransient<Reminders>();
}
9.2. Customizing the Log Message
public Task Execute(IJobExecutionContext context)
{
var userCount = GetPendingUserCount(); // hypothetical service from _provider
var msg = $"{DateTime.Now} [Pending Users: {userCount}]";
Logs(msg);
return Task.CompletedTask;
}
10. Conclusion
โจ Reminders.cs provides a lightweight, extensible Quartz.NET job template. It demonstrates:
- Dependency Injection via
IServiceProvider - Asynchronous Scheduling with
IJob.Execute - Simple File Logging under
wwwroot/Quartz/Logs.txt
You can extend it to send emails, push notifications, or perform any recurring background work by resolving and invoking additional services within the Execute method. Happy scheduling! ๐
End of Documentation