Esempio n. 1
0
        public void Handle(CheckIntervalElapsedMessage message)
        {
            if (Interlocked.CompareExchange(ref _ticket, 1, 0) == 0)
            {
                _taskFactory.StartNew(() =>
                {
                    try
                    {
                        if (!_pluginMetadata.IsSyncronizableProfile)
                        {
                            return;
                        }
                        foreach (var account in _collection)
                        {
                            try
                            {
                                if (account.Profiles.Empty())
                                {
                                    continue;
                                }

                                var queueName            = _transport.GetQueueName(account.Name.Value);
                                var queueIsNotOverloaded = MsmqHelper.QueueIsNotOverloaded(queueName,
                                                                                           "Failed to count messages in queue for account '{0}'"
                                                                                           .Fmt(account.Name.Value), 10);
                                if (!queueIsNotOverloaded)
                                {
                                    continue;
                                }
                                foreach (var profile in account.Profiles)
                                {
                                    var lastSyncDate = profile.Get <LastSyncDate>().FirstOrDefault();
                                    if (IsTimeToSyncronize(profile, lastSyncDate) && profile.Initialized)
                                    {
                                        _bus.SetOut(profile.ConvertToPluginProfile().Name);
                                        _bus.SetOut(account.Name);
                                        _bus.SendLocal(lastSyncDate != null
                                                                                                               ? new TickMessage(lastSyncDate.Value)
                                                                                                               : new TickMessage(null));
                                        ObjectFactory.GetInstance <ILogManager>().GetLogger(GetType()).Info("TickMesage sent");
                                        profile.Get <LastSyncDate>().ReplaceWith(new LastSyncDate(CurrentDate.Value));
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                LogManager.GetLogger(GetType())
                                .Error(string.Format("Failed to send tick message for account '{0}'", account.Name.Value), e);
                            }
                        }
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _ticket);
                    }
                });
            }
        }
Esempio n. 2
0
        public void Init()
        {
            var checkInterval = TimeSpan.FromSeconds(Settings.Default.DefaultCheckInterval);

            _checkTimer = new System.Timers.Timer {
                Interval = checkInterval.TotalMilliseconds
            };
            _checkTimer.Elapsed += (sender, elapsedEventArgs) =>
            {
                try
                {
                    var transport = ObjectFactory.GetInstance <IMsmqTransport>();
                    if (transport != null)
                    {
                        if (transport.QueueIsNotEmpty())
                        {
                            return;
                        }

                        if (
                            !MsmqHelper.QueueIsNotOverloaded(transport.InputQueue,
                                                             "Failed to count messages in main queue '{0}'".Fmt(
                                                                 transport.InputQueue),
                                                             Settings.Default.MessagesInQueueCountThreshold))
                        {
                            return;
                        }
                    }

                    _bus.CleanupOutgoingHeaders();
                    _bus.SendLocal(new CheckIntervalElapsedMessage());
                }
                catch (Exception e)
                {
                    LogManager.GetLogger(GetType()).Fatal(
                        "Failed to send CheckIntervalElapsedMessage. Plugin will not do any synchronization work.", e);
                }
            };

            _checkTimer.Start();

            _infoSenderTimer = new System.Timers.Timer {
                Interval = _infoSendInterval.TotalMilliseconds
            };
            _infoSenderTimer.Elapsed += (sender, elapsedEventArgs) => new PluginInitializer().SendInfoMessages();
            _infoSenderTimer.Start();
        }