Skip to content

Commit

Permalink
Cron Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
HubiBoar committed Jul 22, 2024
1 parent 1349900 commit f8487e8
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Projects -->
<ItemGroup>
<PackageVersion Include="Microsoft.AspNetCore.Routing" Version="2.2.2" />
<PackageVersion Include="NCronTab" Version="3.3.3" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.16.2" />
<PackageVersion Include="OneOf" Version="3.0.263" />
Expand Down
1 change: 1 addition & 0 deletions src/FeatureSlice/FeatureSlice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Definit.Dependencies" />
<PackageReference Include="Definit.Result" />
<PackageReference Include="Momolith.Modules" />
<PackageReference Include="NCronTab" />
<PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup>

Expand Down
243 changes: 241 additions & 2 deletions src/FeatureSlice/Handle/FeatureSlice.Handle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,29 @@ public abstract partial class FeatureSliceBase<TSelf, TRequest, TResult, TRespon
protected HandleSetup Handle<TDep0>
(
Func<TRequest, TDep0, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Singleton
Func<IServiceProvider, TDep0> factory,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
factory(provider)
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0>
(
Func<TRequest, TDep0, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
{
Expand All @@ -30,7 +52,7 @@ protected HandleSetup Handle<TDep0>
protected HandleSetup Handle<TDep0, TDep1>
(
Func<TRequest, TDep0, TDep1, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Singleton
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
Expand All @@ -49,4 +71,221 @@ protected HandleSetup Handle<TDep0, TDep1>
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2>
(
Func<TRequest, TDep0, TDep1, TDep2, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>()
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2, TDep3>
(
Func<TRequest, TDep0, TDep1, TDep2, TDep3, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
where TDep3 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>(),
provider.GetRequiredService<TDep3>()
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2, TDep3, TDep4>
(
Func<TRequest, TDep0, TDep1, TDep2, TDep3, TDep4, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
where TDep3 : notnull
where TDep4 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>(),
provider.GetRequiredService<TDep3>(),
provider.GetRequiredService<TDep4>()
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2, TDep3, TDep4, TDep5>
(
Func<TRequest, TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
where TDep3 : notnull
where TDep4 : notnull
where TDep5 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>(),
provider.GetRequiredService<TDep3>(),
provider.GetRequiredService<TDep4>(),
provider.GetRequiredService<TDep5>()
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, TDep6>
(
Func<TRequest, TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, TDep6, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
where TDep3 : notnull
where TDep4 : notnull
where TDep5 : notnull
where TDep6 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>(),
provider.GetRequiredService<TDep3>(),
provider.GetRequiredService<TDep4>(),
provider.GetRequiredService<TDep5>(),
provider.GetRequiredService<TDep6>()
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, TDep6, TDep7>
(
Func<TRequest, TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, TDep6, TDep7, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
where TDep3 : notnull
where TDep4 : notnull
where TDep5 : notnull
where TDep6 : notnull
where TDep7 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>(),
provider.GetRequiredService<TDep3>(),
provider.GetRequiredService<TDep4>(),
provider.GetRequiredService<TDep5>(),
provider.GetRequiredService<TDep6>(),
provider.GetRequiredService<TDep7>()
),
OnException,
lifetime
);
}

protected HandleSetup Handle<TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, TDep6, TDep7, TDep8>
(
Func<TRequest, TDep0, TDep1, TDep2, TDep3, TDep4, TDep5, TDep6, TDep7, TDep8, Task<TResult>> handle,
ServiceLifetime lifetime = ServiceLifetime.Transient
)
where TDep0 : notnull
where TDep1 : notnull
where TDep2 : notnull
where TDep3 : notnull
where TDep4 : notnull
where TDep5 : notnull
where TDep6 : notnull
where TDep7 : notnull
where TDep8 : notnull
{
return new HandleSetup
(
provider =>
request =>
handle
(
request,
provider.GetRequiredService<TDep0>(),
provider.GetRequiredService<TDep1>(),
provider.GetRequiredService<TDep2>(),
provider.GetRequiredService<TDep3>(),
provider.GetRequiredService<TDep4>(),
provider.GetRequiredService<TDep5>(),
provider.GetRequiredService<TDep6>(),
provider.GetRequiredService<TDep7>(),
provider.GetRequiredService<TDep8>()
),
OnException,
lifetime
);
}
}
53 changes: 53 additions & 0 deletions src/FeatureSlice/Job/Job.Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Definit.Results;
using Microsoft.Extensions.DependencyInjection;
using NCrontab;

namespace FeatureSlice;

Expand Down Expand Up @@ -38,4 +39,56 @@ Func<TRequest> request
));
return options;
}

public static FeatureSliceBase<TRequest, TResult, TResponse>.ISetup WithJob<TRequest, TResult, TResponse>
(
this FeatureSliceBase<TRequest, TResult, TResponse>.ISetup options,
Func<bool> shouldRun,
TRequest request
)
where TRequest : notnull
where TResult : Result_Base<TResponse>
where TResponse : notnull
{
return options.WithJob(shouldRun, () => request);
}

public static FeatureSliceBase<TRequest, TResult, TResponse>.ISetup WithCronJob<TRequest, TResult, TResponse>
(
this FeatureSliceBase<TRequest, TResult, TResponse>.ISetup options,
string cronExpression,
Func<TRequest> request
)
where TRequest : notnull
where TResult : Result_Base<TResponse>
where TResponse : notnull
{
var cron = CrontabSchedule.TryParse(cronExpression);

var lastTime = DateTime.UtcNow;

return options.WithJob(() =>
{
var timeNow = DateTime.UtcNow;
var run = cron.GetNextOccurrences(lastTime, timeNow) is not null;

lastTime = timeNow;

return run;
}
, request);
}

public static FeatureSliceBase<TRequest, TResult, TResponse>.ISetup WithCronJob<TRequest, TResult, TResponse>
(
this FeatureSliceBase<TRequest, TResult, TResponse>.ISetup options,
string cronExpression,
TRequest request
)
where TRequest : notnull
where TResult : Result_Base<TResponse>
where TResponse : notnull
{
return options.WithCronJob(cronExpression, () => request);
}
}
6 changes: 3 additions & 3 deletions src/Samples/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public sealed record Response(int Value0, int Value1, string Value2);
)
.DefaultResponse()
.WithTags("Handler"))
.WithJob
.WithCronJob
(
() => DateTime.Now.TimeOfDay == new TimeSpan(10, 0, 0),
() => new ("testjob", 69, 420)
"5 4 * * *",
new Request("testjob", 1, 2)
);
}

Expand Down

0 comments on commit f8487e8

Please sign in to comment.