better startup flow
This commit is contained in:
1 parent
d7f5dba677
commit
650bcff83a
8 files changed
+345
-68
No files matched your search
@@ -0,0 +1,154 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
|
||||
"slackerbot/internal/manager"
|
||||
"slackerbot/internal/model"
|
||||
)
|
||||
|
||||
type lifecycle struct {
|
||||
b *Bot
|
||||
channel string
|
||||
root string
|
||||
link string
|
||||
}
|
||||
|
||||
func (l *lifecycle) post(ctx context.Context, text string) {
|
||||
if l == nil {
|
||||
return
|
||||
}
|
||||
l.b.say(ctx, l.channel, l.root, text)
|
||||
}
|
||||
|
||||
func (l *lifecycle) url() string {
|
||||
if l == nil {
|
||||
return ""
|
||||
}
|
||||
return l.link
|
||||
}
|
||||
|
||||
func (b *Bot) permalink(ctx context.Context, channel, ts string) string {
|
||||
link, err := b.api.GetPermalinkContext(ctx, &slack.PermalinkParameters{Channel: channel, Ts: ts})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return link
|
||||
}
|
||||
|
||||
// openLifecycle posts a new top-level message for the container, pins it and
|
||||
// unpins the one from its previous run.
|
||||
func (b *Bot) openLifecycle(ctx context.Context, u *model.Owner, headline string) *lifecycle {
|
||||
channel, ts, err := b.api.PostMessageContext(ctx, u.ID, slack.MsgOptionText(headline, false))
|
||||
if err != nil {
|
||||
log.Printf("slack post: %v", err)
|
||||
return nil
|
||||
}
|
||||
if u.LifecycleTS != "" {
|
||||
old := slack.NewRefToMessage(u.LifecycleChannel, u.LifecycleTS)
|
||||
if err := b.api.RemovePinContext(ctx, u.LifecycleChannel, old); err != nil {
|
||||
log.Printf("slack unpin: %v", err)
|
||||
}
|
||||
}
|
||||
if err := b.api.AddPinContext(ctx, channel, slack.NewRefToMessage(channel, ts)); err != nil {
|
||||
log.Printf("slack pin: %v", err)
|
||||
}
|
||||
if err := b.store.SetLifecycle(u.ID, channel, ts); err != nil {
|
||||
log.Printf("saving lifecycle message for %s: %v", u.ID, err)
|
||||
}
|
||||
return &lifecycle{b: b, channel: channel, root: ts, link: b.permalink(ctx, channel, ts)}
|
||||
}
|
||||
|
||||
func (b *Bot) currentLifecycle(ctx context.Context, u *model.Owner) *lifecycle {
|
||||
if u.LifecycleTS == "" {
|
||||
return b.openLifecycle(ctx, u, fmt.Sprintf(":package: Container `%s` for %s", u.Machine, ownerMention(u)))
|
||||
}
|
||||
return &lifecycle{b: b, channel: u.LifecycleChannel, root: u.LifecycleTS,
|
||||
link: b.permalink(ctx, u.LifecycleChannel, u.LifecycleTS)}
|
||||
}
|
||||
|
||||
func ownerMention(u *model.Owner) string {
|
||||
if u.Kind == model.OwnerUser {
|
||||
return "<@" + u.ID + ">"
|
||||
}
|
||||
return "<#" + u.ID + ">"
|
||||
}
|
||||
|
||||
// startLog reports a container start in its lifecycle thread. It stays silent
|
||||
// when the container is already running.
|
||||
type startLog struct {
|
||||
b *Bot
|
||||
m incoming
|
||||
root string
|
||||
ownerID string
|
||||
lc *lifecycle
|
||||
began time.Time
|
||||
}
|
||||
|
||||
func (s *startLog) open(ctx context.Context) {
|
||||
if s.lc != nil {
|
||||
return
|
||||
}
|
||||
u := s.b.store.Owner(s.ownerID)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
s.began = time.Now()
|
||||
head := fmt.Sprintf(":rocket: Starting container `%s` for %s. Started by <@%s> in <%s|this thread>.",
|
||||
u.Machine, ownerMention(u), s.m.user, s.b.permalink(ctx, s.m.channel, s.root))
|
||||
if s.lc = s.b.openLifecycle(ctx, u, head); s.lc != nil {
|
||||
s.b.say(ctx, s.m.channel, s.root, fmt.Sprintf(":rocket: Starting the container. <%s|Follow the startup here>.", s.lc.url()))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *startLog) event(ctx context.Context, ev manager.StartEvent) {
|
||||
s.open(ctx)
|
||||
switch ev.Phase {
|
||||
case manager.StartPlacing:
|
||||
s.lc.post(ctx, fmt.Sprintf(":mag: New container, so it needs a home. Waiting for the balancer to pick the node and drive with the most room for %s.", ev.Size))
|
||||
case manager.StartBooting:
|
||||
s.lc.post(ctx, fmt.Sprintf(":gear: Placed on `%s` (drive `%s`). Waiting for the node to create the home folder, apply the storage quota and launch the container with systemd-nspawn.", ev.Node, ev.Drive))
|
||||
case manager.StartRegistered:
|
||||
text := ":hourglass_flowing_sand: The container is launched and the node can see it. Waiting for the agent (goose) to answer on port 1984"
|
||||
if t := s.b.cfg.BootTimeout; t > 0 {
|
||||
text += fmt.Sprintf(", for up to %s", t)
|
||||
}
|
||||
s.lc.post(ctx, text+".")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *startLog) up(ctx context.Context) {
|
||||
if s.lc == nil {
|
||||
return
|
||||
}
|
||||
s.lc.post(ctx, fmt.Sprintf(":white_check_mark: <@%s> the container is up and the agent is ready (took %s).",
|
||||
s.m.user, time.Since(s.began).Round(time.Second)))
|
||||
}
|
||||
|
||||
func (s *startLog) failed(ctx context.Context, reason string) {
|
||||
if s.lc == nil {
|
||||
return
|
||||
}
|
||||
s.lc.post(ctx, ":x: The container didn't come up. "+reason)
|
||||
}
|
||||
|
||||
// announceStop posts the shutdown in the container's lifecycle thread and
|
||||
// returns a link to it. The thread gets a last message once the machine is gone.
|
||||
func (b *Bot) announceStop(ctx context.Context, u *model.Owner, reason string) string {
|
||||
lc := b.currentLifecycle(ctx, u)
|
||||
lc.post(ctx, ":octagonal_sign: Shutting down. "+reason+" Waiting for the container to power off.")
|
||||
go func() {
|
||||
bg := context.Background()
|
||||
if err := b.mgr.WaitStopped(bg, u); err != nil {
|
||||
lc.post(bg, ":warning: The container was asked to shut down but hasn't stopped: "+err.Error())
|
||||
return
|
||||
}
|
||||
lc.post(bg, ":white_check_mark: The container is down. Its files are kept, and a new message starts it again.")
|
||||
}()
|
||||
return lc.url()
|
||||
}
|
||||
Reference in new issue
Block a user