private async Task OnShiftNotificationQueueReceived(ShiftQueueItem sqi)
        {
            _logger.LogInformation($"{Name}: Shift Queue for shift id {sqi.ShiftId}, starting processing...");
            await ShiftNotificationLogic.ProcessShiftQueueItem(sqi);

            _logger.LogInformation($"{Name}: Finished processing of shift with id of {sqi.ShiftId}.");
        }
        public void EnqueueShiftNotification(ShiftQueueItem shiftQueueItem)
        {
            string serializedObject = String.Empty;

            serializedObject = ObjectSerialization.Serialize(shiftQueueItem);

            SendMessage(ServiceBusConfig.ShiftNotificationsQueueName, serializedObject);
        }
        public async Task <Tuple <bool, string> > ProcessQueueMessage(Message message, CancellationToken token)
        {
            bool   success = true;
            string result  = "";

            if (message != null)
            {
                try
                {
                    var body = message.GetBody <string>();

                    if (!String.IsNullOrWhiteSpace(body))
                    {
                        ShiftQueueItem sqi = null;
                        try
                        {
                            sqi = ObjectSerialization.Deserialize <ShiftQueueItem>(body);
                        }
                        catch (Exception ex)
                        {
                            success = false;
                            result  = "Unable to parse message body Exception: " + ex.ToString();
                            await _client.CompleteAsync(message.SystemProperties.LockToken);

                            //message.Complete();
                        }

                        ProcessShiftQueueItem(sqi);
                    }

                    try
                    {
                        if (success)
                        {
                            await _client.CompleteAsync(message.SystemProperties.LockToken);
                        }
                        //message.Complete();
                    }
                    catch (MessageLockLostException)
                    {
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                    await _client.AbandonAsync(message.SystemProperties.LockToken);

                    //message.Abandon();
                    success = false;
                    result  = ex.ToString();
                }
            }

            return(new Tuple <bool, string>(success, result));
        }
Exemple #4
0
 public void Process(ShiftQueueItem item)
 {
     if (Config.SystemBehaviorConfig.IsAzure)
     {
         ProcessQueueMessage(_client.Receive());
     }
     else
     {
         ProcessShiftQueueItem(item);
     }
 }
Exemple #5
0
        public static Tuple <bool, string> ProcessQueueMessage(BrokeredMessage message)
        {
            bool   success = true;
            string result  = "";

            if (message != null)
            {
                try
                {
                    var body = message.GetBody <string>();

                    if (!String.IsNullOrWhiteSpace(body))
                    {
                        ShiftQueueItem sqi = null;
                        try
                        {
                            sqi = ObjectSerialization.Deserialize <ShiftQueueItem>(body);
                        }
                        catch (Exception ex)
                        {
                            success = false;
                            result  = "Unable to parse message body Exception: " + ex.ToString();
                            message.Complete();
                        }

                        ProcessShiftQueueItem(sqi);
                    }

                    try
                    {
                        if (success)
                        {
                            message.Complete();
                        }
                    }
                    catch (MessageLockLostException)
                    {
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(ex);
                    message.Abandon();
                    success = false;
                    result  = ex.ToString();
                }
            }

            return(new Tuple <bool, string>(success, result));
        }
        public async Task <bool> EnqueueShiftNotification(ShiftQueueItem shiftQueueItem)
        {
            if (SystemBehaviorConfig.ServiceBusType == ServiceBusTypes.Rabbit)
            {
                return(_rabbitOutboundQueueProvider.EnqueueShiftNotification(shiftQueueItem));
            }

            VerifyAndCreateClients();

            Message message = new Message(Encoding.UTF8.GetBytes(ObjectSerialization.Serialize(shiftQueueItem)));

            message.MessageId = Guid.NewGuid().ToString();

            return(await SendMessage(_shiftsClient, message));
        }
Exemple #7
0
            public void Handle(ShiftDaysAddedEvent message)
            {
                if (_outboundQueueProvider == null)
                {
                    _outboundQueueProvider = new OutboundQueueProvider();
                }

                var item = new ShiftQueueItem();

                item.DepartmentId     = message.DepartmentId;
                item.DepartmentNumber = message.DepartmentNumber;
                item.ShiftId          = message.Item.ShiftId;
                item.Type             = (int)ShiftQueueTypes.ShiftDaysAdded;

                _outboundQueueProvider.EnqueueShiftNotification(item);
            }
Exemple #8
0
        public void EnqueueShiftNotification(ShiftQueueItem shiftQueueItem)
        {
            if (SystemBehaviorConfig.ServiceBusType == ServiceBusTypes.Rabbit)
            {
                _rabbitOutboundQueueProvider.EnqueueShiftNotification(shiftQueueItem);
                return;
            }

            VerifyAndCreateClients();

            BrokeredMessage message = new BrokeredMessage(ObjectSerialization.Serialize(shiftQueueItem));

            message.MessageId = Guid.NewGuid().ToString();

            SendMessage(_shiftsClient, message);
        }
Exemple #9
0
            public void Handle(ShiftTradeFilledEvent message)
            {
                if (_outboundQueueProvider == null)
                {
                    _outboundQueueProvider = new OutboundQueueProvider();
                }

                var item = new ShiftQueueItem();

                item.DepartmentId       = message.DepartmentId;
                item.ShiftSignupTradeId = message.ShiftSignupTradeId;
                item.DepartmentNumber   = message.DepartmentNumber;
                item.Type         = (int)ShiftQueueTypes.TradeFilled;
                item.SourceUserId = message.UserId;

                _outboundQueueProvider.EnqueueShiftNotification(item);
            }
        public void Process(ShiftQueueItem item)
        {
            if (Config.SystemBehaviorConfig.IsAzure)
            {
                //ProcessQueueMessage(_client.Receive());

                var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
                {
                    MaxConcurrentCalls = 1,
                    AutoComplete       = false
                };

                // Register the function that will process messages
                _client.RegisterMessageHandler(ProcessQueueMessage, messageHandlerOptions);
            }
            else
            {
                ProcessShiftQueueItem(item);
            }
        }
        public static async Task <bool> ProcessShiftQueueItem(ShiftQueueItem sqi)
        {
            if (sqi != null)
            {
                var _shiftsService        = Bootstrapper.GetKernel().Resolve <IShiftsService>();
                var _communicationService = Bootstrapper.GetKernel().Resolve <ICommunicationService>();
                var _userProfileService   = Bootstrapper.GetKernel().Resolve <IUserProfileService>();

                if (sqi.Type == (int)ShiftQueueTypes.TradeRequested)
                {
                    var tradeRequest = await _shiftsService.GetShiftTradeByIdAsync(sqi.ShiftSignupTradeId);

                    var sourceUserProfile = await _userProfileService.GetProfileByUserIdAsync(tradeRequest.SourceShiftSignup.UserId);

                    var text = _shiftsService.GenerateShiftTradeNotificationText(sourceUserProfile, tradeRequest);

                    var userProfiles = await _userProfileService.GetSelectedUserProfilesAsync(tradeRequest.Users.Select(x => x.UserId).ToList());

                    foreach (var user in tradeRequest.Users)
                    {
                        UserProfile profile = userProfiles.FirstOrDefault(x => x.UserId == user.UserId);
                        _communicationService.SendNotificationAsync(user.UserId, tradeRequest.SourceShiftSignup.Shift.DepartmentId, text, sqi.DepartmentNumber,
                                                                    tradeRequest.SourceShiftSignup.Shift.Name, profile);
                    }
                }
                else if (sqi.Type == (int)ShiftQueueTypes.TradeRejected && !String.IsNullOrWhiteSpace(sqi.SourceUserId))
                {
                    var tradeRequest = await _shiftsService.GetShiftTradeByIdAsync(sqi.ShiftSignupTradeId);

                    var sourceUserProfile = await _userProfileService.GetProfileByUserIdAsync(tradeRequest.SourceShiftSignup.UserId);

                    var targetUserProfile = await _userProfileService.GetProfileByUserIdAsync(sqi.SourceUserId);

                    var text = _shiftsService.GenerateShiftTradeRejectionText(targetUserProfile, tradeRequest);

                    await _communicationService.SendNotificationAsync(sourceUserProfile.UserId, tradeRequest.SourceShiftSignup.Shift.DepartmentId, text, sqi.DepartmentNumber,
                                                                      tradeRequest.SourceShiftSignup.Shift.Name, sourceUserProfile);
                }
                else if (sqi.Type == (int)ShiftQueueTypes.TradeProposed && !String.IsNullOrWhiteSpace(sqi.SourceUserId))
                {
                    var tradeRequest = await _shiftsService.GetShiftTradeByIdAsync(sqi.ShiftSignupTradeId);

                    var sourceUserProfile = await _userProfileService.GetProfileByUserIdAsync(tradeRequest.SourceShiftSignup.UserId);

                    var proposedUserProfile = await _userProfileService.GetProfileByUserIdAsync(sqi.SourceUserId);

                    var text = _shiftsService.GenerateShiftTradeProposedText(proposedUserProfile, tradeRequest);

                    await _communicationService.SendNotificationAsync(sourceUserProfile.UserId, tradeRequest.SourceShiftSignup.Shift.DepartmentId, text, sqi.DepartmentNumber,
                                                                      tradeRequest.SourceShiftSignup.Shift.Name, sourceUserProfile);
                }
                else if (sqi.Type == (int)ShiftQueueTypes.TradeFilled && !String.IsNullOrWhiteSpace(sqi.SourceUserId))
                {
                    var tradeRequest = await _shiftsService.GetShiftTradeByIdAsync(sqi.ShiftSignupTradeId);

                    var sourceUserProfile = await _userProfileService.GetProfileByUserIdAsync(tradeRequest.SourceShiftSignup.UserId);

                    var proposedUserProfile = await _userProfileService.GetProfileByUserIdAsync(sqi.SourceUserId);

                    var text = _shiftsService.GenerateShiftTradeFilledText(sourceUserProfile, tradeRequest);

                    await _communicationService.SendNotificationAsync(proposedUserProfile.UserId, tradeRequest.SourceShiftSignup.Shift.DepartmentId, text, sqi.DepartmentNumber,
                                                                      tradeRequest.SourceShiftSignup.Shift.Name, proposedUserProfile);
                }
                else if (sqi.Type == (int)ShiftQueueTypes.ShiftCreated)
                {
                    var shift = await _shiftsService.GetShiftByIdAsync(sqi.ShiftId);

                    var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(sqi.DepartmentId);

                    var text = $"New Shift {shift.Name} has been created";

                    foreach (var profile in profiles.Select(x => x.Value))
                    {
                        await _communicationService.SendNotificationAsync(profile.UserId, sqi.DepartmentId, text, sqi.DepartmentNumber, "New Shift", profile);
                    }
                }
                else if (sqi.Type == (int)ShiftQueueTypes.ShiftUpdated)
                {
                    var shift = await _shiftsService.GetShiftByIdAsync(sqi.ShiftId);

                    var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(sqi.DepartmentId);

                    var text = $"Shift {shift.Name} has been updated";

                    foreach (var profile in shift.Personnel)
                    {
                        if (profiles.ContainsKey(profile.UserId))
                        {
                            await _communicationService.SendNotificationAsync(profile.UserId, sqi.DepartmentId, text, sqi.DepartmentNumber, shift.Name, profiles[profile.UserId]);
                        }
                        else
                        {
                            await _communicationService.SendNotificationAsync(profile.UserId, sqi.DepartmentId, text, sqi.DepartmentNumber, shift.Name);
                        }
                    }
                }
                else if (sqi.Type == (int)ShiftQueueTypes.ShiftDaysAdded)
                {
                    var shift = await _shiftsService.GetShiftByIdAsync(sqi.ShiftId);

                    var profiles = await _userProfileService.GetAllProfilesForDepartmentAsync(sqi.DepartmentId);

                    var text = $"Shift {shift.Name} has been updated";

                    foreach (var profile in shift.Personnel)
                    {
                        if (profiles.ContainsKey(profile.UserId))
                        {
                            await _communicationService.SendNotificationAsync(profile.UserId, sqi.DepartmentId, text, sqi.DepartmentNumber, shift.Name, profiles[profile.UserId]);
                        }
                        else
                        {
                            await _communicationService.SendNotificationAsync(profile.UserId, sqi.DepartmentId, text, sqi.DepartmentNumber, shift.Name);
                        }
                    }
                }

                _shiftsService        = null;
                _communicationService = null;
                _userProfileService   = null;
            }

            return(true);
        }
Exemple #12
0
        private async Task StartMonitoring()
        {
            if (SystemBehaviorConfig.ServiceBusType == ServiceBusTypes.Rabbit)
            {
                var callQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                callQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        CallQueueItem cqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            cqi = ObjectSerialization.Deserialize <CallQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (cqi != null)
                            {
                                if (CallQueueReceived != null)
                                {
                                    await CallQueueReceived.Invoke(cqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var messageQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                messageQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        MessageQueueItem mqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            mqi = ObjectSerialization.Deserialize <MessageQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (mqi != null)
                            {
                                if (MessageQueueReceived != null)
                                {
                                    await MessageQueueReceived.Invoke(mqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var distributionListQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                distributionListQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        DistributionListQueueItem dlqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            dlqi = ObjectSerialization.Deserialize <DistributionListQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (dlqi != null)
                            {
                                if (DistributionListQueueReceived != null)
                                {
                                    await DistributionListQueueReceived.Invoke(dlqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var notificationQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                notificationQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        NotificationItem ni = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            ni = ObjectSerialization.Deserialize <NotificationItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (ni != null)
                            {
                                if (NotificationQueueReceived != null)
                                {
                                    await NotificationQueueReceived.Invoke(ni);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var shiftNotificationQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                shiftNotificationQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        ShiftQueueItem sqi = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            sqi = ObjectSerialization.Deserialize <ShiftQueueItem>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (sqi != null)
                            {
                                if (ShiftNotificationQueueReceived != null)
                                {
                                    await ShiftNotificationQueueReceived.Invoke(sqi);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var cqrsEventQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                cqrsEventQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        CqrsEvent cqrs = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            cqrs = ObjectSerialization.Deserialize <CqrsEvent>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (cqrs != null)
                            {
                                if (CqrsEventQueueReceived != null)
                                {
                                    await CqrsEventQueueReceived.Invoke(cqrs);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };

                var paymentEventQueueReceivedConsumer = new EventingBasicConsumer(_channel);
                paymentEventQueueReceivedConsumer.Received += async(model, ea) =>
                {
                    if (ea != null && ea.Body.Length > 0)
                    {
                        CqrsEvent cqrs = null;
                        try
                        {
                            var body    = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            cqrs = ObjectSerialization.Deserialize <CqrsEvent>(message);
                        }
                        catch (Exception ex)
                        {
                            _channel.BasicNack(ea.DeliveryTag, false, false);
                            Logging.LogException(ex, Encoding.UTF8.GetString(ea.Body.ToArray()));
                        }

                        try
                        {
                            if (cqrs != null)
                            {
                                if (PaymentEventQueueReceived != null)
                                {
                                    await PaymentEventQueueReceived.Invoke(cqrs);

                                    _channel.BasicAck(ea.DeliveryTag, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.LogException(ex);
                            if (RetryQueueItem(ea, ex))
                            {
                                _channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                _channel.BasicNack(ea.DeliveryTag, false, true);
                            }
                        }
                    }
                };


                String callQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.CallBroadcastQueueName),
                    autoAck: false,
                    consumer: callQueueReceivedConsumer);

                String messageQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.MessageBroadcastQueueName),
                    autoAck: false,
                    consumer: messageQueueReceivedConsumer);

                String distributionListQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.EmailBroadcastQueueName),
                    autoAck: false,
                    consumer: distributionListQueueReceivedConsumer);

                String notificationQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.NotificaitonBroadcastQueueName),
                    autoAck: false,
                    consumer: notificationQueueReceivedConsumer);

                String shiftNotificationQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.ShiftNotificationsQueueName),
                    autoAck: false,
                    consumer: shiftNotificationQueueReceivedConsumer);

                String cqrsEventQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.SystemQueueName),
                    autoAck: false,
                    consumer: cqrsEventQueueReceivedConsumer);

                String paymentEventQueueReceivedConsumerTag = _channel.BasicConsume(
                    queue: SetQueueNameForEnv(ServiceBusConfig.PaymentQueueName),
                    autoAck: false,
                    consumer: paymentEventQueueReceivedConsumer);
            }
        }