Skip to content

Commit

Permalink
⭐ Update CronProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ntxinh committed Dec 3, 2023
1 parent 357d892 commit b329378
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
46 changes: 36 additions & 10 deletions Src/DDD.Domain/Providers/Crons/CronProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,51 @@ public CronProvider(ISchedulerFactory schedulerFactory)
_schedulerFactory = schedulerFactory;
}

public async Task NotifyInactiveUser(NotifyInactiveUserConsumerModel payload)
// public async Task NotifyInactiveUser(NotifyInactiveUserConsumerModel payload)
// {
// IScheduler sched = await _schedulerFactory.GetScheduler();

// IDictionary<string, object> jobData = new Dictionary<string, object>
// {
// { nameof(NotifyInactiveUserConsumerModel.Data), payload.Data },
// { nameof(NotifyInactiveUserConsumerModel.TenantId), payload.TenantId },
// { nameof(NotifyInactiveUserConsumerModel.UserId), payload.UserId },
// };

// var job = JobBuilder.Create<NotifyInactiveUserJob>()
// .WithIdentity(nameof(NotifyInactiveUserJob))
// .Build();

// var replace = true;
// var durable = true;
// await sched.AddJob(job, replace, durable);

// await sched.TriggerJob(new JobKey(nameof(NotifyInactiveUserJob)), new JobDataMap(jobData));
// }

public async Task OneOffJob<T>(IDictionary<string, object> jobData) where T : IJob
{
IScheduler sched = await _schedulerFactory.GetScheduler();

IDictionary<string, object> jobData = new Dictionary<string, object>
{
{ nameof(NotifyInactiveUserConsumerModel.Data), payload.Data },
{ nameof(NotifyInactiveUserConsumerModel.TenantId), payload.TenantId },
{ nameof(NotifyInactiveUserConsumerModel.UserId), payload.UserId },
};
var jobKey = typeof(T).Name;

var job = JobBuilder.Create<NotifyInactiveUserJob>()
.WithIdentity(nameof(NotifyInactiveUserJob))
var job = JobBuilder.Create<T>()
.WithIdentity(jobKey)
.Build();

var replace = true;
var durable = true;
await sched.AddJob(job, replace, durable);

await sched.TriggerJob(new JobKey(nameof(NotifyInactiveUserJob)), new JobDataMap(jobData));
await sched.TriggerJob(new JobKey(jobKey), new JobDataMap(jobData));

// How to use
// var jobData = new Dictionary<string, object>
// {
// { nameof(NotifyInactiveUserConsumerModel.Data), payload.Data },
// { nameof(NotifyInactiveUserConsumerModel.TenantId), payload.TenantId },
// { nameof(NotifyInactiveUserConsumerModel.UserId), payload.UserId },
// };
// await _quartzProvider.OneOffJob<NotifyInactiveUserJob>(jobData);
}
}
5 changes: 4 additions & 1 deletion Src/DDD.Domain/Providers/Crons/ICronProvider.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Quartz;

namespace DDD.Domain.Providers.Crons;

public interface ICronProvider
{
// Fire and Forget, One-Off Job
// https://www.quartz-scheduler.net/documentation/quartz-3.x/how-tos/one-off-job.html
Task NotifyInactiveUser(NotifyInactiveUserConsumerModel payload);
// Task NotifyInactiveUser(NotifyInactiveUserConsumerModel payload);
Task OneOffJob<T>(IDictionary<string, object> jobData) where T : IJob;
}

0 comments on commit b329378

Please sign in to comment.