-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: creates worker context and worker events
- Loading branch information
1 parent
1d17c14
commit 48a7417
Showing
27 changed files
with
299 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/KafkaFlow.Abstractions/Consumers/IWorkerLifetimeContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace KafkaFlow | ||
{ | ||
/// <summary> | ||
/// Provides access to the current consumer worker context. This interface only returns values when inside a middleware with Worker lifetime; otherwise, it will return null. | ||
/// </summary> | ||
public interface IWorkerLifetimeContext | ||
{ | ||
/// <summary> | ||
/// Gets the current worker in the context. | ||
/// </summary> | ||
IWorker Worker { get; } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/KafkaFlow.Abstractions/Consumers/WorkerStoppedSubject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace KafkaFlow | ||
{ | ||
using KafkaFlow.Observer; | ||
|
||
/// <summary> | ||
/// Represents a subject specific to worker stopped events where observers can subscribe to receive notifications. | ||
/// </summary> | ||
public class WorkerStoppedSubject : Subject<WorkerStoppedSubject> | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/KafkaFlow.Abstractions/Consumers/WorkerStoppingSubject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace KafkaFlow | ||
{ | ||
using KafkaFlow.Observer; | ||
|
||
/// <summary> | ||
/// Represents a subject specific to worker stopping events where observers can subscribe to receive notifications. | ||
/// </summary> | ||
public class WorkerStoppingSubject : Subject<WorkerStoppingSubject> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace KafkaFlow.Observer | ||
{ | ||
/// <summary> | ||
/// Represents a subject in the observer design pattern that can be observed by observers. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the subject.</typeparam> | ||
public interface ISubject<T> | ||
where T : ISubject<T> | ||
{ | ||
/// <summary> | ||
/// Subscribes an observer to the subject. | ||
/// </summary> | ||
/// <param name="observer">The observer to subscribe.</param> | ||
void Subscribe(ISubjectObserver<T> observer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace KafkaFlow.Observer | ||
{ | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Represents an observer in the observer design pattern that can receive notifications from a subject. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the subject.</typeparam> | ||
public interface ISubjectObserver<T> | ||
where T : ISubject<T> | ||
{ | ||
/// <summary> | ||
/// Called when a notification is received from the subject. | ||
/// </summary> | ||
/// <returns>A task representing the asynchronous notification handling.</returns> | ||
Task OnNotification(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace KafkaFlow.Observer | ||
{ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Represents a subject in the observer design pattern that can be observed by multiple observers. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the subject.</typeparam> | ||
public abstract class Subject<T> : ISubject<T> | ||
where T : ISubject<T> | ||
{ | ||
private readonly List<ISubjectObserver<T>> observers = new(); | ||
|
||
/// <summary> | ||
/// Subscribes an observer to the subject, allowing it to receive notifications. | ||
/// </summary> | ||
/// <param name="observer">The observer to subscribe.</param> | ||
public void Subscribe(ISubjectObserver<T> observer) => this.observers.Add(observer); | ||
|
||
/// <summary> | ||
/// Notifies all subscribed observers asynchronously. | ||
/// </summary> | ||
/// <returns>A task representing the asynchronous notification operation.</returns> | ||
public async Task NotifyAsync() | ||
{ | ||
foreach (var observer in this.observers) | ||
{ | ||
await observer.OnNotification(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.