Esempio n. 1
0
        public void Start()
        {
            lock (_startStopLock)
            {
                if (_processorThread != null)
                {
                    return;
                }

                OnStart();

                _stoppingSignal.Reset();

                Logger.LogDebug($"Starting batch processor");

                _cancellationTokenSource     = new CancellationTokenSource();
                _jobsCancellationTokenSource = new CancellationTokenSource();

                _processorThread = new Thread(ProcessorFunction)
                {
                    IsBackground = true
                };
                _processorThread.Start();

                Logger.LogDebug($"Batch processor started");
            }
        }
Esempio n. 2
0
        public static void SafeDispose(this IDisposable disposable, IJobLogger logger = null)
        {
            try
            {
                if (disposable == null)
                {
                    return;
                }

                if (logger?.IsEnabled(JobLogLevel.Debug) ?? false)
                {
                    logger.LogDebug($"Disposing {disposable.GetType()}");
                }

                disposable.Dispose();

                if (logger?.IsEnabled(JobLogLevel.Debug) ?? false)
                {
                    logger.LogDebug($"Disposed {disposable.GetType()}");
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Disposal error");
            }
        }
Esempio n. 3
0
        private void StopJobWorkers(bool reconnect, bool cancelRunningJobs)
        {
            lock (WorkersLock)
            {
                _reconnectTimer.Change(Timeout.Infinite, Timeout.Infinite);

                try
                {
                    foreach (var worker in Workers)
                    {
                        try
                        {
                            worker.BeginStop(cancelRunningJobs);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex, $"Begin stop for a worker failed");
                        }
                    }

                    foreach (var worker in Workers)
                    {
                        try
                        {
                            worker.WaitUntilStopped();
                            worker.SafeDispose(Logger);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex, $"Stopping a worker failed");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Stopping workers failed");
                }

                Workers.Clear();

                if (reconnect)
                {
                    if (_reconnectTimeIndex < _reconnectTimes.Length - 1)
                    {
                        _reconnectTimeIndex++;
                    }

                    _reconnectTimer.Change(_reconnectTimes[_reconnectTimeIndex] * 1000, Timeout.Infinite);
                    Logger.LogDebug("Reconnect timer started");
                }
            }
        }
        protected void EnsureConnectionExists()
        {
            lock (_connectionLock)
            {
                if (Connection != null)
                {
                    return;
                }

                Logger.LogDebug("Connecting...");

                try
                {
                    Connection = _connectionFactory.CreateConnection(_rabbitMqSettings.HostNames, $"MassiveJobs.NET/{GetEntryFileName()}");

                    Connection.CallbackException   += ConnectionOnCallbackException;
                    Connection.ConnectionBlocked   += ConnectionOnConnectionBlocked;
                    Connection.ConnectionUnblocked += ConnectionOnConnectionUnblocked;
                    Connection.ConnectionShutdown  += ConnectionOnConnectionShutdown;

                    ModelPool = new ModelPool(Connection, 2, Logger);

                    var model = ModelPool.Get();
                    try
                    {
                        DeclareTopology(model.Model);
                    }
                    finally
                    {
                        ModelPool.Return(model);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Failed connection create");
                    throw;
                }
            }

            Logger.LogWarning("Connected");
        }