/// <inheritdoc/> public async Task CleanupAsync(IWrappedJobCancellationToken ct) { Ensure.ArgumentNotNull(ct, nameof(ct)); try { var cts = CancellationTokenSource.CreateLinkedTokenSource(ct.ShutdownToken); if (configuration.Options.CleanupPeriod > 0) { cts.CancelAfter(configuration.Options.CleanupPeriod * 1000); // TODO: cleanup events as well var command = new ClearObjectsCommand() { RefDate = DateTime.Now.AddMinutes(-configuration.Options.CleanupPeriod) }; await mediator.Send(command, cts.Token); } } catch (OperationCanceledException) { logger.LogInformation($"Cleanup canceled."); } catch (Exception e) { logger.LogWarning(e, $"Cleanup failed. {e.Message}"); } }
/// <inheritdoc/> public async Task ProcessAsync(string webhookId, string eventId, IWrappedJobCancellationToken ct) { Ensure.ArgumentNotNullOrWhiteSpace(webhookId, nameof(webhookId)); Ensure.ArgumentNotNullOrWhiteSpace(eventId, nameof(eventId)); Ensure.ArgumentNotNull(ct, nameof(ct)); var cts = CancellationTokenSource.CreateLinkedTokenSource(ct.ShutdownToken); if (configuration?.Options?.RequestTimeoutMilliseconds > 0) { cts.CancelAfter(configuration.Options.RequestTimeoutMilliseconds); } var webhookModel = await mediator.Send( new Entities.Webhooks.Queries.GetById.GetByIdQuery() { Id = webhookId }, cts.Token); if (webhookModel == null) { throw new UnexpectedNullException("The webhook could not be retrieved."); } var eventModel = await mediator.Send( new Entities.Events.Queries.GetById.GetByIdQuery() { Id = eventId }, cts.Token); if (eventModel == null) { throw new UnexpectedNullException("The event could not be retrieved."); } // TODO: set signature in the header var response = await httpClient.PostAsync(webhookModel.Url, eventModel, cts.Token); if (response == null) { throw new UnexpectedNullException("The webhook response is null."); } if (!response.IsSuccessStatusCode) { throw new AmiException($"The webhook response is not successful '{response.StatusCode.ToString()}'. " + $"WebhookId: {webhookId} EventId: {eventId}"); } }
/// <inheritdoc/> public async Task ProcessAsync(string id, IWrappedJobCancellationToken ct) { Ensure.ArgumentNotNullOrWhiteSpace(id, nameof(id)); Ensure.ArgumentNotNull(ct, nameof(ct)); try { var cts = CancellationTokenSource.CreateLinkedTokenSource(ct.ShutdownToken); if (configuration?.Options?.TimeoutMilliseconds > 0) { cts.CancelAfter(configuration.Options.TimeoutMilliseconds); } var task = await mediator.Send(new GetByIdQuery { Id = id }); if (task == null) { throw new UnexpectedNullException("Task not found."); } if (task.Command == null) { await UpdateStatus(mediator, task, Domain.Enums.TaskStatus.Finished, string.Empty); } else { await UpdateStatus(mediator, task, Domain.Enums.TaskStatus.Processing, string.Empty); switch (task.Command.CommandType) { case CommandType.ProcessObjectCommand: await ProcessObjectAsync(mediator, task, cts.Token); break; default: await UpdateStatus(mediator, task, Domain.Enums.TaskStatus.Finished, string.Empty); break; } } } catch (Exception e) { // Task status could not be updated. logger.LogCritical(e, e.Message); } }