-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb11bc1
commit 8237d99
Showing
10 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Quartz; | ||
using Quartz.Spi; | ||
|
||
namespace Houston.Bot.Crons; | ||
|
||
public class JobFactory : IJobFactory | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public JobFactory(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) | ||
{ | ||
var jobDetail = bundle.JobDetail; | ||
var jobType = jobDetail.JobType; | ||
var jobInstance = _serviceProvider.GetService(jobType); | ||
|
||
if (jobInstance == null || !(jobInstance is IJob)) | ||
{ | ||
throw new SchedulerException($"Failed to create job of type {jobType.FullName}"); | ||
} | ||
|
||
return jobInstance as IJob; | ||
} | ||
|
||
public void ReturnJob(IJob job) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Quartz; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Houston.Bot.Common; | ||
using Microsoft.Extensions.Logging; | ||
using Discord.WebSocket; | ||
using Houston.Bot.Services; | ||
|
||
namespace Houston.Bot.Crons.Jobs; | ||
|
||
[DisallowConcurrentExecution] | ||
public class ExampleJob : IJob | ||
{ | ||
private readonly ILogger<ExampleJob> _logger; | ||
public ExampleJob(ILogger<ExampleJob> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task Execute(IJobExecutionContext context) | ||
{ | ||
try | ||
{ | ||
Console.WriteLine("ExampleJob executed"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error while cleaning up expired keys"); | ||
} | ||
return Task.CompletedTask; | ||
} | ||
|
||
public static ITrigger GetTrigger() | ||
{ | ||
return TriggerBuilder.Create() | ||
.WithIdentity("ExampleJob-trigger") | ||
.WithSimpleSchedule(x => | ||
{ | ||
x.WithRepeatCount(0); | ||
x.WithMisfireHandlingInstructionNextWithRemainingCount(); | ||
}) | ||
.StartAt(DateTimeOffset.Now.AddSeconds(15)) | ||
.ForJob("ExampleJob") | ||
.Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Houston.Bot.Crons.Jobs; | ||
using Houston.Bot.Services; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Quartz; | ||
|
||
namespace Houston.Bot.Crons; | ||
|
||
public class ScheduleJobs | ||
{ | ||
private readonly JobSchedulerService _jobSchedulerService; | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public ScheduleJobs(JobSchedulerService jobSchedulerService, IServiceProvider serviceProvider) | ||
{ | ||
_jobSchedulerService = jobSchedulerService; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public async Task ScheduleAllJobs() | ||
{ | ||
await _jobSchedulerService.ScheduleJob<ExampleJob>("ExampleJob", ExampleJob.GetTrigger()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Quartz; | ||
using Quartz.Impl; | ||
using Houston.Bot.Crons; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Quartz.Spi; | ||
using System; | ||
|
||
namespace Houston.Bot.Services; | ||
|
||
public class JobSchedulerService | ||
{ | ||
private readonly IScheduler _scheduler; | ||
private readonly IJobFactory _jobFactory; | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public JobSchedulerService(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_scheduler = new StdSchedulerFactory().GetScheduler().GetAwaiter().GetResult(); | ||
_jobFactory = new JobFactory(serviceProvider); | ||
} | ||
|
||
public async Task Start() | ||
{ | ||
_scheduler.JobFactory = _jobFactory; | ||
await _scheduler.Start(); | ||
|
||
var scheduleJobs = _serviceProvider.GetRequiredService<ScheduleJobs>(); | ||
await scheduleJobs.ScheduleAllJobs(); | ||
} | ||
|
||
public async Task Stop() | ||
{ | ||
await _scheduler.Shutdown(); | ||
} | ||
|
||
public async Task PauseJob(string jobName) | ||
{ | ||
var jobKey = new JobKey(jobName); | ||
await _scheduler.PauseJob(jobKey); | ||
} | ||
|
||
public async Task ResumeJob(string jobName) | ||
{ | ||
var jobKey = new JobKey(jobName); | ||
await _scheduler.ResumeJob(jobKey); | ||
} | ||
|
||
public async Task DeleteJob(string jobName) | ||
{ | ||
var jobKey = new JobKey(jobName); | ||
await _scheduler.DeleteJob(jobKey); | ||
} | ||
|
||
public async Task ScheduleJob<T>(string jobName, ITrigger trigger) where T : IJob | ||
{ | ||
IJobDetail job = JobBuilder.Create<T>() | ||
.WithIdentity(jobName) | ||
.Build(); | ||
|
||
await _scheduler.ScheduleJob(job, trigger); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters