示例#1
0
 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();
         }
     }
 }
示例#2
0
        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();
                }
            }
        }
示例#3
0
        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();
                }
            }
        }
示例#4
0
        public List <LogEntry> GetLog(string machineName = null, string logName = null)
        {
            if (string.IsNullOrEmpty(logName))
            {
                logName = DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (!logName.ToLower().EndsWith(".txt"))
            {
                logName = logName += ".txt";
            }
            if (machineName == null)
            {
                machineName = ApiHelper.ServerName();
            }
            List <LogEntry> result = new List <LogEntry>();

            var path = basePath + $"{machineName}\\{logName}";

            if (!File.Exists(path))
            {
                return(result);
            }
            var txt  = File.ReadAllText(path);
            var rows = txt.Split("\n\n", StringSplitOptions.RemoveEmptyEntries).Where(x => x != "\r\n" && x != "\n" && x != "\r");

            foreach (var row in rows)
            {
                var fields = row.TrimStart('\r', '\n').Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                if (fields.Length < 5)
                {
                    continue;
                }
                var model   = new LogEntry();
                var dateStr = fields[0].TrimEnd('\r', '\n');
                model.Date          = DateTime.ParseExact(dateStr, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                model.Level         = fields[1].TrimEnd('\r', '\n');
                model.ExceptionType = fields[2].TrimEnd('\r', '\n');
                model.Message       = fields[3].TrimEnd('\r', '\n');
                model.StackTrace    = fields[4].TrimStart().TrimEnd('\r', '\n');
                result.Add(model);
            }
            result.Reverse();
            return(result);
        }
示例#5
0
        public List <string> ListLogs(string machineName = null, int take = 30)
        {
            if (machineName == null)
            {
                machineName = ApiHelper.ServerName();
            }
            List <string> result = new List <string>();
            var           path   = basePath + $"{machineName}";

            if (!Directory.Exists(path))
            {
                return(result);
            }

            var directory = new DirectoryInfo(path);

            result = directory.GetFiles().OrderByDescending(x => x.LastWriteTime).Take(30).Select(x => x.Name).ToList();

            return(result);
        }
示例#6
0
        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();
                }
            }
        }
示例#7
0
 public static bool InitializeSync()
 {
     using (var scope = PuckCache.ServiceProvider.CreateScope())
     {
         var repo           = scope.ServiceProvider.GetService <I_Puck_Repository>();
         var contentService = scope.ServiceProvider.GetService <I_Content_Service>();
         var serverName     = ApiHelper.ServerName();
         var meta           = repo.GetPuckMeta().Where(x => x.Name == DBNames.SyncId && x.Key == serverName).FirstOrDefault();
         if (meta == null)
         {
             var newMeta = new PuckMeta();
             newMeta.Name = DBNames.SyncId;
             newMeta.Key  = ApiHelper.ServerName();
             int?maxId = repo.GetPuckInstruction().Max(x => (int?)x.Id);
             newMeta.Value = (maxId ?? 0).ToString();
             repo.AddPuckMeta(newMeta);
             repo.SaveChanges();
             return(true);
         }
     }
     return(false);
 }
示例#8
0
 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();
         }
     }
 }
示例#9
0
 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();
         }
     }
 }
示例#10
0
        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();
                }
            }
        }
示例#11
0
        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;
                }
            }
        }