Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Foundation/Features/Hangfire/ExampleRecurringJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Hangfire.Console;
using Hangfire.Server;
using System.Threading;

namespace Foundation.Features.Hangfire;

public class ExampleRecurringJob
{
private readonly IContentTypeRepository _contentTypeRepository;


public ExampleRecurringJob(IContentTypeRepository contentTypeRepository)
{
_contentTypeRepository = contentTypeRepository;
}

public void Execute(PerformContext context)
{
context.WriteLine("Hello, world!");
Thread.Sleep(TimeSpan.FromSeconds(1));

context.SetTextColor(ConsoleTextColor.Red);
context.WriteLine("Error! Just joking :)");
Thread.Sleep(TimeSpan.FromSeconds(0.2));
context.ResetTextColor();

var bar = context.WriteProgressBar();
foreach (var contentType in _contentTypeRepository.List().WithProgress(bar))
{
context.WriteLine(contentType.Name);
Thread.Sleep(TimeSpan.FromSeconds(0.3));
}
}
}
12 changes: 12 additions & 0 deletions src/Foundation/Features/Hangfire/HangfireAuthorizationFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Hangfire.Annotations;
using Hangfire.Dashboard;

namespace Foundation.Features.Hangfire;

public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize([NotNull] DashboardContext context)
{
return EPiServer.Security.PrincipalInfo.CurrentPrincipal.IsInRole("CmsAdmins");
}
}
12 changes: 12 additions & 0 deletions src/Foundation/Features/Hangfire/HangfireCmsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Foundation.Features.Hangfire;

[Authorize(Roles = "CmsAdmin,WebAdmins,Administrators")]
[Route("[controller]")]
public class HangfireCmsController : Controller
{
[Route("[action]")]
public ActionResult Index()
{
return View();
}
}
23 changes: 23 additions & 0 deletions src/Foundation/Features/Hangfire/HangfireMenuProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using EPiServer.Authorization;

namespace Foundation.Features.Hangfire;

[MenuProvider]
public class HangfireMenuProvider: IMenuProvider
{
public IEnumerable<MenuItem> GetMenuItems()
{
var hangFireMenuItem = new UrlMenuItem("Hangfire", MenuPaths.Global + "/cms" + "/cmsMenuItem",
"/HangfireCms/index")
{
IsAvailable = request => EPiServer.Security.PrincipalInfo.CurrentPrincipal.IsInRole("CmsAdmins"),
AuthorizationPolicy = CmsPolicyNames.CmsAdmin,
SortIndex = SortIndex.First + 25
};

return new MenuItem[]
{
hangFireMenuItem
};
}
}
39 changes: 39 additions & 0 deletions src/Foundation/Features/Hangfire/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@using EPiServer.Framework.Web.Resources
@using EPiServer.Shell.Navigation

@{
Layout = string.Empty;
}

<!DOCTYPE html>
<html lang="en">

<head>
<title>Hangfire Dashboard</title>
@ClientResources.RenderResources("ShellCore")
@ClientResources.RenderResources("ShellCoreLightTheme")

<style>
html,
body,
.iframe-container {
height: 100%;
}

iframe {
width: 100%;
height: 100%;
}
</style>
</head>

<body>
@Html.CreatePlatformNavigationMenu()
<div @Html.ApplyPlatformNavigation(additionalClass: "iframe-container")>
<iframe src="/episerver/backoffice/Plugins/hangfire" title="Hangfire Dashboard" frameborder="0">
<p>Your browser does not support iframes.</p>
</iframe>
</div>
</body>

</html>
4 changes: 4 additions & 0 deletions src/Foundation/Foundation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />

<!-- Hangfire -->
<PackageReference Include="Hangfire" Version="1.8.14" />
<PackageReference Include="Hangfire.Console" Version="1.4.3" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="lang\**\*" />
Expand Down
28 changes: 28 additions & 0 deletions src/Foundation/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using EPiServer.ServiceApi;
using EPiServer.Shell.Modules;
using Foundation.Features.Checkout.Payments;
using Foundation.Features.Hangfire;
using Foundation.Infrastructure.Cms.ModelBinders;
using Foundation.Infrastructure.Cms.Users;
using Foundation.Infrastructure.Display;
Expand All @@ -24,6 +25,8 @@
using Geta.Optimizely.Categories.Configuration;
using Geta.Optimizely.Categories.Find.Infrastructure.Initialization;
using Geta.Optimizely.Categories.Infrastructure.Initialization;
using Hangfire;
using Hangfire.Console;
using Mediachase.Commerce.Anonymous;
using Mediachase.Commerce.Orders;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -253,6 +256,17 @@ public void ConfigureServices(IServiceCollection services)

// Adds the DAM selector button
services.AddDamSelectButton();

// Add Hangfire services.
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(_configuration.GetConnectionString("EcfSqlConnection"))
.UseConsole());

// Add the processing server as IHostedService
services.AddHangfireServer();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down Expand Up @@ -280,7 +294,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
endpoints.MapControllers();
endpoints.MapRazorPages();
endpoints.MapContent();
endpoints.MapHangfireDashboard();
});

var dashboardOptions = new DashboardOptions
{
Authorization = new[]
{
new HangfireAuthorizationFilter()
},
AppPath = null
};
// Order of middlewares is important! Add it after Authentication and Authorization in order to have a user in the context.
app.UseHangfireDashboard("/episerver/backoffice/Plugins/hangfire", dashboardOptions);

RecurringJob.AddOrUpdate<ExampleRecurringJob>(nameof(ExampleRecurringJob) + "_Id", x => x.Execute(null), Cron.Daily);
}
}
}