Allow restarts of up to 5 times with 5 sec delays

Signed-off-by: Alex <alexellis2@gmail.com>
This commit is contained in:
Alex 2017-08-16 21:31:24 +01:00
parent 07b779cbd3
commit 3ac73340c3
2 changed files with 12 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import (
"log" "log"
"net/http" "net/http"
"strings" "strings"
"time"
"io/ioutil" "io/ioutil"
@ -147,7 +148,7 @@ func MakeDeleteFunctionHandler(metricsOptions metrics.MetricOptions, c *client.C
} }
// MakeNewFunctionHandler creates a new function (service) inside the swarm network. // MakeNewFunctionHandler creates a new function (service) inside the swarm network.
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc { func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client, maxRestarts uint64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body) body, _ := ioutil.ReadAll(r.Body)
@ -175,7 +176,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
} }
options.EncodedRegistryAuth = auth options.EncodedRegistryAuth = auth
} }
spec := makeSpec(&request) spec := makeSpec(&request, maxRestarts)
response, err := c.ServiceCreate(context.Background(), spec, options) response, err := c.ServiceCreate(context.Background(), spec, options)
if err != nil { if err != nil {
@ -185,17 +186,19 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
} }
} }
func makeSpec(request *requests.CreateFunctionRequest) swarm.ServiceSpec { func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm.ServiceSpec {
max := uint64(1)
nets := []swarm.NetworkAttachmentConfig{ nets := []swarm.NetworkAttachmentConfig{
{Target: request.Network}, {Target: request.Network},
} }
restartDelay := time.Second * 5
spec := swarm.ServiceSpec{ spec := swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarm.TaskSpec{
RestartPolicy: &swarm.RestartPolicy{ RestartPolicy: &swarm.RestartPolicy{
MaxAttempts: &max, MaxAttempts: &maxRestarts,
Condition: swarm.RestartPolicyConditionNone, Condition: swarm.RestartPolicyConditionAny,
Delay: &restartDelay,
}, },
ContainerSpec: swarm.ContainerSpec{ ContainerSpec: swarm.ContainerSpec{
Image: request.Image, Image: request.Image,

View File

@ -78,11 +78,13 @@ func main() {
faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions) faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions)
} else { } else {
maxRestarts := uint64(5)
faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger) faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
faasHandlers.RoutelessProxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger) faasHandlers.RoutelessProxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
faasHandlers.Alert = internalHandlers.MakeAlertHandler(internalHandlers.NewSwarmServiceQuery(dockerClient)) faasHandlers.Alert = internalHandlers.MakeAlertHandler(internalHandlers.NewSwarmServiceQuery(dockerClient))
faasHandlers.ListFunctions = internalHandlers.MakeFunctionReader(metricsOptions, dockerClient) faasHandlers.ListFunctions = internalHandlers.MakeFunctionReader(metricsOptions, dockerClient)
faasHandlers.DeployFunction = internalHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient) faasHandlers.DeployFunction = internalHandlers.MakeNewFunctionHandler(metricsOptions, dockerClient, maxRestarts)
faasHandlers.DeleteFunction = internalHandlers.MakeDeleteFunctionHandler(metricsOptions, dockerClient) faasHandlers.DeleteFunction = internalHandlers.MakeDeleteFunctionHandler(metricsOptions, dockerClient)
// This could exist in a separate process - records the replicas of each swarm service. // This could exist in a separate process - records the replicas of each swarm service.