Browse code

Reguardless of success reset time increment

Reset the time increment if the container's execution time is greater
than 10s or else as a container runs and is restarted the time will grow
overtime.

Signed-off-by: Michael Crosby <michael@docker.com>

Michael Crosby authored on 2014/08/13 05:08:26
Showing 1 changed files
... ...
@@ -43,6 +43,9 @@ type containerMonitor struct {
43 43
 	// timeIncrement is the amount of time to wait between restarts
44 44
 	// this is in milliseconds
45 45
 	timeIncrement int
46
+
47
+	// lastStartTime is the time which the monitor last exec'd the container's process
48
+	lastStartTime time.Time
46 49
 }
47 50
 
48 51
 func newContainerMonitor(container *Container, policy runconfig.RestartPolicy) *containerMonitor {
... ...
@@ -113,6 +116,8 @@ func (m *containerMonitor) Start() error {
113 113
 
114 114
 		m.container.LogEvent("start")
115 115
 
116
+		m.lastStartTime = time.Now()
117
+
116 118
 		if exitStatus, err = m.container.daemon.Run(m.container, pipes, m.callback); err != nil {
117 119
 			utils.Errorf("Error running container: %s", err)
118 120
 		}
... ...
@@ -154,19 +159,24 @@ func (m *containerMonitor) Start() error {
154 154
 }
155 155
 
156 156
 // resetMonitor resets the stateful fields on the containerMonitor based on the
157
-// previous runs success or failure
157
+// previous runs success or failure.  Reguardless of success, if the container had
158
+// an execution time of more than 10s then reset the timer back to the default
158 159
 func (m *containerMonitor) resetMonitor(successful bool) {
159
-	// the container exited successfully so we need to reset the failure counter
160
-	// and the timeIncrement back to the default values
161
-	if successful {
162
-		m.failureCount = 0
160
+	executionTime := time.Now().Sub(m.lastStartTime).Seconds()
161
+
162
+	if executionTime > 10 {
163 163
 		m.timeIncrement = defaultTimeIncrement
164 164
 	} else {
165 165
 		// otherwise we need to increment the amount of time we wait before restarting
166 166
 		// the process.  We will build up by multiplying the increment by 2
167
+		m.timeIncrement *= 2
168
+	}
167 169
 
170
+	// the container exited successfully so we need to reset the failure counter
171
+	if successful {
172
+		m.failureCount = 0
173
+	} else {
168 174
 		m.failureCount++
169
-		m.timeIncrement *= 2
170 175
 	}
171 176
 }
172 177