1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

Add identifiers to Triggers and Jobs (#1230)

This commit is contained in:
Oscar Hinton 2021-03-22 20:54:12 +01:00 committed by GitHub
parent 7309a37bdc
commit 905b4b06da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 3 deletions

View File

@ -31,22 +31,27 @@ namespace Bit.Admin.Jobs
} }
var everyTopOfTheHourTrigger = TriggerBuilder.Create() var everyTopOfTheHourTrigger = TriggerBuilder.Create()
.WithIdentity("EveryTopOfTheHourTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 * * * ?") .WithCronSchedule("0 0 * * * ?")
.Build(); .Build();
var everyFiveMinutesTrigger = TriggerBuilder.Create() var everyFiveMinutesTrigger = TriggerBuilder.Create()
.WithIdentity("EveryFiveMinutesTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 */5 * * * ?") .WithCronSchedule("0 */5 * * * ?")
.Build(); .Build();
var everyFridayAt10pmTrigger = TriggerBuilder.Create() var everyFridayAt10pmTrigger = TriggerBuilder.Create()
.WithIdentity("EveryFridayAt10pmTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 22 ? * FRI", x => x.InTimeZone(timeZone)) .WithCronSchedule("0 0 22 ? * FRI", x => x.InTimeZone(timeZone))
.Build(); .Build();
var everySaturdayAtMidnightTrigger = TriggerBuilder.Create() var everySaturdayAtMidnightTrigger = TriggerBuilder.Create()
.WithIdentity("EverySaturdayAtMidnightTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 0 ? * SAT", x => x.InTimeZone(timeZone)) .WithCronSchedule("0 0 0 ? * SAT", x => x.InTimeZone(timeZone))
.Build(); .Build();
var everySundayAtMidnightTrigger = TriggerBuilder.Create() var everySundayAtMidnightTrigger = TriggerBuilder.Create()
.WithIdentity("EverySundayAtMidnightTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 0 ? * SUN", x => x.InTimeZone(timeZone)) .WithCronSchedule("0 0 0 ? * SUN", x => x.InTimeZone(timeZone))
.Build(); .Build();

View File

@ -22,22 +22,27 @@ namespace Bit.Api.Jobs
public override async Task StartAsync(CancellationToken cancellationToken) public override async Task StartAsync(CancellationToken cancellationToken)
{ {
var everyTopOfTheHourTrigger = TriggerBuilder.Create() var everyTopOfTheHourTrigger = TriggerBuilder.Create()
.WithIdentity("EveryTopOfTheHourTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 * * * ?") .WithCronSchedule("0 0 * * * ?")
.Build(); .Build();
var emergencyAccessNotificationTrigger = TriggerBuilder.Create() var emergencyAccessNotificationTrigger = TriggerBuilder.Create()
.WithIdentity("EmergencyAccessNotificationTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 * * * ?") .WithCronSchedule("0 0 * * * ?")
.Build(); .Build();
var emergencyAccessTimeoutTrigger = TriggerBuilder.Create() var emergencyAccessTimeoutTrigger = TriggerBuilder.Create()
.WithIdentity("EmergencyAccessTimeoutTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 * * * ?") .WithCronSchedule("0 0 * * * ?")
.Build(); .Build();
var everyTopOfTheSixthHourTrigger = TriggerBuilder.Create() var everyTopOfTheSixthHourTrigger = TriggerBuilder.Create()
.WithIdentity("EveryTopOfTheSixthHourTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 */6 * * ?") .WithCronSchedule("0 0 */6 * * ?")
.Build(); .Build();
var everyTwelfthHourAndThirtyMinutesTrigger = TriggerBuilder.Create() var everyTwelfthHourAndThirtyMinutesTrigger = TriggerBuilder.Create()
.WithIdentity("EveryTwelfthHourAndThirtyMinutesTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 30 */12 * * ?") .WithCronSchedule("0 30 */12 * * ?")
.Build(); .Build();

View File

@ -31,6 +31,7 @@ namespace Bit.Billing.Jobs
} }
var everyDayAtNinePmTrigger = TriggerBuilder.Create() var everyDayAtNinePmTrigger = TriggerBuilder.Create()
.WithIdentity("EveryDayAtNinePmTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 0 21 * * ?", x => x.InTimeZone(timeZone)) .WithCronSchedule("0 0 21 * * ?", x => x.InTimeZone(timeZone))
.Build(); .Build();

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -65,12 +66,62 @@ namespace Bit.Core.Jobs
await _scheduler.Start(cancellationToken); await _scheduler.Start(cancellationToken);
if (Jobs != null) if (Jobs != null)
{ {
foreach (var job in Jobs) foreach (var (job, trigger) in Jobs)
{ {
var builtJob = JobBuilder.Create(job.Item1).Build(); var dupeT = await _scheduler.GetTrigger(trigger.Key);
await _scheduler.ScheduleJob(builtJob, job.Item2); 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<JobKey>.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<TriggerKey>.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) public virtual async Task StopAsync(CancellationToken cancellationToken)

View File

@ -22,6 +22,7 @@ namespace Bit.Notifications.Jobs
public override async Task StartAsync(CancellationToken cancellationToken) public override async Task StartAsync(CancellationToken cancellationToken)
{ {
var everyFiveMinutesTrigger = TriggerBuilder.Create() var everyFiveMinutesTrigger = TriggerBuilder.Create()
.WithIdentity("EveryFiveMinutesTrigger")
.StartNow() .StartNow()
.WithCronSchedule("0 */30 * * * ?") .WithCronSchedule("0 */30 * * * ?")
.Build(); .Build();