Skip to content

Commit

Permalink
Make default job name more friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Oct 31, 2024
1 parent cb75254 commit b32e2b6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
6 changes: 3 additions & 3 deletions samples/Foundatio.HostingSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
builder.Services.AddJobScheduler();

if (everyMinute)
builder.Services.AddDistributedCronJob<EveryMinuteJob>("* * * * *", j => j.Name(nameof(EveryMinuteJob)));
builder.Services.AddDistributedCronJob<EveryMinuteJob>("* * * * *");

if (evenMinutes)
builder.Services.AddCronJob("EvenMinutes", "*/2 * * * *", async sp =>
Expand All @@ -67,12 +67,12 @@
if (sample1)
builder.Services.AddJob("Sample1", sp => new Sample1Job(sp.GetRequiredService<ILoggerFactory>()), o => o.ApplyDefaults<Sample1Job>().WaitForStartupActions().InitialDelay(TimeSpan.FromSeconds(4)));

builder.Services.AddJob<SampleLockJob>(o => o.WaitForStartupActions().Name(nameof(SampleLockJob)));
builder.Services.AddJob<SampleLockJob>(o => o.WaitForStartupActions());

if (sample2)
{
builder.Services.AddHealthChecks().AddCheck<Sample2Job>("Sample2Job");
builder.Services.AddJob<Sample2Job>(o => o.WaitForStartupActions().Name(nameof(Sample2Job)));
builder.Services.AddJob<Sample2Job>(o => o.WaitForStartupActions());
}

// if you don't specify priority, actions will automatically be assigned an incrementing priority starting at 0
Expand Down
8 changes: 4 additions & 4 deletions src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IServiceCollection AddJob<T>(this IServiceCollection services, Hos
jobOptions.ApplyDefaults<T>();
}
jobOptions.Name ??= typeof(T).FullName;
jobOptions.Name ??= JobOptions.GetDefaultJobName(typeof(T));
jobOptions.JobFactory ??= sp => sp.GetRequiredService<T>();
return new HostedJobService(s, jobOptions, s.GetService<ILoggerFactory>());
Expand All @@ -41,7 +41,7 @@ public static IServiceCollection AddJob<T>(this IServiceCollection services, Act
{
var jobOptionsBuilder = new HostedJobOptionsBuilder();
jobOptionsBuilder.ApplyDefaults<T>();
jobOptionsBuilder.Name(typeof(T).FullName);
jobOptionsBuilder.Name(JobOptions.GetDefaultJobName(typeof(T)));
configureJobOptions?.Invoke(jobOptionsBuilder);
return services.AddJob<T>(jobOptionsBuilder.Target);
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public static IServiceCollection AddCronJob<T>(this IServiceCollection services,
{
services.AddTransient<T>();
var jobOptionsBuilder = new ScheduledJobOptionsBuilder();
jobOptionsBuilder.Name(typeof(T).FullName).CronSchedule(cronSchedule).JobFactory(sp => sp.GetRequiredService<T>());
jobOptionsBuilder.Name(JobOptions.GetDefaultJobName(typeof(T))).CronSchedule(cronSchedule).JobFactory(sp => sp.GetRequiredService<T>());
configureJobOptions?.Invoke(jobOptionsBuilder);
return services.AddCronJob(jobOptionsBuilder.Target);
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public static IServiceCollection AddDistributedCronJob<T>(this IServiceCollectio
{
services.AddTransient<T>();
var jobOptionsBuilder = new ScheduledJobOptionsBuilder();
jobOptionsBuilder.Name(typeof(T).FullName).Distributed().CronSchedule(cronSchedule).JobFactory(sp => sp.GetRequiredService<T>());
jobOptionsBuilder.Name(JobOptions.GetDefaultJobName(typeof(T))).Distributed().CronSchedule(cronSchedule).JobFactory(sp => sp.GetRequiredService<T>());
configureJobOptions?.Invoke(jobOptionsBuilder);
return services.AddCronJob(jobOptionsBuilder.Target);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Foundatio.Extensions.Hosting/Jobs/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public JobManager(IServiceProvider serviceProvider, ILoggerFactory loggerFactory

public void AddOrUpdate<TJob>(string cronSchedule, Action<ScheduledJobOptionsBuilder> configure = null) where TJob : class, IJob
{
string jobName = typeof(TJob).FullName;
string jobName = JobOptions.GetDefaultJobName(typeof(TJob));
lock (_lock)
{
var job = Jobs.FirstOrDefault(j => j.Options.Name == jobName);
Expand Down Expand Up @@ -151,7 +151,7 @@ public void AddOrUpdate(string jobName, string cronSchedule, Action action, Acti

public void Remove<TJob>() where TJob : class, IJob
{
string jobName = typeof(TJob).FullName;
string jobName = JobOptions.GetDefaultJobName(typeof(TJob));
lock (_lock)
{
var job = Jobs.FirstOrDefault(j => j.Options.Name == jobName);
Expand Down Expand Up @@ -191,7 +191,7 @@ public JobStatus[] GetJobStatus()

public async Task RunJobAsync<TJob>(CancellationToken cancellationToken = default) where TJob : class, IJob
{
string jobName = typeof(TJob).FullName;
string jobName = JobOptions.GetDefaultJobName(typeof(TJob));
await RunJobAsync(jobName, cancellationToken).AnyContext();
}

Expand Down
23 changes: 23 additions & 0 deletions src/Foundatio/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;

namespace Foundatio.Extensions;

Expand All @@ -17,4 +18,26 @@ public static string NormalizePath(this string path)

return path;
}

public static string ToSpacedWords(this string text, bool preserveAcronyms = true)
{
if (String.IsNullOrWhiteSpace(text))
return String.Empty;

var sb = new StringBuilder(text.Length * 2);
sb.Append(text[0]);

for (int i = 1; i < text.Length; i++)
{
if (Char.IsUpper(text[i]))
if ((text[i - 1] != ' ' && !Char.IsUpper(text[i - 1])) ||
(preserveAcronyms && Char.IsUpper(text[i - 1]) &&
i < text.Length - 1 && !Char.IsUpper(text[i + 1])))
sb.Append(' ');

sb.Append(text[i]);
}

return sb.ToString();
}
}
18 changes: 11 additions & 7 deletions src/Foundatio/Jobs/JobOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using Foundatio.Extensions;
using Foundatio.Utility;

namespace Foundatio.Jobs;
Expand Down Expand Up @@ -28,13 +29,7 @@ public static void ApplyDefaults(JobOptions jobOptions, Type jobType)

jobOptions.Name = jobAttribute.Name;
if (String.IsNullOrEmpty(jobOptions.Name))
{
string jobName = jobType.Name;
if (jobName.EndsWith("Job"))
jobName = jobName.Substring(0, jobName.Length - 3);

jobOptions.Name = jobName.ToLower();
}
jobOptions.Name = GetDefaultJobName(jobType);

jobOptions.Description = jobAttribute.Description;
jobOptions.RunContinuous = jobAttribute.IsContinuous;
Expand Down Expand Up @@ -89,6 +84,15 @@ public static JobOptions GetDefaults<T>(Func<IServiceProvider, IJob> jobFactory)
jobOptions.JobFactory = jobFactory;
return jobOptions;
}

public static string GetDefaultJobName(Type type)
{
string jobName = type.Name;
if (jobName.EndsWith("Job"))
jobName = jobName.Substring(0, jobName.Length - 3);

return jobName.ToSpacedWords();
}
}

public static class JobOptionExtensions
Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio/Jobs/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public async Task<bool> RunAsync(CancellationToken cancellationToken = default)
}
else if (_options.RunContinuous && _options.InstanceCount == 1)
{
await job.RunContinuousAsync(_options.Interval, _options.IterationLimit, cancellationToken).AnyContext();
await job.RunContinuousAsync(_options, cancellationToken).AnyContext();
}
else
{
Expand Down

0 comments on commit b32e2b6

Please sign in to comment.