public async Task ShouldCreateJobLogRecord()
        {
            const string jobType = "Foo";
            const string message = "Exception";

            RepositoryMock.Setup(r => r.CreateOrUpdateAsync(It.Is <JobLog>(l
                                                                           => l.JobType == jobType && l.Result == JobExecutionState.Failed && l.Error == message)))
            .ReturnsAsync(true)
            .Verifiable();

            await _handler.Handle(ReportJobState.GetFailed(Guid.NewGuid(), jobType, message, DateTime.UtcNow, DateTime.UtcNow));

            RepositoryMock.Verify();
        }
Exemple #2
0
        private async Task RunJob(IServiceScope scope, Type jobType, Dictionary <string, object> parameters)
        {
            var job       = (IJob)scope.ServiceProvider.GetService(jobType);
            var jobName   = jobType.Name;
            var mediator  = scope.ServiceProvider.GetService <IMediator>();
            var jobId     = Guid.NewGuid();
            var startTime = DateTime.UtcNow;

            try
            {
                await mediator.Execute(ReportJobState.GetRunning(jobId, jobName, startTime));

                _logger.LogInformation($"Executing '{jobName}'");
                var details = await job.Execute(parameters);

                await mediator.Execute(ReportJobState.GetSuccessful(jobId, jobName, startTime, DateTime.UtcNow, details));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error while executing '{jobName}'");
                await mediator.Execute(ReportJobState.GetFailed(jobId, jobName, ex.Message, startTime, DateTime.UtcNow));
            }
        }