Skip to content

Commit

Permalink
#72 removed cancel from move
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Aug 21, 2024
1 parent e881f9b commit 7b4b1fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public static async Task AddFlagsAsync(this IMessageSummary messageSummary, Mess
bool peekSourceFolder = !messageSummary.Folder.IsOpen;
if (peekSourceFolder || messageSummary.Folder.Access != FolderAccess.ReadWrite)
_ = await messageSummary.Folder.OpenAsync(FolderAccess.ReadWrite, cancellationToken).ConfigureAwait(false);
resultUid = await messageSummary.Folder.MoveToAsync(messageSummary.UniqueId, destination, cancellationToken).ConfigureAwait(false);
resultUid = await messageSummary.Folder.MoveToAsync(messageSummary.UniqueId, destination, CancellationToken.None).ConfigureAwait(false);
if (peekSourceFolder)
await messageSummary.Folder.CloseAsync(expunge: false, cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public async Task<int> DeleteMessagesAsync(TimeSpan relativeOffset, SearchQuery
bool peekSourceFolder = !source.IsOpen;
// Beware, source must be opened after destination to keep it open
_ = await OpenAsync(source, enableWrite: move, cancellationToken).ConfigureAwait(false);
resultUid = await source.MoveToAsync(messageUid, destination, cancellationToken).ConfigureAwait(false);
resultUid = await source.MoveToAsync(messageUid, destination, CancellationToken.None).ConfigureAwait(false);
_logger.LogTrace("{0} {1} {2} to {3} in {4}.", _imapReceiver, messageUid, verb, resultUid, destination.FullName);
if (peekSourceFolder && source.IsOpen)
await source.CloseAsync(expunge: false, cancellationToken).ConfigureAwait(false);
Expand Down
32 changes: 22 additions & 10 deletions source/MailKitSimplified.Receiver/Services/MailFolderMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public MailFolderMonitor(IImapReceiver imapReceiver, IOptions<FolderMonitorOptio
_folderMonitorOptions = folderMonitorOptions?.Value ?? new FolderMonitorOptions();
_messageArrivalMethod = (m) =>
{
_logger.Log<MailFolderMonitor>($"{_imapReceiver} message #{m.UniqueId} arrival processed.");
_logger.Log<MailFolderMonitor>($"{_imapReceiver} message #{m.UniqueId} arrival processed.", LogLevel.Debug);
return _completedTask;
};
_messageDepartureMethod = (m) =>
{
_logger.Log<MailFolderMonitor>($"{_imapReceiver} message #{m.UniqueId} departure processed.");
_logger.Log<MailFolderMonitor>($"{_imapReceiver} message #{m.UniqueId} departure processed.", LogLevel.Debug);
return _completedTask;
};
}
Expand Down Expand Up @@ -224,7 +224,7 @@ private async Task IdleStartAsync(CancellationToken cancellationToken = default)
_mailFolder = await _imapReceiver.ConnectMailFolderAsync(cancellationToken).ConfigureAwait(false);
_ = await _mailFolder.OpenAsync(FolderAccess.ReadOnly, cancellationToken).ConfigureAwait(false);
var connectOption = _folderMonitorOptions.IgnoreExistingMailOnConnect ? "ignoring" : "fetching";
_logger.Log<MailFolderMonitor>($"{_imapReceiver} ({_mailFolder.Count}) idle monitor started, {connectOption} existing emails.");
_logger.Log<MailFolderMonitor>($"{_imapReceiver} ({_mailFolder.Count}) idle monitor started, {connectOption} existing emails.", LogLevel.Information);

_mailFolder.CountChanged += OnCountChanged;
_mailFolder.MessageExpunged += OnMessageExpunged;
Expand Down Expand Up @@ -297,19 +297,30 @@ private async ValueTask ReconnectAsync(CancellationToken cancellationToken = def
{
await LogDelayAsync(ex, "IMAP protocol exception").ConfigureAwait(false);
}
catch (ImapCommandException ex)
{
await LogDelayAsync(ex, "IMAP command exception").ConfigureAwait(false);
}
catch (SocketException ex)
{
await LogDelayAsync(ex, "IMAP socket exception").ConfigureAwait(false);
}

async Task LogDelayAsync(Exception exception, string exceptionType)
catch (IOException ex)
{
var message = $"{exceptionType} during connection attempt #{++attemptCount}, backing off for {_folderMonitorOptions.EmptyQueueMaxDelayMs}ms. {_imapReceiver}.";
await LogDelayAsync(ex, "IMAP I/O exception").ConfigureAwait(false);
}

async ValueTask LogDelayAsync(Exception exception, string exceptionType)
{
bool isBackoff = attemptCount > 0;
var backoff = isBackoff ? $", backing off for {_folderMonitorOptions.EmptyQueueMaxDelayMs}ms" : string.Empty;
var message = $"{exceptionType} during connection attempt #{++attemptCount}{backoff}. {_imapReceiver}.";
if (attemptCount < _folderMonitorOptions.MaxRetries)
_logger.Log<MailFolderMonitor>(exceptionType, LogLevel.Warning);
_logger.Log<MailFolderMonitor>(message, LogLevel.Information);
else
_logger.Log<MailFolderMonitor>(exception, exceptionType, LogLevel.Error);
await Task.Delay(_folderMonitorOptions.EmptyQueueMaxDelayMs, cancellationToken).ConfigureAwait(false);
_logger.Log<MailFolderMonitor>(exception, message, LogLevel.Error);
if (isBackoff)
await Task.Delay(_folderMonitorOptions.EmptyQueueMaxDelayMs, cancellationToken).ConfigureAwait(false);
}
}
}
Expand Down Expand Up @@ -345,7 +356,8 @@ private async ValueTask WaitForNewMessagesAsync(CancellationToken cancellationTo
}
catch (ImapProtocolException ex)
{
var message = $"{ex.Message} Reconnecting and trying again.";
string error = ex.Message.TrimEnd(new char[] { ' ', '.' });
var message = $"{error}. IMAP protocol exception, reconnecting and trying again.";
if (ex.Message.StartsWith("Idle timeout"))
_logger.Log<MailFolderMonitor>(message, LogLevel.Debug);
else
Expand Down

0 comments on commit 7b4b1fc

Please sign in to comment.