Skip to content

Commit

Permalink
Merge pull request #28 from furkandeveloper/feature/cron-format
Browse files Browse the repository at this point in the history
✨ [FEATURE] Added Cron Format
  • Loading branch information
furkandeveloper authored Feb 6, 2022
2 parents 98d933a + e8a60c5 commit 61c54a8
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion samples/EasyCron.Sample/Jobs/ConsoleCronJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ConsoleCronJob : CronJobService
private readonly ILogger<ConsoleCronJob> logger;

public ConsoleCronJob(ICronConfiguration<ConsoleCronJob> cronConfiguration, ILogger<ConsoleCronJob> logger)
: base(cronConfiguration.CronExpression,cronConfiguration.TimeZoneInfo)
: base(cronConfiguration.CronExpression,cronConfiguration.TimeZoneInfo, cronConfiguration.CronFormat)
{
this.logger = logger;
}
Expand Down
4 changes: 3 additions & 1 deletion samples/EasyCron.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public void ConfigureServices(IServiceCollection services)

services.ApplyResulation<ConsoleCronJob>(options =>
{
options.CronExpression = "* * * * *";
options.CronExpression = "*/10 * * * * *";
options.TimeZoneInfo = TimeZoneInfo.Local;
options.CronFormat = Cronos.CronFormat.IncludeSeconds;
});
services.ApplyResulation<MyJob>(options =>
{
options.CronExpression = "* * * * *";
options.TimeZoneInfo = TimeZoneInfo.Local;
options.CronFormat = Cronos.CronFormat.Standard;
});


Expand Down
8 changes: 5 additions & 3 deletions samples/EasyCron.Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
"AllowedHosts": "*",
"CronJobs": {
"ConsoleCronJob": {
"CronExpression": "* * * * *",
"TimeZoneInfo": "Local"
"CronExpression": "*/10 * * * * *",
"TimeZoneInfo": "Local",
"CronFormat": "IncludeSeconds"
},
"MyJob": {
"CronExpression": "*/2 * * * *",
"TimeZoneInfo": "Local"
"TimeZoneInfo": "Local",
"CronFormat": "Standard"
}
}
}
9 changes: 8 additions & 1 deletion src/EasyCronJob.Abstractions/CronConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Cronos;
using System;

namespace EasyCronJob.Abstractions
{
Expand All @@ -17,5 +18,11 @@ public class CronConfiguration<T> : ICronConfiguration<T>
/// Represents any time zone in the world.
/// </summary>
public TimeZoneInfo TimeZoneInfo { get; set; }

/// <summary>
/// Cron Format
/// Default <see cref="CronFormat.Standard"/>
/// </summary>
public CronFormat CronFormat { get; set; } = CronFormat.Standard;
}
}
6 changes: 4 additions & 2 deletions src/EasyCronJob.Abstractions/CronJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public abstract class CronJobService : IHostedService, IDisposable
{
private System.Timers.Timer timer;
private readonly TimeZoneInfo timeZoneInfo;
private readonly CronFormat cronFormat;
private readonly CronExpression cronExpression;
protected CronJobService(string cronExpression, TimeZoneInfo timeZoneInfo)
protected CronJobService(string cronExpression, TimeZoneInfo timeZoneInfo, CronFormat cronFormat = CronFormat.Standard)
{
this.timeZoneInfo = timeZoneInfo;
this.cronExpression = CronExpression.Parse(cronExpression);
this.cronFormat = cronFormat;
this.cronExpression = CronExpression.Parse(cronExpression, this.cronFormat);
}

protected virtual async Task ScheduleJob(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<Authors>furkandeveloper</Authors>
<Company>furkandeveloper</Company>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Description>This library provides abstraction layer for EasyCronJob.Core library.</Description>
<PackageIcon>easyCronJob.png</PackageIcon>
<PackageProjectUrl>https://github.com/furkandeveloper/EasyCronJob</PackageProjectUrl>
Expand Down
9 changes: 8 additions & 1 deletion src/EasyCronJob.Abstractions/ICronConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Cronos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -23,5 +24,11 @@ public interface ICronConfiguration<T>
/// TimeZone Information
/// </summary>
public TimeZoneInfo TimeZoneInfo { get; set; }

/// <summary>
/// Cron Format
/// Default <see cref="CronFormat.Standard"/>
/// </summary>
public CronFormat CronFormat { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<Authors>furkandeveloper</Authors>
<Company>furkandeveloper</Company>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Description>This repository provides easy cron job to your application on IHostedService.</Description>
<PackageIcon>easyCronJob.png</PackageIcon>
<PackageProjectUrl>https://github.com/furkandeveloper/EasyCronJob</PackageProjectUrl>
Expand Down
23 changes: 20 additions & 3 deletions src/EasyCronJob.AutoConfigurer/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyCronJob.Abstractions;
using Cronos;
using EasyCronJob.Abstractions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -43,12 +44,13 @@ private static List<Type> FindCronJobServices()
/// <returns>
/// Returns <see cref="Tuple{T1, T2}"/>
/// </returns>
private static Tuple<string, TimeZoneInfo> FindServiceParameter(IServiceCollection services, string serviceName)
private static Tuple<string, TimeZoneInfo, CronFormat> FindServiceParameter(IServiceCollection services, string serviceName)
{
var serviceProvider = services.BuildServiceProvider();
var configurationObject = serviceProvider.GetRequiredService<IConfiguration>();
string cronExpression = configurationObject.GetValue<string>("CronJobs:" + serviceName + ":CronExpression");
string info = configurationObject.GetValue<string>("CronJobs:" + serviceName + ":TimeZoneInfo");
string cronFormat = configurationObject.GetValue<string>("CronJobs:" + serviceName + ":CronFormat");
TimeZoneInfo timeZoneInfo;
switch (info)
{
Expand All @@ -62,7 +64,21 @@ private static Tuple<string, TimeZoneInfo> FindServiceParameter(IServiceCollecti
timeZoneInfo = TimeZoneInfo.Local;
break;
}
return new Tuple<string, TimeZoneInfo>(cronExpression, timeZoneInfo);
CronFormat format = CronFormat.Standard;
switch (cronFormat)
{
case "Standard":
format = CronFormat.Standard;
break;
case "IncludeSeconds":
format = CronFormat.IncludeSeconds;
break;
default:
format = CronFormat.Standard;
break;
}

return new Tuple<string, TimeZoneInfo, CronFormat>(cronExpression, timeZoneInfo, format);
}

/// <summary>
Expand Down Expand Up @@ -136,6 +152,7 @@ private static IServiceCollection ConfigureCronServices(IServiceCollection servi
{
findedService.GetType().GetProperty("CronExpression").SetValue(findedService, cronParameters.Item1);
findedService.GetType().GetProperty("TimeZoneInfo").SetValue(findedService, cronParameters.Item2);
findedService.GetType().GetProperty("CronFormat").SetValue(findedService, cronParameters.Item3);
}
ctorServices.Add(findedService);
}
Expand Down
2 changes: 1 addition & 1 deletion src/EasyCronJob.Core/EasyCronJob.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<Authors>furkandeveloper</Authors>
<Company>furkandeveloper</Company>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Description>This repository provides easy cron job to your application on IHostedService.</Description>
<PackageIcon>easyCronJob.png</PackageIcon>
<PackageProjectUrl>https://github.com/furkandeveloper/EasyCronJob</PackageProjectUrl>
Expand Down

0 comments on commit 61c54a8

Please sign in to comment.