Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Mercer.Outsourcing.SmallMarketPortal.Tasks.SchedulerTileCacheTask
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using CommonTask;
- using CommonTask.Service;
- using Service.ApiProxy;
- using Utils.Extensions;
- using Common.Consts;
- using DataAccess.ApiProxy;
- using DataAccess.Events.Caching;
- using Newtonsoft.Json;
- using Service.Caching;
- using Service.Events.Caching;
- using Service.Events.HrAdmin.ViewModel;
- using Service.Mapping;
- using Shared.SharedLibrary;
- using TaskStatus = Shared.ApplicationEvents.TaskManager2.Entities.TaskStatus;
- public class SchedulerTileCacheTask : CommonTask
- {
- private static int TotalEmployeesCount { get; set; }
- private static int UpdatedEmployeesCount { get; set; }
- private readonly ICallerContext callerContext;
- public SchedulerTileCacheTask()
- {
- callerContext = CallerContextExtension.CreateDefault();
- }
- public override async Task<int> RunAsync(IProgress<TaskProgress> progress, CancellationToken token)
- {
- var tileCacheParams = DataDrivenTilesCachingServiceApiProxy.GetTileCacheParams(App.Api, callerContext)
- .OrderByDescending(t => t.Priority)
- .ToList();
- //INFO Don't need try block. Because the Common task has a mechanism to catch exceptions. (also log ang progress methods call)
- try
- {
- UpdateTilesCacheData(tileCacheParams, progress, token);
- }
- catch (Exception e)
- {
- progress.UpdateProgress(100, TaskStatus.FAILED, "Task failed: " + e.Message);
- return -1;
- }
- return 0;
- }
- private void UpdateTilesCacheData(List<TileCacheParams> tileCacheParams, IProgress<TaskProgress> progress, CancellationToken token)
- {
- //INFO Split into two separate methods
- var tileParameters = GetTileParameters(tileCacheParams);
- var tileKeyParameters = tileCacheParams.Select(t => t.GetTileParameters(tileParameters)).ToList(); //INFO GetTileParameters the same name for another method
- TotalEmployeesCount = tileKeyParameters.Sum(t => t.TileParameters.Count);
- UpdateTilesCacheData(tileKeyParameters, progress, token);
- }
- private Dictionary<string, List<TileParameters>> GetTileParameters(List<TileCacheParams> tileCacheParams)
- {
- var tileParameters = new Dictionary<string, List<TileParameters>>();
- foreach (var tileParams in tileCacheParams)
- {
- if (!tileParameters.ContainsKey(tileParams.SqlQuery))
- {
- var parms = CachingDataServiceApiProxy.GetTileParameters(App.Api, callerContext, tileParams.SqlQuery);
- tileParameters.Add(tileParams.SqlQuery, parms);
- }
- }
- return tileParameters;
- }
- private void UpdateTilesCacheData(List<TileCacheKeyParameters> tileKeyParameters, IProgress<TaskProgress> progress, CancellationToken token)
- {
- //INFO Error handling (db timeouts + db locks)
- foreach (var tileParams in tileKeyParameters)
- {
- //INFO Put into separate property
- var percentage = (UpdatedEmployeesCount * 100) / TotalEmployeesCount; //INFO wrong solution
- if (token.IsCancellationRequested)
- {
- progress.UpdateProgress(percentage, TaskStatus.COMPLETED, $"Task was stopped at {percentage} percent.");
- return;
- }
- tileParams.TileParameters.ForEach(p => UpdateTileForEmployee(p, tileParams.TileKey));
- Parallel.ForEach(tileParams.TileParameters,
- new ParallelOptions { CancellationToken = token, MaxDegreeOfParallelism = 4 },
- (tileParam, loopState, i) => UpdateTileForEmployee(tileParam, tileParams.TileKey));
- UpdatedEmployeesCount++;
- progress.IncreaseProgress(percentage);
- }
- }
- private void UpdateTileForEmployee(TileParameters tileCacheParams, string tileKey)
- {
- var companyId = tileCacheParams.CompanyId;
- var parms = CreateParams(tileCacheParams);
- var cacheInfo = new CachedTileDataInfo{ AsOf = DateTime.Now, CompanyId = companyId, CacheStatus = CachingState.Done };
- var nonCachedTileData = new List<DataDrivenTile>();
- try
- {
- nonCachedTileData = DataDrivenTileServiceApiProxy.GetNonCachedTilesDataFromReplica(App.Api, callerContext, companyId, tileKey, parms);
- }
- catch (Exception)
- {
- Logger.Write(Origin, $"An error was thrown during tiles data getting. CompanyId = {companyId}, TileKey = {tileKey}.");
- cacheInfo.CacheStatus = CachingState.Error;
- cacheInfo.CacheObject = JsonConvert.SerializeObject(new DataDrivenTileCachedData());
- SaveErroredTiles(cacheInfo, companyId, tileKey, parms);
- }
- nonCachedTileData.ForEach(tile => SaveTilesCacheData(tile, cacheInfo, companyId, tileKey, parms));
- }
- private void SaveTilesCacheData(DataDrivenTile tile,
- CachedTileDataInfo cacheInfo,
- string companyId,
- string tileKey,
- Dictionary<string, string[]> parms)
- {
- cacheInfo.CacheObject = JsonConvert.SerializeObject(new DataDrivenTileCachedData { Total = tile.Total, Items = tile.Items });
- cacheInfo.CacheKey = DataDrivenTilesCachingServiceApiProxy.GetCacheKey(App.Api, callerContext, companyId, tileKey, tile.Order, parms);
- cacheInfo.StartedTime = tile.StartedTime;
- cacheInfo.CompletedTime = tile.CompletedTime;
- DataDrivenTilesCachingServiceApiProxy.UpdateTileCache(App.Api, callerContext, cacheInfo);
- }
- private void SaveErroredTiles(CachedTileDataInfo cacheInfo, string companyId, string tileKey, Dictionary<string, string[]> parms)
- {
- var sectionTiles = DataDrivenTileDataServiceApiProxy.GetSimplifiedTilesConfig(App.Api, callerContext, tileKey);
- foreach (var tile in sectionTiles)
- {
- cacheInfo.CacheKey = DataDrivenTilesCachingServiceApiProxy.GetCacheKey(App.Api, callerContext, companyId, tileKey, tile.Order, parms);
- DataDrivenTilesCachingServiceApiProxy.UpdateTileCache(App.Api, callerContext, cacheInfo); //INFO Needed update state and not data update
- }
- }
- private Dictionary<string, string[]> CreateParams(TileParameters tileCacheParams)
- {
- return new Dictionary<string, string[]>
- {
- //INFO Maybe transform string to array ("11954,15487,4588" to ["11954", "15487", "4588"])
- { "CONTROL_ID", new[] { tileCacheParams.ControlId } },
- { "COMPANY_ID", new[] { tileCacheParams.CompanyId } },
- { "ELIG_GROUP_IDS", new[] { tileCacheParams.EligGroupIds } },
- { "AVAIL_COMPANY_IDS", new[] { tileCacheParams.CompanyIds } },
- { "AVAIL_ALL_COMPANIES", new[] { tileCacheParams.AvailAllCompanies } }
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement