public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            _stopwatches[context.FireInstanceId].Stop();

            var endDocument = new JobDataEndDocument
            {
                JobId               = context.FireInstanceId,
                JobType             = context.JobDetail.JobType.Name,
                JobName             = context.JobDetail.Key.Name,
                EndDate             = DateTime.Now,
                ElapsedMilliseconds = _stopwatches[context.FireInstanceId].ElapsedMilliseconds
            };

            await jobDataSource.Finish(endDocument);

            if (jobException != null)
            {
                var logDocument = new JobDataLogDocument
                {
                    JobId       = context.FireInstanceId,
                    Level       = "Error",
                    JobName     = context.JobDetail.Key.Name,
                    Description = jobException.ToString(),
                    CreatedDate = DateTime.Now
                };

                await jobDataSource.Log(logDocument);

                await RetryJob(context);
            }

            _stopwatches.Remove(context.FireInstanceId);
        }
Example #2
0
        public Task Finish(JobDataEndDocument document)
        {
            var history = histories.FirstOrDefault((x) => x.JobId == document.JobId);

            if (history != null)
            {
                history.EndDate             = document.EndDate;
                history.ElapsedMilliseconds = document.ElapsedMilliseconds;
            }

            var description = $"{document.JobName} finished executing in {document.EndDate.ToString("dddd, dd MMMM yyyy HH:mm:ss")}. JobId: {document.JobId}. Total Elapsed Milliseconds: {document.ElapsedMilliseconds}.";

            logs.Add(new JobDataLogDocument
            {
                JobId       = document.JobId,
                JobName     = document.JobName,
                Level       = "Information",
                Description = description,
                CreatedDate = DateTime.Now
            });

            return(Task.CompletedTask);
        }