-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskinator.go
86 lines (72 loc) · 1.85 KB
/
taskinator.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
package taskinator
import (
"fmt"
"log"
"sync"
"time"
)
// Task represents a scheduled task
type Task struct {
Name string
Schedule time.Duration
MaxRetries int
RetryDelay time.Duration
Action func() error
}
// Scheduler represents a task scheduler
type Scheduler struct {
tasks []Task
}
// NewScheduler creates a new task scheduler
func NewScheduler() *Scheduler {
return &Scheduler{tasks: make([]Task, 0)}
}
// AddTask adds a task to the scheduler
func (s *Scheduler) AddTask(task Task) {
s.tasks = append(s.tasks, task)
}
// Start starts the task scheduler for the specified duration
func (s *Scheduler) Start(duration ...time.Duration) {
var wg sync.WaitGroup
var endTime time.Time
var hasDuration bool
// Check if a duration was provided
if len(duration) > 0 && duration[0] > 0 {
hasDuration = true
endTime = time.Now().Add(duration[0])
}
for _, task := range s.tasks {
wg.Add(1)
go func(t Task) {
defer wg.Done()
ticker := time.NewTicker(t.Schedule)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := executeWithRetry(t); err != nil {
log.Printf("Task %s failed after retrying: %v\n", t.Name, err)
}
}
// Check if the scheduler has run for the specified duration
if hasDuration && time.Now().After(endTime) {
return
}
}
}(task)
}
fmt.Println("Task scheduler started...")
wg.Wait()
}
// executeWithRetry executes a task with retry logic
func executeWithRetry(task Task) error {
for attempt := 0; attempt <= task.MaxRetries; attempt++ {
if err := task.Action(); err == nil {
return nil // Task executed successfully, no need for retry
} else {
fmt.Printf("Task %s failed (Attempt %d), retrying...\n", task.Name, attempt+1)
time.Sleep(task.RetryDelay) // Delay before retrying
}
}
return fmt.Errorf("Task %s exceeded maximum retries", task.Name)
}