Ejemplo n.º 1
0
        protected Worker(string queueName, int batchSize, int masMaxDegreeOfParallelism, bool singleActiveConsumer,
                         IJobServiceScopeFactory serviceScopeFactory, IJobLogger <Worker> logger)
            : base(batchSize, logger)
        {
            ServiceScopeFactory = serviceScopeFactory;
            ServiceScope        = ServiceScopeFactory.CreateScope();

            _maxDegreeOfParallelism = masMaxDegreeOfParallelism;
            _singleActiveConsumer   = singleActiveConsumer;
            _messageConsumer        = ServiceScope.GetRequiredService <IMessageConsumer>();

            QueueName = queueName;
        }
        public SqlServerMessageReceiver(string queueName, bool singleActiveConsumer, IJobServiceScopeFactory scopeFactory, IJobLogger <SqlServerMessageReceiver <TDbContext> > logger)
        {
            _scopeFactory = scopeFactory;
            _scope        = scopeFactory.CreateScope();

            _queueName            = queueName;
            _singleActiveConsumer = singleActiveConsumer;
            _sqlDialect           = _scope.GetRequiredService <ISqlDialect>();
            _timeProvider         = _scope.GetRequiredService <ITimeProvider>();
            _logger       = logger;
            _instanceName = $"{Environment.MachineName}/{Guid.NewGuid()}";

            _timer              = _scope.GetRequiredService <ITimer>();
            _timer.TimeElapsed += TimerCallback;
        }
Ejemplo n.º 3
0
        public WorkerCoordinator(IJobServiceFactory serviceFactory, IJobLogger <WorkerCoordinator> logger = null)
        {
            ServiceScopeFactory = serviceFactory.GetRequiredService <IJobServiceScopeFactory>();
            ServiceScope        = ServiceScopeFactory.CreateScope();

            _settings = ServiceScope.GetRequiredService <MassiveJobsSettings>();

            _reconnectTimer              = ServiceScope.GetRequiredService <ITimer>();
            _reconnectTimer.TimeElapsed += Reconnect;

            Workers = new List <IWorker>();
            Logger  = logger ?? ServiceScope.GetRequiredService <IJobLogger <WorkerCoordinator> >();

            MessageConsumer = ServiceScope.GetRequiredService <IMessageConsumer>();
            MessageConsumer.Disconnected += MessageBrokerDisconnected;
        }
Ejemplo n.º 4
0
        protected void RunJobs(IReadOnlyList <RawMessage> batch, CancellationToken cancellationToken)
        {
            var parallelOptions = new ParallelOptions
            {
                CancellationToken      = cancellationToken,
                MaxDegreeOfParallelism = _maxDegreeOfParallelism
            };

            try
            {
                Parallel.ForEach(batch, parallelOptions, msg =>
                {
                    using (var scope = ServiceScopeFactory.CreateScope())
                    {
                        if (!TryDeserializeJob(msg, scope, out var job))
                        {
                            throw new Exception($"Unknown job type: {msg.TypeTag}.");
                        }

                        var jobRunner    = scope.GetRequiredService <IJobRunner>();
                        var jobPublisher = scope.GetRequiredService <IJobPublisher>();

                        jobRunner.RunJob(jobPublisher, _messageReceiver, job, msg.DeliveryTag, scope, cancellationToken);
                    }
                });
            }
            catch (OperationCanceledException ex)
            {
                // ignore errors caused by stopping this worker
                if (!cancellationToken.IsCancellationRequested)
                {
                    throw;
                }

                Logger.LogDebug(ex, "Cancelled parallel jobs run");
            }
        }