From b329378450ced0f1b9988e3a271835412677e0cc Mon Sep 17 00:00:00 2001 From: ntxinh Date: Sun, 3 Dec 2023 09:38:09 +0700 Subject: [PATCH] :star: Update CronProvider --- .../Providers/Crons/CronProvider.cs | 46 +++++++++++++++---- .../Providers/Crons/ICronProvider.cs | 5 +- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Src/DDD.Domain/Providers/Crons/CronProvider.cs b/Src/DDD.Domain/Providers/Crons/CronProvider.cs index 797a0a6..d1fd20a 100644 --- a/Src/DDD.Domain/Providers/Crons/CronProvider.cs +++ b/Src/DDD.Domain/Providers/Crons/CronProvider.cs @@ -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 jobData = new Dictionary + // { + // { nameof(NotifyInactiveUserConsumerModel.Data), payload.Data }, + // { nameof(NotifyInactiveUserConsumerModel.TenantId), payload.TenantId }, + // { nameof(NotifyInactiveUserConsumerModel.UserId), payload.UserId }, + // }; + + // var job = JobBuilder.Create() + // .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(IDictionary jobData) where T : IJob { IScheduler sched = await _schedulerFactory.GetScheduler(); - IDictionary jobData = new Dictionary - { - { nameof(NotifyInactiveUserConsumerModel.Data), payload.Data }, - { nameof(NotifyInactiveUserConsumerModel.TenantId), payload.TenantId }, - { nameof(NotifyInactiveUserConsumerModel.UserId), payload.UserId }, - }; + var jobKey = typeof(T).Name; - var job = JobBuilder.Create() - .WithIdentity(nameof(NotifyInactiveUserJob)) + var job = JobBuilder.Create() + .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 + // { + // { nameof(NotifyInactiveUserConsumerModel.Data), payload.Data }, + // { nameof(NotifyInactiveUserConsumerModel.TenantId), payload.TenantId }, + // { nameof(NotifyInactiveUserConsumerModel.UserId), payload.UserId }, + // }; + // await _quartzProvider.OneOffJob(jobData); } } diff --git a/Src/DDD.Domain/Providers/Crons/ICronProvider.cs b/Src/DDD.Domain/Providers/Crons/ICronProvider.cs index 82efafd..6fe5724 100644 --- a/Src/DDD.Domain/Providers/Crons/ICronProvider.cs +++ b/Src/DDD.Domain/Providers/Crons/ICronProvider.cs @@ -1,4 +1,6 @@ +using System.Collections.Generic; using System.Threading.Tasks; +using Quartz; namespace DDD.Domain.Providers.Crons; @@ -6,5 +8,6 @@ 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(IDictionary jobData) where T : IJob; }