Skip to content

Commit 89bc8f7

Browse files
committed
Re-use CRLF instead of LF in files that used to use it
1 parent 7e23bc4 commit 89bc8f7

File tree

2 files changed

+137
-137
lines changed

2 files changed

+137
-137
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
using System;
2-
using System.Threading.Tasks;
3-
using EasyNetQ;
4-
using Microsoft.Extensions.Options;
5-
using Microsoft.Extensions.Logging;
6-
using MiddleMail.Model;
7-
8-
namespace MiddleMail.Client.RabbitMQ {
9-
10-
/// <summary>
11-
/// A client that connects to MiddleMail. It puts messages to be sent into a RabbitMQ queue that MiddleMail listens to.
12-
/// For this client to work, a RabbitMQ Message Source needs to be enabled in the MiddleMail server.
13-
/// This is class is thread safe and should be injected as a transient into the DI container.
14-
/// </summary>
15-
public class MiddleMailClient : IDisposable {
16-
17-
private readonly IBus bus;
18-
private readonly ILogger<MiddleMailClient> logger;
19-
20-
public MiddleMailClient(IOptions<RabbitMQOptions> options, ILogger<MiddleMailClient> logger) {
21-
bus = EasyNetQ.RabbitHutch.CreateBus(options.Value.ConnectionString);
22-
this.logger = logger;
23-
}
24-
25-
/// <summary>
26-
/// Sends an email by publishing it to RabbitMQ. A published email will wait in a queue on RabbitMQ until
27-
/// it is processed by a MiddleMail instance running a RabbitMQ Message Source.
28-
/// Returns true if the email is published to RabbitMQ successfully, false otherwise.
29-
/// </summary>
30-
public async Task<bool> SendEmailAsync(EmailMessage emailMessage) {
31-
try {
32-
await bus.PublishAsync(emailMessage);
33-
return true;
34-
} catch (Exception e) {
35-
logger.LogError("Failed to publish to rabbitmq queue.", e);
36-
return false;
37-
}
38-
}
39-
40-
public void Dispose() {
41-
bus?.Dispose();
42-
}
43-
}
44-
}
1+
using System;
2+
using System.Threading.Tasks;
3+
using EasyNetQ;
4+
using Microsoft.Extensions.Options;
5+
using Microsoft.Extensions.Logging;
6+
using MiddleMail.Model;
7+
8+
namespace MiddleMail.Client.RabbitMQ {
9+
10+
/// <summary>
11+
/// A client that connects to MiddleMail. It puts messages to be sent into a RabbitMQ queue that MiddleMail listens to.
12+
/// For this client to work, a RabbitMQ Message Source needs to be enabled in the MiddleMail server.
13+
/// This is class is thread safe and should be injected as a transient into the DI container.
14+
/// </summary>
15+
public class MiddleMailClient : IDisposable {
16+
17+
private readonly IBus bus;
18+
private readonly ILogger<MiddleMailClient> logger;
19+
20+
public MiddleMailClient(IOptions<RabbitMQOptions> options, ILogger<MiddleMailClient> logger) {
21+
bus = EasyNetQ.RabbitHutch.CreateBus(options.Value.ConnectionString);
22+
this.logger = logger;
23+
}
24+
25+
/// <summary>
26+
/// Sends an email by publishing it to RabbitMQ. A published email will wait in a queue on RabbitMQ until
27+
/// it is processed by a MiddleMail instance running a RabbitMQ Message Source.
28+
/// Returns true if the email is published to RabbitMQ successfully, false otherwise.
29+
/// </summary>
30+
public async Task<bool> SendEmailAsync(EmailMessage emailMessage) {
31+
try {
32+
await bus.PublishAsync(emailMessage);
33+
return true;
34+
} catch (Exception e) {
35+
logger.LogError("Failed to publish to rabbitmq queue.", e);
36+
return false;
37+
}
38+
}
39+
40+
public void Dispose() {
41+
bus?.Dispose();
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
using System.Linq;
2-
using System;
3-
using System.Threading.Tasks;
4-
using MiddleMail.Model;
5-
using Nest;
6-
using MiddleMail.Exceptions;
7-
using Microsoft.Extensions.Configuration;
8-
using Microsoft.Extensions.Options;
9-
10-
namespace MiddleMail.Storage.ElasticSearch {
11-
12-
/// <summary>
13-
/// A mail activity storage backed by ElasticSearch.
14-
/// Use <see cref="ElasticSearchStorageOptions" /> to configure the connection and index.
15-
/// </summary>
16-
public class ElasticSearchStorage : IMailStorage {
17-
18-
private readonly ElasticClient client;
19-
private readonly ElasticSearchStorageOptions options;
20-
public ElasticSearchStorage(IOptions<ElasticSearchStorageOptions> options) {
21-
this.options = options.Value;
22-
var node = new Uri(this.options.Uri);
23-
client = new ElasticClient(new ConnectionSettings(node));
24-
client.Indices.Create(index, index => index
25-
.Map<EmailDocument>(m => m
26-
.AutoMap()
27-
));
28-
}
29-
30-
public async Task SetProcessedAsync(EmailMessage emailMessage) {
31-
await updateOrCreateAsync(emailMessage,
32-
update: (EmailDocument emailDocument) => { },
33-
create: () => new EmailDocument(emailMessage));
34-
}
35-
36-
public async Task SetSentAsync(EmailMessage emailMessage) {
37-
await updateOrCreateAsync(emailMessage,
38-
update: (EmailDocument emailDocument) => {
39-
emailDocument.Sent = DateTime.UtcNow;
40-
},
41-
create: () => new EmailDocument(emailMessage, sent: DateTime.UtcNow));
42-
}
43-
44-
public async Task SetErrorAsync(EmailMessage emailMessage, string errorMessage) {
45-
await updateOrCreateAsync(emailMessage,
46-
update: (EmailDocument emailDocument) => {
47-
emailDocument.Error = errorMessage;
48-
},
49-
create: () => new EmailDocument(emailMessage, error: errorMessage));
50-
}
51-
52-
private async Task updateOrCreateAsync(EmailMessage emailMessage, Action<EmailDocument> update, Func<EmailDocument> create) {
53-
var emailDocument = await searchDocument(emailMessage.Id);
54-
if (emailDocument != null) {
55-
if (emailDocument.Sent != null) {
56-
throw new EMailMessageAlreadySentStorageException(emailMessage);
57-
}
58-
update(emailDocument);
59-
emailDocument.RetryCount = emailMessage.RetryCount;
60-
emailDocument.LastProcessed = DateTime.UtcNow;
61-
} else {
62-
emailDocument = create();
63-
}
64-
var response = await this.client.IndexAsync(emailDocument, i => i.Index(index));
65-
if (!response.IsValid) {
66-
throw new Exception(response.ServerError.ToString());
67-
}
68-
}
69-
70-
public async Task<bool?> GetSentAsync(EmailMessage emailMessage) {
71-
var existingDocument = await searchDocument(emailMessage.Id);
72-
return existingDocument?.Sent != null;
73-
}
74-
75-
public async Task<string> GetErrorAsync(EmailMessage emailMessage) {
76-
var existingDocument = await searchDocument(emailMessage.Id);
77-
return existingDocument?.Error;
78-
}
79-
80-
private string index => options.Index;
81-
82-
private async Task<EmailDocument> searchDocument(Guid id) {
83-
var response = await client.SearchAsync<EmailDocument>(s => s
84-
.Index(index)
85-
.Query(q => q
86-
.Term(c => c
87-
.Field(p => p.Id)
88-
.Value(id))));
89-
90-
return response.Documents.SingleOrDefault();
91-
}
92-
}
93-
}
1+
using System.Linq;
2+
using System;
3+
using System.Threading.Tasks;
4+
using MiddleMail.Model;
5+
using Nest;
6+
using MiddleMail.Exceptions;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.Options;
9+
10+
namespace MiddleMail.Storage.ElasticSearch {
11+
12+
/// <summary>
13+
/// A mail activity storage backed by ElasticSearch.
14+
/// Use <see cref="ElasticSearchStorageOptions" /> to configure the connection and index.
15+
/// </summary>
16+
public class ElasticSearchStorage : IMailStorage {
17+
18+
private readonly ElasticClient client;
19+
private readonly ElasticSearchStorageOptions options;
20+
public ElasticSearchStorage(IOptions<ElasticSearchStorageOptions> options) {
21+
this.options = options.Value;
22+
var node = new Uri(this.options.Uri);
23+
client = new ElasticClient(new ConnectionSettings(node));
24+
client.Indices.Create(index, index => index
25+
.Map<EmailDocument>(m => m
26+
.AutoMap()
27+
));
28+
}
29+
30+
public async Task SetProcessedAsync(EmailMessage emailMessage) {
31+
await updateOrCreateAsync(emailMessage,
32+
update: (EmailDocument emailDocument) => { },
33+
create: () => new EmailDocument(emailMessage));
34+
}
35+
36+
public async Task SetSentAsync(EmailMessage emailMessage) {
37+
await updateOrCreateAsync(emailMessage,
38+
update: (EmailDocument emailDocument) => {
39+
emailDocument.Sent = DateTime.UtcNow;
40+
},
41+
create: () => new EmailDocument(emailMessage, sent: DateTime.UtcNow));
42+
}
43+
44+
public async Task SetErrorAsync(EmailMessage emailMessage, string errorMessage) {
45+
await updateOrCreateAsync(emailMessage,
46+
update: (EmailDocument emailDocument) => {
47+
emailDocument.Error = errorMessage;
48+
},
49+
create: () => new EmailDocument(emailMessage, error: errorMessage));
50+
}
51+
52+
private async Task updateOrCreateAsync(EmailMessage emailMessage, Action<EmailDocument> update, Func<EmailDocument> create) {
53+
var emailDocument = await searchDocument(emailMessage.Id);
54+
if (emailDocument != null) {
55+
if (emailDocument.Sent != null) {
56+
throw new EMailMessageAlreadySentStorageException(emailMessage);
57+
}
58+
update(emailDocument);
59+
emailDocument.RetryCount = emailMessage.RetryCount;
60+
emailDocument.LastProcessed = DateTime.UtcNow;
61+
} else {
62+
emailDocument = create();
63+
}
64+
var response = await this.client.IndexAsync(emailDocument, i => i.Index(index));
65+
if (!response.IsValid) {
66+
throw new Exception(response.ServerError.ToString());
67+
}
68+
}
69+
70+
public async Task<bool?> GetSentAsync(EmailMessage emailMessage) {
71+
var existingDocument = await searchDocument(emailMessage.Id);
72+
return existingDocument?.Sent != null;
73+
}
74+
75+
public async Task<string> GetErrorAsync(EmailMessage emailMessage) {
76+
var existingDocument = await searchDocument(emailMessage.Id);
77+
return existingDocument?.Error;
78+
}
79+
80+
private string index => options.Index;
81+
82+
private async Task<EmailDocument> searchDocument(Guid id) {
83+
var response = await client.SearchAsync<EmailDocument>(s => s
84+
.Index(index)
85+
.Query(q => q
86+
.Term(c => c
87+
.Field(p => p.Id)
88+
.Value(id))));
89+
90+
return response.Documents.SingleOrDefault();
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)