public static void UpdateRedirectMappings(bool addInstruction = false) { using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var meta301 = repo.GetPuckRedirect().Where(x => x.Type == "301").ToList(); var meta302 = repo.GetPuckRedirect().Where(x => x.Type == "302").ToList(); var map301 = new Dictionary <string, string>(); meta301.ForEach(x => { //map301.Add(x.Key.ToLower(), x.Value.ToLower()); map301[x.From.ToLower()] = x.To.ToLower(); }); var map302 = new Dictionary <string, string>(); meta302.ForEach(x => { //map302.Add(x.Key.ToLower(), x.Value.ToLower()); map302[x.From.ToLower()] = x.To.ToLower(); }); PuckCache.Redirect301 = map301; PuckCache.Redirect302 = map302; if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdateRedirects; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public static void UpdatePathLocaleMappings(bool addInstruction = false) { using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var meta = repo.GetPuckMeta() .Where(x => x.Name == DBNames.PathToLocale) .ToList() .Where(x => !string.IsNullOrEmpty(x.Key)) .OrderByDescending(x => x.Key.Count(xx => xx == '/')) .ToList(); var map = new Dictionary <string, string>(); meta.ForEach(x => { map[x.Key.ToLower()] = x.Value.ToLower(); }); PuckCache.PathToLocale = map; if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdatePathLocales; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public JsonResult FieldGroups(Settings model) { string msg = ""; bool success = false; try { //fieldgroup if (model.TypeGroupField != null && model.TypeGroupField.Count > 0) { foreach (var mod in apiHelper.AllModels(true)) { var fieldGroupMeta = repo.GetPuckMeta().Where(x => x.Name.StartsWith(DBNames.FieldGroups + mod.Name)).ToList(); fieldGroupMeta.ForEach(x => { repo.DeleteMeta(x); }); } model.TypeGroupField.ForEach(x => { var values = x.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); var newMeta = new PuckMeta(); newMeta.Name = DBNames.FieldGroups + values[0]; newMeta.Key = values[1]; newMeta.Value = values[2]; repo.AddMeta(newMeta); }); } var modelTypes = apiHelper.Models(); string cacheKeys = ""; foreach (var modelType in modelTypes) { string cacheKey = "fieldGroups_" + modelType.Name; cacheKeys += cacheKey + ","; cache.Remove(cacheKey); } cacheKeys = cacheKeys.TrimEnd(','); var instruction = new PuckInstruction() { Count = modelTypes.Count, ServerName = ApiHelper.ServerName(), InstructionDetail = cacheKeys, InstructionKey = InstructionKeys.RemoveFromCache }; repo.AddPuckInstruction(instruction); repo.SaveChanges(); success = true; } catch (Exception ex) { msg = ex.Message; success = false; } return(Json(new { success = success, message = msg })); }
public static void UpdateCacheMappings(bool addInstruction = false) { using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var metaTypeCache = repo.GetPuckMeta().Where(x => x.Name == DBNames.CachePolicy).ToList(); var metaCacheExclude = repo.GetPuckMeta().Where(x => x.Name == DBNames.CacheExclude).ToList(); var mapTypeCache = new Dictionary <string, int>(); var mapTypeVaryByQs = new Dictionary <string, string>(); foreach (var x in metaTypeCache) { var values = x.Value.Split(":", StringSplitOptions.RemoveEmptyEntries); if (values.Length == 0) { continue; } int cacheMinutes; if (int.TryParse(values[0], out cacheMinutes)) { //mapTypeCache.Add(x.Key, cacheMinutes); mapTypeCache[x.Key] = cacheMinutes; } if (values.Length > 1) { if (!string.IsNullOrEmpty(values[1]) && !string.IsNullOrWhiteSpace(values[1])) { mapTypeVaryByQs[x.Key] = values[1]; } } } var mapCacheExclude = new HashSet <string>(); metaCacheExclude.Where(x => x.Value.ToLower() == bool.TrueString.ToLower()).ToList().ForEach(x => { if (!mapCacheExclude.Contains(x.Key.ToLower())) { mapCacheExclude.Add(x.Key.ToLower()); } }); PuckCache.TypeOutputCache = mapTypeCache; PuckCache.OutputCacheExclusion = mapCacheExclude; PuckCache.TypeOutputCacheVaryByQueryString = mapTypeVaryByQs; if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdateCacheMappings; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public JsonResult DeleteParameters(string key) { bool success = false; string message = ""; try { var metas = repo.GetPuckMeta().Where(x => x.Name == DBNames.EditorSettings && x.Key == key).ToList(); var meta = metas.FirstOrDefault(); metas.ForEach(x => repo.DeleteMeta(x)); //clear cached values var cachePrefix = "editor_settings_"; var cacheKey = cachePrefix + key; cache.Remove(cacheKey); cache.Remove("null_" + cacheKey); var instruction = new PuckInstruction() { Count = 2, ServerName = ApiHelper.ServerName(), InstructionDetail = $"{cacheKey},{"null_" + cacheKey}", InstructionKey = InstructionKeys.RemoveFromCache }; repo.AddPuckInstruction(instruction); repo.SaveChanges(); if (meta != null) { var keyParts = key.Split(new char[] { ':' }); var typeSettings = ApiHelper.EditorSettingTypes().FirstOrDefault(x => x.FullName == keyParts[0]); object model = JsonConvert.DeserializeObject(meta.Value, typeSettings); ApiHelper.OnAfterSettingsDelete(this, new puck.core.Events.AfterEditorSettingsDeleteEventArgs { Setting = (I_Puck_Editor_Settings)model , SettingsTypeFullName = keyParts[0] , ModelTypeName = keyParts[1] , PropertyName = keyParts[2] }); } success = true; } catch (Exception ex) { success = false; message = ex.Message; log.Log(ex); } return(Json(new { success = success, message = message })); }
public static void UpdateCacheMappings(bool addInstruction = false) { using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var metaTypeCache = repo.GetPuckMeta().Where(x => x.Name == DBNames.CachePolicy).ToList(); var metaCacheExclude = repo.GetPuckMeta().Where(x => x.Name == DBNames.CacheExclude).ToList(); var mapTypeCache = new Dictionary <string, int>(); metaTypeCache.ForEach(x => { int cacheMinutes; if (int.TryParse(x.Value, out cacheMinutes)) { //mapTypeCache.Add(x.Key, cacheMinutes); mapTypeCache[x.Key] = cacheMinutes; } }); var mapCacheExclude = new HashSet <string>(); metaCacheExclude.Where(x => x.Value.ToLower() == bool.TrueString.ToLower()).ToList().ForEach(x => { if (!mapCacheExclude.Contains(x.Key.ToLower())) { mapCacheExclude.Add(x.Key.ToLower()); } }); PuckCache.TypeOutputCache = mapTypeCache; PuckCache.OutputCacheExclusion = mapCacheExclude; if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdateCacheMappings; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public static void UpdateTaskMappings(bool addInstruction = false) { using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var apiHelper = scope.ServiceProvider.GetService <I_Api_Helper>(); var tasks = apiHelper.Tasks(); tasks.AddRange(apiHelper.SystemTasks()); //tasks = tasks.Where(x => tdispatcher.CanRun(x)).ToList(); tasks.ForEach(x => x.TaskEnd += tdispatcher.HandleTaskEnd); tdispatcher.Tasks = tasks; if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdateTaskMappings; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public static void UpdateDomainMappings(bool addInstruction = false) { using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var meta = repo.GetPuckMeta().Where(x => x.Name == DBNames.DomainMapping).ToList(); var map = new Dictionary <string, string>(); meta.ForEach(x => { //map.Add(x.Value.ToLower(), x.Key.ToLower()); map[x.Value.ToLower()] = x.Key.ToLower(); }); PuckCache.DomainRoots = map; if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdateDomainMappings; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public static void UpdateCrops(bool addInstruction = false) { var settingsType = typeof(CropsEditorSettings); var modelType = typeof(BaseModel); PuckCache.CropSizes = new Dictionary <string, Models.CropInfo>(); using (var scope = PuckCache.ServiceProvider.CreateScope()) { var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); string key = string.Concat(settingsType.FullName, ":", modelType.Name, ":"); var meta = repo.GetPuckMeta().Where(x => x.Name == DBNames.EditorSettings && x.Key.Equals(key)).FirstOrDefault(); if (meta != null) { var data = JsonConvert.DeserializeObject(meta.Value, settingsType) as CropsEditorSettings; if (data != null) { foreach (var crop in data.Crops ?? new List <Models.CropInfo>()) { if (!string.IsNullOrEmpty(crop.Alias)) { PuckCache.CropSizes[crop.Alias] = crop; } } } } if (addInstruction) { var instruction = new PuckInstruction(); instruction.InstructionKey = InstructionKeys.UpdateCrops; instruction.Count = 1; instruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(instruction); repo.SaveChanges(); } } }
public void DeletePuckInstruction(PuckInstruction pi) { repo.PuckInstruction.Remove(pi); }
public void AddPuckInstruction(PuckInstruction pi) { repo.PuckInstruction.Add(pi); }
public JsonResult AllowedTemplates(Settings model) { string msg = ""; bool success = false; try { //typeallowedtemplates if (model.TypeAllowedTemplates != null && model.TypeAllowedTemplates.Count > 0) { var typeAllowedTemplatesMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.TypeAllowedTemplates).ToList(); typeAllowedTemplatesMeta.ForEach(x => { repo.DeleteMeta(x); }); model.TypeAllowedTemplates.ForEach(x => { var values = x.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); var newMeta = new PuckMeta(); newMeta.Name = DBNames.TypeAllowedTemplates; newMeta.Key = values[0]; newMeta.Value = values[1]; repo.AddMeta(newMeta); }); } else { var typeAllowedTemplatesMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.TypeAllowedTemplates).ToList(); typeAllowedTemplatesMeta.ForEach(x => { repo.DeleteMeta(x); }); } var modelTypes = apiHelper.Models(); string cacheKeys = ""; foreach (var modelType in modelTypes) { string cacheKey = "allowedViews_" + modelType.Name; cacheKeys += cacheKey + ","; cache.Remove(cacheKey); } cacheKeys = cacheKeys.TrimEnd(','); var instruction = new PuckInstruction() { Count = modelTypes.Count, ServerName = ApiHelper.ServerName(), InstructionDetail = cacheKeys, InstructionKey = InstructionKeys.RemoveFromCache }; repo.AddPuckInstruction(instruction); repo.SaveChanges(); success = true; } catch (Exception ex) { msg = ex.Message; success = false; } return(Json(new { success = success, message = msg })); }
public async Task <JsonResult> EditParameters(string puck_settingsType, string puck_modelType, string puck_propertyName, IFormCollection fc) { string key = string.Concat(puck_settingsType, ":", puck_modelType, ":", puck_propertyName); //var targetType = Type.GetType(puck_settingsType); var targetType = ApiHelper.EditorSettingTypes().FirstOrDefault(x => x.FullName == puck_settingsType); var model = Activator.CreateInstance(targetType); bool success = false; string message = ""; try { if (await this.TryUpdateModelAsync(model, targetType, "")) { PuckMeta settingsMeta = null; settingsMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.EditorSettings && x.Key == key).FirstOrDefault(); if (settingsMeta != null) { settingsMeta.Value = JsonConvert.SerializeObject(model); } else { settingsMeta = new PuckMeta(); settingsMeta.Name = DBNames.EditorSettings; settingsMeta.Key = key; settingsMeta.Value = JsonConvert.SerializeObject(model); repo.AddMeta(settingsMeta); } //clear cached values var cachePrefix = "editor_settings_"; var cacheKey = cachePrefix + key; cache.Remove(cacheKey); cache.Remove("null_" + cacheKey); var instruction = new PuckInstruction() { Count = 2, ServerName = ApiHelper.ServerName(), InstructionDetail = $"{cacheKey},{"null_"+cacheKey}", InstructionKey = InstructionKeys.RemoveFromCache }; repo.AddPuckInstruction(instruction); repo.SaveChanges(); ApiHelper.OnAfterSettingsSave(this, new puck.core.Events.AfterEditorSettingsSaveEventArgs { Setting = (I_Puck_Editor_Settings)model , SettingsTypeFullName = puck_settingsType , ModelTypeName = puck_modelType , PropertyName = puck_propertyName }); success = true; } else { success = false; message = string.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); } } catch (Exception ex) { success = false; message = ex.Message; log.Log(ex); } return(Json(new { success = success, message = message })); }
public JsonResult AllowedTemplates(Settings model) { string msg = ""; bool success = false; try { //typeallowedtemplates var i = 0; if (model.TypeAllowedTemplates != null && model.TypeAllowedTemplates.Count > 0) { var typeAllowedTemplatesMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.TypeAllowedTemplates).ToList(); var metaPosted = new List <PuckMeta>(); model.TypeAllowedTemplates.ForEach(x => { var dt = DateTime.Now.AddMinutes(i + 1); var values = x.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); var newMeta = new PuckMeta(); newMeta.Name = DBNames.TypeAllowedTemplates; newMeta.Key = values[0]; newMeta.Value = values[1]; newMeta.Dt = dt; metaPosted.Add(newMeta); var existingMeta = typeAllowedTemplatesMeta.FirstOrDefault(m => m.Name == newMeta.Name && m.Key == newMeta.Key && m.Value == newMeta.Value); if (existingMeta == null) { repo.AddPuckMeta(newMeta); } else { existingMeta.Dt = dt; } i++; }); typeAllowedTemplatesMeta.ForEach(x => { if (!metaPosted.Any(p => p.Name == x.Name && p.Key == x.Key && p.Value == x.Value)) { repo.DeletePuckMeta(x); } }); } else { var typeAllowedTemplatesMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.TypeAllowedTemplates).ToList(); typeAllowedTemplatesMeta.ForEach(x => { repo.DeletePuckMeta(x); }); } var modelTypes = apiHelper.Models(); string cacheKeys = ""; foreach (var modelType in modelTypes) { string cacheKey = "allowedViews_" + modelType.Name; cacheKeys += cacheKey + ","; cache.Remove(cacheKey); } cacheKeys = cacheKeys.TrimEnd(','); var instruction = new PuckInstruction() { Count = modelTypes.Count, ServerName = ApiHelper.ServerName(), InstructionDetail = cacheKeys, InstructionKey = InstructionKeys.RemoveFromCache }; repo.AddPuckInstruction(instruction); repo.SaveChanges(); success = true; } catch (Exception ex) { msg = ex.Message; success = false; } return(Json(new { success = success, message = msg })); }
public static void Sync(CancellationToken ct) { bool taken = false; using (var scope = PuckCache.ServiceProvider.CreateScope()) { var contentService = scope.ServiceProvider.GetService <I_Content_Service>(); var searcher = scope.ServiceProvider.GetService <I_Content_Searcher>(); var repo = scope.ServiceProvider.GetService <I_Puck_Repository>(); var config = scope.ServiceProvider.GetService <IConfiguration>(); var cache = scope.ServiceProvider.GetService <IMemoryCache>(); try { Monitor.TryEnter(lck, lock_wait, ref taken); if (!taken) { return; } var serverName = ApiHelper.ServerName(); var meta = repo.GetPuckMeta().Where(x => x.Name == DBNames.SyncId && x.Key == serverName).FirstOrDefault(); if (meta == null) { return; } var syncId = int.Parse(meta.Value); var instructionsQuery = repo.GetPuckInstruction().Where(x => x.Id > syncId && x.ServerName != serverName); var instructionsCount = instructionsQuery.Count(); if (instructionsCount == 0) { return; } void handleMaxInstructions() { //todo, update settings and republish entire site if (!PuckCache.IsRepublishingEntireSite) { PuckCache.IsRepublishingEntireSite = true; var republishTask = contentService.RePublishEntireSite2(); republishTask.GetAwaiter().GetResult(); } StateHelper.UpdateTaskMappings(); StateHelper.UpdateRedirectMappings(); StateHelper.UpdatePathLocaleMappings(); StateHelper.UpdateDomainMappings(); StateHelper.UpdateCacheMappings(); StateHelper.UpdateCrops(); } void updateMaxInstructionId(List <PuckInstruction> instructions) { //update syncId //var maxInstructionId = instructions.Max(x => x.Id); var maxInstructionIdOrDefault = repo.GetPuckInstruction().Max(x => (int?)x.Id); var maxInstructionId = maxInstructionIdOrDefault.HasValue ? maxInstructionIdOrDefault.Value : 0; meta.Value = maxInstructionId.ToString(); repo.SaveChanges(); OnAfterSync(null, new AfterSyncEventArgs { Instructions = instructions }); } //dosync var hasPublishInstruction = false; var haveRepublished = false; if (instructionsCount > PuckCache.MaxSyncInstructions) { handleMaxInstructions(); updateMaxInstructionId(new List <PuckInstruction>()); } else { var instructions = instructionsQuery.ToList(); var instructionTotal = 0; instructions.ForEach(x => instructionTotal += x.Count); if (instructionTotal > PuckCache.MaxSyncInstructions) { handleMaxInstructions(); } else { foreach (var instruction in instructions) { try { if (instruction.InstructionKey == InstructionKeys.RemoveFromCache) { var keys = instruction.InstructionDetail.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var key in keys) { cache.Remove(key); } } else if (instruction.InstructionKey == InstructionKeys.SetSearcher) { searcher.SetSearcher(); } else if (instruction.InstructionKey == InstructionKeys.Delete) { hasPublishInstruction = true; if (Indexer.CanWrite) { var qh = new QueryHelper <BaseModel>(prependTypeTerm: false); qh.SetQuery(instruction.InstructionDetail); var models = qh.GetAllNoCast(limit: int.MaxValue, fallBackToBaseModel: true); Indexer.Delete(models); } else { searcher.SetSearcher(); } } else if (instruction.InstructionKey == InstructionKeys.RepublishSite && !haveRepublished) { if (Indexer.CanWrite) { if (!PuckCache.IsRepublishingEntireSite) { PuckCache.IsRepublishingEntireSite = true; var republishTask = contentService.RePublishEntireSite2(); republishTask.GetAwaiter().GetResult(); hasPublishInstruction = true; } } else { searcher.SetSearcher(); } haveRepublished = true; } else if (instruction.InstructionKey == InstructionKeys.Publish) { hasPublishInstruction = true; var toIndex = new List <BaseModel>(); //instruction detail holds comma separated list of ids and variants in format id:variant,id:variant var idList = instruction.InstructionDetail.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (Indexer.CanWrite) { foreach (var idAndVariant in idList) { var idAndVariantArr = idAndVariant.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); var id = Guid.Parse(idAndVariantArr[0]); var variant = idAndVariantArr[1]; var publishedOrCurrentRevision = repo.PublishedOrCurrentRevision(id, variant); if (publishedOrCurrentRevision != null) { var model = publishedOrCurrentRevision.ToBaseModel(); if (model != null) { toIndex.Add(model); } } } Indexer.Index(toIndex); } else { searcher.SetSearcher(); } } else if (instruction.InstructionKey == InstructionKeys.UpdateCrops) { StateHelper.UpdateCrops(); } else if (instruction.InstructionKey == InstructionKeys.UpdateCacheMappings) { StateHelper.UpdateCacheMappings(); } else if (instruction.InstructionKey == InstructionKeys.UpdateDomainMappings) { StateHelper.UpdateDomainMappings(); } else if (instruction.InstructionKey == InstructionKeys.UpdatePathLocales) { StateHelper.UpdatePathLocaleMappings(); } else if (instruction.InstructionKey == InstructionKeys.UpdateRedirects) { StateHelper.UpdateRedirectMappings(); } else if (instruction.InstructionKey == InstructionKeys.UpdateTaskMappings) { StateHelper.UpdateTaskMappings(); } } catch (Exception ex) { PuckCache.PuckLog.Log( $"error processing sync instruction (id:{instruction.Id}), skipping. error message:{ex.Message}" , ex.StackTrace , level: "error" , exceptionType: ex.GetType() ); } } if (hasPublishInstruction) { if (((config.GetValue <bool?>("UseAzureDirectory") ?? false) || (config.GetValue <bool?>("UseSyncDirectory") ?? false)) && config.GetValue <bool>("IsEditServer")) { var newInstruction = new PuckInstruction(); newInstruction.InstructionKey = InstructionKeys.SetSearcher; newInstruction.Count = 1; newInstruction.ServerName = ApiHelper.ServerName(); repo.AddPuckInstruction(newInstruction); repo.SaveChanges(); } } } updateMaxInstructionId(instructions); } } catch (Exception ex) { PuckCache.PuckLog.Log(ex); } finally { if (taken) { Monitor.Exit(lck); } PuckCache.IsSyncQueued = false; } } }