Debian Codegraph @

common/tasks/tasks.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package tasks
15
16 import (
17 "sync"
18 "time"
19 )
20
21 // RunEvery runs a function at intervals defined by the function itself.
22 // Functions can be added and removed while running.
23 type RunEvery struct {
24 // Any error returned from the function will be passed to this function.
25 HandleError func(string, error)
26
27 // If set, the function will be run immediately.
28 RunImmediately bool
29
30 // The named functions to run.
31 funcs map[string]*Func
32
33 mu sync.Mutex
34 started bool
35 closed bool
36 quit chan struct{}
37 }
38
39 type Func struct {
40 // The shortest interval between each run.
41 IntervalLow time.Duration
42
43 // The longest interval between each run.
44 IntervalHigh time.Duration
45
46 // The function to run.
47 F func(interval time.Duration) (time.Duration, error)
48
49 interval time.Duration
50 last time.Time
51 }
52
53 func (r *RunEvery) Start() error {
54 if r.started {
55 return nil
56 }
57
58 r.started = true
59 r.quit = make(chan struct{})
60
61 go func() {
62 if r.RunImmediately {
63 r.run()
64 }
65 ticker := time.NewTicker(500 * time.Millisecond)
66 defer ticker.Stop()
67 for {
68 select {
69 case <-r.quit:
70 return
71 case <-ticker.C:
72 r.run()
73 }
74 }
75 }()
76
77 return nil
78 }
79
80 // Close stops the RunEvery from running.
81 func (r *RunEvery) Close() error {
82 if r.closed {
83 return nil
84 }
85 r.closed = true
86 if r.quit != nil {
87 close(r.quit)
88 }
89 return nil
90 }
91
92 // Add adds a function to the RunEvery.
93 func (r *RunEvery) Add(name string, f Func) {
94 r.mu.Lock()
95 defer r.mu.Unlock()
96 if r.funcs == nil {
97 r.funcs = make(map[string]*Func)
98 }
99 if f.IntervalLow == 0 {
100 f.IntervalLow = 500 * time.Millisecond
101 }
102 if f.IntervalHigh <= f.IntervalLow {
103 f.IntervalHigh = 20 * time.Second
104 }
105
106 start := max(f.IntervalHigh/3, f.IntervalLow)
107 f.interval = start
108 f.last = time.Now()
109
110 r.funcs[name] = &f
111 }
112
113 // Remove removes a function from the RunEvery.
114 func (r *RunEvery) Remove(name string) {
115 r.mu.Lock()
116 defer r.mu.Unlock()
117 delete(r.funcs, name)
118 }
119
120 // Has returns whether the RunEvery has a function with the given name.
121 func (r *RunEvery) Has(name string) bool {
122 r.mu.Lock()
123 defer r.mu.Unlock()
124 _, found := r.funcs[name]
125 return found
126 }
127
128 func (r *RunEvery) run() {
129 r.mu.Lock()
130 defer r.mu.Unlock()
131 for name, f := range r.funcs {
132 if time.Now().Before(f.last.Add(f.interval)) {
133 continue
134 }
135 f.last = time.Now()
136 interval, err := f.F(f.interval)
137 if err != nil && r.HandleError != nil {
138 r.HandleError(name, err)
139 }
140
141 if interval < f.IntervalLow {
142 interval = f.IntervalLow
143 }
144
145 if interval > f.IntervalHigh {
146 interval = f.IntervalHigh
147 }
148 f.interval = interval
149 }
150 }