Add error logging to jobs

This commit is contained in:
Cyberboss
2018-04-23 12:08:49 -04:00
parent fc606fa477
commit f50e981dc6
2 changed files with 17 additions and 15 deletions
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Tgstation.Server.Api.Models.Internal
{
@@ -19,14 +18,18 @@ namespace Tgstation.Server.Api.Models.Internal
/// English description of the <see cref="Job"/>
/// </summary>
[Permissions(DenyWrite = true)]
[Required]
public string Description { get; set; }
/// <summary>
/// Details of any exceptions caught during the <see cref="Job"/>
/// </summary>
[Permissions(DenyWrite = true)]
public string ExceptionDetails { get; set; }
/// <summary>
/// When the <see cref="Job"/> was started
/// </summary>
[Permissions(DenyWrite = true)]
[Required]
public DateTimeOffset StartedAt { get; set; }
/// <summary>
+11 -12
View File
@@ -55,24 +55,23 @@ namespace Tgstation.Server.Host.Core
/// <returns>A <see cref="Task"/> representing the running operation</returns>
async Task RunJob(Job job, Func<CancellationToken, Task> operation, CancellationToken cancellationToken)
{
bool cancelled;
try
{
await operation(cancellationToken).ConfigureAwait(false);
cancelled = false;
}
catch (OperationCanceledException)
{
cancelled = true;
}
using (var scope = serviceProvider.CreateScope())
{
var databaseContext = scope.ServiceProvider.GetRequiredService<IDatabaseContext>();
job = new Job { Id = job.Id };
databaseContext.Jobs.Attach(job);
if (cancelled)
try
{
await operation(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
job.Cancelled = true;
}
catch (Exception e)
{
job.ExceptionDetails = e.ToString();
}
job.StoppedAt = DateTimeOffset.Now;
await databaseContext.Save(default).ConfigureAwait(false);
}