|
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