Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. namespace Mercer.Outsourcing.SmallMarketPortal.Tasks.SchedulerTileCacheTask
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Threading;
  7.     using System.Threading.Tasks;
  8.     using CommonTask;
  9.     using CommonTask.Service;
  10.     using Service.ApiProxy;
  11.     using Utils.Extensions;
  12.     using Common.Consts;
  13.     using DataAccess.ApiProxy;
  14.     using DataAccess.Events.Caching;
  15.     using Newtonsoft.Json;
  16.     using Service.Caching;
  17.     using Service.Events.Caching;
  18.     using Service.Events.HrAdmin.ViewModel;
  19.     using Service.Mapping;
  20.     using Shared.SharedLibrary;
  21.     using TaskStatus = Shared.ApplicationEvents.TaskManager2.Entities.TaskStatus;
  22.  
  23.     public class SchedulerTileCacheTask : CommonTask
  24.     {
  25.         private static int TotalEmployeesCount { get; set; }
  26.         private static int UpdatedEmployeesCount { get; set; }
  27.  
  28.         private readonly ICallerContext callerContext;
  29.  
  30.         public SchedulerTileCacheTask()
  31.         {
  32.             callerContext = CallerContextExtension.CreateDefault();
  33.         }
  34.  
  35.         public override async Task<int> RunAsync(IProgress<TaskProgress> progress, CancellationToken token)
  36.         {
  37.             var tileCacheParams = DataDrivenTilesCachingServiceApiProxy.GetTileCacheParams(App.Api, callerContext)
  38.                 .OrderByDescending(t => t.Priority)
  39.                 .ToList();
  40.  
  41.             //INFO Don't need try block. Because the Common task has a mechanism to catch exceptions. (also log ang progress methods call)
  42.             try
  43.             {
  44.                 UpdateTilesCacheData(tileCacheParams, progress, token);
  45.             }
  46.             catch (Exception e)
  47.             {
  48.                 progress.UpdateProgress(100, TaskStatus.FAILED, "Task failed: " + e.Message);
  49.                 return -1;
  50.             }
  51.  
  52.             return 0;
  53.         }
  54.  
  55.         private void UpdateTilesCacheData(List<TileCacheParams> tileCacheParams, IProgress<TaskProgress> progress, CancellationToken token)
  56.         {
  57.             //INFO Split into two separate methods
  58.             var tileParameters = GetTileParameters(tileCacheParams);
  59.             var tileKeyParameters = tileCacheParams.Select(t => t.GetTileParameters(tileParameters)).ToList(); //INFO GetTileParameters the same name for another method
  60.  
  61.             TotalEmployeesCount = tileKeyParameters.Sum(t => t.TileParameters.Count);
  62.  
  63.             UpdateTilesCacheData(tileKeyParameters, progress, token);
  64.         }
  65.  
  66.         private Dictionary<string, List<TileParameters>> GetTileParameters(List<TileCacheParams> tileCacheParams)
  67.         {
  68.             var tileParameters = new Dictionary<string, List<TileParameters>>();
  69.             foreach (var tileParams in tileCacheParams)
  70.             {
  71.                 if (!tileParameters.ContainsKey(tileParams.SqlQuery))
  72.                 {
  73.                     var parms = CachingDataServiceApiProxy.GetTileParameters(App.Api, callerContext, tileParams.SqlQuery);
  74.                     tileParameters.Add(tileParams.SqlQuery, parms);
  75.                 }
  76.             }
  77.  
  78.             return tileParameters;
  79.         }
  80.        
  81.         private void UpdateTilesCacheData(List<TileCacheKeyParameters> tileKeyParameters, IProgress<TaskProgress> progress, CancellationToken token)
  82.         {
  83.             //INFO Error handling (db timeouts + db locks)
  84.             foreach (var tileParams in tileKeyParameters)
  85.             {
  86.                 //INFO Put into separate property
  87.                 var percentage = (UpdatedEmployeesCount * 100) / TotalEmployeesCount; //INFO wrong solution
  88.                 if (token.IsCancellationRequested)
  89.                 {
  90.                     progress.UpdateProgress(percentage, TaskStatus.COMPLETED, $"Task was stopped at {percentage} percent.");
  91.                     return;
  92.                 }
  93.                 tileParams.TileParameters.ForEach(p => UpdateTileForEmployee(p, tileParams.TileKey));
  94.  
  95.                 Parallel.ForEach(tileParams.TileParameters,
  96.                     new ParallelOptions { CancellationToken = token, MaxDegreeOfParallelism = 4 },
  97.                     (tileParam, loopState, i) => UpdateTileForEmployee(tileParam, tileParams.TileKey));
  98.  
  99.                 UpdatedEmployeesCount++;
  100.                 progress.IncreaseProgress(percentage);
  101.             }
  102.         }
  103.  
  104.         private void UpdateTileForEmployee(TileParameters tileCacheParams, string tileKey)
  105.         {
  106.             var companyId = tileCacheParams.CompanyId;
  107.             var parms = CreateParams(tileCacheParams);
  108.  
  109.             var cacheInfo = new CachedTileDataInfo{ AsOf = DateTime.Now, CompanyId = companyId, CacheStatus = CachingState.Done };
  110.  
  111.             var nonCachedTileData = new List<DataDrivenTile>();
  112.             try
  113.             {
  114.                 nonCachedTileData = DataDrivenTileServiceApiProxy.GetNonCachedTilesDataFromReplica(App.Api, callerContext, companyId, tileKey, parms);
  115.             }
  116.             catch (Exception)
  117.             {
  118.                 Logger.Write(Origin, $"An error was thrown during tiles data getting. CompanyId = {companyId}, TileKey = {tileKey}.");
  119.  
  120.                 cacheInfo.CacheStatus = CachingState.Error;
  121.                 cacheInfo.CacheObject = JsonConvert.SerializeObject(new DataDrivenTileCachedData());
  122.  
  123.                 SaveErroredTiles(cacheInfo, companyId, tileKey, parms);
  124.             }
  125.  
  126.             nonCachedTileData.ForEach(tile => SaveTilesCacheData(tile, cacheInfo, companyId, tileKey, parms));
  127.         }
  128.  
  129.         private void SaveTilesCacheData(DataDrivenTile tile,
  130.             CachedTileDataInfo cacheInfo,
  131.             string companyId,
  132.             string tileKey,
  133.             Dictionary<string, string[]> parms)
  134.         {
  135.             cacheInfo.CacheObject = JsonConvert.SerializeObject(new DataDrivenTileCachedData { Total = tile.Total, Items = tile.Items });
  136.             cacheInfo.CacheKey = DataDrivenTilesCachingServiceApiProxy.GetCacheKey(App.Api, callerContext, companyId, tileKey, tile.Order, parms);
  137.             cacheInfo.StartedTime = tile.StartedTime;
  138.             cacheInfo.CompletedTime = tile.CompletedTime;
  139.  
  140.             DataDrivenTilesCachingServiceApiProxy.UpdateTileCache(App.Api, callerContext, cacheInfo);
  141.         }
  142.  
  143.         private void SaveErroredTiles(CachedTileDataInfo cacheInfo, string companyId, string tileKey, Dictionary<string, string[]> parms)
  144.         {
  145.             var sectionTiles = DataDrivenTileDataServiceApiProxy.GetSimplifiedTilesConfig(App.Api, callerContext, tileKey);
  146.             foreach (var tile in sectionTiles)
  147.             {
  148.                 cacheInfo.CacheKey = DataDrivenTilesCachingServiceApiProxy.GetCacheKey(App.Api, callerContext, companyId, tileKey, tile.Order, parms);
  149.  
  150.                 DataDrivenTilesCachingServiceApiProxy.UpdateTileCache(App.Api, callerContext, cacheInfo); //INFO Needed update state and not data update
  151.             }
  152.         }
  153.  
  154.         private Dictionary<string, string[]> CreateParams(TileParameters tileCacheParams)
  155.         {
  156.             return new Dictionary<string, string[]>
  157.             {
  158.                 //INFO Maybe transform string to array ("11954,15487,4588" to ["11954", "15487", "4588"])
  159.                 { "CONTROL_ID", new[] { tileCacheParams.ControlId } },
  160.                 { "COMPANY_ID", new[] { tileCacheParams.CompanyId } },
  161.                 { "ELIG_GROUP_IDS", new[] { tileCacheParams.EligGroupIds } },
  162.                 { "AVAIL_COMPANY_IDS", new[] { tileCacheParams.CompanyIds } },
  163.                 { "AVAIL_ALL_COMPANIES", new[] { tileCacheParams.AvailAllCompanies } }
  164.             };
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement