Skip to content

Commit

Permalink
Refactor index creation and update unit tests
Browse files Browse the repository at this point in the history
The method EnsureIndexesAsync has been created to efficiently handle index creation in the RavenJobStore. This function is now also called in the unit test setup. Furthermore, new unit tests have been provided to ensure that jobs can be scheduled and queried in a not started scheduler. The project file Quartz.Impl.RavenJobStore.csproj has been slightly modified with new metadata.
  • Loading branch information
JezhikLaas committed Dec 12, 2023
1 parent da5ae14 commit 6de2274
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Quartz.Impl.RavenJobStore.UnitTests/ImplementationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public ImplementationTests(ITestOutputHelper output)
{
Logger = Output.BuildLoggerFor<RavenJobStore>()
};

Target.EnsureIndexesAsync(CancellationToken.None).GetAwaiter().GetResult();
}

public override void Dispose()
Expand Down
35 changes: 35 additions & 0 deletions Quartz.Impl.RavenJobStore.UnitTests/SingleSchedulerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,39 @@ public async Task If_a_schedule_tries_to_replace_a_job_with_replace_Then_no_exce
await Scheduler.Invoking(x => x.ScheduleJob(job, new [] { triggerOne }, true, CancellationToken.None))
.Should().NotThrowAsync();
}

[Fact(DisplayName = "If jobs are scheduled to a not started scheduler Then it does not throw")]
public async Task If_jobs_are_scheduled_to_a_not_started_scheduler_Then_it_does_not_throw()
{
Scheduler = await CreateSingleSchedulerAsync("Test", collectionName: "SchedulerData");

var job = JobBuilder
.Create(typeof(PersistentJob))
.WithIdentity("Job", "Group")
.UsingJobData(nameof(PersistentJob.TestProperty), "Initial Value")
.StoreDurably()
.Build();

var triggerOne = (IOperableTrigger)TriggerBuilder.Create()
.WithIdentity("Trigger", "Group")
.StartAt(DateTimeOffset.UtcNow.AddDays(1))
.WithPriority(1)
.ForJob(job)
.Build();

await Scheduler.Invoking(x => x.ScheduleJob(job, triggerOne, CancellationToken.None))
.Should().NotThrowAsync();
}

[Fact(DisplayName = "If a scheduler is not started Then it can be queried anyway")]
public async Task If_a_scheduler_is_not_started_Then_it_can_be_queried_anyway()
{
Scheduler = await CreateSingleSchedulerAsync("Test", collectionName: "SchedulerData");

await Scheduler.Invoking
(
x => x.GetJobKeys(GroupMatcher<JobKey>.GroupEquals("Group"), CancellationToken.None)
)
.Should().NotThrowAsync();
}
}
2 changes: 2 additions & 0 deletions Quartz.Impl.RavenJobStore.UnitTests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public UtilsTests(ITestOutputHelper output)
{
Logger = Output.BuildLoggerFor<RavenJobStore>()
};

Target.EnsureIndexesAsync(CancellationToken.None).GetAwaiter().GetResult();
}

public override void Dispose()
Expand Down
6 changes: 5 additions & 1 deletion Quartz.Impl.RavenJobStore/Quartz.Impl.RavenJobStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.5.0</Version>
<Version>0.5.0-prerelease</Version>
<Title>Quartz Job store using RavenDB</Title>
<Authors>Uwe Laas</Authors>
<PackageProjectUrl>https://github.com/JezhikLaas/quartznet-RavenJobStore</PackageProjectUrl>
<Copyright>Copyright (c) Uwe Laas 2023</Copyright>
<RepositoryUrl>https://github.com/JezhikLaas/quartznet-RavenJobStore</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Quartz.Impl.RavenJobStore/RavenJobStore.IJobStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Task Initialize(
Signaler = signaler;
DocumentStore ??= InitializeDocumentStore();

return Task.CompletedTask;
return EnsureIndexesAsync(cancellationToken);
}

/// <inheritdoc />
Expand Down
10 changes: 0 additions & 10 deletions Quartz.Impl.RavenJobStore/RavenJobStore.Implementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ internal async Task SchedulerStartedAsync(CancellationToken token)

Logger.LogDebug("Scheduler started at {PointInTime}", SystemTime.UtcNow());

// We prefer static indexes.
await DocumentStore!.ExecuteIndexAsync(new JobGroupsIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new TriggerGroupsIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new JobIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new TriggerIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new PausedTriggerGroupIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new PausedJobGroupIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new CalendarIndex(), token: token).ConfigureAwait(false);
await DocumentStore!.ExecuteIndexAsync(new BlockedJobIndex(), token: token).ConfigureAwait(false);

using var session = GetSession();

var exists = await session.Advanced.ExistsAsync(InstanceName, token);
Expand Down
15 changes: 15 additions & 0 deletions Quartz.Impl.RavenJobStore/RavenJobStore.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,21 @@ private void WaitForIndexing()
else break;
}
}

internal async Task EnsureIndexesAsync(CancellationToken token)
{
DocumentStore.ThrowIfNull();

// We prefer static indexes.
await DocumentStore.ExecuteIndexAsync(new JobGroupsIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new TriggerGroupsIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new JobIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new TriggerIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new PausedTriggerGroupIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new PausedJobGroupIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new CalendarIndex(), token: token).ConfigureAwait(false);
await DocumentStore.ExecuteIndexAsync(new BlockedJobIndex(), token: token).ConfigureAwait(false);
}

private async Task RetryConcurrencyConflictAsync(Func<Task> action)
{
Expand Down

0 comments on commit 6de2274

Please sign in to comment.