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";
private ReminderList reminderList = new ReminderList();
private CancellationTokenSource reminderWatcherReset;
private CancellationToken reminderWatcherResetToken;
private SimpleXmppClient client;
/// <summary>
@ -224,6 +225,8 @@ namespace RhinoReminds
client.SendChatReply(message, response);
}
if (succeeded.Count > 0) {
// Ensure that the reminder thread picks up the changes
resetReminderWatcher();
string response = string.Join(", ", succeeded.Select((int nextId) => $"#{nextId}"));
response = $"Deleted reminder{(succeeded.Count != 1 ? "s" : "")} {response} successfully.";
client.SendChatReply(message, response);
@ -299,10 +302,16 @@ namespace RhinoReminds
#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()
{
CancellationTokenSource cancellationSource = new CancellationTokenSource();
CancellationToken cancellationToken = cancellationSource.Token;
reminderWatcherReset = new CancellationTokenSource();
reminderWatcherResetToken = reminderWatcherReset.Token;
Reminder nextReminder = reminderList.GetNextReminder();
reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => {
Reminder newNextReminder = reminderList.GetNextReminder();
@ -311,7 +320,7 @@ namespace RhinoReminds
if (nextReminder != newNextReminder) {
//Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling");
nextReminder = newNextReminder;
cancellationSource.Cancel();
reminderWatcherReset.Cancel();
}
};
@ -323,23 +332,23 @@ namespace RhinoReminds
nextWaitingTime = nextReminder.Time - DateTime.Now;
if (DateTime.Now < nextReminder.Time) {
//Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}");
await Task.Delay(nextWaitingTime, cancellationToken);
await Task.Delay(nextWaitingTime, reminderWatcherResetToken);
}
} else {
//Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted");
await Task.Delay(Timeout.Infinite, cancellationToken);
await Task.Delay(Timeout.Infinite, reminderWatcherResetToken);
}
} catch (TaskCanceledException) {
//Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating");
cancellationSource = new CancellationTokenSource();
cancellationToken = cancellationSource.Token;
reminderWatcherReset = new CancellationTokenSource();
reminderWatcherResetToken = reminderWatcherReset.Token;
continue;
}
if (cancellationToken.IsCancellationRequested) {
if (reminderWatcherResetToken.IsCancellationRequested) {
Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating (but no exception thrown)");
cancellationSource = new CancellationTokenSource();
cancellationToken = cancellationSource.Token;
reminderWatcherReset = new CancellationTokenSource();
reminderWatcherResetToken = reminderWatcherReset.Token;
continue;
}