Skip to content

Commit b634f27

Browse files
authored
Revert email retry (#6290)
* Revert "Don't throw when SmtpUri is not set (#6278)" This reverts commit e60e75f. * Revert "Fix logging exceptions from background thread (#6273)" This reverts commit d1610bb. * Revert "Improve email sending (#6154)" This reverts commit 2616a99.
1 parent 760a814 commit b634f27

29 files changed

Lines changed: 440 additions & 695 deletions

src/NuGetGallery.Core/Services/CoreMessageService.cs

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.ObjectModel;
65
using System.Collections.Generic;
76
using System.Globalization;
87
using System.Linq;
98
using System.Net.Mail;
109
using System.Text;
11-
using System.Threading.Tasks;
1210
using AnglicanGeek.MarkdownMailer;
1311
using NuGet.Services.Validation;
1412
using NuGet.Services.Validation.Issues;
@@ -17,11 +15,9 @@ namespace NuGetGallery.Services
1715
{
1816
public class CoreMessageService : ICoreMessageService
1917
{
20-
private static readonly ReadOnlyCollection<TimeSpan> RetryDelays = Array.AsReadOnly(new[] {
21-
TimeSpan.FromSeconds(0.1),
22-
TimeSpan.FromSeconds(1),
23-
TimeSpan.FromSeconds(10)
24-
});
18+
protected CoreMessageService()
19+
{
20+
}
2521

2622
public CoreMessageService(IMailSender mailSender, ICoreMessageServiceConfiguration coreConfiguration)
2723
{
@@ -32,7 +28,7 @@ public CoreMessageService(IMailSender mailSender, ICoreMessageServiceConfigurati
3228
public IMailSender MailSender { get; protected set; }
3329
public ICoreMessageServiceConfiguration CoreConfiguration { get; protected set; }
3430

35-
public async Task SendPackageAddedNoticeAsync(Package package, string packageUrl, string packageSupportUrl, string emailSettingsUrl)
31+
public void SendPackageAddedNotice(Package package, string packageUrl, string packageSupportUrl, string emailSettingsUrl)
3632
{
3733
string subject = $"[{CoreConfiguration.GalleryOwner.DisplayName}] Package published - {package.PackageRegistration.Id} {package.Version}";
3834
string body = $@"The package [{package.PackageRegistration.Id} {package.Version}]({packageUrl}) was recently published on {CoreConfiguration.GalleryOwner.DisplayName} by {package.User.Username}. If this was not intended, please [contact support]({packageSupportUrl}).
@@ -53,12 +49,12 @@ [change your email notification settings]({emailSettingsUrl}).
5349

5450
if (mailMessage.To.Any())
5551
{
56-
await SendMessageAsync(mailMessage);
52+
SendMessage(mailMessage);
5753
}
5854
}
5955
}
6056

61-
public async Task SendPackageValidationFailedNoticeAsync(Package package, PackageValidationSet validationSet, string packageUrl, string packageSupportUrl, string announcementsUrl, string twitterUrl)
57+
public void SendPackageValidationFailedNotice(Package package, PackageValidationSet validationSet, string packageUrl, string packageSupportUrl, string announcementsUrl, string twitterUrl)
6258
{
6359
var validationIssues = validationSet.GetValidationIssues();
6460

@@ -99,7 +95,7 @@ public async Task SendPackageValidationFailedNoticeAsync(Package package, Packag
9995

10096
if (mailMessage.To.Any())
10197
{
102-
await SendMessageAsync(mailMessage);
98+
SendMessage(mailMessage, copySender: false);
10399
}
104100
}
105101
}
@@ -135,7 +131,7 @@ private static string ParseValidationIssue(ValidationIssue validationIssue, stri
135131
}
136132
}
137133

138-
public async Task SendValidationTakingTooLongNoticeAsync(Package package, string packageUrl)
134+
public void SendValidationTakingTooLongNotice(Package package, string packageUrl)
139135
{
140136
string subject = "[{0}] Package validation taking longer than expected - {1} {2}";
141137
string body = "It is taking longer than expected for your package [{1} {2}]({3}) to get published.\n\n" +
@@ -167,7 +163,7 @@ public async Task SendValidationTakingTooLongNoticeAsync(Package package, string
167163

168164
if (mailMessage.To.Any())
169165
{
170-
await SendMessageAsync(mailMessage);
166+
SendMessage(mailMessage, copySender: false);
171167
}
172168
}
173169
}
@@ -197,54 +193,30 @@ protected static void AddOwnersSubscribedToPackagePushedNotification(PackageRegi
197193
}
198194
}
199195

200-
protected virtual async Task SendMessageAsync(MailMessage mailMessage)
196+
protected void SendMessage(MailMessage mailMessage)
201197
{
202-
int attempt = 0;
203-
bool success = false;
204-
while (!success)
205-
{
206-
try
207-
{
208-
await AttemptSendMessageAsync(mailMessage, attempt + 1);
209-
success = true;
210-
}
211-
catch (SmtpException)
212-
{
213-
if (attempt < RetryDelays.Count)
214-
{
215-
await Task.Delay(RetryDelays[attempt]);
216-
attempt++;
217-
}
218-
else
219-
{
220-
throw;
221-
}
222-
}
223-
}
198+
SendMessage(mailMessage, copySender: false);
224199
}
225200

226-
protected virtual Task AttemptSendMessageAsync(MailMessage mailMessage, int attemptNumber)
201+
virtual protected void SendMessage(MailMessage mailMessage, bool copySender)
227202
{
228-
// AnglicanGeek.MarkdownMailer doesn't have an async overload
229203
MailSender.Send(mailMessage);
230-
return Task.CompletedTask;
231-
}
232-
233-
protected async Task SendMessageToSenderAsync(MailMessage mailMessage)
234-
{
235-
using (var senderCopy = new MailMessage(
236-
CoreConfiguration.GalleryOwner,
237-
mailMessage.ReplyToList.First()))
204+
if (copySender)
238205
{
239-
senderCopy.Subject = mailMessage.Subject + " [Sender Copy]";
240-
senderCopy.Body = string.Format(
241-
CultureInfo.CurrentCulture,
242-
"You sent the following message via {0}: {1}{1}{2}",
243-
CoreConfiguration.GalleryOwner.DisplayName,
244-
Environment.NewLine,
245-
mailMessage.Body);
206+
var senderCopy = new MailMessage(
207+
CoreConfiguration.GalleryOwner,
208+
mailMessage.ReplyToList.First())
209+
{
210+
Subject = mailMessage.Subject + " [Sender Copy]",
211+
Body = string.Format(
212+
CultureInfo.CurrentCulture,
213+
"You sent the following message via {0}: {1}{1}{2}",
214+
CoreConfiguration.GalleryOwner.DisplayName,
215+
Environment.NewLine,
216+
mailMessage.Body),
217+
};
246218
senderCopy.ReplyToList.Add(mailMessage.ReplyToList.First());
247-
await SendMessageAsync(senderCopy);
219+
MailSender.Send(senderCopy);
248220
}
249221
}
250222
}

src/NuGetGallery.Core/Services/ICoreMessageService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using NuGet.Services.Validation;
5-
using System.Threading.Tasks;
65

76
namespace NuGetGallery.Services
87
{
98
public interface ICoreMessageService
109
{
11-
Task SendPackageAddedNoticeAsync(Package package, string packageUrl, string packageSupportUrl, string emailSettingsUrl);
12-
Task SendPackageValidationFailedNoticeAsync(Package package, PackageValidationSet validationSet, string packageUrl, string packageSupportUrl, string announcementsUrl, string twitterUrl);
13-
Task SendValidationTakingTooLongNoticeAsync(Package package, string packageUrl);
10+
void SendPackageAddedNotice(Package package, string packageUrl, string packageSupportUrl, string emailSettingsUrl);
11+
void SendPackageValidationFailedNotice(Package package, PackageValidationSet validationSet, string packageUrl, string packageSupportUrl, string announcementsUrl, string twitterUrl);
12+
void SendValidationTakingTooLongNotice(Package package, string packageUrl);
1413
}
1514
}

src/NuGetGallery/App_Start/DefaultDependenciesModule.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
using NuGetGallery.Infrastructure.Authentication;
3636
using NuGetGallery.Infrastructure.Lucene;
3737
using NuGetGallery.Security;
38-
using NuGetGallery.Services;
3938
using SecretReaderFactory = NuGetGallery.Configuration.SecretReader.SecretReaderFactory;
4039

4140
namespace NuGetGallery
@@ -357,7 +356,7 @@ protected override void Load(ContainerBuilder builder)
357356
.As<IMailSender>()
358357
.InstancePerLifetimeScope();
359358

360-
builder.RegisterType<BackgroundMessageService>()
359+
builder.RegisterType<MessageService>()
361360
.AsSelf()
362361
.As<IMessageService>()
363362
.InstancePerLifetimeScope();

src/NuGetGallery/Controllers/AccountsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public virtual ActionResult ConfirmationRequired(string accountName = null)
9292
[HttpPost]
9393
[ActionName("ConfirmationRequired")]
9494
[ValidateAntiForgeryToken]
95-
public virtual async Task<ActionResult> ConfirmationRequiredPost(string accountName = null)
95+
public virtual ActionResult ConfirmationRequiredPost(string accountName = null)
9696
{
9797
var account = GetAccount(accountName);
9898

@@ -108,7 +108,7 @@ public virtual async Task<ActionResult> ConfirmationRequiredPost(string accountN
108108
ConfirmationViewModel model;
109109
if (!alreadyConfirmed)
110110
{
111-
await SendNewAccountEmailAsync(account);
111+
SendNewAccountEmail(account);
112112

113113
model = new ConfirmationViewModel(account)
114114
{
@@ -122,7 +122,7 @@ public virtual async Task<ActionResult> ConfirmationRequiredPost(string accountN
122122
return View(model);
123123
}
124124

125-
protected abstract Task SendNewAccountEmailAsync(User account);
125+
protected abstract void SendNewAccountEmail(User account);
126126

127127
[UIAuthorize(allowDiscontinuedLogins: true)]
128128
public virtual async Task<ActionResult> Confirm(string accountName, string token)
@@ -163,7 +163,7 @@ public virtual async Task<ActionResult> Confirm(string accountName, string token
163163
// Change notice not required for new accounts.
164164
if (model.SuccessfulConfirmation && !model.ConfirmingNewAccount)
165165
{
166-
await MessageService.SendEmailChangeNoticeToPreviousEmailAddressAsync(account, existingEmail);
166+
MessageService.SendEmailChangeNoticeToPreviousEmailAddress(account, existingEmail);
167167

168168
string returnUrl = HttpContext.GetConfirmationReturnUrl();
169169
if (!String.IsNullOrEmpty(returnUrl))
@@ -254,13 +254,13 @@ public virtual async Task<ActionResult> ChangeEmail(TAccountViewModel model)
254254

255255
if (account.Confirmed)
256256
{
257-
await SendEmailChangedConfirmationNoticeAsync(account);
257+
SendEmailChangedConfirmationNotice(account);
258258
}
259259

260260
return RedirectToAction(AccountAction);
261261
}
262262

263-
protected abstract Task SendEmailChangedConfirmationNoticeAsync(User account);
263+
protected abstract void SendEmailChangedConfirmationNotice(User account);
264264

265265
[HttpPost]
266266
[UIAuthorize]

src/NuGetGallery/Controllers/ApiController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ await AuditingService.SaveAuditRecordAsync(
699699
if (!(ConfigurationService.Current.AsynchronousPackageValidationEnabled && ConfigurationService.Current.BlockingAsynchronousPackageValidationEnabled))
700700
{
701701
// Notify user of push unless async validation in blocking mode is used
702-
await MessageService.SendPackageAddedNoticeAsync(package,
702+
MessageService.SendPackageAddedNotice(package,
703703
Url.Package(package.PackageRegistration.Id, package.NormalizedVersion, relativeUrl: false),
704704
Url.ReportPackage(package.PackageRegistration.Id, package.NormalizedVersion, relativeUrl: false),
705705
Url.AccountSettings(relativeUrl: false));

src/NuGetGallery/Controllers/AuthenticationController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public virtual async Task<ActionResult> Register(LogOnViewModel model, string re
287287
// Send a new account email
288288
if (NuGetContext.Config.Current.ConfirmEmailAddresses && !string.IsNullOrEmpty(user.User.UnconfirmedEmailAddress))
289289
{
290-
await _messageService.SendNewAccountEmailAsync(
290+
_messageService.SendNewAccountEmail(
291291
user.User,
292292
Url.ConfirmEmail(
293293
user.User.Username,
@@ -325,7 +325,7 @@ public virtual ActionResult LogOff(string returnUrl)
325325

326326
[HttpPost]
327327
[ValidateAntiForgeryToken]
328-
public virtual async Task<JsonResult> SignInAssistance(string username, string providedEmailAddress)
328+
public virtual JsonResult SignInAssistance(string username, string providedEmailAddress)
329329
{
330330
// If provided email address is empty or null, return the result with a formatted
331331
// email address, otherwise send sign-in assistance email to the associated mail address.
@@ -352,7 +352,7 @@ public virtual async Task<JsonResult> SignInAssistance(string username, string p
352352
else
353353
{
354354
var externalCredentials = user.Credentials.Where(cred => cred.IsExternal());
355-
await _messageService.SendSigninAssistanceEmailAsync(new MailAddress(email, user.Username), externalCredentials);
355+
_messageService.SendSigninAssistanceEmail(new MailAddress(email, user.Username), externalCredentials);
356356
return Json(new { success = true });
357357
}
358358
}
@@ -674,7 +674,7 @@ private async Task<LoginUserDetails> AssociateCredential(AuthenticatedUser user)
674674
await RemovePasswordCredential(user.User);
675675

676676
// Notify the user of the change
677-
await _messageService.SendCredentialAddedNoticeAsync(user.User, _authService.DescribeCredential(result.Credential));
677+
_messageService.SendCredentialAddedNotice(user.User, _authService.DescribeCredential(result.Credential));
678678

679679
return new LoginUserDetails
680680
{

src/NuGetGallery/Controllers/JsonApiController.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public async Task<JsonResult> AddPackageOwner(string id, string username, string
119119

120120
foreach (var owner in model.Package.Owners)
121121
{
122-
await _messageService.SendPackageOwnerAddedNoticeAsync(owner, model.User, model.Package, packageUrl);
122+
_messageService.SendPackageOwnerAddedNotice(owner, model.User, model.Package, packageUrl);
123123
}
124124
}
125125
else
@@ -147,12 +147,12 @@ public async Task<JsonResult> AddPackageOwner(string id, string username, string
147147
model.User.Username,
148148
relativeUrl: false);
149149

150-
await _messageService.SendPackageOwnerRequestAsync(model.CurrentUser, model.User, model.Package, packageUrl,
150+
_messageService.SendPackageOwnerRequest(model.CurrentUser, model.User, model.Package, packageUrl,
151151
confirmationUrl, rejectionUrl, encodedMessage, policyMessage: string.Empty);
152152

153153
foreach (var owner in model.Package.Owners)
154154
{
155-
await _messageService.SendPackageOwnerRequestInitiatedNoticeAsync(model.CurrentUser, owner, model.User, model.Package, cancellationUrl);
155+
_messageService.SendPackageOwnerRequestInitiatedNotice(model.CurrentUser, owner, model.User, model.Package, cancellationUrl);
156156
}
157157
}
158158

@@ -190,12 +190,12 @@ public async Task<JsonResult> RemovePackageOwner(string id, string username)
190190
throw new InvalidOperationException("You can't remove the only owner from a package.");
191191
}
192192
await _packageOwnershipManagementService.RemovePackageOwnerAsync(model.Package, model.CurrentUser, model.User, commitAsTransaction:true);
193-
await _messageService.SendPackageOwnerRemovedNoticeAsync(model.CurrentUser, model.User, model.Package);
193+
_messageService.SendPackageOwnerRemovedNotice(model.CurrentUser, model.User, model.Package);
194194
}
195195
else
196196
{
197197
await _packageOwnershipManagementService.DeletePackageOwnershipRequestAsync(model.Package, model.User);
198-
await _messageService.SendPackageOwnerRequestCancellationNoticeAsync(model.CurrentUser, model.User, model.Package);
198+
_messageService.SendPackageOwnerRequestCancellationNotice(model.CurrentUser, model.User, model.Package);
199199
}
200200

201201
return Json(new { success = true });

0 commit comments

Comments
 (0)