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()
.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();

View File

@ -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();

View File

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

View File

@ -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,11 +66,61 @@ 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<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);
}
}

View File

@ -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();