Problem statement
Complete the implementation of IDashboardLoader, DashboardLoader and DashboardController so an operations dashboard loads its three panels at once, still answers with the panels that came back when one of them will not, and stops every load still running the moment the caller gives up.
Sample Data
The dashboard lists three sources, in this order: sales, inventory, alerts. Each one answers a line of text of its own, and any of them can be in trouble when the dashboard is asked for, in which case it raises an error carrying the reason instead of answering.
Service
IDashboardSource
A source carries a Name, and one member, LoadAsync, which takes a cancellation token and answers that source's line of text through a Task that finishes later rather than on the spot. A source call handed a token that reports cancellation gives up instead of answering.
IDashboardLoader and DashboardLoader
DashboardLoader is handed the dashboard's sources. Both it and the interface ship declaring one member that hands a finished DashboardResponse straight back to its caller. Change that member on the interface and on the class so it hands back a Task carrying the DashboardResponse instead, and then fill it in:
- Start a load on every source before waiting on any of them, so all three are in flight at once, and then wait for all of them together. One call to the loader loads each source exactly once. The sources make the overlap observable: none of them answers until all three have been called or a pause has passed, so a call that arrives on its own is still answered, after that pause, with the line it would otherwise have given. Loading the sources one after another therefore produces a correct looking dashboard, and only the overlap gives it away.
- Turn each source into one section carrying name, the name of the source, and status, which is loaded for a source that answered and failed for one that raised an error. A loaded section carries the line it answered as payload, and null as error. A failed section carries the message of the error as error, and null as payload.
- Keep a source's failure to itself. The others are started alongside the one that fails rather than after it, and their sections still come back loaded with their own lines. The dashboard is still answered whether one, two or all three of the sources fail.
- Report those sections in the order the dashboard lists their sources, alongside loadedCount and failedCount, how many sections came back in each of the two states.
- Hand the cancellation token LoadAsync was given to every source call. A token that can never be canceled, such as CancellationToken.None, leaves all three loads running long after the caller has gone.
- Treat a cancellation as the end of the whole dashboard rather than as one more failed section. Once cancellation is requested no section is loaded and no dashboard is answered: the call surfaces an OperationCanceledException instead, and it does so for a token that already reports cancellation before the load starts as well.
- Await each source call rather than waiting on it. Reading a task's Result, calling Wait, calling GetAwaiter().GetResult(), and starting the work on another thread with Task.Run all fail, however right the dashboard they hand back looks. The interface keeps declaring exactly the one member, and every member you declare on it and on the class answers through a Task.
API Endpoints to Implement
DashboardController
GET /dashboard
The controller ships declaring one action that answers on the spot, and it keeps declaring exactly that one action. Change it so the action answers through a Task as well and awaits the loader instead of waiting on it, and so that it takes the cancellation token of the request being served as a parameter and hands that token down, which is what stops the loads behind a request whose caller has disconnected.
- 200 with the loaded dashboard, whether every section came back loaded or none of them did.
Sample Requests & Responses
GET /dashboard, with all three sources answering
Response code: 200
{
"sections": [
{ "name": "sales", "status": "loaded", "payload": "214 orders today, 38150.00 booked", "error": null },
{ "name": "inventory", "status": "loaded", "payload": "12 lines below reorder level, 3 on back order", "error": null },
{ "name": "alerts", "status": "loaded", "payload": "2 open incidents, 1 acknowledged", "error": null }
],
"loadedCount": 3,
"failedCount": 0
}
GET /dashboard, with the inventory source raising an error reading Inventory service answered 503.
Response code: 200
{
"sections": [
{ "name": "sales", "status": "loaded", "payload": "214 orders today, 38150.00 booked", "error": null },
{ "name": "inventory", "status": "failed", "payload": null, "error": "Inventory service answered 503." },
{ "name": "alerts", "status": "loaded", "payload": "2 open incidents, 1 acknowledged", "error": null }
],
"loadedCount": 2,
"failedCount": 1
}
More failures change nothing but the sections and the counts:
- With the sales and alerts sources both raising errors, those two sections come back failed carrying their own messages, inventory is the only one loaded, and the counts are a loadedCount of 1 and a failedCount of 2.
- With all three raising errors, every section comes back failed carrying its own message, and the counts are 0 and 3.
A caller who gives up on GET /dashboard part way through gets no response at all, and leaves no source still loading behind the request.
GET / answers 200.
