using ClipForge.Core.Interfaces.Services; using Hangfire; using Microsoft.Extensions.Logging; namespace ClipForge.Infrastructure.Services; public class SchedulerService : ISchedulerService { private readonly IRecurringJobManager _recurringJobs; private readonly IBackgroundJobClient _backgroundJobs; private readonly ILogger _logger; public SchedulerService( IBackgroundJobClient backgroundJobs, IRecurringJobManager recurringJobs, ILogger logger) { _backgroundJobs = backgroundJobs; _recurringJobs = recurringJobs; _logger = logger; } public Task ScheduleFullPipelineAsync(string niche, string platform = "tiktok", CancellationToken ct = default) { _logger.LogInformation("Scheduling full pipeline for niche '{Niche}' on {Platform}", niche, platform); // Handled directly in PipelineEndpoints via Hangfire return Task.CompletedTask; } public Task ScheduleRecurringGenerationAsync(string niche, string cronExpression, string platform = "tiktok", CancellationToken ct = default) { var jobId = $"pipeline-{niche}-{platform}".ToLowerInvariant().Replace(" ", "-"); _logger.LogInformation("Scheduling recurring pipeline '{JobId}' with cron '{Cron}'", jobId, cronExpression); _recurringJobs.AddOrUpdate( jobId, svc => svc.GenerateIdeaAsync(niche, platform, CancellationToken.None), cronExpression); return Task.CompletedTask; } public Task CancelRecurringJobAsync(string jobId, CancellationToken ct = default) { _logger.LogInformation("Cancelling recurring job '{JobId}'", jobId); _recurringJobs.RemoveIfExists(jobId); return Task.CompletedTask; } }