protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 EventLog.WriteEntry("索引服务开始运行...."); WriteToLog("尝试运行索引服务..."); string[] imagePathArgs = Environment.GetCommandLineArgs(); string configfile = System.AppDomain.CurrentDomain.BaseDirectory + @"\config.xml"; if (imagePathArgs.Length >= 2) { configfile = imagePathArgs[1]; } WriteToLog("配置文件:\t" + configfile); try { maker = new IndexMaker(configfile); } catch (Exception ex) { WriteToLog(string.Format("Exception for open config file {0},{1}", configfile, ex.ToString())); } this.timer.Enabled = true; timeStart = DateTime.Now; WriteToLog("索引服务开始..."); }
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (maker == null) { string[] imagePathArgs = Environment.GetCommandLineArgs(); string configfile = System.AppDomain.CurrentDomain.BaseDirectory + @"\config.xml"; if (imagePathArgs.Length >= 2) { configfile = imagePathArgs[1]; } try { maker = new IndexMaker(configfile); } catch (Exception ex) { WriteToLog(string.Format("打开文件 {0}时发生异常:{1}",configfile, ex.ToString())); } } DateTime now = DateTime.Now; TimeSpan span = DateTime.Now - timeStart; if (!busy) { busy = true; if (maker.CanIndex(span, IndexTypeEnum.Ordinary) || maker.CanIndex(span, IndexTypeEnum.Increment)) { try { WriteToLog("停止搜索服务...."); StopSystemService("Searchd"); WriteToLog("搜索服务停止成功!"); } catch (Exception ssse) { WriteToLog("搜索服务无法停止.原因是:" + ssse.Message); } } if (maker.CanIndex(span, IndexTypeEnum.Ordinary)) { WriteToLog("尝试开始写主索引......"); try { Message msg = maker.ExecuteBoostIndexer(span, IndexTypeEnum.Ordinary); if (msg.Success) WriteToLog(msg.ToString()); else if (msg.ExceptionOccur) WriteToLog(msg.ToString()); } catch (Exception exp) { WriteToLog("写主索引出错.原因是:" + exp.Message); EventLog.WriteEntry("写主索引出错.原因是:" + exp.Message); } try { maker.IndexFile(true); } catch (Exception fe) { WriteToLog("文件索引出错。原因是:" + fe.StackTrace.ToString()); } WriteToLog("主索引完成!"); } if (maker.CanIndex(span, IndexTypeEnum.Increment)) { try { WriteToLog("开始增量索引......"); Message msg = maker.ExecuteBoostIndexer( span, IndexTypeEnum.Increment); if (msg.Success) WriteToLog(msg.ToString()); else if (msg.ExceptionOccur) WriteToLog(msg.ToString()); WriteToLog("完成增量索引!"); } catch (Exception exp) { WriteToLog("写增量索引时出错.原因是:" + exp.Message); } } if (maker.CanIndex(span, IndexTypeEnum.Ordinary) || maker.CanIndex(span, IndexTypeEnum.Increment)) { try { WriteToLog("开启搜索服务......"); StartSystemService("Searchd"); WriteToLog("搜索服务开始!"); } catch (Exception ste) { WriteToLog("搜索服务无法开始.原因是:" + ste.Message); } } busy = false; } else { WriteToLog("正在建立索引,请等待......."); } }
static void TestIndexMaker() { string path = @"d:\Indexer\config.xml"; IndexMaker maker = new IndexMaker(path); Console.WriteLine("Begin indexing....." + DateTime.Now.ToShortTimeString()); DateTime start = DateTime.Now; maker.IndexFile(true); TimeSpan span = DateTime.Now - start; Console.WriteLine(span.TotalMilliseconds.ToString()); }