Skip to content

Commit 1207546

Browse files
committed
[#490] [add] impl
[r] to primary constructors
1 parent 14f1377 commit 1207546

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/Simplify.Scheduler/SchedulerExceptionArgs.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,27 @@ namespace Simplify.Scheduler;
1111
/// <summary>
1212
/// Provides scheduler exception event args
1313
/// </summary>
14-
public class SchedulerExceptionArgs
14+
/// <remarks>
15+
/// Initializes a new instance of the <see cref="SchedulerExceptionArgs"/> class.
16+
/// </remarks>
17+
/// <param name="appName">Name of the scheduling application.</param>
18+
/// <param name="exception">The exception.</param>
19+
public class SchedulerExceptionArgs(string appName, Exception exception)
1520
{
16-
/// <summary>
17-
/// Initializes a new instance of the <see cref="SchedulerExceptionArgs"/> class.
18-
/// </summary>
19-
/// <param name="appName">Name of the scheduling application.</param>
20-
/// <param name="exception">The exception.</param>
21-
public SchedulerExceptionArgs(string appName, Exception exception)
22-
{
23-
AppName = appName;
24-
Exception = exception;
25-
}
2621

2722
/// <summary>
2823
/// Gets the name of the application.
2924
/// </summary>
3025
/// <value>
3126
/// The name of the application.
3227
/// </value>
33-
public string AppName { get; }
28+
public string AppName { get; } = appName;
3429

3530
/// <summary>
3631
/// Gets the exception.
3732
/// </summary>
3833
/// <value>
3934
/// The exception.
4035
/// </value>
41-
public Exception Exception { get; }
36+
public Exception Exception { get; } = exception;
4237
}

src/Simplify.Scheduler/SchedulerJobsHandler.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,21 @@ protected async Task StartJobsAsync()
145145
{
146146
Console.WriteLine("Starting Scheduler jobs...");
147147

148-
foreach (var job in _jobs)
148+
try
149149
{
150-
job.Start();
150+
foreach (var job in _jobs)
151+
{
152+
job.Start();
151153

152-
if (!(job is ICrontabSchedulerJob))
153-
await RunBasicJobAsync(job);
154+
if (!(job is ICrontabSchedulerJob))
155+
await RunBasicJobAsync(job);
156+
}
157+
}
158+
catch (Exception e)
159+
{
160+
TryRaiseOnExceptionEvent(e);
161+
162+
throw;
154163
}
155164

156165
Console.WriteLine("Scheduler jobs started.");
@@ -174,6 +183,22 @@ protected void StopJobs()
174183
Console.WriteLine("All jobs finished.");
175184
}
176185

186+
/// <summary>
187+
/// Tries to raise the OnException event, returns true if the OnException event raised, otherwise false.
188+
/// </summary>
189+
/// <param name="e">The exception.</param>
190+
protected bool TryRaiseOnExceptionEvent(Exception e)
191+
{
192+
if (OnException != null)
193+
{
194+
OnException(new SchedulerExceptionArgs(AppName, e));
195+
196+
return true;
197+
}
198+
199+
return false;
200+
}
201+
177202
/// <summary>
178203
/// Releases unmanaged and - optionally - managed resources.
179204
/// </summary>
@@ -252,9 +277,7 @@ private async Task Run(object? state)
252277
}
253278
catch (Exception e)
254279
{
255-
if (OnException != null)
256-
OnException(new SchedulerExceptionArgs(AppName, e));
257-
else
280+
if (!TryRaiseOnExceptionEvent(e))
258281
throw;
259282
}
260283
finally
@@ -292,9 +315,7 @@ private async Task RunBasicJobAsync(ISchedulerJobRepresentation job)
292315
}
293316
catch (Exception e)
294317
{
295-
if (OnException != null)
296-
OnException(new SchedulerExceptionArgs(AppName, e));
297-
else
318+
if (!TryRaiseOnExceptionEvent(e))
298319
throw;
299320
}
300321
}

0 commit comments

Comments
 (0)