jobqueue: add channel to workers

Stores the channel alongside the worker.
This commit is contained in:
Sanne Raymaekers 2024-04-19 11:52:21 +02:00
parent ede798ae6c
commit 1b4935c325
9 changed files with 46 additions and 26 deletions

View file

@ -62,6 +62,7 @@ type fsJobQueue struct {
}
type worker struct {
Channel string `json:"channel"`
Arch string `json:"arch"`
Heartbeat time.Time `json:"heartbeat"`
Tokens map[uuid.UUID]struct{}
@ -467,12 +468,13 @@ func (q *fsJobQueue) RefreshHeartbeat(token uuid.UUID) {
}
}
func (q *fsJobQueue) InsertWorker(arch string) (uuid.UUID, error) {
func (q *fsJobQueue) InsertWorker(channel, arch string) (uuid.UUID, error) {
q.mu.Lock()
defer q.mu.Unlock()
wID := uuid.New()
q.workers[wID] = worker{
Channel: channel,
Arch: arch,
Heartbeat: time.Now(),
Tokens: make(map[uuid.UUID]struct{}),
@ -502,8 +504,9 @@ func (q *fsJobQueue) Workers(olderThan time.Duration) ([]jobqueue.Worker, error)
for wID, w := range q.workers {
if now.Sub(w.Heartbeat) > olderThan {
workers = append(workers, jobqueue.Worker{
ID: wID,
Arch: w.Arch,
ID: wID,
Channel: w.Channel,
Arch: w.Arch,
})
}
}

View file

@ -704,22 +704,23 @@ func test100dequeuers(t *testing.T, q jobqueue.JobQueue) {
// Registers workers and runs jobs against them
func testWorkers(t *testing.T, q jobqueue.JobQueue) {
one := pushTestJob(t, q, "octopus", nil, nil, "")
one := pushTestJob(t, q, "octopus", nil, nil, "chan")
w1, err := q.InsertWorker("x86_64")
w1, err := q.InsertWorker("chan", "x86_64")
require.NoError(t, err)
w2, err := q.InsertWorker("aarch64")
w2, err := q.InsertWorker("chan", "aarch64")
require.NoError(t, err)
workers, err := q.Workers(0)
require.NoError(t, err)
require.Len(t, workers, 2)
require.Equal(t, "chan", workers[0].Channel)
workers, err = q.Workers(time.Hour * 24)
require.NoError(t, err)
require.Len(t, workers, 0)
_, _, _, _, _, err = q.Dequeue(context.Background(), w1, []string{"octopus"}, []string{""})
_, _, _, _, _, err = q.Dequeue(context.Background(), w1, []string{"octopus"}, []string{"chan"})
require.NoError(t, err)
err = q.DeleteWorker(w1)