From 1384359bafa3de292923eb6362518a6ed82ca9c2 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Wed, 12 Dec 2018 17:00:15 +1300 Subject: [PATCH] fix(api): fix snapshot hanging --- api/cron/job_snapshot.go | 58 +++++++++++++++++++++------------------- api/docker/client.go | 2 ++ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/api/cron/job_snapshot.go b/api/cron/job_snapshot.go index aaeff3212..513a324b9 100644 --- a/api/cron/job_snapshot.go +++ b/api/cron/job_snapshot.go @@ -45,39 +45,41 @@ func (runner *SnapshotJobRunner) GetSchedule() *portainer.Schedule { // As a snapshot can be a long process, to avoid any concurrency issue we // retrieve the latest version of the endpoint right after a snapshot. func (runner *SnapshotJobRunner) Run() { - endpoints, err := runner.context.endpointService.Endpoints() - if err != nil { - log.Printf("background schedule error (endpoint snapshot). Unable to retrieve endpoint list (err=%s)\n", err) - return - } - - for _, endpoint := range endpoints { - if endpoint.Type == portainer.AzureEnvironment { - continue + go func() { + endpoints, err := runner.context.endpointService.Endpoints() + if err != nil { + log.Printf("background schedule error (endpoint snapshot). Unable to retrieve endpoint list (err=%s)\n", err) + return } - snapshot, snapshotError := runner.context.snapshotter.CreateSnapshot(&endpoint) + for _, endpoint := range endpoints { + if endpoint.Type == portainer.AzureEnvironment { + continue + } - latestEndpointReference, err := runner.context.endpointService.Endpoint(endpoint.ID) - if latestEndpointReference == nil { - log.Printf("background schedule error (endpoint snapshot). Endpoint not found inside the database anymore (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) - continue - } + snapshot, snapshotError := runner.context.snapshotter.CreateSnapshot(&endpoint) - latestEndpointReference.Status = portainer.EndpointStatusUp - if snapshotError != nil { - log.Printf("background schedule error (endpoint snapshot). Unable to create snapshot (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, snapshotError) - latestEndpointReference.Status = portainer.EndpointStatusDown - } + latestEndpointReference, err := runner.context.endpointService.Endpoint(endpoint.ID) + if latestEndpointReference == nil { + log.Printf("background schedule error (endpoint snapshot). Endpoint not found inside the database anymore (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) + continue + } - if snapshot != nil { - latestEndpointReference.Snapshots = []portainer.Snapshot{*snapshot} - } + latestEndpointReference.Status = portainer.EndpointStatusUp + if snapshotError != nil { + log.Printf("background schedule error (endpoint snapshot). Unable to create snapshot (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, snapshotError) + latestEndpointReference.Status = portainer.EndpointStatusDown + } - err = runner.context.endpointService.UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) - if err != nil { - log.Printf("background schedule error (endpoint snapshot). Unable to update endpoint (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) - return + if snapshot != nil { + latestEndpointReference.Snapshots = []portainer.Snapshot{*snapshot} + } + + err = runner.context.endpointService.UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) + if err != nil { + log.Printf("background schedule error (endpoint snapshot). Unable to update endpoint (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) + return + } } - } + }() } diff --git a/api/docker/client.go b/api/docker/client.go index aebec1adb..421f92c04 100644 --- a/api/docker/client.go +++ b/api/docker/client.go @@ -3,6 +3,7 @@ package docker import ( "net/http" "strings" + "time" "github.com/docker/docker/client" "github.com/portainer/portainer" @@ -102,5 +103,6 @@ func httpClient(endpoint *portainer.Endpoint) (*http.Client, error) { return &http.Client{ Transport: transport, + Timeout: 30 * time.Second, }, nil }