Ejemplo n.º 1
0
        private async Task NotifyAsync(NcMonitor monitor, string subject, string message, bool notifyUp)
        {
            TimeSpan?timeInErrorStatus = monitorManager.TimeInErrorStatus(monitor.Id);

            if (notifyUp && timeInErrorStatus != null ||    //ma se odeslat upozorneni o nahozeni, ale monitor je stale v chybovem stavu
                !notifyUp && timeInErrorStatus == null)     //ma se odeslat upozorneni o spadnuti, ale monitor neni v chybovem stavu
            {
                return;
            }

            List <NcChannel> channels;

            if (notifyUp)
            {//oznamit vsem kanalum co j*z byly upozorneni, ze web j*z bezi
                channels = monitorManager.GetChannelsToLastCycleTest(monitor);
            }
            else
            {//oznamit vsem kanalum co jeste nebyly upozorneni a maji byt upozorneni
                channels = monitorManager.GetChannelsBetweenLastErrorAndTestCycle(monitor, timeInErrorStatus ?? TimeSpan.Zero);
            }


            bool foundChannels = channels.Count > 0;

            if (foundChannels)
            {
                foreach (NcChannel channel in channels)
                {
                    ChannelType chnlType = channel.ChannelTypeEnum();
                    Func <NcChannelSubscriber, string> contactSelect;

                    switch (chnlType)
                    {
                    case ChannelType.Email:
                        contactSelect = x => x.User.Email;
                        break;

                    default:
                        logger.LogError($"Method '{nameof(NotifyAsync)}' is not implemented for  '{chnlType.GetType()}.{chnlType}' yet.");
                        continue;
                    }

                    await queue.PushAsync(QueueType.Notification, new NotifyItem
                    {
                        ChannelType = chnlType,
                        Subject     = subject,
                        Message     = message,
                        Contact     = string.Join(";", channel.NcChannelSubscriber.Select(contactSelect)),
                    });
                }

                await monitorManager.SetLastTestCycleIntervalsAsync(monitor, timeInErrorStatus);
            }
        }