Bugfix: Fix crash if the next reminder is deleted before it can be sent.

Also enable some debug logging, just in case it acts up again.
This commit is contained in:
Starbeamrainbowlabs 2019-02-08 11:30:16 +00:00
parent b5deb649dc
commit a4d996fb3d
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 18 additions and 18 deletions

View file

@ -313,8 +313,9 @@ namespace RhinoReminds
private void interruptReminderWatcher()
{
reminderWatcherReset?.Cancel();
CancellationTokenSource oldToken = reminderWatcherReset;
reminderWatcherReset = new CancellationTokenSource();
oldToken?.Cancel();
}
private async Task watchForReminders()
@ -324,12 +325,12 @@ namespace RhinoReminds
// ----- Events -----
// This will run on the firing thread, not on this thread
reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => {
reminderList.OnReminderListUpdate += (object sender, ReminderListUpdateEventArgs eventArgs) => {
Reminder newNextReminder = reminderList.GetNextReminder();
//Console.WriteLine("[Rhino/Reminderd/Canceller] Reminder added - comparing.");
//Console.WriteLine($"[Rhino/Reminderd/Canceller] {nextReminder} / {newNextReminder}");
Console.WriteLine("[Rhino/Reminderd/Canceller] Reminder added - comparing.");
Console.WriteLine($"[Rhino/Reminderd/Canceller] {nextReminder} / {newNextReminder}");
if (nextReminder != newNextReminder) {
//Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling");
Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling");
nextReminder = newNextReminder;
interruptReminderWatcher();
}
@ -345,35 +346,34 @@ namespace RhinoReminds
nextWaitingTime = nextReminder.Time - DateTime.Now;
if (DateTime.Now < nextReminder.Time)
{
//Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}");
Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}");
await Task.Delay(nextWaitingTime, reminderWatcherResetToken);
}
}
else {
//Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted");
Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted");
await Task.Delay(Timeout.Infinite, reminderWatcherResetToken);
}
}
catch (TaskCanceledException)
{
//Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating");
interruptReminderWatcher();
Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating");
continue;
}
if (reminderWatcherResetToken.IsCancellationRequested)
{
Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating (but no exception thrown)");
interruptReminderWatcher();
continue;
}
Console.WriteLine($"[Rhino/Reminderd] Sending notification {nextReminder}");
Jid targetJid = nextReminder.Jid; // Apparently nextReminder is null after the sendAndDeleteReminder() call - very odd!
sendAndDeleteReminder(nextReminder);
if (nextWaitingTime.TotalMilliseconds < 0) {
client.SendChatMessage(
nextReminder.Jid,
targetJid,
"(Sorry I'm late reminding you! I might not have been running at the time, " +
"or you might have scheduled a reminder for the past)"
);
@ -383,15 +383,12 @@ namespace RhinoReminds
private void sendAndDeleteReminder(Reminder nextReminder)
{
try
{
try {
client.SendChatMessage(
nextReminder.Jid,
$"Hello! You asked me to remind you {nextReminder.Message} at {nextReminder.Time}.".Trim().Replace(@"\s+", " ")
);
}
catch (Exception error)
{
} catch (Exception error) {
Console.Error.WriteLine($"[Rhino/Reminderd] Caught error sending message to client: {error}");
Console.Error.WriteLine($"[Rhink/Reminderd] Offending reminder: {nextReminder}");
client.SendChatMessage(nextReminder.Jid, "Oops! I encountered an error sending you a reminder. Please contact my operator!");

View file

@ -6,7 +6,8 @@ using S22.Xmpp;
namespace RhinoReminds
{
public delegate void OnReminderListUpdateHandler(object sender, Reminder newReminder);
public class ReminderListUpdateEventArgs : EventArgs { }
public delegate void OnReminderListUpdateHandler(object sender, ReminderListUpdateEventArgs newReminder);
public class ReminderList
{
@ -32,7 +33,7 @@ namespace RhinoReminds
result.TweakTime();
Reminders.Add(result);
OnReminderListUpdate(this, result);
OnReminderListUpdate(this, new ReminderListUpdateEventArgs());
return result;
}
@ -50,6 +51,8 @@ namespace RhinoReminds
public void DeleteReminder(Reminder nextReminder) {
if (!Reminders.Remove(nextReminder))
throw new ApplicationException($"Error: Failed to remove the reminder {nextReminder} from the list!");
OnReminderListUpdate(this, new ReminderListUpdateEventArgs());
}