diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-sched')
-rw-r--r-- | vendor/codeberg.org/gruf/go-sched/job.go | 19 | ||||
-rw-r--r-- | vendor/codeberg.org/gruf/go-sched/scheduler.go | 14 |
2 files changed, 31 insertions, 2 deletions
diff --git a/vendor/codeberg.org/gruf/go-sched/job.go b/vendor/codeberg.org/gruf/go-sched/job.go index 66e24fe9a..7831a39bd 100644 --- a/vendor/codeberg.org/gruf/go-sched/job.go +++ b/vendor/codeberg.org/gruf/go-sched/job.go @@ -1,6 +1,9 @@ package sched import ( + "reflect" + "strconv" + "strings" "time" "codeberg.org/gruf/go-atomics" @@ -97,3 +100,19 @@ func (job *Job) Run(now time.Time) { }() job.call(now) } + +// String provides a debuggable string representation of Job including ID, next time and Timing type. +func (job *Job) String() string { + var buf strings.Builder + buf.WriteByte('{') + buf.WriteString("id=") + buf.WriteString(strconv.FormatUint(job.id, 10)) + buf.WriteByte(' ') + buf.WriteString("next=") + buf.WriteString(job.next.Load().Format(time.StampMicro)) + buf.WriteByte(' ') + buf.WriteString("timing=") + buf.WriteString(reflect.TypeOf(job.timing).String()) + buf.WriteByte('}') + return buf.String() +} diff --git a/vendor/codeberg.org/gruf/go-sched/scheduler.go b/vendor/codeberg.org/gruf/go-sched/scheduler.go index d017ddcf6..8d076fea0 100644 --- a/vendor/codeberg.org/gruf/go-sched/scheduler.go +++ b/vendor/codeberg.org/gruf/go-sched/scheduler.go @@ -132,7 +132,13 @@ func (sch *Scheduler) run(ctx context.Context) { // Get next job time next := sch.jobs[0].Next() - if until := next.Sub(now); until <= 0 { + // If this job is _just_ about to be ready, we + // don't bother sleeping. It's wasted cycles only + // sleeping for some obscenely tiny amount of time + // we can't guarantee precision for. + const precision = time.Millisecond + + if until := next.Sub(now); until <= precision/1e3 { // This job is behind schedule, // set timer to always tick tch = alwaysticks @@ -155,6 +161,10 @@ func (sch *Scheduler) run(ctx context.Context) { // Timer ticked, run scheduled case now := <-tch: + if !timerset { + // alwaysticks returns zero times + now = time.Now() + } sch.schedule(now) // Received update, handle job/id @@ -213,7 +223,7 @@ func (sch *Scheduler) schedule(now time.Time) { // Run this job async! go job.Run(now) - if job.Next().IsZero() { + if next.IsZero() { // Zero time, this job is done and can be dropped sch.jobs = append(sch.jobs[:i], sch.jobs[i+1:]...) continue |