mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 07:04:57 +01:00
Add error logging to jobs
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user