diff --git a/client/client.go b/client/client.go index 381442f40f01..3c0faa5f1efa 100644 --- a/client/client.go +++ b/client/client.go @@ -487,9 +487,10 @@ func (c *Client) Stats() map[string]map[string]string { return stats } -// CollectAllocation garbage collects a single allocation -func (c *Client) CollectAllocation(allocID string) { - c.garbageCollector.Collect(allocID) +// CollectAllocation garbage collects a single allocation on a node. Returns +// true if alloc was found and garbage collected; otherwise false. +func (c *Client) CollectAllocation(allocID string) bool { + return c.garbageCollector.Collect(allocID) } // CollectAllAllocs garbage collects all allocations on a node in the terminal diff --git a/client/gc.go b/client/gc.go index 54873e159ca1..a7332ebc4cfb 100644 --- a/client/gc.go +++ b/client/gc.go @@ -196,14 +196,16 @@ func (a *AllocGarbageCollector) Stop() { close(a.shutdownCh) } -// Collect garbage collects a single allocation on a node -func (a *AllocGarbageCollector) Collect(allocID string) { +// Collect garbage collects a single allocation on a node. Returns true if +// alloc was found and garbage collected; otherwise false. +func (a *AllocGarbageCollector) Collect(allocID string) bool { if gcAlloc := a.allocRunners.Remove(allocID); gcAlloc != nil { a.destroyAllocRunner(gcAlloc.allocRunner, "forced collection") - return + return true } a.logger.Printf("[DEBUG] client.gc: alloc %s is invalid or was already garbage collected", allocID) + return false } // CollectAll garbage collects all termianated allocations on a node diff --git a/command/agent/alloc_endpoint.go b/command/agent/alloc_endpoint.go index 41f5d9721a5f..65bcbd0116f5 100644 --- a/command/agent/alloc_endpoint.go +++ b/command/agent/alloc_endpoint.go @@ -133,7 +133,10 @@ func (s *HTTPServer) allocGC(allocID string, resp http.ResponseWriter, req *http return nil, structs.ErrPermissionDenied } - s.agent.Client().CollectAllocation(allocID) + if !s.agent.Client().CollectAllocation(allocID) { + // Could not find alloc + return nil, fmt.Errorf("unable to collect allocation: not present") + } return nil, nil }