Skip to content

Commit

Permalink
internal/worker/client.go: refactor reading worker ID
Browse files Browse the repository at this point in the history
Adds a helper function to the worker client instead of
redeclaring the same inline function.
  • Loading branch information
schuellerf committed Sep 6, 2024
1 parent a6b87b5 commit bb53f48
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions internal/worker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,26 @@ func (c *Client) registerWorker() error {
return nil
}

func (c *Client) getWorkerID() uuid.UUID {
c.workerIDMu.RLock()
defer c.workerIDMu.RUnlock()
return c.workerID
}

func (c *Client) workerHeartbeat() {
//nolint:staticcheck // avoid SA1015, this is an endless function
for range time.Tick(time.Minute * 1) {
workerID := func() uuid.UUID {
c.workerIDMu.RLock()
defer c.workerIDMu.RUnlock()
return c.workerID
}
workerID := c.getWorkerID()

if workerID() == uuid.Nil {
if workerID == uuid.Nil {
err := c.registerWorker()
if err != nil {
logrus.Errorf("Error registering worker, %v", err)
continue
}
}

url, err := c.serverURL.Parse(fmt.Sprintf("workers/%s/status", workerID()))
url, err := c.serverURL.Parse(fmt.Sprintf("workers/%s/status", workerID))
if err != nil {
logrus.Errorf("Error parsing worker status: %v", err)
continue
Expand Down Expand Up @@ -344,12 +346,7 @@ func (c *Client) RequestJob(types []string, arch string) (Job, error) {
Arch: arch,
}

workerID := func() uuid.UUID {
c.workerIDMu.RLock()
defer c.workerIDMu.RUnlock()
return c.workerID
}()

workerID := c.getWorkerID()
if workerID != uuid.Nil {
reqBody.WorkerId = common.ToPtr(workerID.String())
}
Expand Down

0 comments on commit bb53f48

Please sign in to comment.