-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPriceCmdRouter.cs
59 lines (51 loc) · 2.53 KB
/
PriceCmdRouter.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
// -----------------------------------------------------------------------
// <copyright file="PriceCmdRouter.cs" company="Petabridge, LLC">
// Copyright (C) 2015 - 2019 Petabridge, LLC <https://petabridge.com>
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.CQRS.Pricing.Commands;
using Akka.CQRS.Pricing.Views;
using Petabridge.Cmd;
using Petabridge.Cmd.Host;
namespace Akka.CQRS.Pricing.Cli
{
/// <summary>
/// Actor responsible for carrying out <see cref="PricingCmd.PricingCommandPalette"/> commands.
/// </summary>
public sealed class PriceCmdRouter : CommandHandlerActor
{
private IActorRef _priceViewMaster;
public PriceCmdRouter(IActorRef priceViewMaster) : base(PricingCmd.PricingCommandPalette)
{
_priceViewMaster = priceViewMaster;
Process(PricingCmd.TrackPrice.Name, (command, arguments) =>
{
var tickerSymbol = arguments.ArgumentValues("symbol").Single();
// the tracker actor will start automatically recording price information on its own. No further action needed.
var trackerActor =
Context.ActorOf(Props.Create(() => new PriceTrackingActor(tickerSymbol, _priceViewMaster, Sender)));
});
Process(PricingCmd.PriceHistory.Name, (command, arguments) =>
{
var tickerSymbol = arguments.ArgumentValues("symbol").Single();
var getPriceTask = _priceViewMaster.Ask<PriceHistory>(new GetPriceHistory(tickerSymbol), TimeSpan.FromSeconds(5));
var sender = Sender;
// pipe happy results back to the sender only on successful Ask
getPriceTask.ContinueWith(tr =>
{
return Enumerable.Select(tr.Result.HistoricalPrices, x => new CommandResponse(x.ToString(), false))
.Concat(new []{ CommandResponse.Empty });
}, TaskContinuationOptions.OnlyOnRanToCompletion).PipeTo(sender);
// pipe unhappy results back to sender on failure
getPriceTask.ContinueWith(tr =>
new ErroredCommandResponse($"Error while fetching price history for {tickerSymbol} - " +
$"timed out after 5s"), TaskContinuationOptions.NotOnRanToCompletion)
.PipeTo(sender); ;
});
}
}
}