Skip to content

Commit e4fa513

Browse files
committed
Add SetJobData method to support persisting job data to PerformContext.
1 parent 1d55d9d commit e4fa513

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/Hangfire.RecurringJobExtensions/Configuration/FileConfigurationProvider.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,9 @@ private void OnChanged(object sender, FileSystemEventArgs e)
8484

8585
private ExtendedDataJobFilter FindServerFilter()
8686
{
87-
ExtendedDataJobFilter filter = null;
87+
var jobFilter = GlobalJobFilters.Filters.FirstOrDefault(x => x.Instance.GetType() == typeof(ExtendedDataJobFilter));
8888

89-
var enumerator = GlobalJobFilters.Filters.GetEnumerator();
90-
91-
while (enumerator.MoveNext())
92-
{
93-
if (enumerator.Current.Instance.GetType() == typeof(ExtendedDataJobFilter))
94-
{
95-
filter = enumerator.Current.Instance as ExtendedDataJobFilter;
96-
break;
97-
}
98-
}
99-
100-
return filter;
89+
return jobFilter.Instance as ExtendedDataJobFilter;
10190
}
10291

10392
/// <summary>

src/Hangfire.RecurringJobExtensions/ExtendedDataJobFilter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ public void OnPerforming(PerformingContext filterContext)
4141

4242
var jobInfo = RecurringJobInfos[filterContext.BackgroundJob.Job.ToString()];
4343

44-
if (jobInfo == null
45-
|| jobInfo.ExtendedData == null
46-
|| jobInfo.ExtendedData.Count == 0) return;
44+
if (jobInfo == null) return;
4745

4846
var jobDataKey = $"recurringjob-info-{jobInfo.ToString()}";
4947

50-
filterContext.Items[jobDataKey] = jobInfo.ExtendedData;
48+
filterContext.Items[jobDataKey] = jobInfo.ExtendedData ?? new Dictionary<string, object>();
5149
}
5250
/// <summary>
5351
/// Called after the performance of the job.

src/Hangfire.RecurringJobExtensions/PerformContextExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Hangfire.Common;
45
using Hangfire.Server;
56

@@ -47,5 +48,26 @@ public static T GetJobData<T>(this PerformContext context, string name)
4748

4849
return JobHelper.FromJson<T>(json);
4950
}
51+
/// <summary>
52+
/// Persists job data to <see cref="PerformContext"/>.
53+
/// </summary>
54+
/// <param name="context">The <see cref="PerformContext"/>.</param>
55+
/// <param name="name">The dictionary key from the property <see cref="RecurringJobInfo.ExtendedData"/></param>
56+
/// <param name="value">The persisting value.</param>
57+
public static void SetJobData(this PerformContext context, string name, object value)
58+
{
59+
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
60+
61+
var jobDataKey = $"recurringjob-info-{context.BackgroundJob.Job.ToString()}";
62+
63+
if (!context.Items.ContainsKey(jobDataKey))
64+
throw new KeyNotFoundException($"The job data key: {jobDataKey} is not found.");
65+
66+
var jobData = context.Items[jobDataKey] as IDictionary<string, object>;
67+
68+
if (jobData == null) jobData = new Dictionary<string, object>();
69+
70+
jobData[name] = value;
71+
}
5072
}
5173
}

0 commit comments

Comments
 (0)