forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.go
More file actions
121 lines (108 loc) · 3.31 KB
/
tasks.go
File metadata and controls
121 lines (108 loc) · 3.31 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package component
import (
"context"
"time"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/random"
)
// TaskFunc is the task function.
type TaskFunc func(context.Context) error
// TaskRestart defines a task's restart policy.
type TaskRestart int
const (
// TaskRestartNever denotes a restart policy that never restarts tasks after success or failure.
TaskRestartNever TaskRestart = iota
// TaskRestartAlways denotes a restart policy that always restarts tasks, on success and failure.
TaskRestartAlways
// TaskRestartOnFailure denotes a restart policy that restarts tasks on failure.
TaskRestartOnFailure
)
var defaultTaskBackoff = [...]time.Duration{
10 * time.Millisecond,
50 * time.Millisecond,
100 * time.Millisecond,
1 * time.Second,
}
var backoffResetTime = time.Minute
// TaskBackoffDial is a backoff to use for tasks that are dialing services.
var TaskBackoffDial = []time.Duration{100 * time.Millisecond, 1 * time.Second, 10 * time.Second}
type task struct {
ctx context.Context
id string
fn TaskFunc
restart TaskRestart
backoff []time.Duration
}
// RegisterTask registers a task, optionally with restart policy and backoff, to be started after the component started.
func (c *Component) RegisterTask(ctx context.Context, id string, fn TaskFunc, restart TaskRestart, backoff ...time.Duration) {
c.tasks = append(c.tasks, task{
ctx: ctx,
id: id,
fn: fn,
restart: restart,
backoff: backoff,
})
}
// StartTask starts the specified task function, optionally with restart policy and backoff.
func (c *Component) StartTask(ctx context.Context, id string, fn TaskFunc, restart TaskRestart, jitter float64, backoff ...time.Duration) {
logger := log.FromContext(ctx).WithField("task_id", id)
if len(backoff) == 0 {
backoff = defaultTaskBackoff[:]
}
go func() {
invocation := 0
for {
invocation++
startTime := time.Now()
err := fn(ctx)
executionTime := time.Now().Sub(startTime)
if err != nil && err != context.Canceled {
logger.WithField("invocation", invocation).WithError(err).Warn("Task failed")
}
switch restart {
case TaskRestartNever:
return
case TaskRestartAlways:
case TaskRestartOnFailure:
if err == nil {
return
}
}
select {
case <-ctx.Done():
return
default:
}
bi := invocation - 1
if bi >= len(backoff) {
bi = len(backoff) - 1
}
if executionTime > backoffResetTime {
bi = 0
}
s := backoff[bi]
if jitter != 0 {
s = random.Jitter(backoff[bi], jitter)
}
time.Sleep(s)
}
}()
}
func (c *Component) startTasks() {
for _, t := range c.tasks {
c.StartTask(t.ctx, t.id, t.fn, t.restart, 0.1, t.backoff...)
}
}