public void Init() { _cts = new CancellationTokenSource(TimeSpan.FromSeconds(15)); _loggerFactory = Substitute.For <ILoggerFactory>(); _logger = Substitute.For <ILogger <QuidjiboServer> >(); _loggerFactory.CreateLogger <QuidjiboServer>().Returns(_logger); _quidjiboConfiguration = Substitute.For <IQuidjiboConfiguration>(); _workProviderFactory = Substitute.For <IWorkProviderFactory>(); _workProvider = Substitute.For <IWorkProvider>(); _workProviderFactory.CreateAsync(Arg.Any <string>(), Arg.Any <CancellationToken>()).Returns(Task.FromResult(_workProvider)); _workProviderFactory.PollingInterval.Returns(1); _scheduleProviderFactory = Substitute.For <IScheduleProviderFactory>(); _scheduleProvider = Substitute.For <IScheduleProvider>(); _scheduleProviderFactory.CreateAsync(Arg.Any <string[]>(), Arg.Any <CancellationToken>()).Returns(Task.FromResult(_scheduleProvider)); _scheduleProviderFactory.PollingInterval.Returns(1); _progressProviderFactory = Substitute.For <IProgressProviderFactory>(); _progressProvider = Substitute.For <IProgressProvider>(); _progressProviderFactory.CreateAsync(Arg.Any <string[]>(), Arg.Any <CancellationToken>()).Returns(Task.FromResult(_progressProvider)); Substitute.For <IWorkDispatcher>(); Substitute.For <IPayloadSerializer>(); _cronProvider = Substitute.For <ICronProvider>(); _pipeline = Substitute.For <IQuidjiboPipeline>(); _sut = new QuidjiboServer(_loggerFactory, _quidjiboConfiguration, _workProviderFactory, _scheduleProviderFactory, _progressProviderFactory, _cronProvider, _pipeline); }
private async Task ScheduleLoopAsync(string[] queues) { var pollingInterval = TimeSpan.FromSeconds(_scheduleProviderFactory.PollingInterval); var scheduleProvider = await _scheduleProviderFactory.CreateAsync(queues, _cts.Token); while (!_cts.IsCancellationRequested) { try { var items = await scheduleProvider.ReceiveAsync(_cts.Token); foreach (var item in items) { var work = new WorkItem { Attempts = 0, CorrelationId = Guid.NewGuid(), Id = Guid.NewGuid(), Name = item.Name, Payload = item.Payload, Queue = item.Queue, ScheduleId = item.Id }; _logger.LogDebug("Enqueue the scheduled item : {0}", item.Id); var workProvider = await _workProviderFactory.CreateAsync(work.Queue, _cts.Token); await workProvider.SendAsync(work, 1, _cts.Token); _logger.LogDebug("Update the schedule for the next run : {0}", item.Id); item.EnqueueOn = _cronProvider.GetNextSchedule(item.CronExpression); item.EnqueuedOn = DateTime.UtcNow; await scheduleProvider.CompleteAsync(item, _cts.Token); } } catch (Exception exception) { _logger.LogWarning(0, exception, exception.Message); } finally { await Task.Delay(pollingInterval, _cts.Token); } } }
private Task <IScheduleProvider> GetOrCreateScheduleProvider(string queueName, CancellationToken cancellationToken) { return(_scheduleProviderFactory.CreateAsync(queueName, cancellationToken)); }