Skip to content

Commit

Permalink
Refactor Task to ValueTask (#988) (#1964)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko Lahma <marko.lahma@gmail.com>
  • Loading branch information
jafin and lahma committed Apr 10, 2023
1 parent 06fc159 commit 28b5fae
Show file tree
Hide file tree
Showing 164 changed files with 1,653 additions and 1,631 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@

* IJobExecutionContext.RecoveringTriggerKey now returns null if IJobExecutionContext.Recovering is false instead of throwing exception.

### NEW FEATURES
* `Task` return types and parameters have been changed to `ValueTask`. Any consumers of Quartz expecting a `Task` will require to update the signatures to `ValueTask`,
or use the `AsTask()` Method on ValueTask to Return the `ValueTask` as a `Task` (#988)

#### Cron Parser

Expand Down
1 change: 0 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@

<PropertyGroup Condition=" '$(TargetFramework)' == 'net472' ">
<DefineConstants>$(DefineConstants);REMOTING</DefineConstants>
<FullFramework>true</FullFramework>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.1" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="2.1.1" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -65,7 +65,7 @@ private static Task<IResult> AddCalendar(
endpointHelper.AssertIsValid(request);
return endpointHelper.ExecuteWithOkResponse(
schedulerName,
scheduler => scheduler.AddCalendar(request.CalendarName, request.Calendar, request.Replace, request.UpdateTriggers, cancellationToken)
scheduler => scheduler.AddCalendar(request.CalendarName, request.Calendar, request.Replace, request.UpdateTriggers, cancellationToken).AsTask()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -155,7 +155,7 @@ private static Task<IResult> PauseJob(
string jobName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.PauseJob(new JobKey(jobName, jobGroup), cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.PauseJob(new JobKey(jobName, jobGroup), cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down Expand Up @@ -183,7 +183,7 @@ private static Task<IResult> ResumeJob(
string jobName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResumeJob(new JobKey(jobName, jobGroup), cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResumeJob(new JobKey(jobName, jobGroup), cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down Expand Up @@ -214,10 +214,10 @@ private static Task<IResult> TriggerJob(
{
if (request?.JobData != null)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.TriggerJob(new JobKey(jobName, jobGroup), request.JobData, cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.TriggerJob(new JobKey(jobName, jobGroup), request.JobData, cancellationToken).AsTask());
}

return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.TriggerJob(new JobKey(jobName, jobGroup), cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.TriggerJob(new JobKey(jobName, jobGroup), cancellationToken).AsTask());
}

[ProducesResponseType(typeof(InterruptResponse), StatusCodes.Status200OK)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -92,10 +92,10 @@ private static Task<IResult> Start(
{
if (delayMilliseconds.HasValue)
{
return scheduler.StartDelayed(TimeSpan.FromMilliseconds(delayMilliseconds.Value), cancellationToken);
return scheduler.StartDelayed(TimeSpan.FromMilliseconds(delayMilliseconds.Value), cancellationToken).AsTask();
}
return scheduler.Start(cancellationToken);
return scheduler.Start(cancellationToken).AsTask();
});
}

Expand All @@ -105,7 +105,7 @@ private static Task<IResult> Standby(
string schedulerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.Standby(cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.Standby(cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand All @@ -115,7 +115,7 @@ private static Task<IResult> Shutdown(
bool waitForJobsToComplete = false,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.Shutdown(waitForJobsToComplete, cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.Shutdown(waitForJobsToComplete, cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand All @@ -124,7 +124,7 @@ private static Task<IResult> Clear(
string schedulerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.Clear(cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.Clear(cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand All @@ -133,7 +133,7 @@ private static Task<IResult> PauseAll(
string schedulerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.PauseAll(cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.PauseAll(cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand All @@ -142,6 +142,6 @@ private static Task<IResult> ResumeAll(
string schedulerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResumeAll(cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResumeAll(cancellationToken).AsTask());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -139,7 +139,7 @@ private static Task<IResult> ResetTriggerFromErrorState(
string triggerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResetTriggerFromErrorState(new TriggerKey(triggerName, triggerGroup), cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResetTriggerFromErrorState(new TriggerKey(triggerName, triggerGroup), cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand All @@ -150,7 +150,7 @@ private static Task<IResult> PauseTrigger(
string triggerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.PauseTrigger(new TriggerKey(triggerName, triggerGroup), cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.PauseTrigger(new TriggerKey(triggerName, triggerGroup), cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down Expand Up @@ -178,7 +178,7 @@ private static Task<IResult> ResumeTrigger(
string triggerName,
CancellationToken cancellationToken = default)
{
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResumeTrigger(new TriggerKey(triggerName, triggerGroup), cancellationToken));
return endpointHelper.ExecuteWithOkResponse(schedulerName, scheduler => scheduler.ResumeTrigger(new TriggerKey(triggerName, triggerGroup), cancellationToken).AsTask());
}

[ProducesResponseType(StatusCodes.Status200OK)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;

using Quartz.HttpApiContract;
using Quartz.Impl;
Expand Down
4 changes: 2 additions & 2 deletions src/Quartz.Benchmark/DefaultThreadPoolBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public void RunInThread_CompletedTask_MaxConcurrencyIsSixteen_MultiThreaded()
}

/// <summary>
/// The primary goal of this benchamrk is to measure memory allocations.
/// The primary goal of this benchmark is to measure memory allocations.
/// </summary>
/// <remarks>
/// Note that this includes the allocations for initializing the threadpool itself.
/// Note that this includes the allocations for initializing the ThreadPool itself.
/// </remarks>
[Benchmark]
public void RunInThread_OneShot()
Expand Down
12 changes: 6 additions & 6 deletions src/Quartz.Benchmark/ExecutingJobsManagerBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ internal sealed class ExecutingJobsManagerLegacy : IJobListener

private int numJobsFired;

public Task JobToBeExecuted(
public ValueTask JobToBeExecuted(
IJobExecutionContext context,
CancellationToken cancellationToken = default)
{
Interlocked.Increment(ref numJobsFired);
executingJobs[((IOperableTrigger)context.Trigger).FireInstanceId] = context;
return Task.CompletedTask;
return default;
}

public Task JobWasExecuted(IJobExecutionContext context,
public ValueTask JobWasExecuted(IJobExecutionContext context,
JobExecutionException? jobException,
CancellationToken cancellationToken = default)
{
executingJobs.TryRemove(((IOperableTrigger)context.Trigger).FireInstanceId, out _);
return Task.CompletedTask;
return default;
}

public Task JobExecutionVetoed(
public ValueTask JobExecutionVetoed(
IJobExecutionContext context,
CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
return default;
}
}
}
Expand Down
Loading

0 comments on commit 28b5fae

Please sign in to comment.