forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deleted_file_pruner.go
85 lines (72 loc) · 1.71 KB
/
deleted_file_pruner.go
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
package main // import "pixur.org/pixur/tools/cron"
import (
"context"
"flag"
"log"
"time"
"pixur.org/pixur/be/schema"
sdb "pixur.org/pixur/be/schema/db"
tab "pixur.org/pixur/be/schema/tables"
"pixur.org/pixur/be/server/config"
"pixur.org/pixur/be/tasks"
)
func run(ctx context.Context) error {
db, err := sdb.Open(ctx, config.Conf.DbName, config.Conf.DbConfig)
if err != nil {
return err
}
defer db.Close()
j, err := tab.NewJob(ctx, db)
if err != nil {
return err
}
defer j.Rollback()
perPicFn := func(p *schema.Pic) error {
return perPic(p, db, config.Conf.PixPath)
}
return j.ScanPics(sdb.Opts{
Prefix: tab.PicsPrimary{},
Lock: sdb.LockNone,
}, perPicFn)
}
func perPic(p *schema.Pic, db sdb.DB, pixPath string) error {
now := time.Now()
// No deletion info
if p.DeletionStatus == nil {
return nil
}
// Some deletion info, but it isn't on the chopping block.
if p.DeletionStatus.PendingDeletedTs == nil {
return nil
}
// It was already hard deleted, ignore it
if p.DeletionStatus.ActualDeletedTs != nil {
return nil
}
pendingTime := schema.ToTime(p.DeletionStatus.PendingDeletedTs)
// It is pending deletion, just not yet.
if !now.After(pendingTime) {
return nil
}
log.Println("Preparing to delete", p.GetVarPicId(), pendingTime)
var task = &tasks.HardDeletePicTask{
Beg: db,
PixPath: pixPath,
PicId: p.PicId,
}
runner := new(tasks.TaskRunner)
// TODO: use real userid
if err := runner.Run(tasks.CtxFromUserToken(context.TODO(), -12345, -12345), task); err != nil {
return err
}
return nil
}
func main() {
flag.Parse()
if err := run(context.Background()); err != nil {
log.Println(err.(stringer).String())
}
}
type stringer interface {
String() string
}