From 1b68553d70cbff685a16633e3f125dc975ddc353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=A2=D0=B8=D0=BC?= =?UTF-8?q?=D0=BE=D1=84=D0=B5=D0=B5=D0=B2?= Date: Wed, 15 Jul 2020 19:09:56 +0300 Subject: [PATCH] Fix lost subscription when BackgroundJobServer being stopped and then recreated --- .../RedisSubscription.cs | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/Hangfire.Redis.StackExchange/RedisSubscription.cs b/Hangfire.Redis.StackExchange/RedisSubscription.cs index 63702b7..7374db7 100644 --- a/Hangfire.Redis.StackExchange/RedisSubscription.cs +++ b/Hangfire.Redis.StackExchange/RedisSubscription.cs @@ -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(); - } } } -} +} \ No newline at end of file