public async Task <IDictionary <object, object> > PostWaitForAsync([FromBody] IEnumerable <IDictionary <object, object> > filters, int timeout = 60)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }

            var subscriptions = new List <string>();

            try
            {
                var tcs = new TaskCompletionSource <IDictionary <object, object> >();

                foreach (var filter in filters)
                {
                    var subscriptionUid = "api_wait_for:" + Guid.NewGuid().ToString("D");
                    subscriptions.Add(_messageBusService.Subscribe(subscriptionUid, filter, m => tcs.TrySetResult(m)));
                }

                using (var timeoutToken = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)))
                {
                    using (var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(timeoutToken.Token, HttpContext.RequestAborted))
                    {
                        using (linkedToken.Token.Register(() => { tcs.TrySetCanceled(); }))
                        {
                            return(await tcs.Task.ConfigureAwait(false));
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                return(new Dictionary <object, object>
                {
                    ["type"] = "exception.timeout"
                });
            }
            finally
            {
                foreach (var subscription in subscriptions)
                {
                    _messageBusService.Unsubscribe(subscription);
                }
            }
        }
        public async Task <WirehomeDictionary> PostWaitForAsync([FromBody] IEnumerable <WirehomeDictionary> filters, int timeout = 60)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }

            var subscriptions = new List <string>();

            try
            {
                var tcs = new TaskCompletionSource <MessageBusMessage>();
                foreach (var filter in filters)
                {
                    var subscriptionUid = "api_wait_for:" + Guid.NewGuid().ToString("D");
                    subscriptions.Add(_messageBusService.Subscribe(subscriptionUid, filter, m => tcs.TrySetResult(m)));
                }

                using (var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)))
                {
                    timeoutCts.Token.Register(() =>
                    {
                        if (!tcs.Task.IsCanceled && !tcs.Task.IsCompleted && !tcs.Task.IsFaulted)
                        {
                            tcs.TrySetCanceled();
                        }
                    });

                    return((await tcs.Task).Message);
                }
            }
            catch (OperationCanceledException)
            {
                return(new WirehomeDictionary().WithType("exception.timeout"));
            }
            finally
            {
                foreach (var subscription in subscriptions)
                {
                    _messageBusService.Unsubscribe(subscription);
                }
            }
        }
        public async Task <WirehomeDictionary> PostWaitForAsync([FromBody] IEnumerable <WirehomeDictionary> filters, int timeout = 60)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }

            var subscriptions = new List <string>();

            try
            {
                var tcs = new TaskCompletionSource <MessageBusMessage>();
                foreach (var filter in filters)
                {
                    var subscriptionUid = "api_wait_for:" + Guid.NewGuid().ToString("D");
                    subscriptions.Add(_messageBusService.Subscribe(subscriptionUid, filter, m => tcs.TrySetResult(m)));
                }

                var timeoutCts   = new CancellationTokenSource(TimeSpan.FromSeconds(60));
                var cts          = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, HttpContext.RequestAborted);
                var timeoutTask  = Task.Run(async() => await Task.Delay(TimeSpan.FromSeconds(timeout), cts.Token), cts.Token);
                var finishedTask = await Task.WhenAny(tcs.Task, timeoutTask);

                if (finishedTask == timeoutTask)
                {
                    return(new WirehomeDictionary().WithType("exception.timeout"));
                }

                return(tcs.Task.Result.Message);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
            finally
            {
                foreach (var subscription in subscriptions)
                {
                    _messageBusService.Unsubscribe(subscription);
                }
            }
        }
Exemple #4
0
 public void unsubscribe(string uid)
 {
     _messageBusService.Unsubscribe(uid);
 }
Exemple #5
0
 public void DeleteSubscriber(string uid)
 {
     _messageBusService.Unsubscribe(uid);
 }