|
| 1 | +using Hangfire; |
| 2 | +using Lingarr.Core.Data; |
| 3 | +using Lingarr.Core.Enum; |
| 4 | +using Lingarr.Server.Interfaces.Services; |
| 5 | +using Lingarr.Server.Models.Webhooks; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | + |
| 8 | +namespace Lingarr.Server.Jobs; |
| 9 | + |
| 10 | +public class WebhookJob |
| 11 | +{ |
| 12 | + private readonly LingarrDbContext _dbContext; |
| 13 | + private readonly IMediaService _mediaService; |
| 14 | + private readonly IMediaSubtitleProcessor _mediaSubtitleProcessor; |
| 15 | + private readonly ILogger<WebhookJob> _logger; |
| 16 | + |
| 17 | + public WebhookJob( |
| 18 | + LingarrDbContext dbContext, |
| 19 | + IMediaService mediaService, |
| 20 | + IMediaSubtitleProcessor mediaSubtitleProcessor, |
| 21 | + ILogger<WebhookJob> logger) |
| 22 | + { |
| 23 | + _dbContext = dbContext; |
| 24 | + _mediaService = mediaService; |
| 25 | + _mediaSubtitleProcessor = mediaSubtitleProcessor; |
| 26 | + _logger = logger; |
| 27 | + } |
| 28 | + |
| 29 | + [DisableConcurrentExecution(timeoutInSeconds: 2 * 60)] |
| 30 | + [AutomaticRetry(Attempts = 3)] |
| 31 | + [Queue("webhook")] |
| 32 | + public async Task ProcessRadarrWebhook(RadarrWebhookPayload payload) |
| 33 | + { |
| 34 | + if (payload.Movie == null) |
| 35 | + { |
| 36 | + _logger.LogWarning("Radarr webhook payload has no movie data. Skipping."); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + var movieId = await _mediaService.GetMovieIdOrSyncFromRadarrMovieId(payload.Movie.Id); |
| 43 | + if (movieId == 0) |
| 44 | + { |
| 45 | + _logger.LogWarning("Failed to sync or find movie with Radarr ID {RadarrId}. Movie may not have a file yet.", |
| 46 | + payload.Movie.Id); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + var movie = await _dbContext.Movies.FirstOrDefaultAsync(m => m.Id == movieId); |
| 51 | + if (movie == null) |
| 52 | + { |
| 53 | + _logger.LogError("Movie with ID {MovieId} not found in database after sync", movieId); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + await _mediaSubtitleProcessor.ProcessMedia(movie, MediaType.Movie); |
| 58 | + } |
| 59 | + catch (Exception ex) |
| 60 | + { |
| 61 | + _logger.LogError(ex, "Error processing Radarr webhook for movie {MovieTitle} (Radarr ID: {RadarrId})", |
| 62 | + payload.Movie.Title, payload.Movie.Id); |
| 63 | + throw; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [DisableConcurrentExecution(timeoutInSeconds: 2 * 60)] |
| 68 | + [AutomaticRetry(Attempts = 3)] |
| 69 | + [Queue("webhook")] |
| 70 | + public async Task ProcessSonarrWebhook(SonarrWebhookPayload payload) |
| 71 | + { |
| 72 | + if (payload.Series == null || payload.Episodes == null || !payload.Episodes.Any()) |
| 73 | + { |
| 74 | + _logger.LogWarning("Sonarr webhook payload has no series or episode data. Skipping."); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + foreach (var episode in payload.Episodes) |
| 81 | + { |
| 82 | + var episodeId = await _mediaService.GetEpisodeIdOrSyncFromSonarrEpisodeId(episode.Id); |
| 83 | + if (episodeId == 0) |
| 84 | + { |
| 85 | + _logger.LogWarning("Failed to sync or find episode with Sonarr ID {SonarrEpisodeId}. Episode may not have a file yet.", |
| 86 | + episode.Id); |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + var episodeEntity = await _dbContext.Episodes.FirstOrDefaultAsync(e => e.Id == episodeId); |
| 91 | + if (episodeEntity == null) |
| 92 | + { |
| 93 | + _logger.LogError("Episode with ID {EpisodeId} not found in database after sync", episodeId); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + await _mediaSubtitleProcessor.ProcessMedia(episodeEntity, MediaType.Episode); |
| 98 | + } |
| 99 | + |
| 100 | + _logger.LogInformation("Completed processing Sonarr webhook for series: {SeriesTitle}", payload.Series.Title); |
| 101 | + } |
| 102 | + catch (Exception ex) |
| 103 | + { |
| 104 | + _logger.LogError(ex, "Error processing Sonarr webhook for series {SeriesTitle} (Sonarr ID: {SonarrId})", |
| 105 | + payload.Series.Title, payload.Series.Id); |
| 106 | + throw; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments