Bugfix: Notify the reminder thread when a reminder gets deleted from the list

This commit is contained in:
Starbeamrainbowlabs 2018-12-22 16:05:45 +00:00
parent 4174ec85c6
commit adfb482072
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -33,7 +33,8 @@ namespace RhinoReminds
public string ReminderFilePath { get; set; } = "./reminders.xml"; public string ReminderFilePath { get; set; } = "./reminders.xml";
private ReminderList reminderList = new ReminderList(); private ReminderList reminderList = new ReminderList();
private CancellationTokenSource reminderWatcherReset;
private CancellationToken reminderWatcherResetToken;
private SimpleXmppClient client; private SimpleXmppClient client;
/// <summary> /// <summary>
@ -224,6 +225,8 @@ namespace RhinoReminds
client.SendChatReply(message, response); client.SendChatReply(message, response);
} }
if (succeeded.Count > 0) { if (succeeded.Count > 0) {
// Ensure that the reminder thread picks up the changes
resetReminderWatcher();
string response = string.Join(", ", succeeded.Select((int nextId) => $"#{nextId}")); string response = string.Join(", ", succeeded.Select((int nextId) => $"#{nextId}"));
response = $"Deleted reminder{(succeeded.Count != 1 ? "s" : "")} {response} successfully."; response = $"Deleted reminder{(succeeded.Count != 1 ? "s" : "")} {response} successfully.";
client.SendChatReply(message, response); client.SendChatReply(message, response);
@ -299,10 +302,16 @@ namespace RhinoReminds
#region Reminder Listening #region Reminder Listening
private void resetReminderWatcher()
{
// We don't need to create a new token here, as the watcher does thisi automagically
reminderWatcherReset.Cancel();
}
private async Task watchForReminders() private async Task watchForReminders()
{ {
CancellationTokenSource cancellationSource = new CancellationTokenSource(); reminderWatcherReset = new CancellationTokenSource();
CancellationToken cancellationToken = cancellationSource.Token; reminderWatcherResetToken = reminderWatcherReset.Token;
Reminder nextReminder = reminderList.GetNextReminder(); Reminder nextReminder = reminderList.GetNextReminder();
reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => { reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => {
Reminder newNextReminder = reminderList.GetNextReminder(); Reminder newNextReminder = reminderList.GetNextReminder();
@ -311,7 +320,7 @@ namespace RhinoReminds
if (nextReminder != newNextReminder) { if (nextReminder != newNextReminder) {
//Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling"); //Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling");
nextReminder = newNextReminder; nextReminder = newNextReminder;
cancellationSource.Cancel(); reminderWatcherReset.Cancel();
} }
}; };
@ -323,23 +332,23 @@ namespace RhinoReminds
nextWaitingTime = nextReminder.Time - DateTime.Now; nextWaitingTime = nextReminder.Time - DateTime.Now;
if (DateTime.Now < nextReminder.Time) { if (DateTime.Now < nextReminder.Time) {
//Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}"); //Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}");
await Task.Delay(nextWaitingTime, cancellationToken); await Task.Delay(nextWaitingTime, reminderWatcherResetToken);
} }
} else { } else {
//Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted"); //Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted");
await Task.Delay(Timeout.Infinite, cancellationToken); await Task.Delay(Timeout.Infinite, reminderWatcherResetToken);
} }
} catch (TaskCanceledException) { } catch (TaskCanceledException) {
//Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating"); //Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating");
cancellationSource = new CancellationTokenSource(); reminderWatcherReset = new CancellationTokenSource();
cancellationToken = cancellationSource.Token; reminderWatcherResetToken = reminderWatcherReset.Token;
continue; continue;
} }
if (cancellationToken.IsCancellationRequested) { if (reminderWatcherResetToken.IsCancellationRequested) {
Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating (but no exception thrown)"); Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating (but no exception thrown)");
cancellationSource = new CancellationTokenSource(); reminderWatcherReset = new CancellationTokenSource();
cancellationToken = cancellationSource.Token; reminderWatcherResetToken = reminderWatcherReset.Token;
continue; continue;
} }