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 |
package engine
import (
"context"
"git.pablomurad.com/pablo/patchwork/internal/store"
)
// missingScansToRemove: a file must be absent from this many consecutive
// scans before its page leaves the feeds. One miss is often an upload that
// replaces the file by deleting it first.
const missingScansToRemove = 2
// Reconcile is called after a full scan with every path that exists. Pages
// whose file is absent move toward removal; present pages that were missing
// come back.
func (e *Engine) Reconcile(ctx context.Context, present map[string]bool, dryRun bool) ([]Result, error) {
var out []Result
err := e.Store.Tx(ctx, dryRun, func(q *store.Queries) error {
pages, err := q.Pages(ctx, e.Source, true)
if err != nil {
return err
}
now := e.Now()
for i := range pages {
p := &pages[i]
if present[p.Path] {
if p.RemovedAt == nil && p.MissingScans == 0 {
continue
}
restored := p.RemovedAt != nil
p.RemovedAt, p.MissingScans = nil, 0
if err := q.UpdatePage(ctx, p); err != nil {
return err
}
if restored {
out = append(out, Result{Path: p.Path, Slug: p.Slug, Action: Unchanged, Restored: true})
}
continue
}
if p.RemovedAt != nil {
continue
}
p.MissingScans++
if p.MissingScans >= missingScansToRemove {
p.RemovedAt = &now
out = append(out, Result{Path: p.Path, Slug: p.Slug, Action: Removed})
}
if err := q.UpdatePage(ctx, p); err != nil {
return err
}
}
return nil
})
return out, err
}
// ActivePages counts pages that haven't been removed.
func (e *Engine) ActivePages(ctx context.Context) (int, error) {
pages, err := e.Store.Pages(ctx, e.Source, false)
return len(pages), err
}
|