private async Task LinkResources() { //_logger.LogInformation("BackgroundService: Checking linking messages from SQS Queue.... "); Stopwatch stpWatch = new Stopwatch(); stpWatch.Start(); //Check for msgs in a loop int msgcount, totalMsgCount = 0; do { //Check msgs available in SQS var msgs = await _awsSQSHelper.ReceiveLinkingMessageAsync(); msgcount = msgs.Count; totalMsgCount += msgs.Count; //Iterate on each msg which will contain list of resource foreach (var msg in msgs) { //Get reources from the msg List <ResourceLinkingInformation> resourceLinkingInfos = JsonConvert.DeserializeObject <List <ResourceLinkingInformation> >(msg.Body); //List to collect ValidationFacade of each resource List <ResourceLinkingResult> totalLinkingResult = new List <ResourceLinkingResult>(); var tasks = resourceLinkingInfos.Select(async linkInfo => //foreach (ResourceRequestDTO resource in resources) { ResourceLinkingResult curlinkingResult = new ResourceLinkingResult { PidUri = linkInfo.PidUri, LinkType = linkInfo.LinkType, PidUriToLink = linkInfo.PidUriToLink, Requester = linkInfo.Requester }; try { //Add link await _resourceService.AddResourceLink(linkInfo.PidUri, linkInfo.LinkType, linkInfo.PidUriToLink, linkInfo.Requester); //Collect result curlinkingResult.Status = "Linked"; curlinkingResult.TimeTaken = stpWatch.ElapsedMilliseconds.ToString(); curlinkingResult.Message = "Linked Successfully"; } catch (System.Exception ex) { //Collect result curlinkingResult.Status = "Error"; curlinkingResult.TimeTaken = stpWatch.ElapsedMilliseconds.ToString(); curlinkingResult.Message = ex.Message; } totalLinkingResult.Add(curlinkingResult); }); await Task.WhenAll(tasks); //Send msg to output queue and delete message from input queue if (await _awsSQSHelper.SendLinkingMessageAsync(totalLinkingResult)) { await _awsSQSHelper.DeleteLinkingMessageAsync(msg.ReceiptHandle); } } } while (msgcount > 0); stpWatch.Stop(); if (totalMsgCount > 0) { //_logger.LogInformation("BackgroundService: Linked {totMsgCount} resources in {milsec} Milliseconds.", totalMsgCount, stpWatch.ElapsedMilliseconds); } }
public async Task <IActionResult> CreateResourceLinkAsync([FromQuery] string pidUri, [FromQuery] string linkType, [FromQuery] string pidUriToLink, [FromQuery] string requester) { var result = await _resourceService.AddResourceLink(pidUri, linkType, pidUriToLink, requester); return(Ok(result)); }