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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 |
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"
"golang.org/x/sync/errgroup"
"git.pablomurad.com/pablo/patchwork/internal/config"
"git.pablomurad.com/pablo/patchwork/internal/engine"
"git.pablomurad.com/pablo/patchwork/internal/export"
"git.pablomurad.com/pablo/patchwork/internal/render"
"git.pablomurad.com/pablo/patchwork/internal/server"
"git.pablomurad.com/pablo/patchwork/internal/source/folder"
)
// cmdServe watches the content folder and serves the API and feeds,
// rebuilding them after every scan that changed something.
func cmdServe(ctx context.Context, args []string) error {
fs := newFlags("serve", "[-addr host:port] [-poll-only]")
cfgPath := configFlag(fs)
addr := fs.String("addr", "", "listen address (default: http.addr from the config)")
pollOnly := fs.Bool("poll-only", false, "don't use file events")
if err := fs.Parse(args); err != nil {
return err
}
a, err := openApp(*cfgPath)
if err != nil {
return err
}
defer a.Close()
if *addr == "" {
*addr = a.cfg.HTTP.Addr
}
srv := server.New(a.log)
var building sync.Mutex
rebuild := func() {
building.Lock()
defer building.Unlock()
start := time.Now()
t, err := render.Build(ctx, a.store, a.cfg)
if err != nil {
a.log.Error("build failed; still serving the previous build", "err", err)
return
}
srv.Swap(t)
a.log.Info("published", "files", len(t.Files), "revisions", len(t.Model.Revisions), "took", time.Since(start).Round(time.Millisecond))
}
r := a.runner()
r.PollOnly = *pollOnly
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
first := true
err := r.Watch(ctx, a.cfg.Watch.Debounce.Duration, a.cfg.Watch.Poll.Duration, func(rep folder.Report, err error) {
if err != nil {
a.log.Error("scan failed", "err", err)
return
}
printReport(rep, false)
if first || changed(rep) {
rebuild()
}
first = false
})
if errors.Is(err, context.Canceled) {
return nil
}
return err
})
// retract, amend and snapshot run as separate processes against the same
// database. SQLite's data_version moves when they commit; checking it is
// a trivial query, so the site shows their changes within seconds without
// rebuilding when nothing happened.
g.Go(func() error {
last, _ := a.store.DataVersion(ctx)
t := time.NewTicker(outsideWriteCheck)
defer t.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-t.C:
v, err := a.store.DataVersion(ctx)
if err != nil || v == last {
continue
}
last = v
a.log.Info("database changed by another process")
rebuild()
}
}
})
g.Go(func() error {
a.log.Info("serving", "addr", *addr, "source", a.cfg.Source.Path, "db", a.cfg.DB)
return srv.ListenAndServe(ctx, *addr)
})
return g.Wait()
}
// outsideWriteCheck is how often serve looks for commits made by other
// patchwork processes (retract, amend, snapshot).
const outsideWriteCheck = 3 * time.Second
func changed(rep folder.Report) bool {
for _, r := range rep.Results {
if r.Action != engine.Unchanged || r.Restored {
return true
}
}
return false
}
func cmdExport(ctx context.Context, args []string) error {
fs := newFlags("export", "-out DIR [-scan]")
cfgPath := configFlag(fs)
out := fs.String("out", "", "directory to write (replaced as a whole)")
scan := fs.Bool("scan", false, "scan the content folder first")
if err := fs.Parse(args); err != nil {
return err
}
if *out == "" {
fs.Usage()
return errProblems
}
a, err := openApp(*cfgPath)
if err != nil {
return err
}
defer a.Close()
if *scan {
r := a.runner()
r.Settle = 0
rep, err := r.Scan(ctx, false)
if err != nil {
return err
}
printReport(rep, false)
}
t, err := render.Build(ctx, a.store, a.cfg)
if err != nil {
return err
}
if err := export.Write(t, *out); err != nil {
return err
}
fmt.Fprintf(stdout, "wrote %d files to %s\n", len(t.Files), *out)
return nil
}
// cmdHealth exits 0 when a running `patchwork serve` answers /healthz. It
// exists for container health checks, where images have no curl.
func cmdHealth(args []string) error {
fs := newFlags("health", "[-url http://127.0.0.1:8080/healthz]")
cfgPath := configFlag(fs)
url := fs.String("url", "", "health URL (default: built from http.addr)")
if err := fs.Parse(args); err != nil {
return err
}
if *url == "" {
// Load applies PATCHWORK_HTTP_ADDR, so the env wins over the file.
addr := config.Default().HTTP.Addr
if cfg, err := config.Load(*cfgPath); err == nil {
addr = cfg.HTTP.Addr
} else if env := os.Getenv("PATCHWORK_HTTP_ADDR"); env != "" {
addr = env
}
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if host == "" || host == "0.0.0.0" || host == "::" {
host = "127.0.0.1"
}
*url = "http://" + net.JoinHostPort(host, port) + "/healthz"
}
c := &http.Client{Timeout: 3 * time.Second}
resp, err := c.Get(*url)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %s", *url, resp.Status)
}
return nil
}
|