gunk-dev/the-valley

wake.go

source6303fac04580main's published tip history
5.1 KiB
1package main
2
3// A wake directory is how the host tells the check worker that a ref it
4// reads has moved, so a pass starts without waiting for the timer. It
5// holds at most three files, whoever writes and however often:
6//
7// - due, an empty file a writer creates with O_CREAT|O_EXCL. A due that
8// is already there is a mark already made, so a writer counts that as
9// success, and any number of moves leave one file. The host's
10// post-receive hook, the integrator after a landing, and the signer
11// publisher after it moves its ref are the writers. due carries no
12// payload, and nothing reads it as one: it says only "read the refs
13// again". What a pass builds still comes from the refs it reads.
14// - claimed, which due becomes when a pass takes it. A pass renames due
15// to claimed under the lock, before it reads any ref. A writer that
16// marks due after the rename leaves a new due beside claimed. The pass
17// looks for it while it runs, and claims it the same way before it
18// reads the refs again: the rename replaces the claimed already there,
19// which loses nothing, since a mark says only "read the refs again"
20// and the claimed that is left stands for both. A due made after a
21// pass's last claim is left for the next pass.
22// - lock, the consumer's lock. A pass holds it from the claim to the
23// acknowledgement, so two passes over one wake directory take turns,
24// whatever state directories they were given.
25//
26// A pass removes claimed when it ends, once it has read every project
27// and recorded every failure it met (worker.go's handled). It never
28// removes due: removing claimed by name cannot touch a mark made since
29// the last claim. A
30// pass that read less, or a pass that was killed, leaves claimed. Nothing
31// watches claimed; it says that the last claim was not handled, and the
32// next pass to handle everything removes it. That pass reads the refs as
33// they are when it runs, which covers every move the claim stood for.
34//
35// The host makes the directory, and the worker owns it. No file in it is
36// a temporary, so nothing is left to clean up when a writer or a pass is
37// killed.
38
39import (
40 "errors"
41 "fmt"
42 "io"
43 "io/fs"
44 "os"
45 "path/filepath"
46)
47
48// claimFailedStatus is the status a pass exits with when it could not
49// claim its wake directory's due mark. The pass reads and builds all the
50// same, and exits so whatever runs it can hold off before the next pass,
51// rather than start one at once for a due that is still there.
52const claimFailedStatus = 75
53
54// claimFailure is the error a pass returns when it ran but could not
55// claim due. main exits claimFailedStatus for it.
56type claimFailure struct{ msg string }
57
58func (c claimFailure) Error() string { return c.msg }
59
60// wake is one pass's hold on a wake directory.
61type wake struct {
62 dir string
63 lock *os.File // the directory's lock; nil when it could not be taken
64 err error // why due could not be claimed, or nil
65}
66
67// claimWake takes the wake directory's lock, waiting for any pass that
68// holds it, and renames due to claimed. No due is nothing to claim, and
69// not a failure. A lock or a rename that fails is kept in err, and the
70// pass goes on without a claim.
71func claimWake(dir string, stderr io.Writer) *wake {
72 w := &wake{dir: dir}
73 lock, err := lockState(filepath.Join(dir, "lock"), stderr)
74 if err != nil {
75 w.err = fmt.Errorf("could not take the lock in %s: %w", dir, err)
76 return w
77 }
78 w.lock = lock
79 err = os.Rename(filepath.Join(dir, "due"), filepath.Join(dir, "claimed"))
80 if err != nil && !errors.Is(err, fs.ErrNotExist) {
81 w.err = fmt.Errorf("could not claim %s: %w", filepath.Join(dir, "due"), err)
82 }
83 return w
84}
85
86// ack removes claimed, for a pass that handled what it read. A pass that
87// could not claim removes nothing: it holds no claim to acknowledge.
88func (w *wake) ack() error {
89 if w.err != nil || w.lock == nil {
90 return nil
91 }
92 err := os.Remove(filepath.Join(w.dir, "claimed"))
93 if err != nil && !errors.Is(err, fs.ErrNotExist) {
94 return err
95 }
96 return nil
97}
98
99// again claims a mark made since the last claim, for a pass that holds
100// the lock and is about to read the refs again. It does not take the lock
101// again, since the pass holds it. A rename that fails is kept in err, and
102// the pass claims nothing more. Without a claim to build on it does
103// nothing.
104func (w *wake) again(stderr io.Writer) {
105 if w == nil || w.err != nil || w.lock == nil {
106 return
107 }
108 err := os.Rename(filepath.Join(w.dir, "due"), filepath.Join(w.dir, "claimed"))
109 if err != nil && !errors.Is(err, fs.ErrNotExist) {
110 w.err = fmt.Errorf("could not claim %s: %w", filepath.Join(w.dir, "due"), err)
111 fmt.Fprintf(stderr, "valley: %v; the pass claims nothing more\n", w.err)
112 }
113}
114
115// marked reports whether a mark was made since the last claim, for a pass
116// that can claim it. A pass that could not claim does not look, since the
117// due it leaves would read as a new mark for as long as it ran.
118func (w *wake) marked() bool {
119 if w == nil || w.err != nil || w.lock == nil {
120 return false
121 }
122 _, err := os.Stat(filepath.Join(w.dir, "due"))
123 return err == nil
124}
125
126func (w *wake) close() {
127 if w.lock != nil {
128 w.lock.Close()
129 }
130}