void IThumbnailOp.Process(ThumbnailRequest request) { runStateMachine("download-start", request); }
private bool runStateMachine(string state, ThumbnailRequest request, byte[] image = null, Exception error = null) { if (error != null) { return(handleError(state, request, error)); } var trackingId = request.Srv.TrackingId; var downloadKey = request.Url; var thumbnailKey = request.Key; switch (state) { case "download-start": { var(download, firstTouch) = _cache.Get(downloadKey); var cacheHit = download != null; _log.info(trackingId, () => $"{state} firstTouch={firstTouch}, cacheHit={cacheHit}"); if (cacheHit) { return(runStateMachine("download-ready", request, download)); } if (firstTouch) { _async.Start(downloadKey, downloadImage(request, request.Url)); } _async.WhenReady(downloadKey, (bytes, ex) => runStateMachine("download-ready", request, bytes, ex)); return(false); } case "download-ready": { _cache.Put(downloadKey, image); var(thumbnail, firstTouch) = _cache.Get(thumbnailKey); var cacheHit = thumbnail != null; _log.info(trackingId, () => $"{state} firstTouch={firstTouch}, cacheHit={cacheHit}"); if (cacheHit) { return(runStateMachine("thumbnail-ready", request, thumbnail)); } if (firstTouch) { _async.Start(thumbnailKey, resizeImage(request, image)); } _async.WhenReady(thumbnailKey, (bytes, ex) => runStateMachine("thumbnail-ready", request, bytes, ex)); return(false); } case "thumbnail-ready": { _cache.Put(thumbnailKey, image); request.Srv.EndWith(image); _log.info(trackingId, () => $"{state}"); return(true); } default: throw new Exception("Should not get here"); } }