Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / bludgeon / Daemon.cs
blobd1deae515a71bbd28bd87365603cdbd239bd913f
2 using System;
3 using System.Diagnostics;
4 using System.Threading;
6 using Beagle.Util;
8 namespace Bludgeon {
10 public class Daemon {
12 private Daemon () { }
14 // Returns the daemon's version
15 static public string Ping ()
17 Beagle.RequestMessage request;
18 request = new Beagle.DaemonInformationRequest ();
20 Beagle.ResponseMessage response = null;
22 while (response == null) {
23 try {
24 response = request.Send ();
25 } catch {
26 Log.Spew ("ERROR: Daemon is not responding");
27 Thread.Sleep (1000);
31 Beagle.DaemonInformationResponse info;
32 info = (Beagle.DaemonInformationResponse) response;
34 return info.Version;
37 static public void Start ()
39 Log.Spew ("Starting daemon");
41 string beagled;
42 beagled = Environment.GetEnvironmentVariable ("BEAGLED_COMMAND");
44 string args;
45 args = "--debug --bg --allow-backend files";
47 Process p;
48 p = new Process ();
49 p.StartInfo.UseShellExecute = false;
51 p.StartInfo.FileName = beagled;
52 p.StartInfo.Arguments = args;
54 p.StartInfo.EnvironmentVariables ["BEAGLE_HOME"] = Beagle.Util.PathFinder.HomeDir;
55 p.StartInfo.EnvironmentVariables ["BEAGLE_EXERCISE_THE_DOG"] = "1";
57 Thread.Sleep (2000); // wait 2s to let the daemon get started
59 p.Start ();
61 string version;
62 version = Ping (); // Then try to ping the daemon
64 Log.Spew ("Successfully started daemon (version={0})", version);
67 static public string GetStatus ()
69 Beagle.RequestMessage request;
70 request = new Beagle.DaemonInformationRequest ();
72 Beagle.ResponseMessage response = null;
74 int failure_count = 0;
75 while (response == null) {
76 try {
77 response = request.Send ();
78 } catch {
79 ++failure_count;
80 if (failure_count > 10)
81 Log.Spew ("Daemon is not responding");
82 Thread.Sleep (1000);
86 Beagle.DaemonInformationResponse info;
87 info = (Beagle.DaemonInformationResponse) response;
89 return info.HumanReadableStatus;
92 static public void WaitUntilIdle ()
94 Stopwatch sw = new Stopwatch ();
95 sw.Start ();
97 bool first = true;
99 while (true) {
100 string status;
101 status = GetStatus ();
102 if (status.IndexOf ("Waiting on empty queue") != -1)
103 break;
105 if (first)
106 Log.Spew ("Waiting for daemon to become idle");
107 first = false;
109 Thread.Sleep (1000);
111 sw.Stop ();
113 if (! first)
114 Log.Spew ("Waited for {0}", sw);
117 static public void OptimizeIndexes ()
119 Beagle.RequestMessage request;
120 request = new Beagle.OptimizeIndexesRequest ();
122 Log.Spew ("Optimizing Indexes");
124 try {
125 request.Send ();
126 } catch {
127 Log.Failure ("Optimize request failed");
131 static public void Shutdown ()
133 Beagle.RequestMessage request;
134 request = new Beagle.ShutdownRequest ();
136 Log.Spew ("Shutting down daemon");
138 try {
139 request.Send ();
140 } catch {
141 Log.Failure ("beagled shutdown failed");