New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependency injection help #271
Comments
I have a few (small) programs that are structured using DI, broadly the approach is:
To be clear, I only register the services used by the scheduled jobs, not the jobs themselves. Hope this helps (anyone), am happy to provide more detailed code snippets if helpful. |
A simple solution, without specific DI framework dependency, would be to provide an In ASP.NET Core, this allows the Something like that: public interface IScheduleBuilder
{
ISchedule Create(Action job, string cronExpression);
ISchedule Create(Action job, Action<RunSpecifier> specifier));
}
public interface ISchedule
{
bool Running { get; }
DateTime? NextRun { get; }
event EventHandler<JobStartedEventArgs> JobStarted;
event EventHandler<JobEndedEventArgs> JobEnded;
void UseUtc();
void ResetScheduling();
void SetScheduling(Action<RunSpecifier> specifier);
void SetScheduling(string cron);
void Start();
void Stop();
void StopAndBlock();
void StopAndBlock(int timeout);
void StopAndBlock(TimeSpan timeout);
}
public class MyHostedService : IHostingService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ISchedule _myScheduleTask;
public MyHostedService(IServiceScopeFactory scopeFactory, IScheduleBuilder scheduleBuilder)
{
_myScheduleTask = scheduleBuilder.Create(MyTask, "* * * * *");
}
public Task StartAsync(CancellationToken cancellationToken)
{
_myScheduleTask.Start();
}
public Task StopAsync(CancellationToken cancellationToken)
{
_myScheduleTask.Stop();
}
private void MyTask()
{
using var scope = _serviceScopeFactory.CreateScope();
var myJob = scope.ServiceProvider.GetRequiredService<IMyJob>();
myJob.Execute();
}
} Another solution, coupled with ASP.NET Core DI and more plug-and-play, would be to support an |
Pls share your code with us |
I'm using IMediatr and FluentScheduler, and have developed this approach. It's setup in Startup.cs simply: And then uses a dispatch wrappper to properly queue mediatr commands.
|
public async Task StartAsync(CancellationToken cancellationToken) public class SchedulerRegistry : Registry |
@dev-greene Why set this, why not run parallel when it's a DI? |
I am not sure if this is ideal/correct or not but I use IServiceScopeFactory. I've an app running .Net 7 currently but that began with .Net 5. It's been stable through all versions and works very well. I realised that I need to pass several services into my jobs such as DB repo and logging and this is the way I found to do it. In Program.cs.
Then Registry.cs.
Then my job (in this case BookingCleanup) looks like this:
This has worked really well for me and the application has remained stable for several years now. I've tested upgrade to preview .Net 8 and it seemed fine. |
hey Gus, not sure if you're still around but I was wondering if you are still running this and how it works for you? I'm currently in the process of moving stuff around in my c# app I wrote about 2 years ago, and I was wanting to work towards moving fluentscheduler towards a dependency injection system and if it's possible to actually do it using version 5 of fluentscheduler (since it doesn't look like it's active anymore to be able to use V6) Thanks! -Joshua |
Hi Joshua
Yes, it works well for me.
Not had any problems so far. I am using the latest Fluent Scheduler 5.5.1 with .Net 7.
Cheers
Gus
…On Tue, 23 Jan 2024, 13:43 Rpgdudester, ***@***.***> wrote:
I am not sure if this is ideal/correct or not but I use
IServiceScopeFactory. I've an app running .Net 7 currently but that began
with .Net 5. It's been stable through all versions and works very well. I
realised that I need to pass several services into my jobs such as DB repo
and logging and this is the way I found to do it.
In Program.cs.
using FluentScheduler;
using Microsoft.Extensions.DependencyInjection; // IServiceScopeFactory
// Fluent scheduler
IServiceScopeFactory serviceScopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
JobManager.Initialize(new ScheduleRegistry(serviceScopeFactory));
Then Registry.cs.
using FluentScheduler;
using Microsoft.Extensions.DependencyInjection;
namespace MyCode.Code.Fluent_Scheduler;
public class ScheduleRegistry : Registry
{
public ScheduleRegistry(IServiceScopeFactory serviceScopeFactory)
{
// check the bookings every minute for unpaid after 10 mins and set to abandoned
Schedule(() => new BookingCleanupJob(serviceScopeFactory)).NonReentrant().ToRunNow().AndEvery(1).Minutes();
// run the proc to clear out zombie rows once a day
Schedule(() => new ZombieCleanupJob(serviceScopeFactory)).NonReentrant().ToRunEvery(1).Days();
}
}
Then my job (in this case BookingCleanup) looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyCode.Code.Fluent_Scheduler;
public class BookingCleanupJob : IJob
{
private IServiceScopeFactory serviceScopeFactory;
public BookingCleanupJob(IServiceScopeFactory serviceScopeFactory)
{
this.serviceScopeFactory = serviceScopeFactory;
}
// executes the scheduled task
public async void Execute()
{
using var serviceScope = serviceScopeFactory.CreateScope();
ISqlDatabase _repository = serviceScope.ServiceProvider.GetService<ISqlDatabase>();
IConfiguration _config = serviceScope.ServiceProvider.GetService<IConfiguration>();
IWebHostEnvironment _env = serviceScope.ServiceProvider.GetService<IWebHostEnvironment>();
ILogging _logging = serviceScope.ServiceProvider.GetService<ILogging>();
try
{
const string sql = "; EXEC [dbo].[sp_SetAbandonedBookingsJob]";
var AbandonedBookings = await _repository.GetListAsync<Booking>(sql) as List<Booking>;
// logging example
var desc = "sp_SetAbandonedBookingsJob was called and updated: " + AbandonedBookings.Count + " rows.";
await _logging.LogAsync(Constants.LogType_BookingCleanupJobRUN, null, null, desc);
This has worked really well for me and the application has remained stable
for several years now. I've tested upgrade to preview .Net 8 and it seemed
fine.
hey Gus, not sure if you're still around but I was wondering if you are
still running this and how it works for you?
I'm currently in the process of moving stuff around in my c# app I wrote
about 2 years ago, and I was wanting to work towards moving fluentscheduler
towards a dependency injection system and if it's possible to actually do
it using version 5 of fluentscheduler (since it doesn't look like it's
active anymore to be able to use V6)
Thanks!
-Joshua
—
Reply to this email directly, view it on GitHub
<#271 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABHNLLOMMETEFFTHS76XEP3YP65BTAVCNFSM4MCXI6P2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJQGYYDQNBRGE2Q>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
There are some use cases of the library that we don't officially support, but we embrace any community effort on helping folks in need.
Using the library with dependency injection is one of those cases. If you have anything to say on this topic (bug report, question, suggestion, opinion, etc) please comment on this issue instead of opening a new one.
The text was updated successfully, but these errors were encountered: