Skip to content

Commit

Permalink
[add] null checks
Browse files Browse the repository at this point in the history
[edit] switch to better exceptions
  • Loading branch information
i4004 committed Jan 9, 2024
1 parent 4fa1382 commit 8557258
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
7 changes: 7 additions & 0 deletions src/Simplify.Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.4.0] - 2024-01-09

### Added

- Null checks
- Switch to better exceptions

## [1.3.1] - 2023-08-24

### Added
Expand Down
5 changes: 3 additions & 2 deletions src/Simplify.Scheduler/MultitaskScheduler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using Simplify.Scheduler.CommandLine;
Expand Down Expand Up @@ -60,7 +61,7 @@ public bool Start(string[]? args = null)
break;

default:
throw new ArgumentOutOfRangeException();
throw new InvalidEnumArgumentException(nameof(commandLineProcessResult));
}

return true;
Expand All @@ -69,7 +70,7 @@ public bool Start(string[]? args = null)
/// <summary>
/// Called when scheduler is about to stop, main stopping point
/// </summary>
protected void StopJobs(object sender, ConsoleCancelEventArgs args)
protected void StopJobs(object? sender, ConsoleCancelEventArgs args)
{
StopJobs();

Expand Down
41 changes: 19 additions & 22 deletions src/Simplify.Scheduler/SchedulerJobsHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -199,8 +200,11 @@ private void InitializeJob(ICrontabSchedulerJob job)
_jobs.Add(job);
}

private void OnCronTimerTick(object state)
private void OnCronTimerTick(object? state)
{
if (state is null)
throw new ArgumentNullException(nameof(state));

var job = (ICrontabSchedulerJob)state;

if (job.CrontabProcessor == null)
Expand All @@ -214,8 +218,11 @@ private void OnCronTimerTick(object state)
OnStartWork(state);
}

private void OnStartWork(object state)
private void OnStartWork(object? state)
{
if (state is null)
throw new ArgumentNullException(nameof(state));

var job = (ICrontabSchedulerJob)state;

lock (_workingJobsTasks)
Expand All @@ -232,8 +239,11 @@ private void OnStartWork(object state)

#region Execution

private async Task Run(object state)
private async Task Run(object? state)
{
if (state is null)
throw new ArgumentNullException(nameof(state));

var (jobTaskID, job) = (Tuple<long, ICrontabSchedulerJob>)state;

try
Expand Down Expand Up @@ -291,26 +301,13 @@ private async Task RunBasicJob(ISchedulerJobRepresentation job)

private Task InvokeJobMethod(ISchedulerJobRepresentation job, object jobObject)
{
object result;

switch (job.InvokeMethodParameterType)
var result = job.InvokeMethodParameterType switch
{
case InvokeMethodParameterType.Parameterless:
result = job.InvokeMethodInfo.Invoke(jobObject, null);
break;

case InvokeMethodParameterType.AppName:
result = job.InvokeMethodInfo.Invoke(jobObject, new object[] { AppName });
break;

case InvokeMethodParameterType.Args:
result = job.InvokeMethodInfo.Invoke(jobObject, new object[] { job.JobArgs });
break;

default:
throw new ArgumentOutOfRangeException();
}

InvokeMethodParameterType.Parameterless => job.InvokeMethodInfo.Invoke(jobObject, null),
InvokeMethodParameterType.AppName => job.InvokeMethodInfo.Invoke(jobObject, new object[] { AppName }),
InvokeMethodParameterType.Args => job.InvokeMethodInfo.Invoke(jobObject, new object[] { job.JobArgs }),
_ => throw new InvalidEnumArgumentException(nameof(job.InvokeMethodParameterType)),
};
if (result is Task task)
return task;

Expand Down
2 changes: 1 addition & 1 deletion src/Simplify.Scheduler/Simplify.Scheduler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>1.3.1</Version>
<Version>1.4</Version>

<Authors>Alexander Krylkov</Authors>
<Product>Simplify</Product>
Expand Down

0 comments on commit 8557258

Please sign in to comment.