async Task <Game> RetrieveGame(Guid gameId, bool skip = false) { Game game; try { var jsonStr = encoding.GetString(await _cache.Get(GetCacheKey(gameId))); game = JsonConvert.DeserializeObject <Game>(jsonStr, Settings); } catch (KeyNotFoundException) { if (skip) { return(null); } throw new NotFoundException("Item with ID not found: " + gameId); } FixUpDependencies(game.NetworkContent.ToArray()); return(game); }
public async Task <WebhookEventQueueEntity> GetProcessingEventAsync() { var eventStatuses = _cache.Get <ConcurrentDictionary <string, int> >(ConstantString.EventStatusCacheKey); var queueEventStatusId = eventStatuses.GetValueOrDefault(EventStatusEnum.InQueue.ToString()); var processingStatusId = eventStatuses.GetValueOrDefault(EventStatusEnum.Processing.ToString()); var throttleStatusId = eventStatuses.GetValueOrDefault(EventStatusEnum.Skip.ToString()); var inQueueIds = await _encompassEventRepository.SelectInQueueEventAsync(_jobPickerConfiguration.GetWorkerId(), queueEventStatusId).ConfigureAwait(false); if (inQueueIds.Count == 0) { _logger.Info(ConstantString.NoEventInQueue); return(new WebhookEventQueueEntity { Id = 0 }); } var processingEventId = inQueueIds.LastOrDefault(); var throttlingEventIds = inQueueIds.Take(inQueueIds.Count - 1); var throttlingEventIdParamter = string.Join(",", throttlingEventIds); var processingEvent = await _encompassEventRepository.SelectEventAsync(processingEventId).ConfigureAwait(false); var dtTmOffsetNow = DateTimeOffset.Now; processingEvent.PickupDtTm = dtTmOffsetNow; processingEvent.StatusId = processingStatusId; int affectedRows = 0; if (!throttlingEventIds.Any()) { affectedRows = await _encompassEventRepository.UpdateEventAsync(processingEvent).ConfigureAwait(false); } else { affectedRows = await _encompassEventRepository.PerformEventThrottlingAsync(throttlingEventIdParamter, processingEventId, throttleStatusId, dtTmOffsetNow, dtTmOffsetNow, processingEvent).ConfigureAwait(false); } return(processingEvent); }
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"); } }
public async Task <ProcessDocumentResponse> ProcessDocumentAsync(ProcessDocumentRequest request) { var response = new ProcessDocumentResponse(); try { var sdkConfig = _cache.Get <EncompassSdkConfig>(ConstantString.EncompassSdkCacheKey); var tokenResponse = await RequestAccessToken(sdkConfig).ConfigureAwait(false); // get access token if (!tokenResponse.StatusCode.IsSuccessStatusCode()) { response.SuccessBit = false; response.EventStatus = EventStatusEnum.Error; response.ErrorMsgTxt = JsonConvert.SerializeObject(new { errorCode = tokenResponse.StatusCode, errorMsgTxt = JsonConvert.SerializeObject(tokenResponse.Body.FailureResponse) }); } var accessToken = tokenResponse.Body.SuccessResponse.AccessToken; // get list of latest documents via api var getAttachmentApis = await _encompassClient.QueryDocumentAsync(new GetAttachmentRequest { AccessToken = accessToken, LoanGuid = request.LoanGuid }).ConfigureAwait(false); if (getAttachmentApis.Body?.FailureResponse != null) { response.SuccessBit = false; response.EventStatus = EventStatusEnum.Error; response.ErrorMsgTxt = JsonConvert.SerializeObject(new { errorCode = getAttachmentApis.StatusCode, errorMsgTxt = string.Format(ConstantString.ErrorWhileGettingAttachments, JsonConvert.SerializeObject(getAttachmentApis.Body?.FailureResponse)) }); } var cachedDocuments = await GetDocumentCacheAsync(request.LoanGuid).ConfigureAwait(false); var apiDocuments = ParseDoucmentApiResponse(getAttachmentApis.Body); var apiDocumentIds = apiDocuments.Select(x => x.Key).ToHashSet(); var apiNotCacheDocumentIds = GetDocumentIdToDownload(cachedDocuments, apiDocumentIds); // latest documents from api the same as cache if (apiNotCacheDocumentIds.Count == 0) { response.SuccessBit = true; response.EventStatus = EventStatusEnum.Done; response.EventReason = string.Format(ConstantString.AllDocumentDowloaded); return(response); } // here means there are mismatch between documents in cache and in api var latestPersistedDocuments = await GetPersistedDocumentAsync(request.LoanGuid).ConfigureAwait(false); var apiNotDbDocumentIds = GetDocumentIdToDownload(latestPersistedDocuments, apiNotCacheDocumentIds); // there is no new documents if (apiNotDbDocumentIds.Count == 0) { response = await TryUpdateDocumentCache(request.LoanGuid, latestPersistedDocuments, response).ConfigureAwait(false); return(response); } // in db, we do not even have documents so we need to perform download. var addingDbResults = await AddNewDocument(apiNotDbDocumentIds, apiDocuments, request.LoanGuid).ConfigureAwait(false); var successAddingDbResults = ExtractSuccessDbResult(addingDbResults, response.DocErrors, false); var downloadDocumentResponses = await PerformDownloadDocument(successAddingDbResults, request.LoanGuid, accessToken).ConfigureAwait(false); var downloadSuccessResponses = ExtractSuccessApiResult(downloadDocumentResponses, response.DocErrors, false); var updatingDbResults = await UpdateNewDocument(downloadSuccessResponses, request.LoanGuid); var updatedSuccessDbResults = ExtractSuccessDbResult(updatingDbResults, response.DocErrors, true); var updatededDocumentGuids = GetUpdatedDocumentGuid(updatedSuccessDbResults); if (cachedDocuments != null) { updatededDocumentGuids.UnionWith(cachedDocuments); } response = await TryUpdateDocumentCache(request.LoanGuid, updatededDocumentGuids, response).ConfigureAwait(false); } catch (Exception ex) { response.SuccessBit = false; response.EventStatus = EventStatusEnum.Error; response.EventReason = ex.Message; response.ErrorMsgTxt = JsonConvert.SerializeObject(new { message = JsonConvert.SerializeObject(ex.Message), stackTrace = ex.StackTrace }); } return(response); }
public string FindUrl(string code) { return(_localCache.Get <UrlModel>(code)?.OriginalUrl); }