From e4519aa77fecbf5b0b073fbf9c2796018f4c1852 Mon Sep 17 00:00:00 2001
From: Tristan Cavelier <tristan.cavelier@tiolive.com>
Date: Fri, 6 Sep 2013 16:18:20 +0200
Subject: [PATCH] max_retry 0 equals infinite retry -> 0 equals 0 retry so 1
 try is possible

For writer command, default max_retry is null (means infinite try possible).
For reader command, dofault max_retry is 2 (means 3 tries possible).
---
 src/jio/features/jobRetry.js | 54 ++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/src/jio/features/jobRetry.js b/src/jio/features/jobRetry.js
index ca79769..6800878 100644
--- a/src/jio/features/jobRetry.js
+++ b/src/jio/features/jobRetry.js
@@ -1,5 +1,5 @@
 /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, unparam: true */
-/*global arrayExtend, setTimeout, indexOf, min, constants */
+/*global arrayExtend, setTimeout, methodType, min, constants */
 
 function enableJobRetry(jio, shared, options) {
 
@@ -11,6 +11,12 @@ function enableJobRetry(jio, shared, options) {
   // - param.command
 
   // uses
+  // - options.default_writers_max_retry number >= 0 or null
+  // - options.default_readers_max_retry number >= 0 or null
+  // - options.default_max_retry number >= 0 or null
+  // - options.writers_max_retry number >= 0 or null
+  // - options.readers_max_retry number >= 0 or null
+  // - options.max_retry number >= 0 or null
   // - param.modified date
   // - param.tried number >= 0
   // - param.max_retry >= 0 or undefined
@@ -27,20 +33,19 @@ function enableJobRetry(jio, shared, options) {
 
   shared.job_keys = arrayExtend(shared.job_keys || [], ["max_retry"]);
 
+  var writers_max_retry, readers_max_retry, max_retry;
+
   function defaultMaxRetry(param) {
-    if (
-      indexOf(param.method, [
-        'post',
-        'put',
-        'remove',
-        'putAttachment',
-        'removeAttachment',
-        'repair'
-      ]) !== -1
-    ) {
-      return 0;
+    if (methodType(param.method) === 'writers') {
+      if (max_retry === undefined) {
+        return writers_max_retry;
+      }
+      return max_retry;
     }
-    return 3;
+    if (max_retry === undefined) {
+      return readers_max_retry;
+    }
+    return max_retry;
   }
 
   function positiveNumberOrDefault(number, default_value) {
@@ -49,6 +54,25 @@ function enableJobRetry(jio, shared, options) {
             number : default_value);
   }
 
+  function positiveNumberNullOrDefault(number, default_value) {
+    return ((typeof number === 'number' &&
+            number >= 0) || number === null ?
+            number : default_value);
+  }
+
+  max_retry = positiveNumberNullOrDefault(
+    options.max_retry || options.default_max_retry,
+    undefined
+  );
+  writers_max_retry = positiveNumberNullOrDefault(
+    options.writers_max_retry || options.default_writers_max_retry,
+    null
+  );
+  readers_max_retry = positiveNumberNullOrDefault(
+    options.readers_max_retry || options.default_readers_max_retry,
+    2
+  );
+
   // listeners
 
   shared.on('job', function (param) {
@@ -73,8 +97,8 @@ function enableJobRetry(jio, shared, options) {
   shared.on('jobRetry', function (param, args) {
     if (param.state === 'running') {
       if (param.max_retry === undefined ||
-          param.max_retry === 0 ||
-          param.max_retry > param.tried) {
+          param.max_retry === null ||
+          param.max_retry >= param.tried) {
         param.state = 'waiting';
         param.modified = new Date();
         shared.emit('jobStop', param);
-- 
2.30.9