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; /// /// A scheduled job that runs on a configurable cron schedule. /// public sealed class MyJob : IScheduledJob { private readonly ILogger _logger; private readonly MyJobOptions _options; public MyJob(ILogger logger, IOptions options) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); } /// public string JobId => "my-job"; /// public string DisplayName => "My Scheduled Job"; /// public string CronSchedule => _options.CronSchedule; /// public async Task 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()); } 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 }); } } } /// /// Represents a scheduled job that can be executed by the scheduler. /// public interface IScheduledJob { /// /// Gets the unique identifier for this job. /// string JobId { get; } /// /// Gets the display name for this job. /// string DisplayName { get; } /// /// Gets the cron schedule expression for this job. /// string CronSchedule { get; } /// /// Executes the scheduled job. /// Task ExecuteAsync(JobContext context, CancellationToken cancellationToken = default); } /// /// Context for job execution. /// public sealed record JobContext( DateTimeOffset ScheduledTime, DateTimeOffset ActualStartTime, string? CorrelationId = null); /// /// Result of a job execution. /// public sealed record JobResult( bool Success, string? Message, int ItemsProcessed, IReadOnlyList Errors);