public async Task ItWorks() { var stopwatch = Stopwatch.StartNew(); var events = new ConcurrentQueue<TimeSpan>(); var task = new AsyncTask("test task", async () => { events.Enqueue(stopwatch.Elapsed); }, new ConsoleLoggerFactory(false)) { Interval = TimeSpan.FromSeconds(0.2) }; using (task) { task.Start(); await Task.Delay(1199); } await Task.Delay(TimeSpan.FromSeconds(0.7)); Console.WriteLine(string.Join(Environment.NewLine, events)); Assert.That(events.Count, Is.GreaterThanOrEqualTo(5)); Assert.That(events.Count, Is.LessThanOrEqualTo(7)); }
public async Task DoesNotDieOnTransientErrors() { var throwException = true; var taskWasCompleted = false; using (var task = new AsyncTask("bimse", async () => { if (throwException) { throw new Exception("but you told me to do it!"); } taskWasCompleted = true; }, new ConsoleLoggerFactory(false)) { Interval = TimeSpan.FromMilliseconds(400) }) { Console.WriteLine("Starting the task..."); task.Start(); Console.WriteLine("Waiting for task to run a little..."); await Task.Delay(TimeSpan.FromSeconds(1)); Console.WriteLine("Suddenly, the transient error disappears..."); throwException = false; Console.WriteLine("and life goes on..."); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.That(taskWasCompleted, Is.True, "The task did NOT resume properly after experiencing exceptions!"); } }
/// <summary> /// Constructs the in-mem error tracker with the configured number of delivery attempts as the MAX /// </summary> public InMemErrorTracker(int maxDeliveryAttempts) { _maxDeliveryAttempts = maxDeliveryAttempts; _cleanupOldTrackedErrorsTask = new AsyncTask(BackgroundTaskName, CleanupOldTrackedErrors) { Interval = TimeSpan.FromMinutes(1) }; }
/// <summary> /// Constructs the in-mem error tracker with the configured number of delivery attempts as the MAX /// </summary> public InMemErrorTracker(int maxDeliveryAttempts, IRebusLoggerFactory rebusLoggerFactory) { _maxDeliveryAttempts = maxDeliveryAttempts; _log = rebusLoggerFactory.GetCurrentClassLogger(); _cleanupOldTrackedErrorsTask = new AsyncTask(BackgroundTaskName, CleanupOldTrackedErrors, rebusLoggerFactory) { Interval = TimeSpan.FromMinutes(1) }; }
/// <summary> /// Constructs the step, using the given transport and settings /// </summary> public SimpleRetryStrategyStep(ITransport transport, SimpleRetryStrategySettings simpleRetryStrategySettings) { _transport = transport; _simpleRetryStrategySettings = simpleRetryStrategySettings; _cleanupOldTrackedErrorsTask = new AsyncTask(BackgroundTaskName, CleanupOldTrackedErrors) { Interval = TimeSpan.FromMinutes(1) }; }
/// <summary> /// Constructs the step, using the specified <see cref="ITimeoutManager"/> to defer relevant messages /// and the specified <see cref="ITransport"/> to deliver messages when they're due. /// </summary> public HandleDeferredMessagesStep(ITimeoutManager timeoutManager, ITransport transport) { _timeoutManager = timeoutManager; _transport = transport; _dueMessagesSenderBackgroundTask = new AsyncTask("DueMessagesSender", TimerElapsed) { Interval = TimeSpan.FromSeconds(1) }; }
/// <summary> /// Constructs the step, using the specified <see cref="ITimeoutManager"/> to defer relevant messages /// and the specified <see cref="ITransport"/> to deliver messages when they're due. /// </summary> public HandleDeferredMessagesStep(ITimeoutManager timeoutManager, ITransport transport, Options options, IRebusLoggerFactory rebusLoggerFactory) { _timeoutManager = timeoutManager; _transport = transport; _options = options; _log = rebusLoggerFactory.GetCurrentClassLogger(); _dueMessagesSenderBackgroundTask = new AsyncTask(DueMessagesSenderTaskName, TimerElapsed, rebusLoggerFactory) { Interval = TimeSpan.FromSeconds(1) }; }
/// <summary> /// Constructs the transport with the given <see cref="IDbConnectionProvider"/>, using the specified <paramref name="tableName"/> to send/receive messages, /// querying for messages with recipient = <paramref name="inputQueueName"/> /// </summary> public SqlServerTransport(IDbConnectionProvider connectionProvider, string tableName, string inputQueueName) { _connectionProvider = connectionProvider; _tableName = tableName; _inputQueueName = inputQueueName; ExpiredMessagesCleanupInterval = DefaultExpiredMessagesCleanupInterval; _expiredMessagesCleanupTask = new AsyncTask("ExpiredMessagesCleanup", PerformExpiredMessagesCleanupCycle) { Interval = TimeSpan.FromMinutes(1) }; }
/// <summary> /// Constructs the transport with the given <see cref="IDbConnectionProvider"/>, using the specified <paramref name="tableName"/> to send/receive messages, /// querying for messages with recipient = <paramref name="inputQueueName"/> /// </summary> public SqlServerTransport(IDbConnectionProvider connectionProvider, string tableName, string inputQueueName, IRebusLoggerFactory rebusLoggerFactory) { _connectionProvider = connectionProvider; _tableName = tableName; _inputQueueName = inputQueueName; _log = rebusLoggerFactory.GetCurrentClassLogger(); ExpiredMessagesCleanupInterval = DefaultExpiredMessagesCleanupInterval; _expiredMessagesCleanupTask = new AsyncTask("ExpiredMessagesCleanup", PerformExpiredMessagesCleanupCycle, rebusLoggerFactory) { Interval = TimeSpan.FromMinutes(1) }; }
public async Task CanActuallyStopTaskWithLongInterval(int secondsToLetTheTaskRun) { using (var task = new AsyncTask("simulate-azure-service-bus-peek-lock-renewer", async () => { Console.WriteLine("INVOKED!!!"); }, new ConsoleLoggerFactory(false)) { Interval = TimeSpan.FromMinutes(4.5) }) { task.Start(); Console.WriteLine($"Letting the task run for {secondsToLetTheTaskRun} seconds..."); await Task.Delay(TimeSpan.FromSeconds(secondsToLetTheTaskRun)); Console.WriteLine("Quitting...."); } Console.WriteLine("Done!"); }
private AsyncTask CreateRenewalTaskForMessage(Message message, AmazonSQSClient client) { var renewalTask = new AsyncTask(string.Format("RenewPeekLock-{0}", message.MessageId), async () => { _log.Info("Renewing peek lock for message with ID {0}", message.MessageId); await client.ChangeMessageVisibilityAsync(new ChangeMessageVisibilityRequest(_queueUrl, message.ReceiptHandle, (int)_peekLockDuration.TotalSeconds)); }, prettyInsignificant: true) { Interval = _peekLockRenewalInterval }; return renewalTask; }
IDisposable GetRenewalTaskOrFakeDisposable(string messageId, BrokeredMessage brokeredMessage, TimeSpan lockRenewalInterval) { if (_automaticallyRenewPeekLock) { var renewalTask = new AsyncTask(string.Format("RenewPeekLock-{0}", messageId), async () => { _log.Info("Renewing peek lock for message with ID {0}", messageId); await GetRetrier().Execute(brokeredMessage.RenewLockAsync); }, prettyInsignificant: true) { Interval = lockRenewalInterval }; renewalTask.Start(); return renewalTask; } return new FakeDisposable(); }
public AsyncTaskWorker(string name, ITransport transport, IPipeline pipeline, IPipelineInvoker pipelineInvoker, int maxParallelismPerWorker, IRebusLoggerFactory rebusLoggerFactory) { _transport = transport; _pipeline = pipeline; _pipelineInvoker = pipelineInvoker; _parallelOperationsManager = new ParallelOperationsManager(maxParallelismPerWorker); _log = rebusLoggerFactory.GetCurrentClassLogger(); Name = name; _workerTask = new AsyncTask(name, DoWork, new ConsoleLoggerFactory(false), prettyInsignificant: true) { Interval = TimeSpan.FromMilliseconds(1) }; _log.Debug("Starting (task-based) worker {0}", Name); _workerTask.Start(); }
IDisposable GetRenewalTaskOrFakeDisposable(string messageId, BrokeredMessage brokeredMessage, TimeSpan lockRenewalInterval) { if (!AutomaticallyRenewPeekLock) { return new FakeDisposable(); } if (_prefetchingEnabled) { return new FakeDisposable(); } var renewalTask = new AsyncTask($"RenewPeekLock-{messageId}", async () => { _log.Info("Renewing peek lock for message with ID {0}", messageId); await brokeredMessage.RenewLockAsync(); }, _rebusLoggerFactory, prettyInsignificant: true) { Interval = lockRenewalInterval }; renewalTask.Start(); return renewalTask; }