-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
87 lines (78 loc) · 1.69 KB
/
daemon.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
86
87
package main
import (
"context"
"errors"
"time"
"github.com/FurqanSoftware/pog"
"github.com/avast/retry-go"
)
type Daemon struct {
cfg Config
params Parameters
exitCh chan struct{}
abortCh chan error
pog *pog.Pogger
delayNotFound time.Duration
}
func (d Daemon) Loop(ctx context.Context) {
L:
for {
stop, delay := d.iter(ctx)
if stop {
break
}
select {
case <-d.exitCh:
break L
case <-time.After(delay):
}
}
}
func (d Daemon) iter(ctx context.Context) (stop bool, delay time.Duration) {
pr, err := getNextPrint(ctx, d.cfg)
var terr tophError
if errors.As(err, &terr) {
d.pog.SetStatus(statusOffline)
d.pog.Error(err)
if !errors.As(err, &retryableError{}) {
d.abortCh <- err
return true, 0
}
return false, d.cfg.Printd.DelayError
}
var perr noNextPrintError
if errors.As(err, &perr) {
if perr.contestLocked {
d.pog.Info("Contest is locked")
d.abortCh <- err
return true, 0
}
d.pog.SetStatus(statusReady)
return false, d.delayNotFound
}
catch(err)
d.pog.SetStatus(statusPrinting)
d.pog.Infof("Printing %s", pr.ID)
pdf, err := runPrintJob(ctx, d.cfg, pr)
catch(err)
err = retry.Do(func() error {
return markPrintDone(ctx, d.cfg, pr, pdf)
},
retry.RetryIf(func(err error) bool { return errors.As(err, &retryableError{}) }),
retry.Attempts(3),
retry.Delay(500*time.Millisecond),
retry.LastErrorOnly(true),
)
if errors.As(err, &terr) {
d.pog.SetStatus(statusOffline)
d.pog.Error(err)
if !errors.As(err, &retryableError{}) {
d.abortCh <- err
return true, 0
}
return false, d.cfg.Printd.DelayError
}
catch(err)
d.pog.Info("∟ Done")
return false, d.cfg.Printd.DelayAfter
}