From e3bf3e8ca48e31c8877aded75cd5b30447ace301 Mon Sep 17 00:00:00 2001
From: Tristan Cavelier <tristan.cavelier@tiolive.com>
Date: Mon, 26 Aug 2013 13:59:46 +0200
Subject: [PATCH] job checker upgraded to deny jobs and to check single job

---
 src/jio/features/jobChecker.js | 65 +++++++++++++++++++++++++---------
 1 file changed, 48 insertions(+), 17 deletions(-)

diff --git a/src/jio/features/jobChecker.js b/src/jio/features/jobChecker.js
index 335af73..71ea469 100644
--- a/src/jio/features/jobChecker.js
+++ b/src/jio/features/jobChecker.js
@@ -44,8 +44,12 @@ function enableJobChecker(jio, shared, options) {
       new_job.modified = new Date();
     },
     deny: function (original_job, new_job) {
-      // XXX
-      return;
+      new_job.state = 'running';
+      new_job.command.reject(
+        'precondition_failed',
+        'command denied',
+        'Command rejected by the job checker.'
+      );
     }
   };
 
@@ -70,10 +74,18 @@ function enableJobChecker(jio, shared, options) {
       // wrong conditions
       return;
     }
+    if (job_rule.single !== undefined && typeof job_rule.single !== 'boolean') {
+      // wrong single property
+      return;
+    }
     if (indexOf(job_rule.action, shared.job_rule_action_names) === -1) {
       // wrong action
       return;
     }
+    if (job_rule.action !== 'deny' && job_rule.single === true) {
+      // only 'deny' action doesn't require original_job parameter
+      return;
+    }
 
     if (typeof job_rule.after !== 'string') {
       job_rule.after = '';
@@ -97,6 +109,7 @@ function enableJobChecker(jio, shared, options) {
     job_rule = {
       "code_name": job_rule.code_name,
       "conditions": job_rule.conditions,
+      "single": job_rule.single || false,
       "action": job_rule.action || "ok"
     };
 
@@ -137,21 +150,38 @@ function enableJobChecker(jio, shared, options) {
     if (job.state === 'ready') {
       // browsing rules
       for (i = 0; i < shared.job_rules.length; i += 1) {
-        // browsing jobs
-        for (j = 0; j < shared.jobs.length; j += 1) {
-          if (shared.jobs[j] !== job) {
-            if (
-              jobsRespectConditions(
-                shared.jobs[j],
-                job,
-                shared.job_rules[i].conditions
-              )
-            ) {
-              shared.job_rule_actions[shared.job_rules[i].action](
-                shared.jobs[j],
-                job
-              );
-              return;
+        if (shared.job_rules[i].single) {
+          // no browse
+          if (
+            jobsRespectConditions(
+              job,
+              undefined,
+              shared.job_rules[i].conditions
+            )
+          ) {
+            shared.job_rule_actions[shared.job_rules[i].action](
+              undefined,
+              job
+            );
+            return;
+          }
+        } else {
+          // browsing jobs
+          for (j = 0; j < shared.jobs.length; j += 1) {
+            if (shared.jobs[j] !== job) {
+              if (
+                jobsRespectConditions(
+                  shared.jobs[j],
+                  job,
+                  shared.job_rules[i].conditions
+                )
+              ) {
+                shared.job_rule_actions[shared.job_rules[i].action](
+                  shared.jobs[j],
+                  job
+                );
+                return;
+              }
             }
           }
         }
@@ -203,4 +233,5 @@ function enableJobChecker(jio, shared, options) {
   jio.jobRules = function () {
     return deepClone(shared.job_rules);
   };
+
 }
-- 
2.30.9