-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
76 lines (69 loc) · 2.14 KB
/
provider.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
package provider
import (
"math"
"math/rand"
"time"
)
// SourceProvider is used to get tasks
type SourceProvider interface {
Read() error
OnTaskUpdate(func(*Task) error)
Stop() error
TaskStarted(*Task) error
TaskDone(*Task) error
TaskError(*Task, error) error
}
// ExecutionProvider is used for executing cronjobs
type ExecutionProvider interface {
Execute(interface{}, *Response) error
}
// Task represents a task to be run
type Task struct {
Name string `json:"name"`
Running bool `json:"is_running,omitempty"`
Expression string `json:"schedule,omitempty"`
LastRunAt time.Time `json:"last_run_at,omitempty"`
AfterTask string `json:"after_task,omitempty"`
Key string `json:"-"`
SourceProvider SourceProvider `json:"-"`
Executor string `json:"executor"`
ExecutionConfig interface{} `json:"execution_config"`
LastError string `json:"last_error"`
CurrentRetryCount int `json:"current_retry_counter"`
MaxRetries int `json:"max_retries"`
}
// Response contains data that executors can return
type Response struct {
StatusCode int `json:"status_code"`
Response []byte `json:"response"`
}
const maxInt64 = float64(math.MaxInt64 - 512)
// CalculateBackoffForAttempt returns the duration for a specific attempt. This is useful if
// you have a large number of independent Backoffs, but don't want use
// unnecessary memory storing the Backoff parameters per Backoff. The first
// attempt should be 0.
//
// ForAttempt is concurrent-safe.
func CalculateBackoffForAttempt(attempt float64) time.Duration {
// Zero-values are nonsensical, so we use
// them to apply defaults
min := 100 * time.Millisecond
max := 5 * time.Hour
factor := 3.0
//calculate this duration
minf := float64(min)
durc := minf * math.Pow(factor, attempt)
durf := rand.Float64()*(durc-minf) + minf
//ensure float64 wont overflow int64
if durf > maxInt64 {
return max
}
dur := time.Duration(durf)
//keep within bounds
if dur < min {
return min
} else if dur > max {
return max
}
return dur
}