Esempio n. 1
0
        public static void Update(Window mainWindow, Instance instance)
        {
            if (instance != null)
            {
                const string current = "Selected instance";
                const string all     = "All instances";
                var          options = new[]
                {
                    current, all
                };

                var result = WindowHelper.AskForSelection(Title, null, "You have selected \"{0}\" Sitecore instance. \nWould you like to update the license file only there?".FormatWith(instance.Name), options, mainWindow);

                if (result == null)
                {
                    return;
                }

                if (result == all)
                {
                    instance = null;
                }
            }

            var filePath = ProfileManager.Profile.License;

            const string settings = "Definied in settings";
            const string custom   = "Another license file";
            var          options2 = new[]
            {
                settings, custom
            };
            var result2 = WindowHelper.AskForSelection(Title, null, "Which license file would you like to use?", options2, mainWindow);

            if (result2 == null)
            {
                return;
            }

            if (result2 == custom)
            {
                var openDialog = new OpenFileDialog
                {
                    Filter = @"License files|*.xml"
                };

                if (openDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                filePath = openDialog.FileName;
            }


            WindowHelper.LongRunningTask(() => DoUpdateLicense(filePath, instance), "Updating license...", mainWindow);
        }
        public static void OpenCurrentLogFile([NotNull] Instance instance, [NotNull] Window owner, [CanBeNull] string logFileType = null)
        {
            Assert.ArgumentNotNull(instance, nameof(instance));
            Assert.ArgumentNotNull(owner, nameof(owner));

            var dataFolderPath = instance.DataFolderPath;

            FileSystem.FileSystem.Local.Directory.AssertExists(dataFolderPath, "The data folder ({0}) of the {1} instance doesn't exist".FormatWith(dataFolderPath, instance.Name));

            var logsFolderPath = instance.LogsFolderPath;
            var logFilePrefix  = logFileType ?? GetLogFileTypes(owner, logsFolderPath);

            if (logFilePrefix == null)
            {
                Action waitForLogs = delegate
                {
                    while (logFilePrefix == null)
                    {
                        logFilePrefix = GetLogFileTypes(owner, logsFolderPath);
                        Thread.Sleep(100);
                    }
                };

                WindowHelper.LongRunningTask(waitForLogs, "Waiting for log files", owner, null, "Waiting for log files to be created in the \"{0}\" folder.".FormatWith(logsFolderPath));
            }

            var logFilePattern = logFilePrefix + "*.txt";
            var files          = FileSystem.FileSystem.Local.Directory.GetFiles(logsFolderPath, logFilePattern) ?? new string[0];
            var logFilePath    = files.OrderByDescending(FileSystem.FileSystem.Local.File.GetCreationTimeUtc).FirstOrDefault();

            if (string.IsNullOrEmpty(logFilePath))
            {
                Action waitForLogs = delegate
                {
                    while (string.IsNullOrEmpty(logFilePath))
                    {
                        var files2 = FileSystem.FileSystem.Local.Directory.GetFiles(logsFolderPath, logFilePattern) ?? new string[0];
                        logFilePath = files2.OrderByDescending(FileSystem.FileSystem.Local.File.GetCreationTimeUtc).FirstOrDefault();
                        Thread.Sleep(100);
                    }
                };

                WindowHelper.LongRunningTask(waitForLogs, "Waiting for log files", owner, null, "Waiting for log files to be created in the \"{0}\" folder.".FormatWith(logsFolderPath));
            }

            var logViewer = GetLogViewer();

            if (string.IsNullOrEmpty(logViewer))
            {
                return;
            }

            var fileSystemWatcher = new FileSystemWatcher(logsFolderPath)
            {
                Filter = logFilePattern,
                IncludeSubdirectories = false
            };

            var reopenLogViewer = false;

            var currentProcess = CoreApp.RunApp(logViewer, logFilePath);

            if (currentProcess == null)
            {
                return;
            }

            // we need to stop all this magic when application closes
            currentProcess.Exited += delegate
            {
                // but shouldn't if it is initiated by this magic
                if (reopenLogViewer)
                {
                    reopenLogViewer = false;
                    return;
                }

                fileSystemWatcher.EnableRaisingEvents = false;
            };

            fileSystemWatcher.Created += (sender, args) =>
            {
                try
                {
                    if (args.ChangeType != WatcherChangeTypes.Created)
                    {
                        return;
                    }

                    var filePath = args.FullPath;
                    if (!filePath.Contains(logFilePrefix))
                    {
                        return;
                    }

                    // indicate that magic begins
                    reopenLogViewer = true;

                    // magic begins
                    currentProcess.Kill();

                    currentProcess = CoreApp.RunApp(logViewer, filePath);

                    // we need to stop all this magic when application closes
                    currentProcess.Exited += delegate
                    {
                        // but shouldn't if it is initiated by this magic
                        if (reopenLogViewer)
                        {
                            reopenLogViewer = false;
                            return;
                        }

                        fileSystemWatcher.EnableRaisingEvents = false;
                    };
                }
                catch (Exception ex)
                {
                    fileSystemWatcher.EnableRaisingEvents = false;
                    Log.Error(ex, string.Format("Unhandled error happened while reopening log file"));
                }
            };

            fileSystemWatcher.EnableRaisingEvents = true;
        }
        public static bool PreheatInstance(Instance instance, Window mainWindow)
        {
            if (!EnsureAppPoolState(instance, mainWindow))
            {
                return(false);
            }

            if (!WinAppSettings.AppPreheatEnabled.Value)
            {
                return(true);
            }

            // Check if the instance is responsive now
            if (!InstanceHelper.IsInstanceResponsive(instance, "fast"))
            {
                // It is not responsive so we need to preheat it
                // i.e. request with larger timeout and with the
                // progress bar shown to the user to avoid UI lag
                Exception ex  = null;
                var       res = WindowHelper.LongRunningTask(() => PreheatInstance(instance, out ex), "Starting Sitecore", mainWindow,
                                                             "Sitecore is being initialized",
                                                             "It may take up to a few minutes on large solutions or slow machines.",
                                                             true, true, true);
                if (res == null)
                {
                    return(false);
                }

                // if error happened
                if (ex != null)
                {
                    const string cancel          = "Cancel";
                    const string openLog         = "Open SIM log file";
                    const string openSitecoreLog = "Open Sitecore log file";
                    const string openAnyway      = "Open in browser";
                    var          message         = "The instance returned an error. \n\n" + ex.Message;
                    Log.Error(ex, message);
                    var result = WindowHelper.AskForSelection("Running instance failed", null, message,
                                                              new[]
                    {
                        cancel, openLog, openSitecoreLog, openAnyway
                    }, mainWindow);
                    switch (result)
                    {
                    case openLog:
                        CoreApp.OpenFile(ApplicationManager.LogsFolder);
                        return(false);

                    case openSitecoreLog:
                        OpenCurrentLogFile(instance, mainWindow);
                        return(false);

                    case openAnyway:
                        return(true);

                    default:
                        return(false);
                    }
                }
            }

            return(true);
        }