-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
9 changed files
with
544 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,16 @@ | ||
using System; | ||
|
||
namespace thorn.Reminder | ||
{ | ||
public class JobSchedule | ||
{ | ||
public JobSchedule(Type jobType, string cronExpression) | ||
{ | ||
JobType = jobType; | ||
CronExpression = cronExpression; | ||
} | ||
|
||
public Type JobType { get; } | ||
public string CronExpression { get; } | ||
} | ||
} |
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,68 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Quartz; | ||
using Quartz.Spi; | ||
|
||
namespace thorn.Reminder | ||
{ | ||
public class QuartzHostedService : IHostedService | ||
{ | ||
private readonly ISchedulerFactory _schedulerFactory; | ||
private readonly IJobFactory _jobFactory; | ||
private readonly IEnumerable<JobSchedule> _jobSchedules; | ||
|
||
public QuartzHostedService( | ||
ISchedulerFactory schedulerFactory, | ||
IJobFactory jobFactory, | ||
IEnumerable<JobSchedule> jobSchedules) | ||
{ | ||
_schedulerFactory = schedulerFactory; | ||
_jobSchedules = jobSchedules; | ||
_jobFactory = jobFactory; | ||
} | ||
public IScheduler Scheduler { get; set; } | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
Scheduler = await _schedulerFactory.GetScheduler(cancellationToken); | ||
Scheduler.JobFactory = _jobFactory; | ||
|
||
foreach (var jobSchedule in _jobSchedules) | ||
{ | ||
var job = CreateJob(jobSchedule); | ||
var trigger = CreateTrigger(jobSchedule); | ||
|
||
await Scheduler.ScheduleJob(job, trigger, cancellationToken); | ||
} | ||
|
||
await Scheduler.Start(cancellationToken); | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
await Scheduler?.Shutdown(cancellationToken); | ||
} | ||
|
||
private static IJobDetail CreateJob(JobSchedule schedule) | ||
{ | ||
var jobType = schedule.JobType; | ||
return JobBuilder | ||
.Create(jobType) | ||
.WithIdentity(jobType.FullName ?? string.Empty) | ||
.WithDescription(jobType.Name) | ||
.Build(); | ||
} | ||
|
||
private static ITrigger CreateTrigger(JobSchedule schedule) | ||
{ | ||
return TriggerBuilder | ||
.Create() | ||
.WithIdentity($"{schedule.JobType.FullName}.trigger") | ||
.WithCronSchedule(schedule.CronExpression) | ||
.WithDescription(schedule.CronExpression) | ||
.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,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Discord; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using Quartz; | ||
using thorn.Services; | ||
|
||
namespace thorn.Reminder | ||
{ | ||
public class ReminderJob : IJob | ||
{ | ||
private readonly ILogger<ReminderJob> _logger; | ||
private readonly SocketTextChannel _channel; | ||
private readonly PairsService _pairs; | ||
|
||
private readonly Dictionary<string, string> _daily; | ||
|
||
public ReminderJob(ILogger<ReminderJob> logger, DiscordSocketClient client, PairsService pairs, DataStorageService data) | ||
{ | ||
_logger = logger; | ||
_pairs = pairs; | ||
|
||
_channel = client.GetChannel(ulong.Parse(pairs.GetString("GENERAL_CHANNEL_ID"))) as SocketTextChannel; | ||
_daily = DataStorageService.GetDictionary<string>("Config/daily.json"); | ||
} | ||
|
||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
var day = DateTime.Now; | ||
var description = _daily[day.ToString("dd MM")] + | ||
$"\n\nPřeji krásný den {_pairs.GetString("AGRLOVE_EMOTE")}"; | ||
|
||
var embed = new EmbedBuilder | ||
{ | ||
Title = "Krásné dobré ráno!", | ||
Description = description, | ||
ThumbnailUrl = "https://cdn.discordapp.com/attachments/537064369725636611/733080455217283131/calendar-flat.png", | ||
Color = Color.Green | ||
}.Build(); | ||
|
||
await _channel.SendMessageAsync(embed: embed); | ||
_logger.LogInformation("Sent daily reminder for {Day}", day.Date); | ||
} | ||
} | ||
} |
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 System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Quartz; | ||
using Quartz.Spi; | ||
|
||
namespace thorn.Reminder | ||
{ | ||
public class SingletonJobFactory : IJobFactory | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
public SingletonJobFactory(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) | ||
{ | ||
return _serviceProvider.GetRequiredService(bundle.JobDetail.JobType) 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
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