]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/core.vala
Actually decrement usage count when releasing a service
[gregoa/zavai.git] / src / core.vala
index 27c621b5a3bf034b35af7467ae5e83e4d0881a19..6eb4de43e8a32e9c3c1dca964e369f6d7cfe4301 100644 (file)
@@ -141,7 +141,9 @@ public abstract class Service : Object, Resource {
         if (el == null)
             return false;
 
-        requests.delete_link(el);
+        --el.data.count;
+        if (el.data.count == 0)
+            requests.delete_link(el);
 
         if (requests != null)
             return false;
@@ -153,14 +155,15 @@ public abstract class Service : Object, Resource {
 
 public abstract class ScriptService : Service
 {
+    protected string script;
+
     protected bool script_start()
     {
         try {
-            // Then run our own script
-            zavai.app.run_script(zavai.config.homedir + "/" + name + " start");
+            zavai.config.find_and_run_script(name, "start");
             return true;
         } catch (Error e) {
-            zavai.log.error("Running " + zavai.config.homedir + "/" + name + " start: " + e.message);
+            zavai.log.error("Running " + name + " start: " + e.message);
             return false;
         }
     }
@@ -168,11 +171,10 @@ public abstract class ScriptService : Service
     protected bool script_stop()
     {
         try {
-            // Then run our own script
-            zavai.app.run_script(zavai.config.homedir + "/" + name + " stop");
+            zavai.config.find_and_run_script(name, "stop");
             return true;
         } catch (Error e) {
-            zavai.log.error("Running " + zavai.config.homedir + "/" + name + " stop: " + e.message);
+            zavai.log.error("Running " + name + " stop: " + e.message);
             return false;
         }
     }
@@ -181,11 +183,22 @@ public abstract class ScriptService : Service
     {
         string std_out;
         string std_err;
-        string command = zavai.config.homedir + "/" + name + " status";
-        int res = zavai.app.run_script_sync(command, out std_out, out std_err);
-        if (res != 0)
+        string script = zavai.config.find_script(name);
+        if (script == null)
         {
-            zavai.log.error("Running " + command + ": " + std_err);
+            zavai.log.error("Hook " + name + " does not exist");
+            return false;
+        }
+        string command = script + " status";
+        try {
+            int res = zavai.config.run_script_sync(command, out std_out, out std_err);
+            if (res != 0)
+            {
+                zavai.log.error("Running " + command + ": " + std_err);
+                return false;
+            }
+        } catch (SpawnError e) {
+            zavai.log.error("Running " + command + ": " + e.message);
             return false;
         }
 
@@ -208,18 +221,18 @@ public abstract class ScriptMonitorService : Service
 
     protected bool script_start()
     {
-        string command = zavai.config.homedir + "/" + name + " pre";
+        string script = zavai.config.find_script(name);
+        zavai.log.info("Run program: " + script + " pre");
         try {
             // Then run our own script
-            zavai.app.run_script(command);
+            zavai.config.run_script(script + " pre");
         } catch (Error e) {
-            zavai.log.error("Running " + command + ": " + e.message);
+            zavai.log.error("Running " + script + " pre: " + e.message);
             return false;
         }
 
-        command = zavai.config.homedir + "/" + name + " run";
-        zavai.log.info("Run program: " + command);
-        string[] args = command.split(" ");
+        zavai.log.info("Run program: " + script + " run");
+        string[] args = { script, "run" };
         try {
             Process.spawn_async(
                 Environment.get_home_dir(),
@@ -229,7 +242,7 @@ public abstract class ScriptMonitorService : Service
                 null,
                 out child_pid);
         } catch (SpawnError e) {
-            zavai.log.error("Running " + command + ": " + e.message);
+            zavai.log.error("Running " + script + " run: " + e.message);
             return false;
         }
 
@@ -248,15 +261,13 @@ public abstract class ScriptMonitorService : Service
     protected void on_child(Pid pid, int status)
     {
         zavai.log.info("Exited");
-stderr.printf("STATUS %d\n", status);
         Process.close_pid(pid);
 
-        string command = zavai.config.homedir + "/" + name + " post";
         try {
             // Then run our own script
-            zavai.app.run_script(command);
+            zavai.config.find_and_run_script(name, "post");
         } catch (Error e) {
-            zavai.log.error("Running " + command + ": " + e.message);
+            zavai.log.error("Running " + name + " post: " + e.message);
             return;
         }