up
This commit is contained in:
105
templates/stellaops-plugin-scheduler/MyJob.cs
Normal file
105
templates/stellaops-plugin-scheduler/MyJob.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StellaOps.Plugin.Versioning;
|
||||
|
||||
// Declare plugin version for compatibility checking
|
||||
[assembly: StellaPluginVersion("1.0.0-template", MinimumHostVersion = "1.0.0")]
|
||||
|
||||
namespace StellaOps.Plugin.MyJob;
|
||||
|
||||
/// <summary>
|
||||
/// A scheduled job that runs on a configurable cron schedule.
|
||||
/// </summary>
|
||||
public sealed class MyJob : IScheduledJob
|
||||
{
|
||||
private readonly ILogger<MyJob> _logger;
|
||||
private readonly MyJobOptions _options;
|
||||
|
||||
public MyJob(ILogger<MyJob> logger, IOptions<MyJobOptions> options)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string JobId => "my-job";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string DisplayName => "My Scheduled Job";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string CronSchedule => _options.CronSchedule;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<JobResult> ExecuteAsync(JobContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_logger.LogInformation("Starting scheduled job {JobId} at {StartTime}", JobId, context.ScheduledTime);
|
||||
|
||||
try
|
||||
{
|
||||
// TODO: Implement your job logic here
|
||||
await Task.Delay(100, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Completed scheduled job {JobId}", JobId);
|
||||
|
||||
return new JobResult(
|
||||
Success: true,
|
||||
Message: "Job completed successfully",
|
||||
ItemsProcessed: 0,
|
||||
Errors: Array.Empty<string>());
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to execute scheduled job {JobId}", JobId);
|
||||
|
||||
return new JobResult(
|
||||
Success: false,
|
||||
Message: ex.Message,
|
||||
ItemsProcessed: 0,
|
||||
Errors: new[] { ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a scheduled job that can be executed by the scheduler.
|
||||
/// </summary>
|
||||
public interface IScheduledJob
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the unique identifier for this job.
|
||||
/// </summary>
|
||||
string JobId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display name for this job.
|
||||
/// </summary>
|
||||
string DisplayName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cron schedule expression for this job.
|
||||
/// </summary>
|
||||
string CronSchedule { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Executes the scheduled job.
|
||||
/// </summary>
|
||||
Task<JobResult> ExecuteAsync(JobContext context, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Context for job execution.
|
||||
/// </summary>
|
||||
public sealed record JobContext(
|
||||
DateTimeOffset ScheduledTime,
|
||||
DateTimeOffset ActualStartTime,
|
||||
string? CorrelationId = null);
|
||||
|
||||
/// <summary>
|
||||
/// Result of a job execution.
|
||||
/// </summary>
|
||||
public sealed record JobResult(
|
||||
bool Success,
|
||||
string? Message,
|
||||
int ItemsProcessed,
|
||||
IReadOnlyList<string> Errors);
|
||||
Reference in New Issue
Block a user