Skip to content

Commit

Permalink
fixup! Ensure different dependent schedules are honored
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Nov 2, 2024
1 parent a9eaf92 commit a1f99b7
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/NCronJob/Registry/JobRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace NCronJob;

internal sealed class JobRegistry
{
private readonly HashSet<JobDefinition> allJobs = new(JobDefinitionEqualityComparer.Instance);
private readonly HashSet<JobDefinition> allJobs
= new(JobDefinitionEqualityComparer.Pure);
public List<DynamicJobRegistration> DynamicJobRegistrations { get; } = [];
private readonly Dictionary<JobDefinition, List<DependentJobRegistryEntry>> dependentJobsPerJobDefinition = [];
private readonly Dictionary<JobDefinition, List<DependentJobRegistryEntry>> dependentJobsPerJobDefinition
= new(DependentJobDefinitionEqualityComparer.Instance);

public IReadOnlyCollection<JobDefinition> GetAllJobs() => [.. allJobs];

Expand Down Expand Up @@ -90,6 +92,8 @@ public void RegisterJobDependency(ICollection<JobDefinition> parentJobdefinition

entries.Add(entry);
}

entries.Add(entry);

Check failure on line 96 in src/NCronJob/Registry/JobRegistry.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'entries' does not exist in the current context

Check failure on line 96 in src/NCronJob/Registry/JobRegistry.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

The name 'entries' does not exist in the current context
}

public IReadOnlyCollection<JobDefinition> GetDependentSuccessJobTypes(JobDefinition parentJobDefinition)
Expand Down Expand Up @@ -148,4 +152,59 @@ private void AssertNoDuplicateJobNames(string? additionalJobName = null)
throw new InvalidOperationException($"Duplicate job names found: {string.Join(", ", duplicateJobName)}");
}
}

private sealed class JobDefinitionEqualityComparer : IEqualityComparer<JobDefinition>
{
public static readonly JobDefinitionEqualityComparer Pure = new();

public bool Equals(JobDefinition? x, JobDefinition? y) =>
(x is null && y is null) || (x is not null && y is not null
&& x.Type == y.Type && x.Type != typeof(DynamicJobFactory)
&& x.Parameter == y.Parameter
&& x.CronExpression == y.CronExpression
&& x.TimeZone == y.TimeZone
&& x.CustomName == y.CustomName
&& x.IsStartupJob == y.IsStartupJob);

public int GetHashCode(JobDefinition obj) => HashCode.Combine(
obj.Type,
obj.Parameter,
obj.CronExpression,
obj.TimeZone,
obj.CustomName,
obj.IsStartupJob);
}

private sealed class DependentJobDefinitionEqualityComparer : IEqualityComparer<JobDefinition>
{
// TODO: Maybe is the code conflating two different concepts.
// Dependent jobs may have a name, a type and a parameter, but that's the most of it.
// And the code currently uses the same type to hold the configuration of "lead" jobs
// and dependent jobs.
//
// Which brings this dependent job only comparer.
//
// Maybe should a DependentJobDefinition type spawn?

public static readonly DependentJobDefinitionEqualityComparer Instance = new();

public bool Equals(JobDefinition? x, JobDefinition? y) =>
(x is null && y is null) || (x is not null && y is not null
&& x.Type == y.Type && x.Type != typeof(DynamicJobFactory)
&& x.Parameter == y.Parameter
//&& x.CronExpression == y.CronExpression
//&& x.TimeZone == y.TimeZone
&& x.CustomName == y.CustomName
//&& x.IsStartupJob == y.IsStartupJob
);

public int GetHashCode(JobDefinition obj) => HashCode.Combine(
obj.Type,
obj.Parameter,
//obj.CronExpression,
//obj.TimeZone,
obj.CustomName
//obj.IsStartupJob
);
}
}

0 comments on commit a1f99b7

Please sign in to comment.