How to run scheduled tasks in Orchard? - c#

I have to run a automated job every 5 hours.
I found this post on how to create scheduled tasks, using IScheduledTaskHandler and IScheduledTaskManager.
Scheduled tasks using Orchard CMS
I copied the same code, I added my service call inside the Process function. It compiles fine. But I am not sure if I have to 'start' this scheduled task, like a windows service start. Does it get picked up automatically after I build the solution? When does the clock starts ticking if I want to run this job in 5 hours? And if I want to stop/pause, how can I do that?
Thanks.
EDIT:
I am getting an exception if I try to enable the custom module with task handler.
Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: source
Line 241: var shellContext = _shellContexts.FirstOrDefault(c => c.Settings.Name == settings.Name);
Source File: \orchard-1.4\src\Orchard\Environment\DefaultOrchardHost.cs Line: 241
The _shellContexts is coming up as null. If I remove the task handler class from the project/module, everything works fine.
Here is the task handler code.
public class ScheduledTaskHandler : IScheduledTaskHandler
{
private const string TaskType = "MyTaskUniqueID";
private readonly IScheduledTaskManager _taskManager;
private readonly IMyService _myService;
public ILogger Logger { get; set; }
public ScheduledTaskHandler(IScheduledTaskManager taskManager, IMyService myService)
{
_myService = myService;
_taskManager = taskManager;
Logger = NullLogger.Instance;
try
{
DateTime firstDate = new DateTime().AddMinutes(5);
ScheduleNextTask(firstDate);
}
catch (Exception e)
{
this.Logger.Error(e, e.Message);
}
}
public void Process(ScheduledTaskContext context)
{
if (context.Task.TaskType == TaskType)
{
try
{
_myService.RunJob();
}
catch (Exception e)
{
this.Logger.Error(e, e.Message);
}
finally
{
DateTime nextTaskDate = new DateTime().AddHours(5);
ScheduleNextTask(nextTaskDate);
}
}
}
private void ScheduleNextTask(DateTime date)
{
if (date > DateTime.UtcNow)
{
var tasks = this._taskManager.GetTasks(TaskType);
if (tasks == null || tasks.Count() == 0)
this._taskManager.CreateTask(TaskType, date, null);
}
}
}

Clock starts ticking automatically when you start the app - you cannot stop/pause it.
Scheduler runs in 1 minute intervals - it checks if there are tasks that should be ran now and runs them. Tasks are being stored in database - corresponding record is always deleted just before task execution is about to start (to ensure that a given task will run only once).
If you need a recurrent job to be ran, you need to create a new task just after the previous one has finished (like in the example you linked to).

if somebody is interested I've simply removed the starter code from ScheduledTaskHandler.
The following I put on some controller constructor
private void ScheduleStartTask() {
var tasks = _scheduledTaskManager.GetTasks(TaskType);
if (tasks == null || tasks.Count() == 0) {
var date = _clock.UtcNow.AddSeconds(5);
_scheduledTaskManager.CreateTask(TaskType, date, null);
}
}
and on the handler
public void Process(ScheduledTaskContext context) {
if (context.Task.TaskType == TaskType) {
try {
var x = "kuku";
} catch (Exception e) {
this.Logger.Error(e, e.Message);
} finally {
this.ScheduleNextTask();
}
}
}
private void ScheduleNextTask() {
var date = _clock.UtcNow.AddSeconds(5);
_taskManager.DeleteTasks(null, a => a.TaskType == TaskType);
_taskManager.CreateTask(TaskType, date, null);
}

Use ILoggerFactory Instead of ILogger and then Get Logger Instance from it.
public ScheduledTaskHandler(IScheduledTaskManager taskManager,ILoggerFactory LoggerFactory, IMyService myService)
{
_myService = myService;
_taskManager = taskManager;
Logger = NLoggerFactory.CreateLogger(typeof(ScheduledTaskHandler));;
try
{
DateTime firstDate = new DateTime().AddMinutes(5);
ScheduleNextTask(firstDate);
}
catch (Exception e)
{
this.Logger.Error(e, e.Message);
}
}

Related

Azure Event Hub process events indefinitely

I need to create software that can read events from Azure Event Hub in an infinite way and save them on the db following some logic. The first question I wanted to ask is if such thing is feasible using:
EventProcessorClient (or rather EventProcessor<TPartition> as I would always like to use the SqlServer for checkpoints instead of an Azure Blob Storage) in a BackgroundService as Windows Service and then run it in infinitely and every so many events to checkpoint?
I tried this code reading from a single partition but the application crashes after an exception and the loop ends without continuing to process the events.
public class LogConsumerBackgroundService : BackgroundService
{
private const string EventHubConnectionString =
"ConnString";
private const string ConsumerGroup = "ConsGroup";
private static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
private readonly ILogger<LogConsumerBackgroundService> _logger;
private readonly IServiceProvider _serviceProvider;
private int _count;
public LogConsumerBackgroundService(ILogger<LogConsumerBackgroundService> logger, IServiceProvider serviceProvider)
{
Guard.Against.Null(logger);
Guard.Against.Null(serviceProvider);
_logger = logger;
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
var eventHubConsumerClientOptions = new EventHubConsumerClientOptions
{
ConnectionOptions =
{
TransportType = EventHubsTransportType.AmqpWebSockets
}
};
await using var consumer = new EventHubConsumerClient(ConsumerGroup, EventHubConnectionString, eventHubConsumerClientOptions);
var startingPosition = EventPosition.Earliest;
var partitionIds = await consumer.GetPartitionIdsAsync(CancellationToken.None);
var partitionId = partitionIds.First();
var scope = _serviceProvider.CreateScope();
var repository = scope.ServiceProvider.GetRequiredService<IRepository>();
while (!stoppingToken.IsCancellationRequested)
{
await foreach (var receivedEvent in consumer.ReadEventsFromPartitionAsync(partitionId, startingPosition,
CancellationToken.None))
{
try
{
// Some logic to save data on bd
_logger.LogInformation("Processed row: {Count}", countInfo += _count);
await repository.SaveChangesAsync();
}
catch (Exception e)
{
_logger.LogError(e.Demystify(), "{EventBody}", receivedEvent.Data?.EventBody);
}
}
}
}
catch (TaskCanceledException e)
{
_logger.LogError(e.Demystify(), "Task was canceled");
}
catch (Exception e)
{
_logger.LogError(e.Demystify(), "An unhandled exception has occurred");
}
}
}

Logging the execution of a Hangfire RecurringJob in database?

I have set up hangfire successfully for my ASP.NET project, i.e. the 11 Hangfire tables are created in my database. I tried the following command inside the Application_Start() of my project's Global.asax:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start(
{
System.Diagnostics.Debug.WriteLine("Recurring job will be set up.");
RecurringJob.AddOrUpdate(
"some-id",
() => System.Diagnostics.Debug.WriteLine("Job instance started at " +
DateTime.Now)),
"*/2 * * * 1-5");
}
}
}
Sadly, inside Visual Studio's window Output > Debug I only see Reccuring job will be set up. and nothing ever after. However, a SELECT * FROM [myContext].[HangFire].[Set] shows me
Key Score Value ExpireAt
recurring-jobs 1579116240 some-id NULL
So far so good, this means that the job is indeed set up.
But how do I log inside my DB each and each time when the RecurringJob is executed? Do I assume correctly that Hangfire does not do that out of the box and I have to log it myself within the arrow-function? Or is there a more elegant way?
Question on the side: Why don't I see any output of System.Diagnostics.Debug.WriteLine within my recurring job?
References
Hangfire doesn't create tables in IIS
How to configure hangfire with ASP.NET to obtain connection string from config file?
Official hangfire.io docu on recurrent tasks
You can use SeriLog with Hangfire out of the box. Serilog comes with different sinks, e.g. Serilog.Sinks.MSSqlServer. You can configure it in startup.cs:
using Serilog;
using Serilog.Sinks.MSSqlServer;
Log.Logger = new LoggerConfiguration()
.WriteTo
.MSSqlServer(
connectionString: hangfireConnectionString,
tableName: "Logs",
autoCreateSqlTable: true
).CreateLogger();
// will display any issues with Serilog config. comment out in prod.
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
GlobalConfiguration.Configuration
.UseSqlServerStorage(hangfireConnectionString)
.UseSerilogLogProvider();
After you schedule your job, you can log it with
Log.Information(string.Format("Hanfire Job Scheduled at {0}", DateTime.Now));
Hangfire includes a concept of job filters (similar to ASP.NET MVC's Action Filters). For your use case, you would define one that would write to your database (adjust based on your needs):
using Hangfire.Common;
using Hangfire.Server;
class LogCompletionAttribute : JobFilterAttribute, IServerFilter
{
public void OnPerforming(PerformingContext filterContext)
{
// Code here if you care when the execution **has begun**
}
public void OnPerformed(PerformedContext context)
{
// Check that the job completed successfully
if (!context.Canceled && context.Exception != null)
{
// Here you would write to your database.
// Example with entity framework:
using (var ctx = new YourDatabaseContext())
{
ctx.Something.Add(/**/);
ctx.SaveChanges();
}
}
}
}
And then apply the filter to the job method:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start(
{
System.Diagnostics.Debug.WriteLine("Recurring job will be set up.");
RecurringJob.AddOrUpdate("some-id", () => MyJob(), "*/2 * * * 1-5");
}
[LogCompletion]
public static void MyJob()
{
System.Diagnostics.Debug.WriteLine("Job instance started at " + DateTime.Now)
}
}
}
Docs: https://docs.hangfire.io/en/latest/extensibility/using-job-filters.html
So the cron is set to fire At every 2nd minute on every day-of-week from Monday through Friday. I assume you are waiting for the job to execute and that it is in the right window of time.
Most of the references that I found on the web indicated that you can do.
RecurringJob.AddOrUpdate(() => Console.WriteLine("This job will execute once in every minute"), Cron.Minutely);
Maybe you have to line up the dots a bit better to write to the vs console.
There is also an admin portal that can be configured to see what is begin run and when.
I have the following setup.
Global.asax.cs
protected void Application_Start()
{
HangfireJobsConfig.Register();
}
public class HangfireJobsConfig
{
public static void Register()
{
if (App1Config.RunHangfireService)
{
JobStorage.Current = new SqlServerStorage(App1Config.DefaultConnectionStringName.Split('=').Last());
GlobalConfiguration.Configuration.UseConsole();
RecurringJob.AddOrUpdate("RunJob1", () => RunJob1(null), Cron.MinuteInterval(App1Config.RunJob1Interval));
RecurringJob.AddOrUpdate("RunJob2", () => RunJob2(null), Cron.MinuteInterval(App1Config.RunJob2Interval));
}
}
[AutomaticRetry(Attempts = 0, Order = 1)]
public static void RunJob1(PerformContext context)
{
//dostuff
}
[AutomaticRetry(Attempts = 0, Order = 2)]
public static void RunJob2(PerformContext context)
{
//do stuff
}
}
Startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ConfigureHangFire(app);
}
public void ConfigureHangFire(IAppBuilder app)
{
if (App1Config.RunHangfireService)
{
GlobalConfiguration.Configuration.UseSqlServerStorage(
AppiConfig.DefaultConnectionStringName.Split('=').Last());
GlobalConfiguration.Configuration.UseConsole();
app.UseHangfireServer();
var options = new DashboardOptions
{
AuthorizationFilters = new[]
{
new AuthorizationFilter { Roles = "Inventory" }
}
};
app.UseHangfireDashboard("/hangfire", options);
}
}
}
The actual problem was a very trivial one, the initialization of the actual background server was missing BackgroundJobServer();. Here the fully functional code:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
string connString = ConfigurationManager.ConnectionStrings["myContext"].ToString();
Hangfire.GlobalConfiguration.Configuration.UseConsole();
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(connString,
new SqlServerStorageOptions {
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
});
var bgndJS = new BackgroundJobServer(); // <--- this is essential!
RecurringJob.AddOrUpdate("myRecurringJob", () => HangfireRecurringJob(), "*/2 * * * 1-5");
System.Diagnostics.Debug.WriteLine("---> RecurringJob 'myHangfireJob' initated.");
}
public void HangfireRecurringJob() {
System.Diagnostics.Debug.WriteLine("---> HangfireRecurringJob() executed at" + DateTime.Now);
Console.Beep(); // <-- I was really happy to hear the beep
}
}
}

Hangfire, .Net Core and Entity Framework: concurrency exception

I am developing a .Net core application with Hangfire and facing the below exception
A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
I have used Hangfire for scheduling the jobs with 1 hour interval. I am facing the above issue when the new process/job gets started before the earlier job has finished its process.
How can we implement multiple Hangfire processes/jobs(multiple workers) to work (in parallel) to accomplish the task. (Resolved now, by using the default AspNetCoreJobActivator)
var scopeFactory = serviceProvider.GetService<IServiceScopeFactory>();
if (scopeFactory != null)
GlobalConfiguration.Configuration.UseActivator(new AspNetCoreJobActivator(scopeFactory));
Now, I am getting the following exception in CreateOrderData.cs:-
/*System.InvalidOperationException: An exception has been raised that
is likely due to a transient failure. If you are connecting to a SQL
Azure database consider using SqlAzureExecutionStrategy. --->
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred
while updating the entries. See the inner exception for details. --->
System.Data.SqlClient.SqlException: Transaction (Process ID 103) was
deadlocked on lock resources with another process and has been chosen
as the deadlock victim. Rerun the transaction. */
I am scheduling the hangfire cron job as below:-
RecurringJob.AddOrUpdate<IS2SScheduledJobs>(x => x.ProcessInputXML(), Cron.MinuteInterval(1));
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string hangFireConnection = Configuration["ConnectionStrings:HangFire"];
GlobalConfiguration.Configuration.UseSqlServerStorage(hangFireConnection);
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
);
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
services.AddScoped<IHangFireJob, HangFireJob>();
services.AddScoped<IScheduledJobs, ScheduledJobs>();
services.AddScoped<BusinessLogic>();
services.AddHangfire(opt =>
opt.UseSqlServerStorage(Configuration["ConnectionStrings:HangFire"]));
services.AddEntityFrameworkSqlServer().AddDbContext<ABCContext>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:ABC"]));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
{
GlobalConfiguration.Configuration.UseActivator(new HangFireActivator(serviceProvider));
//hangFireJob.Jobs();
// add NLog to ASP.NET Core
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddNLog();
// app.UseCors("AllowSpecificOrigin");
foreach (DatabaseTarget target in LogManager.Configuration.AllTargets.Where(t => t is DatabaseTarget))
{
target.ConnectionString = Configuration.GetConnectionString("Logging");
}
LogManager.ReconfigExistingLoggers();
}
Hangfire.cs
public class HangFireJob : IHangFireJob
{
private ABCContext _abcContext;
private IScheduledJobs scheduledJobs;
public HangFireJob(ABCContext abcContext, IScheduledJobs scheduledJobs)
{
_abcContext = abcContext;
this.scheduledJobs = scheduledJobs;
}
public void Jobs()
{
RecurringJob.AddOrUpdate<IScheduledJobs>(x => x.ProcessInputXML(), Cron.HourInterval(1));
}
}
ScheduledJobs.cs
public class S2SScheduledJobs : IS2SScheduledJobs
{
private BusinessLogic _businessLogic;
public ScheduledJobs(BusinessLogic businessLogic)
{
_businessLogic = businessLogic;
}
public async Task<string> ProcessInputXML()
{
await _businessLogic.ProcessXML();
}
}
BusinessLogic.cs
public class BusinessLogic
{
private ABCContext _abcContext;
public BusinessLogic(ABCContext abcContext) : base(abcContext)
{
_abcContext = abcContext;
}
public async Task ProcessXML()
{
var batchRepository = new BatchRepository(_abcContext);
var unprocessedBatchRecords = await BatchRepository.GetUnprocessedBatch();
foreach (var batchRecord in unprocessedBatchRecords)
{
try
{
int orderId = await LoadDataToOrderTable(batchRecord.BatchId);
await UpdateBatchProcessedStatus(batchRecord.BatchId);
if (orderId > 0)
{
await CreateOrderData(orderId);
}
}
catch(Exception ex)
{
}
}
}
CreateOrderData.cs
public async Task<int> CreateOrderData(int orderId)
{
try
{
await OrderRepo.InsertOrder(order);
await _abcContext.SaveChangesAsync();
}
catch(Exception ex)
{
/*System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy. ---> Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Transaction (Process ID 103) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. */
}
}
InsertOrder.cs
public async Task InsertOrder(Order o)
{
// creation of large number of entites(more than 50) to be inserted in the database
woRepo.Insert(p);
poRepo.Insert(y);
//and many more like above
Insert(order);
}
Insert.cs
public virtual void Insert(TEntity entity)
{
entity.ObjectState = ObjectState.Added;
if (entity is IXYZEntity xyzEntity)
{
xyzEntity.CreatedDate = DateTime.Now;
xyzEntity.UpdatedDate = xyzEntity.CreatedDate;
xyzEntity.CreatedBy = _context.UserName ?? string.Empty;
xyzEntity.UpdatedBy = _context.UserName ?? string.Empty;
}
else if (entity is IxyzEntityNull xyzEntityNull)
{
xyzEntityNull.CreatedDate = DateTime.Now;
xyzEntityNull.UpdatedDate = xyzEntityNull.CreatedDate;
xyzEntityNull.CreatedBy = _context.UserName;
xyzEntityNull.UpdatedBy = _context.UserName;
}
_dbSet.Add(entity);
_context.SyncObjectState(entity);
}
LoadDataToOrder.cs
public async Task<int> LoadDataToOrder(int batchId)
{
// using (var unitOfWork = new UnitOfWork(_abcContext))
// {
var orderRepo = new OrderRepository(_abcContext);
Entities.Order order = new Entities.Order();
order.Guid = Guid.NewGuid();
order.BatchId = batchId;
order.VendorId = null;
orderRepo.Insert(order);
//unitOfWork.SaveChanges();
await _abcContext.SaveChangesAsync();
return order.OrderId;
//
}
}
HangfireActivator.cs
public class HangFireActivator : Hangfire.JobActivator
{
private readonly IServiceProvider _serviceProvider;
public HangFireActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}
Please advise.
Thanks.
Following solutions worked for the 2 problems:
Implementation of multiple Hangfire processes/jobs(multiple workers) to work (in parallel).
Answer: This got resolved when I used the built-in AspNetCoreJobActivator instead that's available out of the box, i.e. removed the HangfireActivator class and removed the call to the UseActivator method.
var scopeFactory = serviceProvider.GetService<IServiceScopeFactory>();
if (scopeFactory != null)
GlobalConfiguration.Configuration.UseActivator(new AspNetCoreJobActivator(scopeFactory));
SqlAzureExecutionStrategy Exception in CreateOrder.cs (transaction was deadlocked)
Answer: Resolved this issue by retrying the query automatically when deadlock occurs.
Thanks odinserj for the suggestions.

Hangfire dependency injection with .NET Core

How can I use .NET Core's default dependency injection in Hangfire?
I am new to Hangfire and searching for an example which works with ASP.NET Core.
See full example on GitHub https://github.com/gonzigonz/HangfireCore-Example.
Live site at http://hangfirecore.azurewebsites.net/
Make sure you have the Core version of Hangfire:
dotnet add package Hangfire.AspNetCore
Configure your IoC by defining a JobActivator. Below is the config for use with the default asp.net core container service:
public class HangfireActivator : Hangfire.JobActivator
{
private readonly IServiceProvider _serviceProvider;
public HangfireActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}
Next register hangfire as a service in the Startup.ConfigureServices method:
services.AddHangfire(opt =>
opt.UseSqlServerStorage("Your Hangfire Connection string"));
Configure hangfire in the Startup.Configure method. In relationship to your question, the key is to configure hangfire to use the new HangfireActivator we just defined above. To do so you will have to provide hangfire with the IServiceProvider and this can be achieved by just adding it to the list of parameters for the Configure method. At runtime, DI will providing this service for you:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IServiceProvider serviceProvider)
{
...
// Configure hangfire to use the new JobActivator we defined.
GlobalConfiguration.Configuration
.UseActivator(new HangfireActivator(serviceProvider));
// The rest of the hangfire config as usual.
app.UseHangfireServer();
app.UseHangfireDashboard();
}
When you enqueue a job, use the registered type which usually is your interface. Don't use a concrete type unless you registered it that way. You must use the type registered with your IoC else Hangfire won't find it.
For Example say you've registered the following services:
services.AddScoped<DbManager>();
services.AddScoped<IMyService, MyService>();
Then you could enqueue DbManager with an instantiated version of the class:
BackgroundJob.Enqueue(() => dbManager.DoSomething());
However you could not do the same with MyService. Enqueuing with an instantiated version would fail because DI would fail as only the interface is registered. In this case you would enqueue like this:
BackgroundJob.Enqueue<IMyService>( ms => ms.DoSomething());
DoritoBandito's answer is incomplete or deprecated.
public class EmailSender {
public EmailSender(IDbContext dbContext, IEmailService emailService)
{
_dbContext = dbContext;
_emailService = emailService;
}
}
Register services:
services.AddTransient<IDbContext, TestDbContext>();
services.AddTransient<IEmailService, EmailService>();
Enqueue:
BackgroundJob.Enqueue<EmailSender>(x => x.Send(13, "Hello!"));
Source:
http://docs.hangfire.io/en/latest/background-methods/passing-dependencies.html
Note: if you want a full sample, see my blog post on this.
All of the answers in this thread are wrong/incomplete/outdated. Here's an example with ASP.NET Core 3.1 and Hangfire.AspnetCore 1.7.
Client:
//...
using Hangfire;
// ...
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddHangfire(config =>
{
// configure hangfire per your requirements
});
}
}
public class SomeController : ControllerBase
{
private readonly IBackgroundJobClient _backgroundJobClient;
public SomeController(IBackgroundJobClient backgroundJobClient)
{
_backgroundJobClient = backgroundJobClient;
}
[HttpPost("some-route")]
public IActionResult Schedule([FromBody] SomeModel model)
{
_backgroundJobClient.Schedule<SomeClass>(s => s.Execute(model));
}
}
Server (same or different application):
{
//...
services.AddScoped<ISomeDependency, SomeDependency>();
services.AddHangfire(hangfireConfiguration =>
{
// configure hangfire with the same backing storage as your client
});
services.AddHangfireServer();
}
public interface ISomeDependency { }
public class SomeDependency : ISomeDependency { }
public class SomeClass
{
private readonly ISomeDependency _someDependency;
public SomeClass(ISomeDependency someDependency)
{
_someDependency = someDependency;
}
// the function scheduled in SomeController
public void Execute(SomeModel someModel)
{
}
}
As far as I am aware, you can use .net cores dependency injection the same as you would for any other service.
You can use a service which contains the jobs to be executed, which can be executed like so
var jobId = BackgroundJob.Enqueue(x => x.SomeTask(passParamIfYouWish));
Here is an example of the Job Service class
public class JobService : IJobService
{
private IClientService _clientService;
private INodeServices _nodeServices;
//Constructor
public JobService(IClientService clientService, INodeServices nodeServices)
{
_clientService = clientService;
_nodeServices = nodeServices;
}
//Some task to execute
public async Task SomeTask(Guid subject)
{
// Do some job here
Client client = _clientService.FindUserBySubject(subject);
}
}
And in your projects Startup.cs you can add a dependency as normal
services.AddTransient< IClientService, ClientService>();
Not sure this answers your question or not
Currently, Hangfire is deeply integrated with Asp.Net Core. Install Hangfire.AspNetCore to set up the dashboard and DI integration automatically. Then, you just need to define your dependencies using ASP.NET core as always.
If you are trying to quickly set up Hangfire with ASP.NET Core (tested in ASP.NET Core 2.2) you can also use Hangfire.MemoryStorage. All the configuration can be performed in Startup.cs:
using Hangfire;
using Hangfire.MemoryStorage;
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(opt => opt.UseMemoryStorage());
JobStorage.Current = new MemoryStorage();
}
protected void StartHangFireJobs(IApplicationBuilder app, IServiceProvider serviceProvider)
{
app.UseHangfireServer();
app.UseHangfireDashboard();
//TODO: move cron expressions to appsettings.json
RecurringJob.AddOrUpdate<SomeJobService>(
x => x.DoWork(),
"* * * * *");
RecurringJob.AddOrUpdate<OtherJobService>(
x => x.DoWork(),
"0 */2 * * *");
}
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
StartHangFireJobs(app, serviceProvider)
}
Of course, everything is store in memory and it is lost once the application pool is recycled, but it is a quick way to see that everything works as expected with minimal configuration.
To switch to SQL Server database persistence, you should install Hangfire.SqlServer package and simply configure it instead of the memory storage:
services.AddHangfire(opt => opt.UseSqlServerStorage(Configuration.GetConnectionString("Default")));
I had to start HangFire in main function. This is how I solved it:
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var liveDataHelper = services.GetRequiredService<ILiveDataHelper>();
var justInitHangfire = services.GetRequiredService<IBackgroundJobClient>();
//This was causing an exception (HangFire is not initialized)
RecurringJob.AddOrUpdate(() => liveDataHelper.RePopulateAllConfigDataAsync(), Cron.Daily());
// Use the context here
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Can't start " + nameof(LiveDataHelper));
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Actually there is an easy way for dependency injection based job registration.
You just need to use the following code in your Startup:
public class Startup {
public void Configure(IApplicationBuilder app)
{
var factory = app.ApplicationServices
.GetService<IServiceScopeFactory>();
GlobalConfiguration.Configuration.UseActivator(
new Hangfire.AspNetCore.AspNetCoreJobActivator(factory));
}
}
However i personally wanted a job self registration including on demand jobs (recurring jobs which are never executed, except by manual trigger on hangfire dashboard), which was a little more complex then just that. I was (for example) facing issues with the job service activation, which is why i decided to share most of my implementation code.
//I wanted an interface to declare my jobs, including the job Id.
public interface IBackgroundJob {
string Id { get; set; }
void Invoke();
}
//I wanted to retrieve the jobs by id. Heres my extension method for that:
public static IBackgroundJob GetJob(
this IServiceProvider provider,
string jobId) => provider
.GetServices<IBackgroundJob>()
.SingleOrDefault(j => j.Id == jobId);
//Now i needed an invoker for these jobs.
//The invoker is basically an example of a dependency injected hangfire job.
internal class JobInvoker {
public JobInvoker(IServiceScopeFactory factory) {
Factory = factory;
}
public IServiceScopeFactory Factory { get; }
public void Invoke(string jobId)
{
//hangfire jobs should always be executed within their own scope.
//The default AspNetCoreJobActivator should technically already do that.
//Lets just say i have trust issues.
using (var scope = Factory.CreateScope())
{
scope.ServiceProvider
.GetJob(jobId)?
.Invoke();
}
}
//Now i needed to tell hangfire to use these jobs.
//Reminder: The serviceProvider is in IApplicationBuilder.ApplicationServices
public static void RegisterJobs(IServiceProvider serviceProvider) {
var factory = serviceProvider.GetService();
GlobalConfiguration.Configuration.UseActivator(new Hangfire.AspNetCore.AspNetCoreJobActivator(factory));
var manager = serviceProvider.GetService<IRecurringJobManager>();
var config = serviceProvider.GetService<IConfiguration>();
var jobs = serviceProvider.GetServices<IBackgroundJob>();
foreach (var job in jobs) {
var jobConfig = config.GetJobConfig(job.Id);
var schedule = jobConfig?.Schedule; //this is a cron expression
if (String.IsNullOrWhiteSpace(schedule))
schedule = Cron.Never(); //this is an on demand job only!
manager.AddOrUpdate(
recurringJobId: job.Id,
job: GetJob(job.Id),
cronExpression: schedule);
}
//and last but not least...
//My Method for creating the hangfire job with injected job id
private static Job GetJob(string jobId)
{
var type = typeof(JobInvoker);
var method = type.GetMethod("Invoke");
return new Job(
type: type,
method: method,
args: jobId);
}
Using the above code i was able to create hangfire job services with full dependency injection support. Hope it helps someone.
Use the below code for Hangfire configuration
using eForms.Core;
using Hangfire;
using Hangfire.SqlServer;
using System;
using System.ComponentModel;
using System.Web.Hosting;
namespace eForms.AdminPanel.Jobs
{
public class JobManager : IJobManager, IRegisteredObject
{
public static readonly JobManager Instance = new JobManager();
//private static readonly TimeSpan ZeroTimespan = new TimeSpan(0, 0, 10);
private static readonly object _lockObject = new Object();
private bool _started;
private BackgroundJobServer _backgroundJobServer;
private JobManager()
{
}
public int Schedule(JobInfo whatToDo)
{
int result = 0;
if (!whatToDo.IsRecurring)
{
if (whatToDo.Delay == TimeSpan.Zero)
int.TryParse(BackgroundJob.Enqueue(() => Run(whatToDo.JobId, whatToDo.JobType.AssemblyQualifiedName)), out result);
else
int.TryParse(BackgroundJob.Schedule(() => Run(whatToDo.JobId, whatToDo.JobType.AssemblyQualifiedName), whatToDo.Delay), out result);
}
else
{
RecurringJob.AddOrUpdate(whatToDo.JobType.Name, () => RunRecurring(whatToDo.JobType.AssemblyQualifiedName), Cron.MinuteInterval(whatToDo.Delay.TotalMinutes.AsInt()));
}
return result;
}
[DisplayName("Id: {0}, Type: {1}")]
[HangFireYearlyExpirationTime]
public static void Run(int jobId, string jobType)
{
try
{
Type runnerType;
if (!jobType.ToType(out runnerType)) throw new Exception("Provided job has undefined type");
var runner = runnerType.CreateInstance<JobRunner>();
runner.Run(jobId);
}
catch (Exception ex)
{
throw new JobException($"Error while executing Job Id: {jobId}, Type: {jobType}", ex);
}
}
[DisplayName("{0}")]
[HangFireMinutelyExpirationTime]
public static void RunRecurring(string jobType)
{
try
{
Type runnerType;
if (!jobType.ToType(out runnerType)) throw new Exception("Provided job has undefined type");
var runner = runnerType.CreateInstance<JobRunner>();
runner.Run(0);
}
catch (Exception ex)
{
throw new JobException($"Error while executing Recurring Type: {jobType}", ex);
}
}
public void Start()
{
lock (_lockObject)
{
if (_started) return;
if (!AppConfigSettings.EnableHangFire) return;
_started = true;
HostingEnvironment.RegisterObject(this);
GlobalConfiguration.Configuration
.UseSqlServerStorage("SqlDbConnection", new SqlServerStorageOptions { PrepareSchemaIfNecessary = false })
//.UseFilter(new HangFireLogFailureAttribute())
.UseLog4NetLogProvider();
//Add infinity Expiration job filter
//GlobalJobFilters.Filters.Add(new HangFireProlongExpirationTimeAttribute());
//Hangfire comes with a retry policy that is automatically set to 10 retry and backs off over several mins
//We in the following remove this attribute and add our own custom one which adds significant backoff time
//custom logic to determine how much to back off and what to to in the case of fails
// The trick here is we can't just remove the filter as you'd expect using remove
// we first have to find it then save the Instance then remove it
try
{
object automaticRetryAttribute = null;
//Search hangfire automatic retry
foreach (var filter in GlobalJobFilters.Filters)
{
if (filter.Instance is Hangfire.AutomaticRetryAttribute)
{
// found it
automaticRetryAttribute = filter.Instance;
System.Diagnostics.Trace.TraceError("Found hangfire automatic retry");
}
}
//Remove default hangefire automaticRetryAttribute
if (automaticRetryAttribute != null)
GlobalJobFilters.Filters.Remove(automaticRetryAttribute);
//Add custom retry job filter
GlobalJobFilters.Filters.Add(new HangFireCustomAutoRetryJobFilterAttribute());
}
catch (Exception) { }
_backgroundJobServer = new BackgroundJobServer(new BackgroundJobServerOptions
{
HeartbeatInterval = new System.TimeSpan(0, 1, 0),
ServerCheckInterval = new System.TimeSpan(0, 1, 0),
SchedulePollingInterval = new System.TimeSpan(0, 1, 0)
});
}
}
public void Stop()
{
lock (_lockObject)
{
if (_backgroundJobServer != null)
{
_backgroundJobServer.Dispose();
}
HostingEnvironment.UnregisterObject(this);
}
}
void IRegisteredObject.Stop(bool immediate)
{
Stop();
}
}
}
Admin Job Manager
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
if (Core.AppConfigSettings.EnableHangFire)
{
JobManager.Instance.Start();
new SchedulePendingSmsNotifications().Schedule(new Core.JobInfo() { JobId = 0, JobType = typeof(SchedulePendingSmsNotifications), Delay = TimeSpan.FromMinutes(1), IsRecurring = true });
}
}
protected void Application_End(object sender, EventArgs e)
{
if (Core.AppConfigSettings.EnableHangFire)
{
JobManager.Instance.Stop();
}
}
}

Azure - Structuremap. How to pass a dependency to a class that implements IEventProcessor(Event Hub)

I am using azure event hub and I am trying to work out how pass in dependencies into the EventProcessor class used to process events off the event hub in my worker role. This class inherits the .net interface IEventProcessor.
My event processor class is shown below. I am struggling using structure map to inject the OrchestrationService and its dependancies in through constructor injection.
Any suggestions will be gratefully accepted :-)
public class EventHubEventProcessor : IEventProcessor
{
private readonly IOrchestrationService _orchestrationService;
private readonly IEventReceiver _eventReceiver;
IDictionary<string, int> map;
PartitionContext partitionContext;
Stopwatch checkpointStopWatch;
public EventHubEventProcessor(IOrchestrationService orchestrationService)
{
_orchestrationService = orchestrationService;
}
public Task OpenAsync(PartitionContext context)
{
Console.WriteLine(string.Format("SimpleEventProcessor initialize. Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset));
this.partitionContext = context;
this.checkpointStopWatch = new Stopwatch();
this.checkpointStopWatch.Start();
return Task.FromResult<object>(null);
}
public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> events)
{
try
{
foreach (EventData eventData in events)
{
_orchestrationService.Orchestrate(eventData);
Console.WriteLine("Processed Event " + eventData.PartitionKey);
}
//Call checkpoint every 5 minutes, so that worker can resume processing from the 5 minutes back if it restarts.
if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5))
{
await context.CheckpointAsync();
this.checkpointStopWatch.Restart();
}
}
catch (Exception exp)
{
Console.WriteLine("Error in processing: " + exp.Message);
}
}
public async Task CloseAsync(PartitionContext context, CloseReason reason)
{
Console.WriteLine(string.Format("Processor Shuting Down. Partition '{0}', Reason: '{1}'.", this.partitionContext.Lease.PartitionId, reason.ToString()));
if (reason == CloseReason.Shutdown)
{
await context.CheckpointAsync();
}
}
}
}
Perhaps you need to implement IEventProcessorFactory:
https://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.ieventprocessorfactory.aspx
...and pass an instance of that into EventProcessorHost.RegisterEventProcessorFactoryAsync():
https://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.eventprocessorhost.registereventprocessorfactoryasync.aspx
That way your factory can handle the StructureMap magic and inject OrchestrationService as needed.

Categories

Resources