wrap job in job slot

This commit is contained in:
rmanach 2024-09-24 10:44:33 +02:00
parent 052958eb7e
commit 4da553d156
2 changed files with 35 additions and 17 deletions

View File

@ -71,7 +71,7 @@ type Job struct {
chAbort chan struct{} chAbort chan struct{}
} }
func NewJob(task FnJob, row, col int) Job { func NewJob(task FnJob) Job {
return Job{ return Job{
id: uuid.New(), id: uuid.New(),
createdAt: time.Now().UTC(), createdAt: time.Now().UTC(),

View File

@ -21,9 +21,27 @@ const (
const MaxSlotsIdx = 59 const MaxSlotsIdx = 59
type JobSlot struct { type Priority int
const (
Low Priority = iota
Medium
High
)
type jobSlot struct {
*job.Job *job.Job
row int row int
col int
}
func newJobSlot(task job.FnJob, row, col int) jobSlot {
j := job.NewJob(task)
return jobSlot{
Job: &j,
row: row,
col: col,
}
} }
// SchedulerCycle is a dumb scheduler. // SchedulerCycle is a dumb scheduler.
@ -44,10 +62,10 @@ type SchedulerCycle struct {
interval time.Duration interval time.Duration
currentSlot int currentSlot int
slots [60][]*job.Job slots [60][]*jobSlot
jobs map[uuid.UUID]*job.Job jobs map[uuid.UUID]*jobSlot
chJobs chan *JobSlot chJobs chan *jobSlot
} }
func NewSchedulerCycle(ctx context.Context, interval time.Duration) *SchedulerCycle { func NewSchedulerCycle(ctx context.Context, interval time.Duration) *SchedulerCycle {
@ -59,9 +77,9 @@ func NewSchedulerCycle(ctx context.Context, interval time.Duration) *SchedulerCy
fnCancel: fnCancel, fnCancel: fnCancel,
interval: interval, interval: interval,
currentSlot: 0, currentSlot: 0,
slots: [60][]*job.Job{}, slots: [60][]*jobSlot{},
jobs: make(map[uuid.UUID]*job.Job), jobs: make(map[uuid.UUID]*jobSlot),
chJobs: make(chan *JobSlot), chJobs: make(chan *jobSlot),
} }
c.run() c.run()
@ -125,7 +143,7 @@ func (c *SchedulerCycle) Delay(fnJob job.FnJob) uuid.UUID {
nextSlot = 0 nextSlot = 0
} }
j := job.NewJob(fnJob, nextSlot, len(c.slots[nextSlot])) j := newJobSlot(fnJob, nextSlot, len(c.slots[nextSlot]))
c.slots[nextSlot] = append(c.slots[nextSlot], &j) c.slots[nextSlot] = append(c.slots[nextSlot], &j)
c.jobs[j.GetID()] = &j c.jobs[j.GetID()] = &j
@ -241,7 +259,7 @@ func (c *SchedulerCycle) display() { //nolint:gocyclo // not complex
fmt.Println(tableFormat) fmt.Println(tableFormat)
} }
func (c *SchedulerCycle) getJob(id uuid.UUID) *job.Job { func (c *SchedulerCycle) getJob(id uuid.UUID) *jobSlot {
c.l.RLock() c.l.RLock()
defer c.l.RUnlock() defer c.l.RUnlock()
@ -255,19 +273,19 @@ func (c *SchedulerCycle) getJob(id uuid.UUID) *job.Job {
// getCurrentSlotJobs collects all the current slot jobs // getCurrentSlotJobs collects all the current slot jobs
// and clean the slot. // and clean the slot.
func (c *SchedulerCycle) getCurrentSlotJobs() (int, []*job.Job) { func (c *SchedulerCycle) getCurrentSlotJobs() (int, []*jobSlot) {
c.l.Lock() c.l.Lock()
defer c.l.Unlock() defer c.l.Unlock()
jobs := c.slots[c.currentSlot] jobs := c.slots[c.currentSlot]
c.slots[c.currentSlot] = []*job.Job{} c.slots[c.currentSlot] = []*jobSlot{}
return c.currentSlot, jobs return c.currentSlot, jobs
} }
// updateSlot add a job to the slot where it was before. // updateSlot add a job to the slot where it was before.
func (c *SchedulerCycle) updateSlot(row int, j *job.Job) { func (c *SchedulerCycle) updateSlot(row int, j *jobSlot) {
c.l.Lock() c.l.Lock()
defer c.l.Unlock() defer c.l.Unlock()
@ -275,7 +293,7 @@ func (c *SchedulerCycle) updateSlot(row int, j *job.Job) {
} }
// updateCurrentSlot add a job to the current slot. // updateCurrentSlot add a job to the current slot.
func (c *SchedulerCycle) updateCurrentSlot(j *job.Job) { func (c *SchedulerCycle) updateCurrentSlot(j *jobSlot) {
c.l.Lock() c.l.Lock()
defer c.l.Unlock() defer c.l.Unlock()
@ -309,7 +327,7 @@ func (c *SchedulerCycle) dispatch() {
} }
select { select {
case c.chJobs <- &JobSlot{row: row, Job: j}: case c.chJobs <- j:
default: default:
log.Warn().Msg("unable to put job in workers, trying next cycle") log.Warn().Msg("unable to put job in workers, trying next cycle")
c.updateSlot(row, j) c.updateSlot(row, j)
@ -333,7 +351,7 @@ func (c *SchedulerCycle) workers() {
for { for {
select { select {
case j := <-c.chJobs: case j := <-c.chJobs:
c.executeJob(j.Job, c.updateCurrentSlot) c.executeJob(j, c.updateCurrentSlot)
case <-c.ctx.Done(): case <-c.ctx.Done():
log.Error().Msg("context done, worker is stopping...") log.Error().Msg("context done, worker is stopping...")
return return
@ -343,7 +361,7 @@ func (c *SchedulerCycle) workers() {
} }
} }
func (c *SchedulerCycle) executeJob(j *job.Job, fnFallBack func(*job.Job)) { func (c *SchedulerCycle) executeJob(j *jobSlot, fnFallBack func(*jobSlot)) {
j.Run(c.ctx) j.Run(c.ctx)
if j.GetState() == job.Pending { if j.GetState() == job.Pending {
fnFallBack(j) fnFallBack(j)