diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-10-26 18:32:35 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-26 18:32:35 -0500 |
| commit | e8238d19b3c7b29d2892caeafd66733b5622d1b2 (patch) | |
| tree | e8ced369b7631ef46d3043118aac5518dca76d35 /src/slskd/Events | |
| parent | 54590c78f2102634d982698608784e8d3c9043e0 (diff) | |
| parent | 334627cd06476df897cf3f3ec02ca70b51b7fec1 (diff) | |
| download | slskd-e8238d19b3c7b29d2892caeafd66733b5622d1b2.tar.gz | |
Merge pull request #1232 from slskd/scripts
Add the ability to run user-defined scripts when application events are raised
Diffstat (limited to 'src/slskd/Events')
| -rw-r--r-- | src/slskd/Events/EventBus.cs | 15 | ||||
| -rw-r--r-- | src/slskd/Events/EventsDbContext.cs | 5 | ||||
| -rw-r--r-- | src/slskd/Events/Types/EventRecord.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Events/Types/Events.cs | 11 |
4 files changed, 31 insertions, 9 deletions
diff --git a/src/slskd/Events/EventBus.cs b/src/slskd/Events/EventBus.cs index e5552d2d..16b7c9e1 100644 --- a/src/slskd/Events/EventBus.cs +++ b/src/slskd/Events/EventBus.cs @@ -78,12 +78,12 @@ public class EventBus // save the event to the database before broadcasting to consumers Events.Add(EventRecord.From<T>(data)); - // broadcast the event in a fire-and-forget fashion + // broadcast the event to granular subscribers in a fire-and-forget fashion // we don't need to wait for anything, just need to kick off the tasks // if something throws it will be logged and the consumer can figure it out if (Subscriptions.TryGetValue(typeof(T), out var subscribers)) { - Log.Debug("{Count} subscriber(s) for {Type}: {Names}", subscribers.Count, typeof(T), string.Join(", ", subscribers.Keys)); + Log.Debug("{Count} subscriber(s) for {Type}: {Names}", subscribers.Count, data.Type, string.Join(", ", subscribers.Keys)); // we don't care about any of these tasks; contractually we are only obligated to invoke them _ = Task.WhenAll(subscribers.Select(subscriber => @@ -94,6 +94,17 @@ public class EventBus { Log.Debug("No subscribers for {Type}", typeof(T)); } + + // broadcast the event to catch-all subscribers of type Event; listening for all types + if (Subscriptions.TryGetValue(typeof(Event), out subscribers)) + { + Log.Debug("{Count} catch-all subscriber(s): {Names}", subscribers.Count, string.Join(", ", subscribers.Keys)); + + // we don't care about any of these tasks; contractually we are only obligated to invoke them + _ = Task.WhenAll(subscribers.Select(subscriber => + Task.Run(() => (subscriber.Value as Func<Event, Task>)(data)) + .ContinueWith(task => Log.Error(task.Exception, "Catch-All Subscriber {Name} for {Type} encountered an error: {Message}", subscriber.Key, typeof(Event), task.Exception.Message)))); + } } /// <summary> diff --git a/src/slskd/Events/EventsDbContext.cs b/src/slskd/Events/EventsDbContext.cs index 9acbc9a4..a86f5d1c 100644 --- a/src/slskd/Events/EventsDbContext.cs +++ b/src/slskd/Events/EventsDbContext.cs @@ -34,6 +34,11 @@ public class EventsDbContext : DbContext { modelBuilder .Entity<EventRecord>() + .Property(e => e.Type) + .HasConversion<string>(); + + modelBuilder + .Entity<EventRecord>() .Property(e => e.Timestamp) .HasConversion(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); } diff --git a/src/slskd/Events/Types/EventRecord.cs b/src/slskd/Events/Types/EventRecord.cs index 11825c09..4323a42b 100644 --- a/src/slskd/Events/Types/EventRecord.cs +++ b/src/slskd/Events/Types/EventRecord.cs @@ -19,7 +19,6 @@ namespace slskd.Events; using System; using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Nodes; @@ -31,7 +30,7 @@ using Microsoft.EntityFrameworkCore; public record EventRecord { public DateTime Timestamp { get; init; } = DateTime.UtcNow; - public string Type { get; private set; } + public EventType Type { get; private set; } public string Data { get; init; } [Key] public Guid Id { get; private set; } @@ -61,10 +60,6 @@ public record EventRecord public static EventRecord From<T>(Event e) where T : Event { - // this will be 'slskd.NameOfEvent'; we want to chop off 'slskd.' and 'Event' = 'NameOf' - var type = e.GetType().Name.Split('.').TakeLast(1).First(); - type = type.Substring(0, type.Length - "Event".Length); // chop off "Event" suffix - // construct the data for the record by serializing the event and removing redundant properties var json = JsonSerializer.Serialize(e as T, JsonSerializerOptions); var data = JsonNode.Parse(json).AsObject(); @@ -76,7 +71,7 @@ public record EventRecord return new EventRecord { Timestamp = e.Timestamp, - Type = type, + Type = e.Type, Id = e.Id, Data = data.ToJsonString(JsonSerializerOptions), }; diff --git a/src/slskd/Events/Types/Events.cs b/src/slskd/Events/Types/Events.cs index 20ec3f9f..9a3a71ed 100644 --- a/src/slskd/Events/Types/Events.cs +++ b/src/slskd/Events/Types/Events.cs @@ -24,10 +24,20 @@ public abstract record Event { public Guid Id { get; } = Guid.NewGuid(); public DateTime Timestamp { get; init; } = DateTime.UtcNow; + public abstract EventType Type { get; } +} + +public enum EventType +{ + None = 0, + Any = 1, + DownloadFileComplete = 2, + DownloadDirectoryComplete = 3, } public sealed record DownloadFileCompleteEvent : Event { + public override EventType Type => EventType.DownloadFileComplete; public string LocalFilename { get; init; } public string RemoteFilename { get; init; } public Transfer Transfer { get; init; } @@ -35,6 +45,7 @@ public sealed record DownloadFileCompleteEvent : Event public sealed record DownloadDirectoryCompleteEvent : Event { + public override EventType Type => EventType.DownloadDirectoryComplete; public string LocalDirectoryName { get; init; } public string RemoteDirectoryName { get; init; } public string Username { get; init; } |
