Repository navigation
Expand file tree
/
Copy pathhostipc.go
More file actions
61 lines (51 loc) · 2.08 KB
/
Copy pathhostipc.go
File metadata and controls
61 lines (51 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"fmt"
agentv0 "github.com/codefly-dev/core/generated/go/codefly/services/agent/v0"
)
// hostResourceRecoveryCapability lets a host require recovery of this agent's
// leaked host resources without recognizing PostgreSQL. What a leaked resource
// looks like, and which one is safe to remove, are facts about the database
// this agent owns, so the rules live here and only the promise is generic.
const hostResourceRecoveryCapability = "host-resource-recovery/v1"
const recoverHostResourcesCommandName = "recover-host-resources"
// recoverHostResources is indirected so the lifecycle wiring can be observed
// without sweeping the developer's real IPC tables. Production always runs
// reapHostIPC; only tests replace it.
var recoverHostResources = reapHostIPC
// hostResourceRecovery counts what one pass removed.
type hostResourceRecovery struct {
SharedSegments int
SemaphoreSets int
}
func (r hostResourceRecovery) empty() bool {
return r.SharedSegments == 0 && r.SemaphoreSets == 0
}
func (r hostResourceRecovery) summary() string {
if r.empty() {
return "no orphaned host resources"
}
return fmt.Sprintf("removed %d orphaned shared-memory segment(s) and %d semaphore set(s)",
r.SharedSegments, r.SemaphoreSets)
}
func recoverHostResourcesCommand() *agentv0.CommandDefinition {
return &agentv0.CommandDefinition{
Name: recoverHostResourcesCommandName,
Description: "Remove host IPC resources leaked by an interrupted native run of this service",
Usage: recoverHostResourcesCommandName,
Tags: []string{"recovery"},
Destructive: true,
}
}
// runRecoverHostResources returns a cleanup failure rather than reporting it.
// A caller reaching for this command has collection as its whole purpose, so a
// resource left behind has to reach it; the lifecycle paths, where recovery is
// incidental to starting or stopping a database, warn instead.
func runRecoverHostResources(ctx context.Context, _ []string) (string, error) {
recovered, err := recoverHostResources(ctx)
if err != nil {
return "", err
}
return recovered.summary(), nil
}