Problem statement
Complete the implementation of ExportService and ExportsController so that a long export stops as soon as the caller has stopped waiting for it.
Sample Data
Three exports are seeded. orders holds twenty four rows, ORD-1001 through ORD-1024. invoices holds six rows, INV-5001 through INV-5006. refunds is a real export that holds no rows at all. No other name belongs to an export.
Service
ExportService
ExportService is handed an IExportRowSource, which counts and reads the rows of an export, and an IExportSink, which is where a row is written. Both take a cancellation token on every call, and a source call handed a token that already reports cancellation gives up instead of answering.
Implement ExportAsync:
- Ask the source for the row count of the named export, once. A count of null means no export goes by that name: answer null, having read and written nothing.
- Otherwise read the rows one at a time, from index 0 up to that count, and write each row to the sink as it is read. Every row is read once, and the rows reach the sink in the order the source counts them.
- Hand the cancellation token ExportAsync was given to every call it makes: the count, every read and every write. A token that can never be canceled, such as CancellationToken.None, leaves the export running long after the caller has gone.
- Stop as soon as cancellation is requested. No more than one further row is read past that point, and the rest of the export is neither read nor written. The rows already written stay written.
- Read nothing at all when the token already reports cancellation before the export starts.
- Surface the cancellation as an OperationCanceledException rather than returning a short result.
- Return an ExportResult carrying exportName, the name it was asked for, rowsWritten, the number of rows it wrote, and completed, which is true once the export has reached the end of its rows. A canceled export never returns a result, so every result that does come back carries true.
API Endpoints to Implement
ExportsController
GET /exports/{exportName}
- Accept the cancellation token of the request being served as a parameter, and hand it to ExportAsync, so a caller who disconnects part way through stops the export running behind the request.
- Answer 200 with the result of the export.
- Answer 404 when ExportAsync answers null, with a message built from the template Export {exportName} was not found., naming the export that was asked for.
Sample Requests & Responses
GET /exports/orders
Every one of the twenty four rows is read and written, in the order the source counts them, so the sink ends up holding ORD-1001 through ORD-1024.
Response code: 200
{
"exportName": "orders",
"rowsWritten": 24,
"completed": true
}
- GET /exports/invoices answers 200 with a rowsWritten of 6, and the sink holds INV-5001 through INV-5006.
- GET /exports/refunds answers 200 with a rowsWritten of 0 and a completed of true, having read no row and written none.
GET /exports/payroll
Response code: 404
{
"message": "Export payroll was not found."
}
A caller who gives up on GET /exports/orders part way through leaves the rows written so far written, the rows past the one being read unread, and no response to send.
