2023-11-08 17:11:42 +00:00
|
|
|
package gocron
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"reflect"
|
2025-01-31 15:17:45 +00:00
|
|
|
"slices"
|
2023-11-08 17:11:42 +00:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func callJobFuncWithParams(jobFunc any, params ...any) error {
|
|
|
|
|
if jobFunc == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
f := reflect.ValueOf(jobFunc)
|
|
|
|
|
if f.IsZero() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if len(params) != f.Type().NumIn() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
in := make([]reflect.Value, len(params))
|
|
|
|
|
for k, param := range params {
|
|
|
|
|
in[k] = reflect.ValueOf(param)
|
|
|
|
|
}
|
2023-11-22 12:43:50 +00:00
|
|
|
returnValues := f.Call(in)
|
|
|
|
|
for _, val := range returnValues {
|
2023-11-08 17:11:42 +00:00
|
|
|
i := val.Interface()
|
|
|
|
|
if err, ok := i.(error); ok {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 16:03:34 +00:00
|
|
|
func requestJob(id uuid.UUID, ch chan *jobOutRequest) *internalJob {
|
2023-11-08 17:11:42 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
return requestJobCtx(ctx, id, ch)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 16:03:34 +00:00
|
|
|
func requestJobCtx(ctx context.Context, id uuid.UUID, ch chan *jobOutRequest) *internalJob {
|
2023-11-08 17:11:42 +00:00
|
|
|
resp := make(chan internalJob, 1)
|
|
|
|
|
select {
|
2025-08-27 16:03:34 +00:00
|
|
|
case ch <- &jobOutRequest{
|
2023-11-08 17:11:42 +00:00
|
|
|
id: id,
|
|
|
|
|
outChan: resp,
|
|
|
|
|
}:
|
2024-03-23 18:54:40 +00:00
|
|
|
case <-ctx.Done():
|
2023-11-08 17:11:42 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var j internalJob
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return nil
|
|
|
|
|
case jobReceived := <-resp:
|
|
|
|
|
j = jobReceived
|
|
|
|
|
}
|
|
|
|
|
return &j
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeSliceDuplicatesInt(in []int) []int {
|
2025-01-31 15:17:45 +00:00
|
|
|
slices.Sort(in)
|
|
|
|
|
return slices.Compact(in)
|
2023-11-08 17:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertAtTimesToDateTime(atTimes AtTimes, location *time.Location) ([]time.Time, error) {
|
|
|
|
|
if atTimes == nil {
|
|
|
|
|
return nil, errAtTimesNil
|
|
|
|
|
}
|
|
|
|
|
var atTimesDate []time.Time
|
|
|
|
|
for _, a := range atTimes() {
|
|
|
|
|
if a == nil {
|
|
|
|
|
return nil, errAtTimeNil
|
|
|
|
|
}
|
|
|
|
|
at := a()
|
|
|
|
|
if at.hours > 23 {
|
|
|
|
|
return nil, errAtTimeHours
|
|
|
|
|
} else if at.minutes > 59 || at.seconds > 59 {
|
|
|
|
|
return nil, errAtTimeMinSec
|
|
|
|
|
}
|
|
|
|
|
atTimesDate = append(atTimesDate, at.time(location))
|
|
|
|
|
}
|
2024-06-23 18:14:35 +00:00
|
|
|
slices.SortStableFunc(atTimesDate, ascendingTime)
|
2023-11-08 17:11:42 +00:00
|
|
|
return atTimesDate, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-23 18:14:35 +00:00
|
|
|
func ascendingTime(a, b time.Time) int {
|
|
|
|
|
return a.Compare(b)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 17:11:42 +00:00
|
|
|
type waitGroupWithMutex struct {
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *waitGroupWithMutex) Add(delta int) {
|
|
|
|
|
w.mu.Lock()
|
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
w.wg.Add(delta)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *waitGroupWithMutex) Done() {
|
|
|
|
|
w.wg.Done()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *waitGroupWithMutex) Wait() {
|
|
|
|
|
w.mu.Lock()
|
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
w.wg.Wait()
|
|
|
|
|
}
|