forked from petabridge/akkadotnet-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedParserCoordinator.cs
259 lines (206 loc) · 8.95 KB
/
FeedParserCoordinator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using Akka.Actor;
using QDFeedParser;
namespace PipeTo.App.Actors
{
/// <summary>
/// Actor responsible for figuring out if we're done processing a RSS feed or not
/// </summary>
public class FeedParserCoordinator : ReceiveActor
{
#region Message types
/// <summary>
/// Message class for signaling that the feed has no contents
/// </summary>
public class EmptyFeed { }
/// <summary>
/// Uses a scheduled task to determine if we've finished downloading all feed content
/// </summary>
public class CheckFeedCompleted { }
/// <summary>
/// Used to request <see cref="DownloadStats"/> to an external source
/// </summary>
public class GetStats { }
/// <summary>
/// Used
/// </summary>
public class ErrorParsingFeed
{
public ErrorParsingFeed(Uri feed)
{
Feed = feed;
}
public Uri Feed { get; private set; }
}
/// <summary>
/// Message class for signaling that there are assets that still need to be downloaded
/// </summary>
public class RemainingDownloadCount
{
public RemainingDownloadCount(string feedUrl, int htmlPages, int images)
{
Images = images;
HtmlPages = htmlPages;
FeedUrl = feedUrl;
}
public string FeedUrl { get; private set; }
public int HtmlPages { get; private set; }
public int Images { get; private set; }
}
/// <summary>
/// Message class for signaling that assets have been downloaded
/// </summary>
public class DownloadComplete
{
public DownloadComplete(string feedUrl, int htmlPages, int images)
{
Images = images;
HtmlPages = htmlPages;
FeedUrl = feedUrl;
}
public string FeedUrl { get; private set; }
public int HtmlPages { get; private set; }
public int Images { get; private set; }
}
/// <summary>
/// Used to track the download statistics for a specific feed
/// </summary>
public class DownloadStats
{
public DownloadStats() : this(0, 0, 0, 0) { }
public DownloadStats(int totalPages, int completedPages, int totalImages, int completedImages)
{
CompletedImages = completedImages;
TotalImages = totalImages;
CompletedPages = completedPages;
TotalPages = totalPages;
}
public int TotalPages { get; private set; }
public int CompletedPages { get; private set; }
public int TotalImages { get; private set; }
public int CompletedImages { get; private set; }
public bool IsComplete
{
get { return TotalPages != 0 && CompletedPages == TotalPages && CompletedImages == TotalImages; }
}
#region Merge / Copy functions
public DownloadStats Merge(DownloadComplete complete)
{
var copy = Copy();
copy.CompletedImages += complete.Images;
copy.CompletedPages += complete.HtmlPages;
return copy;
}
public DownloadStats Merge(RemainingDownloadCount remainingDownloadCount)
{
var copy = Copy();
copy.TotalImages += remainingDownloadCount.Images;
copy.TotalPages += remainingDownloadCount.HtmlPages;
return copy;
}
public DownloadStats Copy()
{
return new DownloadStats(TotalPages, CompletedPages, TotalImages, CompletedImages);
}
#endregion
}
#endregion
private readonly Uri _feedUri;
private DownloadStats _downloadStats;
private readonly string _consoleWriterActorPath;
private IActorRef _feedParserActor;
private IActorRef _httpDownloaderActor;
/// <summary>
/// This constructor will actually get called by the <see cref="FeedValidatorActor"/> to begin processing messages
/// </summary>
public FeedParserCoordinator(Uri feedUri) : this(feedUri, new DownloadStats(), ActorNames.ConsoleWriterActor.Path)
{
}
/// <summary>
/// This constructor is for unit testing.
/// </summary>
public FeedParserCoordinator(Uri feedUri, DownloadStats downloadStats, string consoleWriterActorPath)
{
_feedUri = feedUri;
_downloadStats = downloadStats;
_consoleWriterActorPath = consoleWriterActorPath;
Initialize();
}
/// <summary>
/// Used to set up <see cref="Receive"/> methods for <see cref="FeedParserCoordinator"/>
/// </summary>
private void Initialize()
{
//Give the Sender a copy of our latest DownloadStats
Receive<GetStats>(stats => Sender.Tell(_downloadStats));
//Get an update on remaining downloads that need to be processed
Receive<RemainingDownloadCount>(count =>
{
_downloadStats = _downloadStats.Merge(count);
SendMessage(string.Format("Need to process an additional {0} pages and {1} images for {2}", count.HtmlPages, count.Images, count.FeedUrl));
});
//Process an update on completed downloads
Receive<DownloadComplete>(complete =>
{
_downloadStats = _downloadStats.Merge(complete);
SendMessage(string.Format("Completed processing of {0} pages and {1} images for {2}", complete.HtmlPages, complete.Images, complete.FeedUrl), PipeToSampleStatusCode.Success);
//Check to see if this is the last outstanding item that needs to be downloaded
if (_downloadStats.IsComplete)
{
SignalFeedProcessingCompleted();
}
});
//Feed contained no itmes
Receive<EmptyFeed>(feed => SignalFeedProcessingCompleted());
Receive<ErrorParsingFeed>(
feed => SignalFeedProcessingFailure(string.Format("Error parsing feed {0}", _feedUri), _feedUri.ToString()));
}
protected override void PreStart()
{
//create the HttpDownloaderActor first, since the FeedParserActor depends on it
_httpDownloaderActor = Context.ActorOf(Props.Create(() => new HttpDownloaderActor(_consoleWriterActorPath)));
_feedParserActor =
Context.ActorOf(
Props.Create(
() => new FeedParserActor(new HttpFeedFactory(), _httpDownloaderActor, _consoleWriterActorPath)));
//send the initial signal to FeedParserActor that it needs to begin consuming the feed
_feedParserActor.Tell(new FeedParserActor.BeginProcessFeed(_feedUri));
}
protected override void PreRestart(Exception reason, object message)
{
// Kill any child actors explicitly on shutdown
Context.Stop(_httpDownloaderActor);
Context.Stop(_feedParserActor);
base.PreRestart(reason, message);
}
#region Console output methods
private void SendMessage(string message, PipeToSampleStatusCode pipeToSampleStatus = PipeToSampleStatusCode.Normal)
{
//create the message instance
var consoleMsg = StatusMessageHelper.CreateMessage(message, pipeToSampleStatus);
//Select the ConsoleWriterActor and send it a message
Context.ActorSelection(_consoleWriterActorPath).Tell(consoleMsg);
}
private void SendDownloadComplete(string message, string feedUri)
{
//create the FAILURE message instance
var consoleMsg = StatusMessageHelper.CreateOperationCompletedSuccessfullyMessage(message, feedUri);
//Select the ConsoleWriterActor and send it a message
Context.ActorSelection(_consoleWriterActorPath).Tell(consoleMsg);
}
private void SignalFeedProcessingCompleted()
{
SendDownloadComplete(string.Format("Completed download of all items for {0}", _feedUri), _feedUri.ToString());
//Self-terminate - which will also kill off all child actors
Context.Self.Tell(PoisonPill.Instance);
}
private void SignalFeedProcessingFailure(string message, string feedUri)
{
//create the FAILURE message instance
var consoleMsg = StatusMessageHelper.CreateFailureMessage(message, feedUri);
//Select the ConsoleWriterActor and send it a message
Context.ActorSelection(_consoleWriterActorPath).Tell(consoleMsg);
}
#endregion
}
}