/// <summary> /// Prepare the notification for the handler. /// </summary> /// <param name="context">Background task execution context.</param> public override async Task ExecuteAsync(BackgroundTaskContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } await NotifyAsync(context, context.Value as Envelope); }
/// <summary> /// Setup the task. /// </summary> /// <param name="context">Background task execution context.</param> protected virtual async Task SetupTaskAsync(BackgroundTaskContext context) { TaskContext = context; // We allways want to yield for the async state machine. await Task.Yield(); Logger = ServiceProvider.GetRequiredService <ILogger <BackgroundTask> >(); }
/// <summary> /// Run the background task body. /// </summary> /// <param name="context">Background task execution context.</param> public override async Task ExecuteAsync(BackgroundTaskContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } await SetupTaskAsync(context); HostEnvironment = context.ServiceProvider.GetRequiredService <IHostEnvironment>(); Context = new(context.Id) { CancellationToken = context.CancellationToken, ServiceProvider = context.ServiceProvider, Value = context.Value, QueuedAt = context.QueuedAt, StartedAt = context.StartedAt, Delay = context.Delay, RetryCount = context.RetryCount, KeepWorkspace = HostEnvironment.IsDevelopment(), Failed = false, }; try { await SetupAsync(Context); Context.CancellationToken.ThrowIfCancellationRequested(); await ExecuteCommandAsync(Context); } catch { Context.Failed = true; throw; } finally { await TeardownAsync(Context); } }
public async Task Dequeue_With_Susseful_WorkItemName() { var workItemName = "TestItem"; var context = new BackgroundTaskContext(); var service = new BackgroundTaskQueue(context); service.QueueBackgroundWorkItem(async token => { await new TaskCompletionSource <object>().Task; } , workItemName); var task = await service.DequeueAsync(CancellationToken.None); await task.workItem(CancellationToken.None); Assert.Equal(workItemName, task.workItemName); }
/// <summary> /// Notify a single user. /// </summary> /// <param name="context">The background context.</param> public override async Task ExecuteAsync(BackgroundTaskContext context) { if (context is null) { throw new ArgumentNullException(nameof(context)); } var notificationContext = (NotificationContext)context.Value; // Check if we can reach the user that should be notified. if (!await _notificationRegistrationRepository.ExistsAsync(notificationContext.NotifiedUserId)) { _logger.LogTrace($"Couldn't get notification registration for user {notificationContext.NotifiedUserId}"); return; } var pushDetails = await _userRepository.GetPushDetailsAsync(notificationContext.NotifiedUserId); await _notificationClient.SendNotificationAsync(pushDetails, notificationContext.Notification); _logger.LogTrace($"Sent notification to user {notificationContext.NotifiedUserId}"); }
/// <summary> /// Handle the incoming notification. /// </summary> /// <param name="context">Notification context.</param> /// <param name="envelope">Envelope containing the notification.</param> public abstract Task NotifyAsync(BackgroundTaskContext context, Envelope envelope);
/// <summary> /// Execute synchronous operation. /// </summary> /// <param name="context">Background task execution context.</param> public virtual void Execute(BackgroundTaskContext context) { // NOTE: This is deliberately left empty for any derived class. }
/// <summary> /// Execute asynchronous operation. /// </summary> /// <param name="context">Background task execution context.</param> public virtual async Task ExecuteAsync(BackgroundTaskContext context) { await SetupTaskAsync(context); Execute(context); }