private void ProcessMessage(BrokeredMessage message)
 {
     try
     {
         using (var s = message.GetBody <Stream>())
         {
             using (var tr = new StreamReader(s))
             {
                 using (var jr = new JsonTextReader(tr))
                 {
                     var obj  = JObject.ReadFrom(jr);
                     var msg  = obj.ToObject <Message>();
                     var room = msg.Room;
                     log.Debug($"Got notification for {room.RoomAddress} as {room.Id} @ {room.OrganizationId}");
                     _meetingCacheService.ClearUpcomingAppointmentsForRoom(room.RoomAddress);
                     _broadcastService.BroadcastUpdate(new OrganizationEntity()
                     {
                         Id = room.OrganizationId
                     }, room);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         log.Warn($"Failed to handle incoming message: {ex}");
     }
 }
 private void BroadcastUpdate(IRoom room)
 {
     _meetingCacheService.ClearUpcomingAppointmentsForRoom(room.RoomAddress);
     _broadcastService.BroadcastUpdate(_contextService.CurrentOrganization, room);
 }
Пример #3
0
 private void BroadcastUpdate(string roomAddress)
 {
     _meetingCacheService.ClearUpcomingAppointmentsForRoom(roomAddress);
     _broadcastService.BroadcastUpdate(roomAddress);
 }
Пример #4
0
            private async Task DoWork()
            {
                while (true)
                {
                    log.DebugFormat("Starting RestChangeNotification loop: {0}", _container);
                    try
                    {
                        _cancellationTokenSource = new CancellationTokenSource();
                        var token = _cancellationTokenSource.Token;
                        using (var cn = _container.CreateChildContainer())
                        {
                            var orgTask  = cn.Resolve <IOrganizationRepository>().GetAsync(_organizationId);
                            var roomTask = cn.Resolve <IRoomMetadataRepository>().GetRoomInfosForOrganizationAsync(_organizationId);

                            var org   = await orgTask;
                            var rooms = (await roomTask).ToList();

                            cn.RegisterInstance((IContextService) new CustomOrganizationContextService(org));
                            rooms.ForEach(r => _meetingCacheService.ClearUpcomingAppointmentsForRoom(r.RoomAddress));
                            var exch = cn.Resolve <ExchangeRestWrapper>();
                            log.DebugFormat("Creating {0} tasks", rooms.Count);
                            var subTasks = rooms.Select(r => new { Room = r, Task = exch.CreateNotification(r.RoomAddress) }).ToList();
                            await Task.WhenAll(subTasks.Select(_ => _.Task));

                            log.DebugFormat("Completed {0} tasks", rooms.Count);
                            var subscriptions = subTasks.Where(i => i.Task.IsCompleted).ToDictionary(i => i.Task.Result.Id, i => i.Room);
                            log.DebugFormat("Created {0} subscriptions", subscriptions.Count);
                            foreach (var room in subscriptions.Values)
                            {
                                _meetingCacheService.ClearUpcomingAppointmentsForRoom(room.RoomAddress);
                            }

                            try
                            {
                                var tasks = new List <Task>();
                                foreach (var keys in subscriptions.Keys.Chunk(20))
                                {
                                    log.DebugFormat("Waiting for notifications from {0} subscriptions", keys.Length);
                                    // stream subscriptions
                                    var req = new NotificationRequest()
                                    {
                                        ConnectionTimeoutInMinutes             = 120,
                                        KeepAliveNotificationIntervalInSeconds = 30,
                                        SubscriptionIds = keys,
                                    };
                                    tasks.Add(Task.Run(async() =>
                                    {
                                        await exch.GetNotifications(req, resp =>
                                        {
                                            var room = subscriptions.TryGetValue(resp.SubscriptionId);
                                            log.DebugFormat("Processing {0} for {1} on {2}", resp.ChangeType, room.RoomAddress, resp.SubscriptionId);
                                            _meetingCacheService.ClearUpcomingAppointmentsForRoom(room.RoomAddress);
                                            _broadcastService.BroadcastUpdate(org, room);
                                        }, _cancellationTokenSource.Token);
                                    }));
                                }
                                try
                                {
                                    await Task.WhenAny(tasks);
                                }
                                catch (Exception ex)
                                {
                                    log.DebugFormat("WhenAny got error: {0}", ex.Message);
                                }
                                log.DebugFormat("Cancelling outstanding work");
                                _cancellationTokenSource.Cancel();
                                await Task.WhenAll(tasks);
                            }
                            catch (Exception ex)
                            {
                                log.DebugFormat("DoWork got error: {0}", ex.Message);
                            }
                            _cancellationTokenSource.Cancel();
                        }
                    }
                    catch (Exception ex)
                    {
                        log.DebugFormat("Issue from main change-detect loop: {0}", ex);
                        await Task.Delay(1000);
                    }
                }
            }
Пример #5
0
 private void OnNotificationEvent(object sender, NotificationEventArgs args)
 {
     _meetingCacheService.ClearUpcomingAppointmentsForRoom(_room.RoomAddress);
     _broadcastService.BroadcastUpdate(_organization, _room);
 }