Exemple #1
0
        private void ReceiveMessages(out string errorMessage)
        {
            int sumReceived      = 0;
            int messagesReceived = 0;

            errorMessage = string.Empty;

            FileLogger.Log("Start receiving messages from database.");

            try
            {
                IMessageConsumer consumer = Services.GetService <IMessageConsumer>();
                messagesReceived = consumer.ReceiveMessages(Settings.MessagesPerTransaction, out errorMessage);
                sumReceived     += messagesReceived;
                while (messagesReceived > 0)
                {
                    messagesReceived = consumer.ReceiveMessages(Settings.MessagesPerTransaction, out errorMessage);
                    sumReceived     += messagesReceived;
                }
            }
            catch (Exception error)
            {
                errorMessage += (string.IsNullOrEmpty(errorMessage) ? string.Empty : Environment.NewLine)
                                + ExceptionHelper.GetErrorText(error);
            }

            FileLogger.Log(string.Format("{0} messages have been received from database successfully.", sumReceived));
        }
Exemple #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                ReceiveMessages(out string errorMessage);
                if (!string.IsNullOrEmpty(errorMessage))
                {
                    FileLogger.Log(errorMessage);
                    FileLogger.Log(string.Format("Critical error delay of {0} seconds started.", Settings.CriticalErrorDelay));
                    await Task.Delay(Settings.CriticalErrorDelay * 1000, stoppingToken);
                }

                if (Settings.UseNotifications)
                {
                    int resultCode = AwaitNotification(Settings.WaitForNotificationTimeout * 1000);
                    if (resultCode == 1) // notifications are not supported by database
                    {
                        await Task.Delay(Settings.ReceivingMessagesPeriodicity * 1000, stoppingToken);
                    }
                }
                else
                {
                    await Task.Delay(Settings.ReceivingMessagesPeriodicity * 1000, stoppingToken);
                }
            }
        }
        public static void Main(string[] args)
        {
            InitializeAppSettings();

            FileLogger.LogSize = AppSettings.LogSize;

            FileLogger.Log("Hosting service is started.");
            CreateHostBuilder(args).Build().Run();
            FileLogger.Log("Hosting service is stopped.");
        }
 private void LogExceptions()
 {
     if (SendingExceptions.Count > 0)
     {
         while (SendingExceptions.TryDequeue(out Exception error))
         {
             FileLogger.Log(ExceptionHelper.GetErrorText(error));
         }
     }
     SendingExceptions.Clear();
 }
        public void Publish(List <DaJetMessage> batch)
        {
            if (batch == null || batch.Count == 0)
            {
                return;
            }

            if (ConnectionIsBlocked)
            {
                throw new OperationCanceledException("Connection was blocked: sending messages operation is canceled.");
            }

            bool throwException;

            SendingExceptions = new ConcurrentQueue <Exception>();

            ConfigureConnection();
            ConfigureProducerChannels();
            ConfigureProducerQueues(batch);
            AssignProducerQueuesToChannels();

            int messagesSent     = 0;
            int messagesToBeSent = batch.Count;

            try
            {
                messagesSent = PublishMessagesInParallel();

                throwException = (messagesSent != messagesToBeSent);
            }
            catch (Exception error)
            {
                throwException = true;
                SendingExceptions.Enqueue(error);
            }

            LogExceptions();

            if (throwException)
            {
                throw new OperationCanceledException("Sending messages operation was canceled.");
            }
            else
            {
                FileLogger.Log(string.Format("{0} messages have been published successfully.", messagesSent));
            }
        }
        private void HandleConnectionBlocked(object sender, ConnectionBlockedEventArgs args)
        {
            ConnectionIsBlocked = true;
            FileLogger.Log("Connection blocked: " + args.Reason);

            if (SendingCancellation != null)
            {
                try
                {
                    SendingCancellation.Cancel();
                }
                catch
                {
                    // SendingCancellation can be already disposed ...
                }
            }
        }
Exemple #7
0
        private int AwaitNotification(int timeout)
        {
            int    resultCode   = 0;
            string errorMessage = string.Empty;

            FileLogger.Log("Start awaiting notification ...");

            try
            {
                IMessageConsumer consumer = Services.GetService <IMessageConsumer>();
                resultCode = consumer.AwaitNotification(timeout, out errorMessage);
            }
            catch (Exception error)
            {
                errorMessage += (string.IsNullOrEmpty(errorMessage) ? string.Empty : Environment.NewLine)
                                + ExceptionHelper.GetErrorText(error);
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                FileLogger.Log(errorMessage);
            }

            if (resultCode == 0)
            {
                FileLogger.Log("Notification received successfully.");
            }
            else if (resultCode == 1)
            {
                FileLogger.Log("Notifications are not supported.");
            }
            else if (resultCode == 2)
            {
                FileLogger.Log("No notification received.");
            }

            return(resultCode);
        }
 private void HandleConnectionUnblocked(object sender, EventArgs args)
 {
     ConnectionIsBlocked = false;
     FileLogger.Log("Connection unblocked.");
 }
Exemple #9
0
 public override Task StopAsync(CancellationToken cancellationToken)
 {
     FileLogger.Log("Worker is stoped.");
     return(base.StopAsync(cancellationToken));
 }