Skip to content

Commit

Permalink
Make IJobWrapper public (#2313)
Browse files Browse the repository at this point in the history
* make IJobWrapper public
* implement SetObjectProperties and ReturnJob recursive for any combination of JobWrapping with IJobWrapper;
* add a Property-Test for MicrosoftDependencyInjectionJobFactory
  • Loading branch information
Philipp-Binder authored and lahma committed May 9, 2024
1 parent f21ed7d commit b27ce46
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ protected virtual void ConfigureScope(IServiceScope scope, TriggerFiredBundle bu
// the ambient context of a Job
}

public override void SetObjectProperties(object obj, JobDataMap data)
{
// we need to check if job is actually a scoped job wrapper
var target = obj is ScopedJob scopedJob ? scopedJob.InnerJob : obj;
base.SetObjectProperties(target, data);
}

private (IJob Job, bool FromContainer) CreateJob(TriggerFiredBundle bundle, IServiceProvider serviceProvider)
{
var job = (IJob?) serviceProvider.GetService(bundle.JobDetail.JobType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ public async Task JobsShouldBeDisposedAfterExecute()
var schedulerBuilder = SchedulerBuilder.Create()
.Build();

const string testValue = "test";

var jobDetail = JobBuilder.Create<TestJob>()
.StoreDurably()
.UsingJobData(nameof(TestJob.Test), testValue)
.Build();

var serviceCollection = new ServiceCollection();
Expand All @@ -48,6 +51,7 @@ public async Task JobsShouldBeDisposedAfterExecute()

TestJob.Executed.Should().BeTrue();
TestJob.Disposed.Should().BeTrue();
TestJob.TestValue.Should().Be(testValue);

Dependency.Disposed.Should().BeTrue();
}
Expand All @@ -56,6 +60,9 @@ private class TestJob : IJob, IDisposable
{
public static bool Executed { get; set; }
public static bool Disposed { get; set; }
public static string TestValue { get; set; }

public string Test { get; set; }

public TestJob(Dependency dependency)
{
Expand All @@ -64,6 +71,7 @@ public TestJob(Dependency dependency)
public Task Execute(IJobExecutionContext context)
{
Executed = true;
TestValue = Test;
return Task.CompletedTask;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Quartz/IJobWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Quartz;

internal interface IJobWrapper
public interface IJobWrapper
{
IJob Target { get; }
}
21 changes: 14 additions & 7 deletions src/Quartz/Simpl/PropertySettingJobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,20 @@ protected virtual IJob InstantiateJob(TriggerFiredBundle bundle, IScheduler sche
/// </summary>
/// <param name="obj">The object to set properties to.</param>
/// <param name="data">The data to set.</param>
public virtual void SetObjectProperties(object obj, JobDataMap data)
{
foreach (string name in data.Keys)
{
SetObjectProperty(obj, name, data[name]);
}
}
public virtual void SetObjectProperties(object obj, JobDataMap data)
{
if (obj is IJobWrapper jobWrapper)
{
SetObjectProperties(jobWrapper.Target, data);
}
else
{
foreach (string name in data.Keys)
{
SetObjectProperty(obj, name, data[name]);
}
}
}

/// <summary>
/// Sets specific property to object, handles conversion and error conditions.
Expand Down
33 changes: 24 additions & 9 deletions src/Quartz/Simpl/SimpleJobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,32 @@ async Task IJobWithAsyncReturnFactory.ReturnJobAsync(IJob job)
{
disposableJob.Dispose();
}

// check for wrapped jobs only if the current job is not disposable
// disposable wrappers should handle inner disposal on its own
else if (job is IJobWrapper jobWrapper)
{
await ((IJobWithAsyncReturnFactory) this).ReturnJobAsync(jobWrapper.Target).ConfigureAwait(false);
}
}
#endif
/// <summary>
/// Allows the job factory to destroy/cleanup the job if needed.
/// No-op when using SimpleJobFactory.
/// </summary>
public virtual void ReturnJob(IJob job)
{
var disposable = job as IDisposable;
disposable?.Dispose();
}
/// <summary>
/// Allows the job factory to destroy/cleanup the job if needed.
/// No-op when using SimpleJobFactory.
/// </summary>
public virtual void ReturnJob(IJob job)
{
if (job is IDisposable disposableJob)
{
disposableJob.Dispose();
}

// check for wrapped jobs only if the current job is not disposable
// disposable wrappers should handle inner disposal on its own
else if (job is IJobWrapper jobWrapper)
{
ReturnJob(jobWrapper.Target);
}
}
}
}

0 comments on commit b27ce46

Please sign in to comment.