Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/cron/cron.go
1560 views
1
package cron
2
3
import "time"
4
5
type Cron struct {
6
d time.Duration
7
ch chan struct{}
8
}
9
10
func NewCron(d time.Duration) *Cron {
11
return &Cron{
12
d: d,
13
ch: make(chan struct{}),
14
}
15
}
16
17
func (c *Cron) Do(f func()) {
18
go func() {
19
ticker := time.NewTicker(c.d)
20
defer ticker.Stop()
21
for {
22
select {
23
case <-ticker.C:
24
f()
25
case <-c.ch:
26
return
27
}
28
}
29
}()
30
}
31
32
func (c *Cron) Stop() {
33
select {
34
case _, _ = <-c.ch:
35
default:
36
c.ch <- struct{}{}
37
close(c.ch)
38
}
39
}
40
41