Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Adds notification support for wasari daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cordeiro committed Jun 26, 2023
1 parent 3578da3 commit 72dcf5c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
11 changes: 9 additions & 2 deletions Wasari.Daemon/Handlers/DownloadRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Wasari.App;
using Microsoft.Extensions.Options;
using Wasari.App;
using Wasari.App.Abstractions;
using Wasari.Daemon.Models;
using Wasari.Daemon.Options;

namespace Wasari.Daemon.Handlers;

public class DownloadRequestHandler
{
public async ValueTask Handle(DownloadRequest request, ILogger<DownloadRequestHandler> logger, DownloadServiceSolver downloadServiceSolver)
public async ValueTask Handle(DownloadRequest request, ILogger<DownloadRequestHandler> logger, DownloadServiceSolver downloadServiceSolver, IServiceProvider serviceProvider, IOptions<NotificationOptions> notificationOptions)
{
logger.LogInformation("Starting download of {Url}", request.Url);

Expand All @@ -24,5 +26,10 @@ public async ValueTask Handle(DownloadRequest request, ILogger<DownloadRequestHa
{
logger.LogError("Failed to download {Episode}", downloadedEpisode);
}

if (notificationOptions.Value.Enabled && serviceProvider.GetService<NotificationService>() is {} notificationService)
{
await notificationService.SendNotifcationForDownloadedEpisodeAsync(episodes);
}
}
}
6 changes: 6 additions & 0 deletions Wasari.Daemon/Options/NotificationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Wasari.Daemon.Options;

public record NotificationOptions
{
public bool Enabled { get; set; }
}
13 changes: 12 additions & 1 deletion Wasari.Daemon/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Wasari.Daemon.Handlers;
using Wasari.Daemon.HostedServices;
using Wasari.Daemon.Models;
using Wasari.Daemon.Options;
using Wasari.FFmpeg;
using Wasari.YoutubeDlp;
using WasariEnvironment;
Expand All @@ -20,6 +21,7 @@
var username = Environment.GetEnvironmentVariable("USERNAME") ?? throw new InvalidOperationException("USERNAME environment variable is not set");
var password = Environment.GetEnvironmentVariable("PASSWORD") ?? throw new InvalidOperationException("PASSWORD environment variable is not set");
var postgresCs = Environment.GetEnvironmentVariable("POSTGRESQL_CONNECTION_STRING") ?? throw new InvalidOperationException("POSTGRESQL_CONNECTION_STRING environment variable is not set");
var webhookUrl = Environment.GetEnvironmentVariable("WEBHOOK_URL");

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -74,7 +76,7 @@

opts.Discovery.DisableConventionalDiscovery();
opts.Discovery.IncludeType<DownloadRequestHandler>();

opts.PersistMessagesWithPostgresql(postgresCs);
opts.UseEntityFrameworkCoreTransactions();
opts.Policies.AutoApplyTransactions();
Expand All @@ -84,6 +86,15 @@
opts.UseFluentValidation();
});

if (webhookUrl != null)
{
builder.Services.AddHttpClient<NotificationService>(c => { c.BaseAddress = new Uri(webhookUrl); });
builder.Services.Configure<NotificationOptions>(o =>
{
o.Enabled = true;
});
}

builder.Host.UseResourceSetupOnStartup();
var app = builder.Build();

Expand Down

0 comments on commit 72dcf5c

Please sign in to comment.