Skip to content

Commit 86d2b14

Browse files
committed
Check if service exists before trying to manipulate it
1 parent 0162ebf commit 86d2b14

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

spawnclient/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func (sc *Client) Inspect(uri string) (*daemon.Heartbeat, map[string]daemon.Serv
8989
daemonHb = hb
9090
}
9191

92-
svcClient := sc.bwClient.RegisterService(uri, "s.spawnpoint")
93-
iFaceClient := svcClient.RegisterInterface("+", "i.spawnable")
92+
svcClient := sc.bwClient.NewServiceClient(uri, "s.spawnpoint")
93+
iFaceClient := svcClient.AddInterface("+", "i.spawnable")
9494
svcHeartbeatMsgs, err := sc.bwClient.Query(&bw2.QueryParams{
9595
URI: iFaceClient.SignalURI("heartbeat"),
9696
})

spawnctl/main.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const deploymentHistVar = "SPAWNCTL_HISTORY_FILE"
21-
const healthHorizon = 5 * time.Minute
21+
const healthHorizon = 2 * time.Minute
2222

2323
func main() {
2424
app := cli.NewApp()
@@ -344,6 +344,18 @@ func manipulateService(c *cli.Context, command string) error {
344344
fmt.Printf("Last seen %s ago\n", age.String())
345345
os.Exit(1)
346346
}
347+
age, err = checkServiceHealth(spawnClient, spawnpointURI, svcName)
348+
if err != nil {
349+
fmt.Printf("Failed to check service health: %s\n", err)
350+
os.Exit(1)
351+
} else if age == 0 {
352+
fmt.Printf("Service %s does not exist at spawnpoint %s\n", svcName, spawnpointURI)
353+
os.Exit(1)
354+
} else if age > healthHorizon {
355+
fmt.Printf("Service %s is not running\n", svcName)
356+
fmt.Printf("Last seen %s ago\n", age.String())
357+
os.Exit(1)
358+
}
347359

348360
var ctx context.Context
349361
var cancel context.CancelFunc
@@ -490,3 +502,17 @@ func checkSpawnpointHealth(sc *spawnclient.Client, uri string) (time.Duration, e
490502
duration := time.Now().Sub(daemonTimestamp)
491503
return duration, nil
492504
}
505+
506+
func checkServiceHealth(sc *spawnclient.Client, uri string, svcName string) (time.Duration, error) {
507+
_, svcHbs, err := sc.Inspect(uri)
508+
if err != nil {
509+
return 0, errors.Wrap(err, "Failed to inspect spawnpoint")
510+
}
511+
svcHb, ok := svcHbs[svcName]
512+
if !ok {
513+
return 0, nil
514+
}
515+
svcTimestamp := time.Unix(0, svcHb.Time)
516+
duration := time.Now().Sub(svcTimestamp)
517+
return duration, nil
518+
}

0 commit comments

Comments
 (0)