mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
package queues
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/hanzoai/tasks/common/predicates"
|
|
"github.com/hanzoai/tasks/service/history/tasks"
|
|
)
|
|
|
|
func NewRandomKey() tasks.Key {
|
|
return tasks.NewKey(time.Unix(0, rand.Int63()).UTC(), rand.Int63())
|
|
}
|
|
|
|
func NewRandomRange() Range {
|
|
maxKey := NewRandomKey()
|
|
minKey := tasks.NewKey(
|
|
time.Unix(0, rand.Int63n(maxKey.FireTime.UnixNano())).UTC(),
|
|
rand.Int63n(maxKey.TaskID),
|
|
)
|
|
return NewRange(minKey, maxKey)
|
|
}
|
|
|
|
func NewRandomKeyInRange(
|
|
r Range,
|
|
) tasks.Key {
|
|
if r.IsEmpty() {
|
|
panic("can not create key in range for an empty range")
|
|
}
|
|
|
|
minFireTimeUnixNano := r.InclusiveMin.FireTime.UnixNano()
|
|
maxFireTimeUnixNano := r.ExclusiveMax.FireTime.UnixNano()
|
|
minTaskID := r.InclusiveMin.TaskID
|
|
maxTaskID := r.ExclusiveMax.TaskID
|
|
|
|
if minFireTimeUnixNano == maxFireTimeUnixNano {
|
|
return tasks.NewKey(
|
|
r.InclusiveMin.FireTime,
|
|
rand.Int63n(1+maxTaskID-minTaskID)+minTaskID,
|
|
)
|
|
}
|
|
|
|
fireTime := time.Unix(0, rand.Int63n(1+maxFireTimeUnixNano-minFireTimeUnixNano)+minFireTimeUnixNano)
|
|
if fireTime.Equal(r.InclusiveMin.FireTime) {
|
|
return tasks.NewKey(
|
|
fireTime,
|
|
rand.Int63n(math.MaxInt64-minTaskID)+minTaskID,
|
|
)
|
|
}
|
|
|
|
if fireTime.Equal(r.ExclusiveMax.FireTime) {
|
|
return tasks.NewKey(
|
|
fireTime,
|
|
rand.Int63n(maxTaskID),
|
|
)
|
|
}
|
|
|
|
return tasks.NewKey(fireTime, rand.Int63())
|
|
}
|
|
|
|
func NewRandomOrderedRangesInRange(
|
|
r Range,
|
|
numRanges int,
|
|
) []Range {
|
|
ranges := []Range{r}
|
|
for len(ranges) < numRanges {
|
|
r := ranges[0]
|
|
left, right := r.Split(NewRandomKeyInRange(r))
|
|
left.ExclusiveMax.FireTime = left.ExclusiveMax.FireTime.Add(-time.Nanosecond)
|
|
right.InclusiveMin.FireTime = right.InclusiveMin.FireTime.Add(time.Nanosecond)
|
|
ranges = append(ranges[1:], left, right)
|
|
}
|
|
|
|
slices.SortFunc(ranges, func(a, b Range) int {
|
|
return a.InclusiveMin.CompareTo(b.InclusiveMin)
|
|
})
|
|
|
|
return ranges
|
|
}
|
|
|
|
func NewRandomScopes(
|
|
numScopes int,
|
|
) []Scope {
|
|
ranges := NewRandomOrderedRangesInRange(
|
|
NewRandomRange(),
|
|
numScopes,
|
|
)
|
|
|
|
scopes := make([]Scope, 0, 10)
|
|
for _, r := range ranges {
|
|
scopes = append(scopes, NewScope(r, predicates.Universal[tasks.Task]()))
|
|
}
|
|
|
|
return scopes
|
|
}
|