Skip to content

Commit 358bdb3

Browse files
committed
Tidied up usings and implemented file-scoped namespaces
1 parent c9f5b0d commit 358bdb3

20 files changed

+396
-546
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.cs]
2+
csharp_style_namespace_declarations = file_scoped:warning
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
using Cofoundry.Core.BackgroundTasks;
2-
using Cofoundry.Core.Configuration;
32
using Cofoundry.Core.DependencyInjection;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Text;
8-
using System.Threading.Tasks;
93

10-
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire
4+
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire;
5+
6+
public class HangfireBackgroundTasksDependencyRegistration : IDependencyRegistration
117
{
12-
public class HangfireBackgroundTasksDependencyRegistration : IDependencyRegistration
8+
public void Register(IContainerRegister container)
139
{
14-
public void Register(IContainerRegister container)
15-
{
16-
container
17-
.Register<IHangfireBackgroundTaskInitializer, HangfireBackgroundTaskInitializer>()
18-
.Register<IHangfireServerInitializer, HangfireServerInitializer>()
19-
.Register<IBackgroundTaskScheduler, HangfireBackgroundTaskScheduler>()
20-
;
21-
}
10+
container
11+
.Register<IHangfireBackgroundTaskInitializer, HangfireBackgroundTaskInitializer>()
12+
.Register<IHangfireServerInitializer, HangfireServerInitializer>()
13+
.Register<IBackgroundTaskScheduler, HangfireBackgroundTaskScheduler>()
14+
;
2215
}
2316
}
Lines changed: 115 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,170 @@
11
using Cofoundry.Core.BackgroundTasks;
22
using Hangfire;
3-
using System;
43

5-
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire
4+
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire;
5+
6+
public class HangfireBackgroundTaskScheduler : IBackgroundTaskScheduler
67
{
7-
public class HangfireBackgroundTaskScheduler : IBackgroundTaskScheduler
8+
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int days, int atHour, int atMinute) where TTask : IRecurringBackgroundTask
89
{
9-
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int days, int atHour, int atMinute) where TTask : IRecurringBackgroundTask
10-
{
11-
ValidateDailyTaskParameters(days, atHour, atMinute);
10+
ValidateDailyTaskParameters(days, atHour, atMinute);
1211

13-
string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute);
14-
RegisterRecurringTask<TTask>(cronExpression);
12+
string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute);
13+
RegisterRecurringTask<TTask>(cronExpression);
1514

16-
return this;
17-
}
15+
return this;
16+
}
1817

19-
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IRecurringBackgroundTask
18+
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IRecurringBackgroundTask
19+
{
20+
ValidateHourlyTaskParameters(hours, minute);
21+
22+
if (hours == 0 && minute > 0)
2023
{
21-
ValidateHourlyTaskParameters(hours, minute);
24+
return RegisterRecurringTask<TTask>(minute);
25+
}
2226

23-
if (hours == 0 && minute > 0)
24-
{
25-
return RegisterRecurringTask<TTask>(minute);
26-
}
27+
string cronExpression = string.Format("{1} */{0} * * *", hours, minute);
28+
RegisterRecurringTask<TTask>(cronExpression);
2729

28-
string cronExpression = string.Format("{1} */{0} * * *", hours, minute);
29-
RegisterRecurringTask<TTask>(cronExpression);
30+
return this;
31+
}
3032

31-
return this;
32-
}
33+
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int minutes) where TTask : IRecurringBackgroundTask
34+
{
35+
ValidateMinuteTaskParameters(minutes);
3336

34-
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int minutes) where TTask : IRecurringBackgroundTask
37+
if (minutes > 59)
3538
{
36-
ValidateMinuteTaskParameters(minutes);
39+
var hours = minutes / 60;
40+
minutes = minutes % 60;
41+
return RegisterRecurringTask<TTask>(hours, minutes);
42+
}
3743

38-
if (minutes > 59)
39-
{
40-
var hours = minutes / 60;
41-
minutes = minutes % 60;
42-
return RegisterRecurringTask<TTask>(hours, minutes);
43-
}
44+
string cronExpression = string.Format("*/{0} * * * *", minutes);
45+
RegisterRecurringTask<TTask>(cronExpression);
4446

45-
string cronExpression = string.Format("*/{0} * * * *", minutes);
46-
RegisterRecurringTask<TTask>(cronExpression);
47+
return this;
48+
}
4749

48-
return this;
49-
}
50+
public IBackgroundTaskScheduler DeregisterRecurringTask<TTask>() where TTask : IRecurringBackgroundTask
51+
{
52+
RecurringJob.RemoveIfExists(GetJobId<TTask>());
5053

51-
public IBackgroundTaskScheduler DeregisterRecurringTask<TTask>() where TTask : IRecurringBackgroundTask
52-
{
53-
RecurringJob.RemoveIfExists(GetJobId<TTask>());
54+
return this;
55+
}
5456

55-
return this;
56-
}
57+
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int days, int atHour = 0, int atMinute = 0) where TTask : IAsyncRecurringBackgroundTask
58+
{
59+
ValidateDailyTaskParameters(days, atHour, atMinute);
5760

58-
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int days, int atHour = 0, int atMinute = 0) where TTask : IAsyncRecurringBackgroundTask
59-
{
60-
ValidateDailyTaskParameters(days, atHour, atMinute);
61+
string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute);
62+
RegisterAsyncRecurringTask<TTask>(cronExpression);
6163

62-
string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute);
63-
RegisterAsyncRecurringTask<TTask>(cronExpression);
64+
return this;
65+
}
6466

65-
return this;
66-
}
67+
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IAsyncRecurringBackgroundTask
68+
{
69+
ValidateHourlyTaskParameters(hours, minute);
6770

68-
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IAsyncRecurringBackgroundTask
71+
if (hours == 0 && minute > 0)
6972
{
70-
ValidateHourlyTaskParameters(hours, minute);
73+
return RegisterAsyncRecurringTask<TTask>(minute);
74+
}
7175

72-
if (hours == 0 && minute > 0)
73-
{
74-
return RegisterAsyncRecurringTask<TTask>(minute);
75-
}
76+
string cronExpression = string.Format("{1} */{0} * * *", hours, minute);
77+
RegisterAsyncRecurringTask<TTask>(cronExpression);
7678

77-
string cronExpression = string.Format("{1} */{0} * * *", hours, minute);
78-
RegisterAsyncRecurringTask<TTask>(cronExpression);
79+
return this;
80+
}
7981

80-
return this;
81-
}
82+
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int minutes) where TTask : IAsyncRecurringBackgroundTask
83+
{
84+
ValidateMinuteTaskParameters(minutes);
8285

83-
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int minutes) where TTask : IAsyncRecurringBackgroundTask
86+
if (minutes > 59)
8487
{
85-
ValidateMinuteTaskParameters(minutes);
88+
var hours = minutes / 60;
89+
minutes = minutes % 60;
90+
return RegisterAsyncRecurringTask<TTask>(hours, minutes);
91+
}
8692

87-
if (minutes > 59)
88-
{
89-
var hours = minutes / 60;
90-
minutes = minutes % 60;
91-
return RegisterAsyncRecurringTask<TTask>(hours, minutes);
92-
}
93+
string cronExpression = string.Format("*/{0} * * * *", minutes);
94+
RegisterAsyncRecurringTask<TTask>(cronExpression);
9395

94-
string cronExpression = string.Format("*/{0} * * * *", minutes);
95-
RegisterAsyncRecurringTask<TTask>(cronExpression);
96+
return this;
97+
}
9698

97-
return this;
98-
}
99+
public IBackgroundTaskScheduler DeregisterAsyncRecurringTask<TTask>() where TTask : IAsyncRecurringBackgroundTask
100+
{
101+
RecurringJob.RemoveIfExists(GetJobId<TTask>());
99102

100-
public IBackgroundTaskScheduler DeregisterAsyncRecurringTask<TTask>() where TTask : IAsyncRecurringBackgroundTask
101-
{
102-
RecurringJob.RemoveIfExists(GetJobId<TTask>());
103+
return this;
104+
}
103105

104-
return this;
105-
}
106+
private string GetJobId<TTask>()
107+
{
108+
return typeof(TTask).FullName;
109+
}
106110

107-
private string GetJobId<TTask>()
108-
{
109-
return typeof(TTask).FullName;
110-
}
111+
private void RegisterRecurringTask<TTask>(string cronExpression) where TTask : IRecurringBackgroundTask
112+
{
113+
RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.Execute(), cronExpression);
114+
}
111115

112-
private void RegisterRecurringTask<TTask>(string cronExpression) where TTask : IRecurringBackgroundTask
116+
private void RegisterAsyncRecurringTask<TTask>(string cronExpression) where TTask : IAsyncRecurringBackgroundTask
117+
{
118+
RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.ExecuteAsync(), cronExpression);
119+
}
120+
121+
private void ValidateDailyTaskParameters(int days, int atHour, int atMinute)
122+
{
123+
ValidateNotNegative(days, nameof(days));
124+
ValidateNotNegative(atHour, nameof(atHour));
125+
ValidateNotNegative(atMinute, nameof(atMinute));
126+
127+
if (days == 0)
113128
{
114-
RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.Execute(), cronExpression);
129+
throw new ArgumentOutOfRangeException(nameof(days), "Daily recurring tasks need to have an interval of at least 1 day.");
115130
}
131+
}
116132

117-
private void RegisterAsyncRecurringTask<TTask>(string cronExpression) where TTask : IAsyncRecurringBackgroundTask
133+
private void ValidateHourlyTaskParameters(int hours, int minute)
134+
{
135+
ValidateNotNegative(hours, nameof(hours));
136+
ValidateNotNegative(minute, nameof(minute));
137+
138+
if (hours == 0 && minute == 0)
118139
{
119-
RecurringJob.AddOrUpdate<TTask>(GetJobId<TTask>(), t => t.ExecuteAsync(), cronExpression);
140+
throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks need to have an interval of at least 1 minute.");
120141
}
121142

122-
private void ValidateDailyTaskParameters(int days, int atHour, int atMinute)
143+
if (hours > 23)
123144
{
124-
ValidateNotNegative(days, nameof(days));
125-
ValidateNotNegative(atHour, nameof(atHour));
126-
ValidateNotNegative(atMinute, nameof(atMinute));
127-
128-
if (days == 0)
129-
{
130-
throw new ArgumentOutOfRangeException(nameof(days), "Daily recurring tasks need to have an interval of at least 1 day.");
131-
}
145+
throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval of 24 hours or more should be scheduled using a daily interval instead.");
132146
}
133147

134-
private void ValidateHourlyTaskParameters(int hours, int minute)
148+
if (minute > 59)
135149
{
136-
ValidateNotNegative(hours, nameof(hours));
137-
ValidateNotNegative(minute, nameof(minute));
138-
139-
if (hours == 0 && minute == 0)
140-
{
141-
throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks need to have an interval of at least 1 minute.");
142-
}
143-
144-
if (hours > 23)
145-
{
146-
throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval of 24 hours or more should be scheduled using a daily interval instead.");
147-
}
148-
149-
if (minute > 59)
150-
{
151-
throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval measured in hours and minutes cannot have an minute component greater than 59 minutes.");
152-
}
150+
throw new ArgumentOutOfRangeException(nameof(hours), "Recurring tasks with an interval measured in hours and minutes cannot have an minute component greater than 59 minutes.");
153151
}
152+
}
154153

155-
private void ValidateMinuteTaskParameters(int minutes)
154+
private void ValidateMinuteTaskParameters(int minutes)
155+
{
156+
ValidateNotNegative(minutes, nameof(minutes));
157+
if (minutes == 0)
156158
{
157-
ValidateNotNegative(minutes, nameof(minutes));
158-
if (minutes == 0)
159-
{
160-
throw new ArgumentOutOfRangeException(nameof(minutes), "Recurring tasks need to have an interval of at least 1 minute.");
161-
}
159+
throw new ArgumentOutOfRangeException(nameof(minutes), "Recurring tasks need to have an interval of at least 1 minute.");
162160
}
161+
}
163162

164-
private void ValidateNotNegative(int number, string argumentName)
163+
private void ValidateNotNegative(int number, string argumentName)
164+
{
165+
if (number < 0)
165166
{
166-
if (number < 0)
167-
{
168-
throw new ArgumentOutOfRangeException(argumentName, "Recurring tasks cannot set the interval to negative numbers.");
169-
}
167+
throw new ArgumentOutOfRangeException(argumentName, "Recurring tasks cannot set the interval to negative numbers.");
170168
}
171169
}
172170
}
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Hangfire.Dashboard;
1+
using Cofoundry.Domain;
62
using Hangfire.Annotations;
7-
using Cofoundry.Domain;
3+
using Hangfire.Dashboard;
84

9-
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire
5+
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire;
6+
7+
public class HangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
108
{
11-
public class HangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
9+
public bool Authorize([NotNull] DashboardContext context)
1210
{
13-
public bool Authorize([NotNull] DashboardContext context)
14-
{
15-
var service = (IUserContextService)context.GetHttpContext().RequestServices.GetService(typeof(IUserContextService));
16-
17-
// Hangfire does not support async auth:
18-
// https://github.com/HangfireIO/Hangfire/issues/827
19-
var userContext = service
20-
.GetCurrentContextByUserAreaAsync(CofoundryAdminUserArea.AreaCode)
21-
.ConfigureAwait(false).GetAwaiter().GetResult();
11+
var service = (IUserContextService)context.GetHttpContext().RequestServices.GetService(typeof(IUserContextService));
12+
13+
// Hangfire does not support async auth:
14+
// https://github.com/HangfireIO/Hangfire/issues/827
15+
var userContext = service
16+
.GetCurrentContextByUserAreaAsync(CofoundryAdminUserArea.AreaCode)
17+
.ConfigureAwait(false).GetAwaiter().GetResult();
2218

23-
return userContext.IsCofoundryUser();
24-
}
19+
return userContext.IsCofoundryUser();
2520
}
2621
}

0 commit comments

Comments
 (0)