internal/control/buildfollow.go

f8c1e35051a416f451bc97dd49f0253c6673c765
gitbay/internal/control/buildfollow.go

117 lines · 3169 bytes

  1package control
  2
  3import (
  4	"fmt"
  5	"sync"
  6	"time"
  7
  8	"gitbay.org/gitbay/internal/protocol"
  9	"gitbay.org/gitbay/internal/store"
 10)
 11
 12// maxFollows is how many build log follows one account holds open at
 13// once. Signed-out web viewers are account 0 and share it.
 14const maxFollows = 8
 15
 16var (
 17	// followPoll bounds a wait with no wake. A write from another process
 18	// (gitbayd admin, or any session under gitbayd shell) wakes nobody;
 19	// this is how its bytes still arrive.
 20	followPoll = 2 * time.Second
 21	// followSettle is how long a follow keeps reading after the build has
 22	// an outcome: a cancel appends its line after the status changes, and
 23	// a cancelled runner's stream runs on until its next check.
 24	followSettle = time.Second
 25	// followQueued bounds how long a follow waits on a build that stays
 26	// pending: nothing reaps a queued build (ReapStaleBuilds only reaps
 27	// running builds), and a running one is already bounded by the
 28	// reaper's deadline, so a follow needs its own limit for the queued
 29	// case or it never ends.
 30	followQueued = 10 * time.Minute
 31)
 32
 33var (
 34	followMu sync.Mutex
 35	follows  = map[int64]int{}
 36)
 37
 38func takeFollow(uid int64) bool {
 39	followMu.Lock()
 40	defer followMu.Unlock()
 41	if follows[uid] >= maxFollows {
 42		return false
 43	}
 44	follows[uid]++
 45	return true
 46}
 47
 48func dropFollow(uid int64) {
 49	followMu.Lock()
 50	defer followMu.Unlock()
 51	if follows[uid]--; follows[uid] <= 0 {
 52		delete(follows, uid)
 53	}
 54}
 55
 56// followBuildLog writes the build's log as it grows and returns once the
 57// build has an outcome and its last bytes are written. The outcome goes
 58// to stderr, so stdout is the log byte for byte.
 59func followBuildLog(c *Ctx, b store.Build) int {
 60	if !takeFollow(c.User.ID) {
 61		return c.fail(protocol.ExitDenied, "%d follows are already open for this account; close one and retry", maxFollows)
 62	}
 63	defer dropFollow(c.User.ID)
 64
 65	var off int64
 66	var settleBy time.Time
 67	var queuedSince time.Time
 68	for {
 69		wake := c.Store.BuildLogWait(b.ID)
 70		status, chunk, err := c.Store.BuildLogFrom(b.ID, off)
 71		if err != nil {
 72			return c.fail(protocol.ExitFailure, "%v", err)
 73		}
 74		if len(chunk) > 0 {
 75			if _, err := c.Stdout.Write(chunk); err != nil {
 76				return protocol.ExitFailure
 77			}
 78			off += int64(len(chunk))
 79		}
 80		if status == "pending" {
 81			if queuedSince.IsZero() {
 82				queuedSince = time.Now()
 83			}
 84		} else {
 85			queuedSince = time.Time{}
 86		}
 87		wait := followPoll
 88		if status == "pending" {
 89			left := queuedSince.Add(followQueued).Sub(time.Now())
 90			if left <= 0 {
 91				fmt.Fprintf(c.Stderr, "build %d is still queued; nothing claimed it in %v. Follow again once a runner has.\n", b.Number, followQueued)
 92				return protocol.ExitFailure
 93			}
 94			wait = min(wait, left)
 95		}
 96		if status != "pending" && status != "running" {
 97			if settleBy.IsZero() {
 98				settleBy = time.Now().Add(followSettle)
 99			}
100			left := time.Until(settleBy)
101			if left <= 0 && len(chunk) == 0 {
102				fmt.Fprintf(c.Stderr, "build %d %s\n", b.Number, status)
103				return protocol.ExitOK
104			}
105			wait = min(wait, max(left, 0))
106		}
107		t := time.NewTimer(wait)
108		select {
109		case <-wake:
110		case <-t.C:
111		case <-c.Done:
112			t.Stop()
113			return protocol.ExitFailure
114		}
115		t.Stop()
116	}
117}