Exemplo n.º 1
0
        /// <summary>
        /// Performs the update if needed.
        /// </summary>
        /// <param name="appPaths">The app paths.</param>
        /// <param name="logger">The logger.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
        {
            // Not supported
            if (IsRunningAsService)
            {
                return(false);
            }

            // Look for the existence of an update archive
            var updateArchive = Path.Combine(appPaths.TempUpdatePath, "MBServer" + ".zip");

            if (File.Exists(updateArchive))
            {
                logger.Info("An update is available from {0}", updateArchive);

                // Update is there - execute update
                try
                {
                    var serviceName = IsRunningAsService ? BackgroundService.GetExistingServiceName() : string.Empty;
                    new ApplicationUpdater().UpdateApplication(appPaths, updateArchive, logger, serviceName);

                    // And just let the app exit so it can update
                    return(true);
                }
                catch (Exception e)
                {
                    logger.ErrorException("Error starting updater.", e);

                    MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message));
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        private static bool IsAlreadyRunningAsService(string applicationPath)
        {
            var serviceName = BackgroundService.GetExistingServiceName();

            WqlObjectQuery             wqlObjectQuery             = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName));
            ManagementObjectSearcher   managementObjectSearcher   = new ManagementObjectSearcher(wqlObjectQuery);
            ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();

            foreach (ManagementObject managementObject in managementObjectCollection)
            {
                var obj = managementObject.GetPropertyValue("PathName");
                if (obj == null)
                {
                    continue;
                }
                var path = obj.ToString();

                _logger.Info("Service path: {0}", path);
                // Need to use indexOf instead of equality because the path will have the full service command line
                if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    _logger.Info("The windows service is already running");
                    MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service.");
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        private static bool CanRestartWindowsService()
        {
            var startInfo = new ProcessStartInfo
            {
                FileName       = "cmd.exe",
                CreateNoWindow = true,
                WindowStyle    = ProcessWindowStyle.Hidden,
                Verb           = "runas",
                ErrorDialog    = false,
                Arguments      = String.Format("/c sc query {0}", BackgroundService.GetExistingServiceName())
            };

            using (var process = Process.Start(startInfo))
            {
                process.WaitForExit();
                if (process.ExitCode == 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Starts the service.
        /// </summary>
        private static void StartService(ILogManager logManager)
        {
            var service = new BackgroundService(logManager.GetLogger("Service"));

            service.Disposed += service_Disposed;

            ServiceBase.Run(service);
        }
Exemplo n.º 5
0
        private static void RunServiceInstallationIfNeeded(string applicationPath)
        {
            var serviceName = BackgroundService.GetExistingServiceName();
            var ctl         = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName);

            if (ctl == null)
            {
                RunServiceInstallation(applicationPath);
            }
        }
Exemplo n.º 6
0
        private static void ShutdownWindowsService()
        {
            _logger.Info("Stopping background service");
            var service = new ServiceController(BackgroundService.GetExistingServiceName());

            service.Refresh();

            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
            }
        }
Exemplo n.º 7
0
        private static bool IsServiceInstalled()
        {
            try
            {
                var serviceName = BackgroundService.GetExistingServiceName();
                var ctl         = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName);

                return(ctl != null);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Starts the service.
        /// </summary>
        private static void StartService(ILogManager logManager)
        {
            var service = new BackgroundService(logManager.GetLogger("Service"));

            service.Disposed += service_Disposed;

            ServiceBase.Run(service);
        }
Exemplo n.º 9
0
        private static void RestartWindowsService()
        {
            _logger.Info("Restarting background service");

            var startInfo = new ProcessStartInfo
            {
                FileName       = "cmd.exe",
                CreateNoWindow = true,
                WindowStyle    = ProcessWindowStyle.Hidden,
                Verb           = "runas",
                ErrorDialog    = false,
                Arguments      = String.Format("/c sc stop {0} & sc start {0} & sc start {0}", BackgroundService.GetExistingServiceName())
            };

            Process.Start(startInfo);
        }