Skip to content

Commit

Permalink
Fixes for background jobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
impworks committed Jun 29, 2023
1 parent 7d609d2 commit dc46120
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
3 changes: 0 additions & 3 deletions src/Bonsai/Areas/Admin/Logic/MediaManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ public async Task<MediaUploadResultVM> UploadAsync(MediaUploadRequestVM vm, IFor

_db.Media.Add(media);

if (!handler.IsImmediate)
await _jobs.RunAsync(JobBuilder.For<MediaEncoderJob>().WithArgs(media.Id));

var changeset = await GetChangesetAsync(null, _mapper.Map<MediaEditorVM>(media), id, principal, null);
_db.Changes.Add(changeset);

Expand Down
16 changes: 10 additions & 6 deletions src/Bonsai/Areas/Admin/Logic/Tree/EntireTreeLayoutJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override async Task ExecuteAsync(CancellationToken token)

foreach (var tree in trees)
{
var rendered = await RenderTree(_js, tree, _config.GetDynamicConfig());
var rendered = await RenderTree(_js, tree, _config.GetDynamicConfig(), token);
var layout = new TreeLayout
{
Id = Guid.NewGuid(),
Expand Down Expand Up @@ -241,7 +241,7 @@ private string GetPhoto(string actual, bool gender)
/// <summary>
/// Renders the tree using ELK.js.
/// </summary>
private async Task<string> RenderTree(INodeJSService js, TreeLayoutVM tree, DynamicConfig cfg)
private async Task<string> RenderTree(INodeJSService js, TreeLayoutVM tree, DynamicConfig cfg, CancellationToken token)
{
var thoroughness = Interpolator.MapValue(
cfg.TreeRenderThoroughness,
Expand All @@ -251,7 +251,11 @@ private async Task<string> RenderTree(INodeJSService js, TreeLayoutVM tree, Dyna
);

var json = JsonConvert.SerializeObject(tree);
var result = await js.InvokeFromFileAsync<string>("./External/tree/tree-layout.js", args: new object[] { json, thoroughness });
var result = await js.InvokeFromFileAsync<string>(
"./External/tree/tree-layout.js",
args: new object[] {json, thoroughness},
cancellationToken: token
);

if (string.IsNullOrEmpty(result))
throw new Exception("Failed to render tree: output is empty.");
Expand All @@ -270,9 +274,9 @@ private async Task FlushTreeAsync(AppDbContext db)
{
using var conn = db.GetConnection();
await conn.ExecuteAsync(@"
UPDATE ""Pages"" SET ""TreeLayoutId"" = NULL;
DELETE FROM ""TreeLayouts""
");
UPDATE ""Pages"" SET ""TreeLayoutId"" = NULL;
DELETE FROM ""TreeLayouts""
");
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/Bonsai/Code/DomainModel/Relations/RelationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Bonsai.Data.Models;
using Bonsai.Data.Utils;
using Dapper;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace Bonsai.Code.DomainModel.Relations
Expand Down
4 changes: 4 additions & 0 deletions src/Bonsai/Code/Services/Jobs/BackgroundJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public void Cancel(string key)

var jobsToTerminate = _jobs.Values.Where(x => x.ResourceKey == key).ToList();
foreach (var job in jobsToTerminate)
{
_logger.Information($"Cancelling job {job}.");
job.Cancellation.Cancel();
}
}

#endregion
Expand Down Expand Up @@ -158,6 +161,7 @@ private async Task<JobDescriptor> CreateDescriptorAsync(JobBuilder jb)
Arguments = jb.Args,
ResourceKey = job.GetResourceKey(jb.Args),
Cancellation = new CancellationTokenSource(),
Scope = scope,
JobStateId = state.Id
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/Bonsai/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Reflection;
using Bonsai.Areas.Admin.Logic.MediaHandlers;
using Bonsai.Areas.Admin.Logic.Tree;
using Bonsai.Code.Config;
using Bonsai.Code.Services.Jobs;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -30,7 +31,7 @@ public static void Main(string[] args)
.ConfigureWebHostDefaults(web =>
{
web.UseKestrel()
// .UseUrls("http://0.0.0.0:80/")
.UseUrls("http://0.0.0.0:80/")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIIS()
.UseStartup<Startup>();
Expand All @@ -42,6 +43,7 @@ public static void Main(string[] args)
services.AddHostedService(sp => sp.GetService<BackgroundJobService>());
services.AddTransient<MediaEncoderJob>();
services.AddTransient<EntireTreeLayoutJob>();
})
.Build()
.Run();
Expand Down

0 comments on commit dc46120

Please sign in to comment.