diff --git a/docker-compose.yml b/docker-compose.yml index d6839fc5..4a9ee839 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: - "/var/run/docker.sock:/var/run/docker.sock" ports: - 8080:8080 - image: functions/gateway:0.6.0 + image: functions/gateway:0.6.1 networks: - functions environment: diff --git a/gateway/handlers/functionshandler.go b/gateway/handlers/functionshandler.go index 866fa4e7..f41f9a2c 100644 --- a/gateway/handlers/functionshandler.go +++ b/gateway/handlers/functionshandler.go @@ -12,6 +12,7 @@ import ( "log" "net/http" "strings" + "time" "io/ioutil" @@ -147,7 +148,7 @@ func MakeDeleteFunctionHandler(metricsOptions metrics.MetricOptions, c *client.C } // 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) { defer r.Body.Close() body, _ := ioutil.ReadAll(r.Body) @@ -175,7 +176,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie } options.EncodedRegistryAuth = auth } - spec := makeSpec(&request) + spec := makeSpec(&request, maxRestarts) response, err := c.ServiceCreate(context.Background(), spec, options) if err != nil { @@ -185,17 +186,19 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie } } -func makeSpec(request *requests.CreateFunctionRequest) swarm.ServiceSpec { - max := uint64(1) +func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm.ServiceSpec { nets := []swarm.NetworkAttachmentConfig{ {Target: request.Network}, } + restartDelay := time.Second * 5 + spec := swarm.ServiceSpec{ TaskTemplate: swarm.TaskSpec{ RestartPolicy: &swarm.RestartPolicy{ - MaxAttempts: &max, - Condition: swarm.RestartPolicyConditionNone, + MaxAttempts: &maxRestarts, + Condition: swarm.RestartPolicyConditionAny, + Delay: &restartDelay, }, ContainerSpec: swarm.ContainerSpec{ Image: request.Image, diff --git a/gateway/server.go b/gateway/server.go index c3cde85f..c12da0fc 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -78,11 +78,13 @@ func main() { faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions) } else { + maxRestarts := uint64(5) + faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger) faasHandlers.RoutelessProxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger) faasHandlers.Alert = internalHandlers.MakeAlertHandler(internalHandlers.NewSwarmServiceQuery(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) // This could exist in a separate process - records the replicas of each swarm service.