private Task Watch(ILongRunningTask longRunningHandler, BrokeredMessage message) { _logger.Debug("Starting long-running task wrapper for message {MessageId}", message.MessageId); var task = _taskFactory.StartNew(() => WatchHandlerTask(longRunningHandler, message), TaskContext.LongRunningTaskWatcher).Unwrap(); return(task); }
public LongRunningTaskWrapper(Task <T> handlerTask, ILongRunningTask longRunningHandler, BrokeredMessage message, IClock clock, ILogger logger, TimeSpan messageLockDuration, INimbusTaskFactory taskFactory) : base(handlerTask, longRunningHandler, message, clock, logger, taskFactory, messageLockDuration) { }
protected LongRunningTaskWrapperBase(Task handlerTask, ILongRunningTask longRunningHandler, BrokeredMessage message, IClock clock, IZombusLogger logger, TimeSpan messageLockDuration) { HandlerTask = handlerTask; _longRunningHandler = longRunningHandler; _message = message; _clock = clock; _logger = logger; _messageLockDuration = messageLockDuration; _logger.Debug("Long-lived task wrapper created for message {0}", message.MessageId); }
protected LongRunningTaskWrapperBase(Task handlerTask, ILongRunningTask longRunningHandler, BrokeredMessage message, IClock clock, ILogger logger, INimbusTaskFactory taskFactory, TimeSpan messageLockDuration) { HandlerTask = handlerTask; _longRunningHandler = longRunningHandler; _message = message; _clock = clock; _logger = logger; _messageLockDuration = messageLockDuration; _taskFactory = taskFactory; _logger.Debug("Long-lived task wrapper created for message {MessageId}", message.MessageId); }
protected override void OnHandleWork(Intent p0) { var parcelable = (UploadLongRunningTaskParcelable)p0.GetParcelableExtra("task"); longTask = parcelable.Task; _cts = new CancellationTokenSource(); Task.Run(async() => { try { //INVOKE THE SHARED CODE //var counter = new TaskCounter(); //counter.RunCounter(_cts.Token).Wait(); if (longTask != null) { await longTask.Run(_cts.Token); } } catch (System.OperationCanceledException ex) { Console.WriteLine(ex.Message); } finally { if (_cts.IsCancellationRequested) { //var message = new CancelledMessage(); //Device.BeginInvokeOnMainThread( // () => MessagingCenter.Send(message, "CancelledMessage") //); } } }, _cts.Token); }
private async Task WatchHandlerTask(ILongRunningTask longRunningHandler, BrokeredMessage message) { _logger.Debug("Started long-running task wrapper for message {MessageId}", message.MessageId); while (true) { var now = _clock.UtcNow; var lockedUntil = LockedUntilUtcStrategy(message); var remainingLockTime = lockedUntil.Subtract(now); if (remainingLockTime < TimeSpan.Zero) { // oops. Missed that boat :| _logger.Warn( "Long-running task wrapper {HandlerType} for message {MessageId} woke up too late (had {LockTimeRemaining} seconds remaining when it woke up).", longRunningHandler.GetType().FullName, message.MessageId, remainingLockTime); return; } var acceptableRemainingLockDuration = TimeSpan.FromMilliseconds(_messageLockDuration.TotalMilliseconds*_acceptableRemainingLockProportion); var remainingTimeBeforeRenewalRequired = remainingLockTime - acceptableRemainingLockDuration; var timeToDelay = remainingTimeBeforeRenewalRequired <= TimeSpan.Zero ? TimeSpan.Zero : remainingTimeBeforeRenewalRequired; if (timeToDelay > TimeSpan.Zero) { _logger.Debug( "Sleeping for {SleepDuration} before checking whether lock for message {MessageId} requires renewal (currently has {LockTimeRemaining} remaining seconds).", timeToDelay, message.MessageId, remainingLockTime); await Task.Delay(timeToDelay); //Thread.Sleep(timeToDelay); } object dispatchComplete; if (message.Properties.TryGetValue(MessagePropertyKeys.DispatchComplete, out dispatchComplete) && dispatchComplete as bool? == true) { //_logger.Debug("Long-running task wrapper awoke after message {0} had already been dispatched. Nothing to see here.", message.MessageId); return; } object dispatchAbandoned; if (message.Properties.TryGetValue(MessagePropertyKeys.DispatchAbandoned, out dispatchAbandoned) && dispatchAbandoned as bool? == true) { //_logger.Debug("Long-running task wrapper awoke after message {0} had already been dispatched. Nothing to see here.", message.MessageId); return; } _logger.Info( "Long-running handler {HandlerType} for message {MessageId} requires a lock renewal ({LockTimeRemaining} seconds remaining; {LockTimeRequired} required).", longRunningHandler.GetType().FullName, message.MessageId, lockedUntil.Subtract(now), acceptableRemainingLockDuration); if (!longRunningHandler.IsAlive) throw new BusException("Long-running handler died or stopped responding."); try { await RenewLockStrategy(message); _logger.Debug("Long-running handler {HandlerType} for message {MessageId} renewed its lock (now has {LockTimeRemaining} seconds remaining).", longRunningHandler.GetType().FullName, message.MessageId, LockedUntilUtcStrategy(message).Subtract(now)); } catch (Exception exc) { _logger.Error(exc, "Long-running handler {HandlerType} for message {MessageId} failed to renew its lock (had {LockTimeRemaining} seconds remaining when it attempted to).", longRunningHandler.GetType().FullName, message.MessageId, remainingLockTime); throw; } } }
private Task Watch(ILongRunningTask longRunningHandler, BrokeredMessage message) { _logger.Debug("Starting long-running task wrapper for message {MessageId}", message.MessageId); var task = _taskFactory.StartNew(() => WatchHandlerTask(longRunningHandler, message), TaskContext.LongRunningTaskWatcher).Unwrap(); return task; }
private Task Watch(ILongRunningTask longRunningHandler, BrokeredMessage message) { _logger.Debug("Starting long-running task wrapper for message {0}", message.MessageId); var task = Task.Run(() => WatchHandlerTask(longRunningHandler, message)); return task; }
public LongRunningTask(ILongRunningTask task) { longTask = task; }
private async Task WatchHandlerTask(ILongRunningTask longRunningHandler, BrokeredMessage message) { _logger.Debug("Started long-running task wrapper for message {MessageId}", message.MessageId); while (true) { var now = _clock.UtcNow; var lockedUntil = LockedUntilUtcStrategy(message); var remainingLockTime = lockedUntil.Subtract(now); if (remainingLockTime < TimeSpan.Zero) { // oops. Missed that boat :| _logger.Warn( "Long-running task wrapper {HandlerType} for message {MessageId} woke up too late (had {LockTimeRemaining} seconds remaining when it woke up).", longRunningHandler.GetType().FullName, message.MessageId, remainingLockTime); return; } var acceptableRemainingLockDuration = TimeSpan.FromMilliseconds(_messageLockDuration.TotalMilliseconds * _acceptableRemainingLockProportion); var remainingTimeBeforeRenewalRequired = remainingLockTime - acceptableRemainingLockDuration; var timeToDelay = remainingTimeBeforeRenewalRequired <= TimeSpan.Zero ? TimeSpan.Zero : remainingTimeBeforeRenewalRequired; if (timeToDelay > TimeSpan.Zero) { _logger.Debug( "Sleeping for {SleepDuration} before checking whether lock for message {MessageId} requires renewal (currently has {LockTimeRemaining} remaining seconds).", timeToDelay, message.MessageId, remainingLockTime); await Task.Delay(timeToDelay); //Thread.Sleep(timeToDelay); } object dispatchComplete; if (message.Properties.TryGetValue(MessagePropertyKeys.DispatchComplete, out dispatchComplete) && dispatchComplete as bool? == true) { //_logger.Debug("Long-running task wrapper awoke after message {0} had already been dispatched. Nothing to see here.", message.MessageId); return; } _logger.Info( "Long-running handler {HandlerType} for message {MessageId} requires a lock renewal ({LockTimeRemaining} seconds remaining; {LockTimeRequired} required).", longRunningHandler.GetType().FullName, message.MessageId, lockedUntil.Subtract(now), acceptableRemainingLockDuration); if (!longRunningHandler.IsAlive) { throw new BusException("Long-running handler died or stopped responding."); } try { await RenewLockStrategy(message); _logger.Debug("Long-running handler {HandlerType} for message {MessageId} renewed its lock (now has {LockTimeRemaining} seconds remaining).", longRunningHandler.GetType().FullName, message.MessageId, LockedUntilUtcStrategy(message).Subtract(now)); } catch (Exception exc) { _logger.Error(exc, "Long-running handler {HandlerType} for message {MessageId} failed to renew its lock (had {LockTimeRemaining} seconds remaining when it attempted to).", longRunningHandler.GetType().FullName, message.MessageId, remainingLockTime); throw; } } }