Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions Hangfire.Redis.StackExchange/RedisSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,29 @@ namespace Hangfire.Redis
internal class RedisSubscription : IServerComponent
#pragma warning restore 618
{
private readonly ManualResetEvent _mre = new ManualResetEvent(false);
private readonly RedisStorage _storage;
private readonly ISubscriber _subscriber;
private readonly AutoResetEvent _resetEvent = new AutoResetEvent(false);

public RedisSubscription([NotNull] RedisStorage storage, [NotNull] ISubscriber subscriber)
{
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
Channel = _storage.GetRedisKey("JobFetchChannel");
if (storage == null) throw new ArgumentNullException(nameof(storage));
if (subscriber == null) throw new ArgumentNullException(nameof(subscriber));

_subscriber = subscriber ?? throw new ArgumentNullException(nameof(subscriber));
_subscriber.Subscribe(Channel, (channel, value) => _mre.Set());

Channel = storage.GetRedisKey("JobFetchChannel");

subscriber.Subscribe(Channel, (channel, value) => _resetEvent.Set());
}

public string Channel { get; }

public void WaitForJob(TimeSpan timeout, CancellationToken cancellationToken)
{
_mre.Reset();
WaitHandle.WaitAny(new[] { _mre, cancellationToken.WaitHandle }, timeout);
WaitHandle.WaitAny(new[] {_resetEvent, cancellationToken.WaitHandle}, timeout);
}

void IServerComponent.Execute(CancellationToken cancellationToken)
{
cancellationToken.WaitHandle.WaitOne();

if (cancellationToken.IsCancellationRequested)
{
_subscriber.Unsubscribe(Channel);
_mre.Dispose();
}
}
}
}
}