Пример #1
0
        private void pingTimerExpired(object State)
        {
            try
            {
                if (SolrWrapper.Ping() == "OK")
                {
                    // Log the successful ping
                    EventLogHelper.Write_Event(ServiceSettings.Logging.PingEventId, "Successfully pinged solr/lucene for routine monitoring");

                    // Tell the timer to fire again in XX milliseconds, and only fire that once
                    pingTimer.Change(ServiceSettings.Monitor.PauseDuration * 1000, Timeout.Infinite);
                }
                else
                {
                    // Write the error message
                    EventLogHelper.Write_Error(ServiceSettings.Logging.FailedPingEventId, "Error while pinging solr/lucene for routine monitoring");

                    // For now, terminate on the first failed ping
                    ExitCode = 1064;
                    Stop();
                }
            }
            catch (Exception ex)
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedPingEventId, "EXCEPTION CAUGHT while pinging solr/lucene for routine monitoring", ex);
            }
        }
        public static string Ping()
        {
            // Add verbose log
            EventLogHelper.Write_Verbose("Inside SolrWrapper.Ping() method");

            // Need to have a core to monitor/ping
            if ((ServiceSettings.Monitor.Cores == null) || (ServiceSettings.Monitor.Cores.Count == 0))
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedPingEventId, "No core found to monitor, so no core to ping.\n\nUpdate the app.config Monitor.Cores value.");
                return("Service Configuration Error");
            }

            // Get the ping information
            string ping_url = "http://*****:*****@name='status']").InnerText;

                EventLogHelper.Write_Verbose("Parsed the XML and received a status of '" + status + "'");
                return(status);
            }
            catch (Exception ee)
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.ExceptionEventId,
                                           "EXCEPTION caught in SolrWrapper.Ping() conversting response to a XML document and reading the status.\n\n--------------------\n\nRESPONSE:\n\n" +
                                           response, ee);
                return(null);
            }
        }
Пример #3
0
        /// <summary> Look for the Java executable information, and determine the version and the ability of
        /// this version to support different capabilities </summary>
        /// <returns> TRUE if this version will work with solr/lucene </returns>
        public static bool Check_Java()
        {
            // Log some of this work
            StringBuilder loggingBuilder = new StringBuilder();

            // Was the Java executable hard coded in the settings?
            string java_executable;

            if (!String.IsNullOrEmpty(ServiceSettings.Java.Home))
            {
                string java_home = ServiceSettings.Java.Home;
                loggingBuilder.AppendLine("Found Java Home in the configuration file: " + java_home);

                // Do the directory and java executable exist?
                java_executable = Path.Combine(java_home, "bin", "java.exe");
                if ((!Directory.Exists(java_home)) || (!File.Exists(java_executable)))
                {
                    loggingBuilder.AppendLine("ERROR: Java.exe not found in " + java_home + "\\bin.  Please set Java.Home to a valid JRE/JDK directory in the configuration file.");

                    // For chuckles, see if the java information could be pulled from the registry
                    string regCheckError2;
                    string javaFromRegistry2 = Java_Executable_From_Registry(out regCheckError2);
                    if (!String.IsNullOrEmpty(javaFromRegistry2))
                    {
                        loggingBuilder.AppendLine("From the registry, Java appears to be at: " + javaFromRegistry2);
                        loggingBuilder.AppendLine("If you remove the Java home in the configuration file, this Java executable will be used.");
                    }

                    // Log this critical error and return FALSE
                    EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, loggingBuilder.ToString());
                    return(false);
                }
            }
            else
            {
                // Look in the registry for the Java location
                string regCheckError;
                java_executable = Java_Executable_From_Registry(out regCheckError);
                if (String.IsNullOrEmpty(java_executable))
                {
                    loggingBuilder.AppendLine("ERROR: Unable to determine location of the Java executable from the registry.");
                    loggingBuilder.AppendLine(regCheckError);

                    loggingBuilder.AppendLine();
                    loggingBuilder.AppendLine("Please set Java.Home to a valid JRE/JDK directory in the configuration file.");

                    // Log this critical error and return FALSE
                    EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, loggingBuilder.ToString());
                    return(false);
                }

                // (Potentially) log this
                loggingBuilder.AppendLine("Found Java executable in the registry: " + java_executable);
            }

            // Valid, so save this
            Java_Executable = java_executable;
            Is64bit         = true;

            // This should now be a valid Java executable, so check for version and capability for the -server flag
            SimpleJavaResponse output = Run_Java_Executable("-version");

            if (output.ExitCode != 0)
            {
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("UNKNOWN ERROR: Error running the java exectuable to determine version.");
                loggingBuilder.AppendLine("'" + Java_Executable + " -version' fails with exit code of not zero.");

                // Log this critical error and return FALSE
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, loggingBuilder.ToString());
                return(false);
            }

            // Parse for the build / version
            if (output.Response.IndexOf(" version ", StringComparison.OrdinalIgnoreCase) < 0)
            {
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("UNKNOWN ERROR: Output from java version check is invalid.");
                loggingBuilder.AppendLine("'" + Java_Executable + " -version' output is not as expected.");

                // Log this critical error and return FALSE
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, loggingBuilder.ToString());
                return(false);
            }

            string version_info;

            try
            {
                version_info = output.Response.Substring(output.Response.IndexOf(" version ", StringComparison.OrdinalIgnoreCase) + 8).Split("\r\n".ToCharArray())[0].Trim().Replace("\"", "");
                string[] split = version_info.Split(".".ToCharArray());
                Java_Version = Int32.Parse(split[1]);
                Java_Build   = -1;
                if (version_info.IndexOf("_") > 0)
                {
                    string java_build_string = version_info.Split("_".ToCharArray())[1];
                    Java_Build = Int32.Parse(java_build_string);
                }

                // Add to the log
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("Java version is " + version_info + " ( Major version " + Java_Version + ", build " + Java_Build + " )");
            }
            catch
            {
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("UNKNOWN ERROR: Output from java version check is invalid.");
                loggingBuilder.AppendLine("'" + Java_Executable + " -version' output is not as expected.");

                // Log this critical error and return FALSE
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, loggingBuilder.ToString());
                return(false);
            }

            // Java must be version 1.7 or later to run Solr
            if (Java_Version < 7)
            {
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("ERROR: Java 1.7 or later is required to run Solr.  Current Java version is: " + version_info);

                // Log this critical error and return FALSE
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, loggingBuilder.ToString());
                return(false);
            }

            // Keep track of if a warning should be logged during this start-up
            bool warning_included = false;

            // Set some GC_TUNE value differently if this is version 7
            if (Java_Version == 7)
            {
                if (ServiceSettings.StartArgs.GcTune.IndexOf("-XX:CMSFullGCsBeforeCompaction=1", StringComparison.Ordinal) < 0)
                {
                    ServiceSettings.StartArgs.GcTune = ServiceSettings.StartArgs.GcTune + " -XX:CMSFullGCsBeforeCompaction=1";
                }
                if (ServiceSettings.StartArgs.GcTune.IndexOf("-XX:CMSTriggerPermRatio=80", StringComparison.Ordinal) < 0)
                {
                    ServiceSettings.StartArgs.GcTune = ServiceSettings.StartArgs.GcTune + " -XX:CMSTriggerPermRatio=80";
                }

                // Some builds don't work well with Lucene
                if ((Java_Build >= 40) && (Java_Build <= 51))
                {
                    if (ServiceSettings.StartArgs.GcTune.IndexOf("-XX:-UseSuperWord", StringComparison.Ordinal) < 0)
                    {
                        ServiceSettings.StartArgs.GcTune = ServiceSettings.StartArgs.GcTune + " -XX:-UseSuperWord";
                    }

                    // Log this to warn
                    warning_included = true;
                    loggingBuilder.AppendLine();
                    loggingBuilder.AppendLine("WARNING: Java version " + version_info + " has known bugs with Lucene and requires the -XX:-UseSuperWord flag.");
                    loggingBuilder.AppendLine("Please consider upgrading your JVM.");
                }
            }

            // Check to see if this version of java supports the -server VM flag
            SimpleJavaResponse output_server = Run_Java_Executable("-server -version");

            if (output_server.ExitCode != 0)
            {
                warning_included = true;
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("WARNING: You are using a JRE without support for -server option.  Please update to latest JDK for best performance.");
                ServerFlagSupported = false;
            }
            else
            {
                ServerFlagSupported = true;
            }

            SimpleJavaResponse output_x64 = Run_Java_Executable("-d64 -version");

            if (output_x64.ExitCode != 0)
            {
                warning_included = true;
                loggingBuilder.AppendLine();
                loggingBuilder.AppendLine("WARNING: 32-bit Java detected.  Not recommended for production.  Update your version or enter the path to the appropriate Java version in the configuration file.");
                Is64bit = false;
            }

            // If a warning was set
            if (warning_included)
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.StartEventId, "JAVA WARNING" + Environment.NewLine + loggingBuilder);
            }

            return(true);
        }
        public static bool Start_Direct(int TimeOut)
        {
            // Add verbose log
            EventLogHelper.Write_Verbose("Inside SolrWrapper.Start_Direct() method");

            // Log this start
            StringBuilder logStartBuilder = new StringBuilder();


            // Check to see if the solr home directory exists
            string solr_top = ServiceSettings.Solr.Directory;

            if (!Directory.Exists(solr_top))
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, "Solr directory " + solr_top + " not found!" + Environment.NewLine + Environment.NewLine + "Install solr/lucene and add the solr server directory under Solr.Directory to the configuration file for this service.");
                return(false);
            }

            // Get and test the solr server directory
            string solr_server_dir = Path.Combine(ServiceSettings.Solr.Directory, "server");

            if (!String.IsNullOrEmpty(ServiceSettings.Override.SolrServerDir))
            {
                solr_server_dir = ServiceSettings.Override.SolrServerDir;
            }
            if (!Directory.Exists(solr_server_dir))
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, "Solr server directory " + solr_server_dir + " not found!" + Environment.NewLine + Environment.NewLine + "Re-install solr/lucene and add the solr server directory under Solr.Directory to the configuration file for this service." + Environment.NewLine + Environment.NewLine + "If your setup is non-standard, you can use the Override.SOLR_SERVER_DIR value in the configuration file for this service.");
                return(false);
            }

            // Get and test the solr home directory
            string solr_home = Path.Combine(ServiceSettings.Solr.Directory, "server", "solr");

            if (!String.IsNullOrEmpty(ServiceSettings.Override.SolrHome))
            {
                solr_home = ServiceSettings.Override.SolrHome;
            }
            if (!Directory.Exists(solr_home))
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, "Solr home directory " + solr_home + " not found!" + Environment.NewLine + Environment.NewLine + "Re-install solr/lucene and add the solr server directory under Solr.Directory to the configuration file for this service." + Environment.NewLine + Environment.NewLine + "If your setup is non-standard, you can use the Override.SOLR_HOME value in the configuration file for this service.");
                return(false);
            }

            // Backup log files ( use current timestamp for backup names )
            string logs_directory = Path.Combine(ServiceSettings.Solr.Directory, "server", "logs");

            if (!String.IsNullOrEmpty(ServiceSettings.Override.SolrLogsDir))
            {
                logs_directory = ServiceSettings.Override.SolrLogsDir;
            }


            // Only continue if log directory exists
            if (Directory.Exists(logs_directory))
            {
                string now_ts = DateTime.Now.Year + "-" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "-" +
                                DateTime.Now.Day.ToString().PadLeft(2, '0') + "_" +
                                DateTime.Now.Hour.ToString().PadLeft(2, '0') +
                                DateTime.Now.Minute.ToString().PadLeft(2, '0');

                // Backing up the solr.log
                string solr_log = Path.Combine(logs_directory, "solr.log");
                if (File.Exists(solr_log))
                {
                    logStartBuilder.AppendLine("Backing up " + solr_log);
                    try
                    {
                        string new_solr_log = Path.Combine(logs_directory, "solr_log_" + now_ts + ".bak");
                        if (File.Exists(new_solr_log))
                        {
                            char append = 'a';
                            while ((append != 'z') &&
                                   (File.Exists(Path.Combine(logs_directory, "solr_log_" + now_ts + append + ".bak"))))
                            {
                                append = (char)(((int)append) + 1);
                            }
                            new_solr_log = Path.Combine(logs_directory, "solr_log_" + now_ts + append + ".bak");
                        }
                        File.Move(solr_log, new_solr_log);
                    }
                    catch (Exception ee)
                    {
                        logStartBuilder.AppendLine("Error backing up log file: " + ee.Message);
                    }
                }

                // Backing up the garbage collection log
                string solr_gc_log = Path.Combine(logs_directory, "solr_gc.log");
                if (File.Exists(solr_gc_log))
                {
                    logStartBuilder.AppendLine("Backing up " + solr_gc_log);
                    try
                    {
                        string new_solr_gc_log = Path.Combine(logs_directory, "solr_gc_log_" + now_ts + ".bak");
                        if (File.Exists(new_solr_gc_log))
                        {
                            char append = 'a';
                            while ((append != 'z') &&
                                   (File.Exists(Path.Combine(logs_directory, "solr_gc_log_" + now_ts + append + ".bak"))))
                            {
                                append = (char)(((int)append) + 1);
                            }
                            new_solr_gc_log = Path.Combine(logs_directory, "solr_gc_log_" + now_ts + append + ".bak");
                        }
                        File.Move(solr_gc_log, new_solr_gc_log);
                    }
                    catch (Exception ee)
                    {
                        logStartBuilder.AppendLine("Error backing up garbage collection log file: " + ee.Message);
                    }
                }
            }
            else
            {
                // log directory does not exist, try to create it
                try
                {
                    Directory.CreateDirectory(logs_directory);
                }
                catch (Exception ee)
                {
                    EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, "Logging directory " + ServiceSettings.Solr.Directory + " not found and the request to create the directory failed!", ee);
                    return(false);
                }
            }

            // Check to see if the port is currently in use?
            if (is_port_in_use(ServiceSettings.Solr.Port))
            {
                EventLogHelper.Write_Event(ServiceSettings.Logging.FailedStartEventId, "Solr port (" + ServiceSettings.Solr.Port + ") is currently in use." + Environment.NewLine + Environment.NewLine + "Will attemp to restart solr by first issuing a stop command.");

                // Issue the stop
                Stop();

                // Now, double check that it is still not in use
                if (is_port_in_use(ServiceSettings.Solr.Port))
                {
                    EventLogHelper.Write_Event(ServiceSettings.Logging.FailedStartEventId, "A process is already listening on port " + ServiceSettings.Solr.Port + ".  If this is not Solr, then please choose a different port in the service configuration file.");
                    return(false);
                }
            }

            // Set some initial values
            Exception CaughtException = null;

            // Determine some of the directory values that are derivaties of the solr home
            string log4j_props = Path.Combine(ServiceSettings.Solr.Directory, "server", "resources", "log4j.properties");

            if (!String.IsNullOrEmpty(ServiceSettings.Override.Log4jConfig))
            {
                log4j_props = ServiceSettings.Override.Log4jConfig;
            }

            string java_tempdir = Path.Combine(solr_server_dir, "tmp");
            //  string gc_log = Path.Combine(logs_directory, "solr_gc.log");
            //   string jar_file = Path.Combine(solr_server_dir, "start.jar");

            // Build the arguments
            StringBuilder argsBuilder = new StringBuilder();

            argsBuilder.Append(ServiceSettings.StartArgs.ServerOpts);
            argsBuilder.Append(" -Xss256k");
            argsBuilder.Append(" " + ServiceSettings.StartArgs.JavaMemory);
            argsBuilder.Append(" -Duser.timezone=" + ServiceSettings.StartArgs.TimeZone);
            argsBuilder.Append(" " + ServiceSettings.StartArgs.GcTune);
            argsBuilder.Append(" " + ServiceSettings.StartArgs.GcLogOpts);
            argsBuilder.Append(" -Xloggc:\"" + logs_directory + "\"/solr_gc.log");
            argsBuilder.Append(" -Dlog4j.configuration=\"file:" + log4j_props + "\"");
            argsBuilder.Append(" -DSTOP.PORT=" + ServiceSettings.Solr.StopPort);
            argsBuilder.Append(" -DSTOP.KEY=" + ServiceSettings.Solr.StopKey);
            argsBuilder.Append(" -Djetty.port=" + ServiceSettings.Solr.Port);
            argsBuilder.Append(" -Dsolr.solr.home=\"" + solr_home + "\"");
            argsBuilder.Append(" -Dsolr.install.dir=\"" + solr_top + "\"");
            argsBuilder.Append(" -DJetty.home=\"" + solr_server_dir + "\"");
            argsBuilder.Append(" -Djava.io.tmpdir=\"" + java_tempdir + "\"");
            argsBuilder.Append(" -jar start.jar");
            argsBuilder.Append(" \"--module=http\"");

            string args = argsBuilder.ToString();

            // Using string builders to collect the standard output and error
            StringBuilder output = new StringBuilder();
            StringBuilder error  = new StringBuilder();

            // Perform all this work within using tag to encourage disposing of the process and events
            bool returnValue = true;

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    using (Process startSolrProcess = new Process())
                    {
                        try
                        {
                            startSolrProcess.StartInfo.FileName         = JavaWrapper.Java_Executable;
                            startSolrProcess.StartInfo.Arguments        = args;
                            startSolrProcess.StartInfo.WorkingDirectory = solr_server_dir;

                            startSolrProcess.StartInfo.UseShellExecute        = false;
                            startSolrProcess.StartInfo.RedirectStandardOutput = true;
                            startSolrProcess.StartInfo.RedirectStandardError  = true;
                            startSolrProcess.StartInfo.CreateNoWindow         = true;

                            // Add a function to handle asynchronous reading of the standard output
                            startSolrProcess.OutputDataReceived += (sender, e) =>
                            {
                                if (e.Data == null)
                                {
                                    outputWaitHandle.Set();
                                }
                                else
                                //if (e.Data != null)
                                {
                                    output.AppendLine("     " + e.Data);
                                }
                            };

                            // Add a function to handle asynchronous reading of the standard error
                            startSolrProcess.ErrorDataReceived += (sender, e) =>
                            {
                                if (e.Data == null)
                                {
                                    errorWaitHandle.Set();
                                }
                                else
                                //if (e.Data != null)
                                {
                                    error.AppendLine("     " + e.Data);
                                }
                            };

                            // Log (verbose) actual arguments
                            EventLogHelper.Write_Verbose("About to run command to start solr/jetty." + Environment.NewLine + Environment.NewLine + "Executable: " + JavaWrapper.Java_Executable + Environment.NewLine + Environment.NewLine + "Arguments: " + args + Environment.NewLine + Environment.NewLine + "Working Directory: " + solr_server_dir);

                            // Start the process
                            startSolrProcess.Start();

                            // Start the asyncronous reading of the standard input and standard output
                            startSolrProcess.BeginOutputReadLine();
                            startSolrProcess.BeginErrorReadLine();

                            // Wait for the process to copmlete
                            if (startSolrProcess.WaitForExit(TimeOut))
                            {
                                // Give the output readers slightly more time to finish any reading
                                if (outputWaitHandle.WaitOne(100) && errorWaitHandle.WaitOne(100))
                                {
                                    // Successfully ended and process completed.  Check process exit code here.
                                    int exitCode = startSolrProcess.ExitCode;
                                    EventLogHelper.Write_Verbose("Solr/Lucene request closed with exit code of " + exitCode);
                                }

                                startSolrProcess.CancelOutputRead();
                                startSolrProcess.CancelErrorRead();
                            }
                            else
                            {
                                // Timed out.
                                EventLogHelper.Write_Verbose("Timeout occurred during solr/lucene startup");

                                outputWaitHandle.WaitOne(100);
                                errorWaitHandle.WaitOne(100);

                                startSolrProcess.CancelOutputRead();
                                startSolrProcess.CancelErrorRead();
                            }

                            startSolrProcess.Close();

                            // Add the error output if something was received
                            if (error.ToString().Trim().Length > 0)
                            {
                                logStartBuilder.AppendLine("==============================" + Environment.NewLine + "STANDARD OUTPUT: " + Environment.NewLine + output.ToString() + Environment.NewLine + Environment.NewLine + "STANDARD ERROR:" + Environment.NewLine + error.ToString());
                            }
                            EventLogHelper.Write_Event(ServiceSettings.Logging.StartEventId, "Solr/Lucene start attempt completed" + Environment.NewLine + logStartBuilder.ToString() + Environment.NewLine);
                        }
                        catch (Exception ee)
                        {
                            CaughtException = ee;
                            EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStartEventId, String.Format(ERROR_MESSAGE_TEMPLATE, "EXCEPTION detected during solr/lucene startup attempt", solrExecutable + " " + argsBuilder, output.ToString(), error.ToString()), ee);
                            returnValue = true;
                        }
                    }
                }

            // Log this to the solr log directory
            string solr_log_file = Path.Combine(logs_directory, "solr-" + ServiceSettings.Solr.Port + "-service.log");

            try
            {
                StreamWriter writer = new StreamWriter(solr_log_file, false);

                writer.WriteLine("LOG FILE CREATED " + DateTime.Now.ToString() + " BY SOLR SERVICE WRAPPER");
                writer.WriteLine();

                writer.WriteLine("Executable: " + JavaWrapper.Java_Executable);
                writer.WriteLine();
                writer.WriteLine("Arguments: " + args);
                writer.WriteLine();
                writer.WriteLine("Working directory: " + solr_server_dir);
                writer.WriteLine();

                writer.WriteLine("====================================================");
                writer.WriteLine();
                writer.WriteLine(logStartBuilder.ToString());
                writer.WriteLine();

                if (error.ToString().Trim().Length > 0)
                {
                    writer.WriteLine("====================================================");
                    writer.WriteLine();
                    writer.WriteLine("REDIRECTED ERROR:");
                    writer.WriteLine(error.ToString());
                    writer.WriteLine();
                }

                if (CaughtException != null)
                {
                    writer.WriteLine("====================================================");
                    writer.WriteLine();
                    writer.WriteLine("CAUGHT EXCEPTION:");
                    writer.WriteLine(CaughtException.Message);
                    writer.WriteLine();
                    writer.WriteLine(CaughtException.StackTrace);
                    writer.WriteLine();
                }

                writer.WriteLine("====================================================");
                writer.WriteLine();
                writer.WriteLine("REDIRECTED OUTPUT:");
                writer.WriteLine(output.ToString());
                writer.WriteLine();


                writer.Flush();
                writer.Close();
            }
            catch (Exception ee)
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.ExceptionEventId, "Unexpected exception trying to write the log file " + solr_log_file, ee);
            }

            return(returnValue);
        }
        private static string Get_Solr_Web_Response(string URL, bool LogRequest)
        {
            // Return string
            string responseFromServer = String.Empty;

            // Add verbose log
            if (LogRequest)
            {
                EventLogHelper.Write_Verbose("In SolrWrapper.Get_Solr_Response('" + URL + "')");
            }

            try
            {
                // Create a request for the URL
                WebRequest request = WebRequest.Create(URL);
                request.Credentials = CredentialCache.DefaultCredentials;
                ((HttpWebRequest)request).UserAgent = ServiceSettings.Name;

                // Get the response object
                using (WebResponse response = request.GetResponse())
                {
                    // Log the status
                    if (LogRequest)
                    {
                        EventLogHelper.Write_Verbose("Status received: " + ((HttpWebResponse)response).StatusDescription);
                    }

                    // Get the stream containing content returned by the server.
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        if (dataStream != null)
                        {
                            dataStream.ReadTimeout = 30000;

                            // Open the stream using a StreamReader for easy access.
                            using (StreamReader reader = new StreamReader(dataStream))
                            {
                                // Read the content.
                                responseFromServer = reader.ReadToEnd();

                                // Clean up the streams and the response.
                                reader.Close();
                            }

                            dataStream.Close();
                        }
                    }
                    response.Close();
                }
            }
            catch (WebException we)
            {
                // Try to read the response from the exception
                try
                {
                    Stream       dataStream    = we.Response.GetResponseStream();
                    StreamReader reader        = new StreamReader(dataStream);
                    string       errorResponse = reader.ReadToEnd();

                    EventLogHelper.Write_Error(ServiceSettings.Logging.ExceptionEventId, "WEB EXCEPTION caught in SolrWrapper.Get_Solr_Response('" + URL + "').  Request returned a status of " + we.Status.ToString() + "\n\n--------------------\n\nRESPONSE:\n\n" + errorResponse, we);
                    return("404");
                }
                catch (Exception)
                {
                    EventLogHelper.Write_Error(ServiceSettings.Logging.ExceptionEventId, "WEB EXCEPTION caught in SolrWrapper.Get_Solr_Response('" + URL + "').  Request returned a status of " + we.Status.ToString(), we);
                    return("500");
                }
            }
            catch (Exception ee)
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.ExceptionEventId, "GENERAL EXCEPTION caught in SolrWrapper.Get_Solr_Response('" + URL + "')", ee);
                return(null);
            }

            // Log the response
            if (LogRequest)
            {
                EventLogHelper.Write_Verbose("RESPONSE RECEIVED:\n\n" + responseFromServer);
            }

            return(responseFromServer);
        }
        public static bool Stop()
        {
            // Add verbose log
            EventLogHelper.Write_Verbose("Inside SolrWrapper.Stop() method");

            // Check to see if the port is open
            if (!is_port_in_use(ServiceSettings.Solr.Port))
            {
                EventLogHelper.Write_Event(ServiceSettings.Logging.FailedStopEventId, "Solr port (" + ServiceSettings.Solr.Port + ") is not currently in use." + Environment.NewLine + Environment.NewLine + "Solr stop command will not be executed.");
                return(true);
            }

            // Get and test the solr server directory
            string solr_server_dir = Path.Combine(ServiceSettings.Solr.Directory, "server");

            if (!String.IsNullOrEmpty(ServiceSettings.Override.SolrServerDir))
            {
                solr_server_dir = ServiceSettings.Override.SolrServerDir;
            }
            if (!Directory.Exists(solr_server_dir))
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStopEventId, "Solr server directory " + solr_server_dir + " not found!" + Environment.NewLine + Environment.NewLine + "Re-install solr/lucene and add the solr server directory under Solr.Directory to the configuration file for this service." + Environment.NewLine + Environment.NewLine + "If your setup is non-standard, you can use the Override.SOLR_SERVER_DIR value in the configuration file for this service.");
                return(false);
            }

            // Build the arguments
            StringBuilder argsBuilder = new StringBuilder();

            argsBuilder.Append(" -DJetty.home=\"" + solr_server_dir + "\"");
            argsBuilder.Append(" -jar start.jar");
            argsBuilder.Append(" STOP.PORT=" + ServiceSettings.Solr.StopPort);
            argsBuilder.Append(" STOP.KEY=" + ServiceSettings.Solr.StopKey);
            argsBuilder.Append(" --stop");
            string args = argsBuilder.ToString();

            // Using string builders to collect the standard output and error
            StringBuilder output = new StringBuilder();
            StringBuilder error  = new StringBuilder();

            // Perform all this work within using tag to encourage disposing of the process and events
            bool returnValue = true;

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    using (Process startSolrProcess = new Process())
                    {
                        try
                        {
                            startSolrProcess.StartInfo.FileName         = JavaWrapper.Java_Executable;
                            startSolrProcess.StartInfo.Arguments        = args;
                            startSolrProcess.StartInfo.WorkingDirectory = solr_server_dir;

                            startSolrProcess.StartInfo.UseShellExecute        = false;
                            startSolrProcess.StartInfo.RedirectStandardOutput = true;
                            startSolrProcess.StartInfo.RedirectStandardError  = true;
                            startSolrProcess.StartInfo.CreateNoWindow         = true;

                            // Add a function to handle asynchronous reading of the standard output
                            startSolrProcess.OutputDataReceived += (sender, e) =>
                            {
                                if (e.Data == null)
                                {
                                    outputWaitHandle.Set();
                                }
                                else
                                //if (e.Data != null)
                                {
                                    output.AppendLine("     " + e.Data);
                                }
                            };

                            // Add a function to handle asynchronous reading of the standard error
                            startSolrProcess.ErrorDataReceived += (sender, e) =>
                            {
                                if (e.Data == null)
                                {
                                    errorWaitHandle.Set();
                                }
                                else
                                //if (e.Data != null)
                                {
                                    error.AppendLine("     " + e.Data);
                                }
                            };

                            // Log (verbose) actual arguments
                            EventLogHelper.Write_Verbose("About to run command to start solr/jetty." + Environment.NewLine + Environment.NewLine + "Executable: " + JavaWrapper.Java_Executable + Environment.NewLine + Environment.NewLine + "Arguments: " + args + Environment.NewLine + Environment.NewLine + "Working Directory: " + solr_server_dir);

                            // Start the process
                            startSolrProcess.Start();

                            // Start the asyncronous reading of the standard input and standard output
                            startSolrProcess.BeginOutputReadLine();
                            startSolrProcess.BeginErrorReadLine();

                            // Wait for the process to copmlete
                            if (startSolrProcess.WaitForExit(5000))
                            {
                                // Give the output readers slightly more time to finish any reading
                                if (outputWaitHandle.WaitOne(100) && errorWaitHandle.WaitOne(100))
                                {
                                    // Successfully ended and process completed.  Check process exit code here.
                                    int exitCode = startSolrProcess.ExitCode;
                                    EventLogHelper.Write_Verbose("Solr/Lucene request closed with exit code of " + exitCode);
                                }

                                startSolrProcess.CancelOutputRead();
                                startSolrProcess.CancelErrorRead();
                            }
                            else
                            {
                                // Timed out.
                                EventLogHelper.Write_Verbose("Timeout occurred during solr/lucene stop");

                                outputWaitHandle.WaitOne(100);
                                errorWaitHandle.WaitOne(100);

                                startSolrProcess.CancelOutputRead();
                                startSolrProcess.CancelErrorRead();
                            }

                            startSolrProcess.Close();

                            // If there was error caught from the misdirection add it here
                            StringBuilder stopSolrBuilder = new StringBuilder("Solr/Lucene stop attempt completed");
                            if ((error.ToString().Trim().Length > 0) || (output.ToString().Trim().Length > 0))
                            {
                                stopSolrBuilder.AppendLine();
                                stopSolrBuilder.AppendLine(" ============================== ");
                                stopSolrBuilder.AppendLine();

                                if (output.ToString().Trim().Length > 0)
                                {
                                    stopSolrBuilder.AppendLine("Standard output (redirected):");
                                    stopSolrBuilder.AppendLine(output.ToString().Trim());
                                    stopSolrBuilder.AppendLine();
                                }

                                if (error.ToString().Trim().Length > 0)
                                {
                                    stopSolrBuilder.AppendLine("Standard error (redirected):");
                                    stopSolrBuilder.AppendLine(error.ToString().Trim());
                                    stopSolrBuilder.AppendLine();
                                }
                            }

                            EventLogHelper.Write_Event(ServiceSettings.Logging.StopEventId, stopSolrBuilder.ToString());
                        }
                        catch (Exception ee)
                        {
                            EventLogHelper.Write_Error(ServiceSettings.Logging.FailedStopEventId, String.Format(ERROR_MESSAGE_TEMPLATE, "EXCEPTION detected during solr/lucene stop attempt", solrExecutable + " " + argsBuilder, output.ToString(), error.ToString()), ee);
                            return(false);
                        }
                    }
                }

            return(true);
        }
Пример #7
0
        protected override void OnStart(string[] args)
        {
            EventLogHelper.Write_Verbose("In SolrService.OnStart() method");

            // Check the JAVA version
            if (!JavaWrapper.Check_Java())
            {
                // Just abort, since the java wrapper logs any errors
                return;
            }

            // Try to start SOLR
            if (!SolrWrapper.Start_Direct(20000))
            {
                try
                {
                    EventLogHelper.Write_Verbose("Failed to start - exiting service with exit code of 1064");
                    ExitCode = 1064;
                    Stop();
                }
                catch (Exception ee)
                {
                    EventLogHelper.Write_Verbose("EXCEPTION caught while trying to exit service with exit code of 1064 due to failed solr/lucene start");
                }

                return;
            }

            // Try to ping the solr/lucene instance, just to ensure it is working
            try
            {
                if (SolrWrapper.Ping() == "OK")
                {
                    // Log the successful ping
                    EventLogHelper.Write_Event(ServiceSettings.Logging.PingEventId, "Successfully pinged solr/lucene for routine monitoring");
                }
                else
                {
                    // Write the error message
                    EventLogHelper.Write_Error(ServiceSettings.Logging.FailedPingEventId, "Error while pinging solr/lucene for routine monitoring");

                    // During startup, terminate on the first failed ping
                    ExitCode = 1064;
                    Stop();
                }
            }
            catch (Exception ex)
            {
                EventLogHelper.Write_Error(ServiceSettings.Logging.FailedPingEventId, "EXCEPTION CAUGHT while pinging solr/lucene for routine monitoring", ex);
                // During startup, terminate on the first failed ping
                ExitCode = 1064;
                Stop();
            }

            // Since Solr started, start up the timer
            if (ServiceSettings.Monitor.PauseDuration > 0)
            {
                EventLogHelper.Write_Verbose("Starting the ping timer in SolrService.OnStart() with an internval of " + ServiceSettings.Monitor.PauseDuration + " seconds.");

                // Tell the timer to fire again in XX milliseconds, and only fire that once
                pingTimer = new Timer(pingTimerExpired, null, ServiceSettings.Monitor.PauseDuration * 1000, Timeout.Infinite);
            }
        }