|
package main |
|
|
|
import ( |
|
"fmt" |
|
"log" |
|
"os" |
|
"sort" |
|
"time" |
|
) |
|
|
|
func main() { |
|
// setInverals([]string{"1m", "1m"}) |
|
// 1m0s started now, next: 1m0s, 2m0s, 3m0s, 4m0s |
|
// 1m0s started at 30s, next: 1m30s, 2m30s, 3m30s, 4m30s |
|
|
|
// setInverals([]string{"5m", "5m"}) |
|
// 5m0s started now, next: 5m0s, 10m0s, 15m0s, 20m0s |
|
// 5m0s started at 2m30s, next: 7m30s, 12m30s, 17m30s, 22m30s |
|
|
|
// setInverals([]string{"20m", "30m"}) |
|
// 20m0s started now, next: 20m0s, 40m0s, 1h0m0s, 1h20m0s |
|
// 30m0s started at 25m0s, next: 55m0s, 1h25m0s, 1h55m0s, 2h25m0s |
|
|
|
// setInverals([]string{"5m", "5m", "5m"}) |
|
// 5m0s started now, next: 5m0s, 10m0s, 15m0s, 20m0s |
|
// 5m0s started at 1m40s, next: 6m40s, 11m40s, 16m40s, 21m40s |
|
// 5m0s started at 3m20s, next: 8m20s, 13m20s, 18m20s, 23m20s |
|
|
|
// setInverals([]string{"5m", "5m", "10m"}) |
|
// 5m0s started now, next: 5m0s, 10m0s, 15m0s, 20m0s |
|
// 5m0s started at 1m40s, next: 6m40s, 11m40s, 16m40s, 21m40s |
|
// 10m0s started at 3m20s, next: 13m20s, 23m20s, 33m20s, 43m20s |
|
|
|
// setInverals([]string{"5m", "5m", "15m"}) |
|
// 5m0s started now, next: 5m0s, 10m0s, 15m0s, 20m0s |
|
// 5m0s started at 3m20s, next: 8m20s, 13m20s, 18m20s, 23m20s |
|
// 15m0s started at 1m40s, next: 16m40s, 31m40s, 46m40s, 1h1m40s |
|
|
|
// setInverals([]string{"5m", "5m", "30m"}) |
|
// 5m0s started now, next: 5m0s, 10m0s, 15m0s, 20m0s |
|
// 5m0s started at 3m20s, next: 8m20s, 13m20s, 18m20s, 23m20s |
|
// 30m0s started at 26m40s, next: 56m40s, 1h26m40s, 1h56m40s, 2h26m40s |
|
|
|
setInverals([]string{"1h", "30m", "15m"}) |
|
// 15m0s started now, next: 15m0s, 30m0s, 45m0s, 1h0m0s |
|
// 30m0s started at 5m0s, next: 35m0s, 1h5m0s, 1h35m0s, 2h5m0s |
|
// 1h0m0s started at 10m0s, next: 1h10m0s, 2h10m0s, 3h10m0s, 4h10m0s |
|
|
|
select {} // blocking this thread |
|
} |
|
|
|
func setInverals(intervalsString []string) { |
|
var intervalsInt []int |
|
var sumInterval int |
|
for _, v := range intervalsString { |
|
duration, err := time.ParseDuration(v) |
|
if err != nil { |
|
fmt.Println(err) |
|
} |
|
durationNanoseconds := int(duration.Nanoseconds()) |
|
sumInterval += durationNanoseconds |
|
intervalsInt = append(intervalsInt, durationNanoseconds) |
|
} |
|
sort.Ints(intervalsInt) |
|
|
|
var lenIntervals = len(intervalsInt) |
|
for i, value := range intervalsInt { |
|
go func(i, value int) { |
|
if i == 0 { |
|
log.Printf("i: %d, starts now and next time at %dns\n", i, value) |
|
} else { |
|
var delay int |
|
if lenIntervals == sumInterval/intervalsInt[i] && sumInterval%intervalsInt[i] == 0 { |
|
delay = intervalsInt[i] / lenIntervals * i |
|
} else { |
|
delay = sumInterval / lenIntervals * i |
|
} |
|
|
|
for delay > intervalsInt[i] { |
|
delay -= intervalsInt[i] |
|
} |
|
|
|
fmt.Printf("(%dns start at %dns)\n", value, delay) |
|
time.Sleep(time.Duration(delay) * time.Nanosecond) |
|
} |
|
ticker := time.NewTicker(time.Duration(value) * time.Nanosecond) |
|
for { |
|
<-ticker.C |
|
log.Printf("i: %d, value: %dns\n", i, value) |
|
os.Exit(1) |
|
} |
|
}(i, value) |
|
} |
|
} |