internal/worker/client.go: refactor reading worker ID
Adds a helper function to the worker client instead of redeclaring the same inline function.
This commit is contained in:
parent
a6b87b58f2
commit
bb53f4833f
1 changed files with 10 additions and 13 deletions
|
|
@ -203,16 +203,18 @@ func (c *Client) registerWorker() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) getWorkerID() uuid.UUID {
|
||||||
|
c.workerIDMu.RLock()
|
||||||
|
defer c.workerIDMu.RUnlock()
|
||||||
|
return c.workerID
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) workerHeartbeat() {
|
func (c *Client) workerHeartbeat() {
|
||||||
//nolint:staticcheck // avoid SA1015, this is an endless function
|
//nolint:staticcheck // avoid SA1015, this is an endless function
|
||||||
for range time.Tick(time.Minute * 1) {
|
for range time.Tick(time.Minute * 1) {
|
||||||
workerID := func() uuid.UUID {
|
workerID := c.getWorkerID()
|
||||||
c.workerIDMu.RLock()
|
|
||||||
defer c.workerIDMu.RUnlock()
|
|
||||||
return c.workerID
|
|
||||||
}
|
|
||||||
|
|
||||||
if workerID() == uuid.Nil {
|
if workerID == uuid.Nil {
|
||||||
err := c.registerWorker()
|
err := c.registerWorker()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("Error registering worker, %v", err)
|
logrus.Errorf("Error registering worker, %v", err)
|
||||||
|
|
@ -220,7 +222,7 @@ func (c *Client) workerHeartbeat() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
logrus.Errorf("Error parsing worker status: %v", err)
|
logrus.Errorf("Error parsing worker status: %v", err)
|
||||||
continue
|
continue
|
||||||
|
|
@ -344,12 +346,7 @@ func (c *Client) RequestJob(types []string, arch string) (Job, error) {
|
||||||
Arch: arch,
|
Arch: arch,
|
||||||
}
|
}
|
||||||
|
|
||||||
workerID := func() uuid.UUID {
|
workerID := c.getWorkerID()
|
||||||
c.workerIDMu.RLock()
|
|
||||||
defer c.workerIDMu.RUnlock()
|
|
||||||
return c.workerID
|
|
||||||
}()
|
|
||||||
|
|
||||||
if workerID != uuid.Nil {
|
if workerID != uuid.Nil {
|
||||||
reqBody.WorkerId = common.ToPtr(workerID.String())
|
reqBody.WorkerId = common.ToPtr(workerID.String())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue