gunk-dev/the-valley

wake.go

source6ad905b8e97cmain's published tip history
3.7 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, for the
17// next pass.
18// - lock, the consumer's lock. A pass holds it from the claim to the
19// acknowledgement, so two passes over one wake directory take turns,
20// whatever state directories they were given.
21//
22// A pass removes claimed once it has read every project and recorded
23// every failure it met (worker.go's handled). It never removes due:
24// removing claimed by name cannot touch a mark made since the claim. A
25// pass that read less, or a pass that was killed, leaves claimed. Nothing
26// watches claimed; it says that the last claim was not handled, and the
27// next pass to handle everything removes it. That pass reads the refs as
28// they are when it runs, which covers every move the claim stood for.
29//
30// The host makes the directory, and the worker owns it. No file in it is
31// a temporary, so nothing is left to clean up when a writer or a pass is
32// killed.
33
34import (
35 "errors"
36 "fmt"
37 "io"
38 "io/fs"
39 "os"
40 "path/filepath"
41)
42
43// claimFailedStatus is the status a pass exits with when it could not
44// claim its wake directory's due mark. The pass reads and builds all the
45// same, and exits so whatever runs it can hold off before the next pass,
46// rather than start one at once for a due that is still there.
47const claimFailedStatus = 75
48
49// claimFailure is the error a pass returns when it ran but could not
50// claim due. main exits claimFailedStatus for it.
51type claimFailure struct{ msg string }
52
53func (c claimFailure) Error() string { return c.msg }
54
55// wake is one pass's hold on a wake directory.
56type wake struct {
57 dir string
58 lock *os.File // the directory's lock; nil when it could not be taken
59 err error // why due could not be claimed, or nil
60}
61
62// claimWake takes the wake directory's lock, waiting for any pass that
63// holds it, and renames due to claimed. No due is nothing to claim, and
64// not a failure. A lock or a rename that fails is kept in err, and the
65// pass goes on without a claim.
66func claimWake(dir string, stderr io.Writer) *wake {
67 w := &wake{dir: dir}
68 lock, err := lockState(filepath.Join(dir, "lock"), stderr)
69 if err != nil {
70 w.err = fmt.Errorf("could not take the lock in %s: %w", dir, err)
71 return w
72 }
73 w.lock = lock
74 err = os.Rename(filepath.Join(dir, "due"), filepath.Join(dir, "claimed"))
75 if err != nil && !errors.Is(err, fs.ErrNotExist) {
76 w.err = fmt.Errorf("could not claim %s: %w", filepath.Join(dir, "due"), err)
77 }
78 return w
79}
80
81// ack removes claimed, for a pass that handled what it read. A pass that
82// could not claim removes nothing: it holds no claim to acknowledge.
83func (w *wake) ack() error {
84 if w.err != nil || w.lock == nil {
85 return nil
86 }
87 err := os.Remove(filepath.Join(w.dir, "claimed"))
88 if err != nil && !errors.Is(err, fs.ErrNotExist) {
89 return err
90 }
91 return nil
92}
93
94func (w *wake) close() {
95 if w.lock != nil {
96 w.lock.Close()
97 }
98}