📄 JobSchedule Class Documentation
File:
IrbyImaging.BusinessLogic/Scheduler/JobSchedule.cs
📑 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- Key Functional Areas
- Public API
- 5.1 Constructors
- 5.2 Properties
- Usage Examples
- Integration with a Scheduler (e.g., Quartz.NET)
- Error Handling & Validation
- Third‑Party Integrations
- Conclusion
1. Overview & Purpose
🔍 JobSchedule is a simple business-model class used to define scheduled jobs in the IrbyImaging application. Each instance ties:
- A job type (
Type) – the .NET class implementing the job logic. - A Cron expression (
string) – defining when the job should run.
By encapsulating scheduling metadata in one place, you can easily feed a scheduler engine (e.g., Quartz.NET) at startup.
2. Dependencies & Technologies
| Layer/Area | Technology/Library |
|---|---|
| Language | C# (.NET Core / .NET 5+) |
| Scheduling Engine* | Quartz.NET (recommended) |
| Build System | MSBuild / dotnet CLI |
* Although
JobScheduledoes not directly reference Quartz.NET, it’s primarily designed to work with scheduler libraries.
3. Class Diagram (UML)
classDiagram
class JobSchedule {
+Type JobType {get;}
+string CronExpression {get;}
+JobSchedule(Type jobType, string cronExpression)
}
4. Key Functional Areas
- Encapsulation of scheduling metadata
- Read-only properties to ensure immutability after construction
- Plain .NET Type to decouple from any specific scheduler implementation
5. Public API
5.1 Constructors
| Signature | Description |
|---|---|
JobSchedule(Type jobType, string cronExpression) |
Create a schedule binding the CLR type to a Cron schedule. |
Constructor Logic
public JobSchedule(Type jobType, string cronExpression)
{
JobType = jobType;
CronExpression = cronExpression;
}
- JobType: The
Typeof your scheduled job (must implement your job interface/contract). - CronExpression: A valid Cron string (e.g.,
"0 0/5 * * * ?").
5.2 Properties
| Property | Type | Description |
|---|---|---|
| JobType | Type | CLR Type of the job class. |
| CronExpression | string | Cron-formatted schedule expression. |
6. Usage Examples
using IrbyImaging.BusinessLogic.Scheduler;
using System;
using System.Collections.Generic;
public class SchedulerConfigurator
{
public IEnumerable<JobSchedule> GetJobSchedules()
{
return new List<JobSchedule>
{
// Run CleanupJob every day at 3 AM
new JobSchedule(typeof(CleanupJob), "0 0 3 * * ?"),
// Run ReportJob every Monday at 1:30 AM
new JobSchedule(typeof(ReportJob), "0 30 1 ? * MON"),
// Run MaintenanceJob every 5 minutes
new JobSchedule(typeof(MaintenanceJob), "0 0/5 * * * ?")
};
}
}
7. Integration with a Scheduler (eg. Quartz.NET)
Example of how you might register these schedules with Quartz.NET in a .NET Core Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// 1️⃣ Add Quartz services
services.AddQuartz(q =>
{
// 2️⃣ Retrieve schedules
var schedules = new SchedulerConfigurator().GetJobSchedules();
foreach (var schedule in schedules)
{
// Unique job key
var jobKey = new JobKey(schedule.JobType.FullName);
// 3️⃣ Register the job
q.AddJob(schedule.JobType, j => j.WithIdentity(jobKey));
// 4️⃣ Create a trigger based on CronExpression
q.AddTrigger(t => t
.ForJob(jobKey)
.WithIdentity($"{jobKey}.trigger")
.WithCronSchedule(schedule.CronExpression));
}
});
services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});
}
8. Error Handling & Validation
- Immutable after instantiation; no setters expose invalid states at runtime.
- No built‑in validation of the Cron expression:
- 💡 Recommendation: Validate at startup using your scheduler’s Cron parser (e.g., Quartz’s
CronExpression.IsValidExpression(...)), and throw early if invalid.
- 💡 Recommendation: Validate at startup using your scheduler’s Cron parser (e.g., Quartz’s
9. Third-Party Integrations
| Integration | Role |
|---|---|
| Quartz.NET | Parses and schedules jobs using the CronExpression / JobType info. |
| Hangfire | Could be another consumer for JobSchedule definitions. |
You can easily adapt
JobScheduleto other frameworks (e.g., Hangfire, FluentScheduler) by reading its properties and registering accordingly.
10. Conclusion
🚀 JobSchedule is a lightweight, immutable model that:
- Centralizes scheduling metadata
- Decouples your job definitions from the scheduler engine
- Provides a clean, testable way to manage Cron-based jobs
By using this class, you gain flexibility to swap out scheduling frameworks, perform early validation, and keep your scheduling logic DRY and maintainable.
Happy Scheduling! ⏰✨