AFAIRE
sdk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.7 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021-2022 losyme ################################################# MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"sync"
"time"
"forge.chapril.org/dune/jw"
)
type Worker struct {
Jobs int
Busy bool
JobID string
JobNamespace string
JobType string
StartedAt time.Time
}
type state struct {
workers map[string]*Worker
mutex sync.Mutex
}
func newState() *state {
return &state{
workers: make(map[string]*Worker),
}
}
func (s *state) addJob(wID string, job *jw.Job) int {
s.mutex.Lock()
defer s.mutex.Unlock()
w := s.workers[wID]
w.Jobs++
w.Busy = true
w.JobID = job.ID
w.JobNamespace = job.Namespace
w.JobType = job.Type
w.StartedAt = time.Now()
return w.Jobs
}
func (s *state) removeJob(wID string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.workers[wID].Busy = false
}
func (s *state) addWorker(wID string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.workers[wID] = new(Worker)
}
func (s *state) removeWorker(wID string) {
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.workers, wID)
}
func (s *state) data() map[string]*Worker {
s.mutex.Lock()
defer s.mutex.Unlock()
data := make(map[string]*Worker)
for id, w := range s.workers {
clone := new(Worker)
*clone = *w
data[id] = clone
}
return data
}
/*
######################################################################################################## @(°_°)@ #######
*/