-
Notifications
You must be signed in to change notification settings - Fork 1
/
await_shutdown.go
27 lines (24 loc) · 735 Bytes
/
await_shutdown.go
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
package main
import (
"context"
"firetail-lambda-extension/extensionsapi"
"github.com/pkg/errors"
)
// awaitShutdown calls /event/next until a shutdown event is received, or the context is cancelled.
// It returns a reason, or an error, depending upon the cause of the shutdown.
func awaitShutdown(extensionClient *extensionsapi.Client, ctx context.Context) (string, error) {
for {
select {
case <-ctx.Done():
return "context cancelled", nil
default:
res, err := extensionClient.NextEvent(ctx) // This is a blocking call
if err != nil {
return "", errors.WithMessage(err, "failed to get next event")
}
if res.EventType == extensionsapi.Shutdown {
return "received shutdown event", nil
}
}
}
}