replace slot attempts int with atomics
This commit is contained in:
parent
b6df47ed0c
commit
3cbea438f6
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -35,36 +36,32 @@ type JobSlotDetails struct {
|
||||
}
|
||||
|
||||
type jobSlot struct {
|
||||
l sync.RWMutex
|
||||
job.Job
|
||||
slot int
|
||||
attempts int
|
||||
*job.Job
|
||||
slot atomic.Uint32
|
||||
attempts atomic.Uint32
|
||||
// priority Priority
|
||||
}
|
||||
|
||||
func newJobSlot(task job.FnJob, slot int) jobSlot {
|
||||
return jobSlot{
|
||||
Job: job.NewJob(task),
|
||||
slot: slot,
|
||||
func newJobSlot(task job.FnJob, slot int) *jobSlot {
|
||||
j := job.NewJob(task)
|
||||
js := jobSlot{
|
||||
Job: &j,
|
||||
}
|
||||
|
||||
js.slot.Add(uint32(slot))
|
||||
|
||||
return &js
|
||||
}
|
||||
|
||||
func (j *jobSlot) run(ctx context.Context) {
|
||||
j.l.Lock()
|
||||
defer j.l.Unlock()
|
||||
|
||||
j.attempts += 1
|
||||
|
||||
j.attempts.Add(1)
|
||||
j.Job.Run(ctx)
|
||||
}
|
||||
|
||||
func (j *jobSlot) getDetails() JobSlotDetails {
|
||||
j.l.RLock()
|
||||
defer j.l.RUnlock()
|
||||
|
||||
return JobSlotDetails{
|
||||
JobDetails: j.IntoDetails(),
|
||||
Attempts: j.attempts,
|
||||
Attempts: int(j.attempts.Load()),
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,8 +172,8 @@ func (c *SchedulerCycle) Delay(fnJob job.FnJob) uuid.UUID {
|
||||
|
||||
j := newJobSlot(fnJob, nextSlot)
|
||||
|
||||
c.slots[nextSlot] = append(c.slots[nextSlot], &j)
|
||||
c.jobs[j.GetID()] = &j
|
||||
c.slots[nextSlot] = append(c.slots[nextSlot], j)
|
||||
c.jobs[j.GetID()] = j
|
||||
|
||||
log.Info().Str("job", j.GetID().String()).Msg("job added successfully")
|
||||
return j.GetID()
|
||||
@ -329,6 +326,7 @@ func (c *SchedulerCycle) updateCurrentSlot(j *jobSlot) {
|
||||
c.l.Lock()
|
||||
defer c.l.Unlock()
|
||||
|
||||
j.slot.CompareAndSwap(j.slot.Load(), uint32(c.currentSlot))
|
||||
c.slots[c.currentSlot] = append(c.slots[c.currentSlot], j)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user