From 905b4b06da3066d0740f350cb81ec9b0827523d6 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Mon, 22 Mar 2021 20:54:12 +0100 Subject: [PATCH] Add identifiers to Triggers and Jobs (#1230) --- src/Admin/Jobs/JobsHostedService.cs | 5 ++ src/Api/Jobs/JobsHostedService.cs | 5 ++ src/Billing/Jobs/JobsHostedService.cs | 1 + src/Core/Jobs/BaseJobsHostedService.cs | 57 +++++++++++++++++++-- src/Notifications/Jobs/JobsHostedService.cs | 1 + 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/Admin/Jobs/JobsHostedService.cs b/src/Admin/Jobs/JobsHostedService.cs index 72bf1d395..06842eaca 100644 --- a/src/Admin/Jobs/JobsHostedService.cs +++ b/src/Admin/Jobs/JobsHostedService.cs @@ -31,22 +31,27 @@ namespace Bit.Admin.Jobs } var everyTopOfTheHourTrigger = TriggerBuilder.Create() + .WithIdentity("EveryTopOfTheHourTrigger") .StartNow() .WithCronSchedule("0 0 * * * ?") .Build(); var everyFiveMinutesTrigger = TriggerBuilder.Create() + .WithIdentity("EveryFiveMinutesTrigger") .StartNow() .WithCronSchedule("0 */5 * * * ?") .Build(); var everyFridayAt10pmTrigger = TriggerBuilder.Create() + .WithIdentity("EveryFridayAt10pmTrigger") .StartNow() .WithCronSchedule("0 0 22 ? * FRI", x => x.InTimeZone(timeZone)) .Build(); var everySaturdayAtMidnightTrigger = TriggerBuilder.Create() + .WithIdentity("EverySaturdayAtMidnightTrigger") .StartNow() .WithCronSchedule("0 0 0 ? * SAT", x => x.InTimeZone(timeZone)) .Build(); var everySundayAtMidnightTrigger = TriggerBuilder.Create() + .WithIdentity("EverySundayAtMidnightTrigger") .StartNow() .WithCronSchedule("0 0 0 ? * SUN", x => x.InTimeZone(timeZone)) .Build(); diff --git a/src/Api/Jobs/JobsHostedService.cs b/src/Api/Jobs/JobsHostedService.cs index 06d1d4dad..2b71f6c0a 100644 --- a/src/Api/Jobs/JobsHostedService.cs +++ b/src/Api/Jobs/JobsHostedService.cs @@ -22,22 +22,27 @@ namespace Bit.Api.Jobs public override async Task StartAsync(CancellationToken cancellationToken) { var everyTopOfTheHourTrigger = TriggerBuilder.Create() + .WithIdentity("EveryTopOfTheHourTrigger") .StartNow() .WithCronSchedule("0 0 * * * ?") .Build(); var emergencyAccessNotificationTrigger = TriggerBuilder.Create() + .WithIdentity("EmergencyAccessNotificationTrigger") .StartNow() .WithCronSchedule("0 0 * * * ?") .Build(); var emergencyAccessTimeoutTrigger = TriggerBuilder.Create() + .WithIdentity("EmergencyAccessTimeoutTrigger") .StartNow() .WithCronSchedule("0 0 * * * ?") .Build(); var everyTopOfTheSixthHourTrigger = TriggerBuilder.Create() + .WithIdentity("EveryTopOfTheSixthHourTrigger") .StartNow() .WithCronSchedule("0 0 */6 * * ?") .Build(); var everyTwelfthHourAndThirtyMinutesTrigger = TriggerBuilder.Create() + .WithIdentity("EveryTwelfthHourAndThirtyMinutesTrigger") .StartNow() .WithCronSchedule("0 30 */12 * * ?") .Build(); diff --git a/src/Billing/Jobs/JobsHostedService.cs b/src/Billing/Jobs/JobsHostedService.cs index 9954a0dd0..01cbadac7 100644 --- a/src/Billing/Jobs/JobsHostedService.cs +++ b/src/Billing/Jobs/JobsHostedService.cs @@ -31,6 +31,7 @@ namespace Bit.Billing.Jobs } var everyDayAtNinePmTrigger = TriggerBuilder.Create() + .WithIdentity("EveryDayAtNinePmTrigger") .StartNow() .WithCronSchedule("0 0 21 * * ?", x => x.InTimeZone(timeZone)) .Build(); diff --git a/src/Core/Jobs/BaseJobsHostedService.cs b/src/Core/Jobs/BaseJobsHostedService.cs index 64c4233ef..72758091a 100644 --- a/src/Core/Jobs/BaseJobsHostedService.cs +++ b/src/Core/Jobs/BaseJobsHostedService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; @@ -65,12 +66,62 @@ namespace Bit.Core.Jobs await _scheduler.Start(cancellationToken); if (Jobs != null) { - foreach (var job in Jobs) + foreach (var (job, trigger) in Jobs) { - var builtJob = JobBuilder.Create(job.Item1).Build(); - await _scheduler.ScheduleJob(builtJob, job.Item2); + var dupeT = await _scheduler.GetTrigger(trigger.Key); + if (dupeT != null) + { + await _scheduler.RescheduleJob(trigger.Key, trigger); + } + + var jobDetail = JobBuilder.Create(job) + .WithIdentity(job.FullName) + .Build(); + + var dupeJ = await _scheduler.GetJobDetail(jobDetail.Key); + if (dupeJ != null) + { + await _scheduler.DeleteJob(jobDetail.Key); + } + + await _scheduler.ScheduleJob(jobDetail, trigger); } } + + // Delete old Jobs and Triggers + var existingJobKeys = await _scheduler.GetJobKeys(GroupMatcher.AnyGroup()); + var jobKeys = Jobs.Select(j => + { + var job = j.Item1; + return JobBuilder.Create(job) + .WithIdentity(job.FullName) + .Build().Key; + }); + + foreach (var key in existingJobKeys) + { + if (jobKeys.Contains(key)) + { + continue; + } + + _logger.LogInformation($"Deleting old job with key {key}"); + await _scheduler.DeleteJob(key); + } + + var existingTriggerKeys = await _scheduler.GetTriggerKeys(GroupMatcher.AnyGroup()); + var triggerKeys = Jobs.Select(j => j.Item2.Key); + + foreach (var key in existingTriggerKeys) + { + if (triggerKeys.Contains(key)) + { + continue; + } + + _logger.LogInformation($"Unscheduling old trigger with key {key}"); + await _scheduler.UnscheduleJob(key); + } } public virtual async Task StopAsync(CancellationToken cancellationToken) diff --git a/src/Notifications/Jobs/JobsHostedService.cs b/src/Notifications/Jobs/JobsHostedService.cs index e59b0939a..3d78083c6 100644 --- a/src/Notifications/Jobs/JobsHostedService.cs +++ b/src/Notifications/Jobs/JobsHostedService.cs @@ -22,6 +22,7 @@ namespace Bit.Notifications.Jobs public override async Task StartAsync(CancellationToken cancellationToken) { var everyFiveMinutesTrigger = TriggerBuilder.Create() + .WithIdentity("EveryFiveMinutesTrigger") .StartNow() .WithCronSchedule("0 */30 * * * ?") .Build();