Skip to content

Commit c32b07e

Browse files
committed
This PR lets an agent or system workflow to run to allow signalling of updates
1 parent 71afe04 commit c32b07e

13 files changed

Lines changed: 749 additions & 239 deletions

internal/common/client.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@ package common
22

33
import (
44
"crypto/sha256"
5+
"os"
6+
"strings"
57

68
"github.com/denisbrodbeck/machineid"
79
"github.com/google/uuid"
810
)
911

12+
const clientIdentifierEnvVar = "THAND_AGENT_ID"
13+
1014
// GetClientIdentifier returns a UUID that uniquely identifies this system.
1115
// It uses the machine's hardware ID to generate a consistent, system-specific UUID.
1216
func GetClientIdentifier() uuid.UUID {
17+
// If set, this env var overrides the default machine-derived identifier.
18+
//
19+
// Accepts:
20+
// - a UUID string (returned as-is)
21+
// - any non-empty string (hashed deterministically to a UUID)
22+
if value := strings.TrimSpace(os.Getenv(clientIdentifierEnvVar)); value != "" {
23+
if parsed, err := uuid.Parse(value); err == nil {
24+
return parsed
25+
}
26+
27+
hash := sha256.Sum256([]byte(value))
28+
return uuid.UUID(hash[:16])
29+
}
1330

1431
// TODO(hugh): Check if the thand.io config exists and use that for an identifier.
1532

internal/config/services/client.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"sync"
66

77
"github.com/sirupsen/logrus"
8+
"github.com/thand-io/agent/internal/common"
89
"github.com/thand-io/agent/internal/config/services/temporal"
910
"github.com/thand-io/agent/internal/models"
10-
"github.com/thand-io/agent/internal/sessions"
1111
)
1212

1313
type localClient struct {
@@ -371,45 +371,18 @@ func (e *localClient) ReloadTemporal() error {
371371
e.mu.Unlock()
372372
}
373373

374-
// Client mode NEVER starts Temporal
375-
if e.config.IsClient() {
376-
logrus.Info("Skipping Temporal initialization in client mode")
377-
return nil
378-
}
379-
380374
logrus.Infof("Initializing temporal...")
381375

382-
// Determine identities based on mode
383-
environment := e.config.GetEnvironment()
384-
identities := []string{environment.GetIdentifier()}
385-
386-
if e.config.IsAgent() {
387-
// Agent mode: query session manager for all active identities + hostname
388-
sessionMgr := sessions.GetSessionManager()
389-
loginServerName := e.config.GetLoginServerHostname()
390-
391-
loginServer, err := sessionMgr.GetLoginServer(loginServerName)
392-
393-
if err != nil {
394-
logrus.WithError(err).Warn("Failed to get login server, using hostname identity only")
395-
} else {
396-
activeSessions := loginServer.GetSessions()
397-
398-
// Add active session providers as identities
399-
for providerName, session := range activeSessions {
400-
if !session.IsExpired() {
401-
identities = append(identities, providerName)
402-
logrus.WithFields(logrus.Fields{
403-
"provider": providerName,
404-
"expiry": session.Expiry,
405-
}).Debug("Adding active session identity to worker pool")
406-
}
407-
}
408-
}
409-
410-
logrus.WithField("identities", identities).Info("Configuring Temporal workers for agent mode")
376+
// Determine task queue based on mode:
377+
// - Server: shared default task queue
378+
// - Agent / Client: per-client task queue derived from the client identifier
379+
taskQueue := temporal.DefaultTaskQueue
380+
if e.config.IsAgent() || e.config.IsClient() {
381+
taskQueue = common.GetClientIdentifier().String()
411382
}
412383

384+
logrus.WithField("taskQueue", taskQueue).Info("Configuring Temporal worker")
385+
413386
// Get Temporal config from services
414387
servicesConfig := e.config.GetServicesConfig()
415388

@@ -422,7 +395,7 @@ func (e *localClient) ReloadTemporal() error {
422395
temporalService := temporal.NewTemporalClient(
423396
temporalConfig,
424397
e.vault,
425-
identities...,
398+
taskQueue,
426399
)
427400
if err := temporalService.Initialize(); err != nil {
428401
logrus.Errorf("Error initializing temporal: %v", err)

internal/config/services/temporal/auth_apikey_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func TestHasAPIKeyAuth(t *testing.T) {
9595
temporalClient := NewTemporalClient(
9696
config,
9797
nil,
98+
"test-identity",
9899
)
99100

100101
got := temporalClient.hasAPIKeyAuth()

internal/config/services/temporal/auth_mtls_file_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func TestHasMTLSFile(t *testing.T) {
145145
temporalClient := NewTemporalClient(
146146
config,
147147
nil,
148+
"test-identity",
148149
)
149150

150151
got := temporalClient.hasMTLSFile()

internal/config/services/temporal/auth_mtls_inline_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func TestHasMTLSInline(t *testing.T) {
129129
temporalClient := NewTemporalClient(
130130
config,
131131
nil,
132+
"test-identity",
132133
)
133134

134135
got := temporalClient.hasMTLSInline()

internal/config/services/temporal/auth_mtls_vault_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ func TestHasMTLSVault(t *testing.T) {
341341
temporalClient := NewTemporalClient(
342342
config,
343343
nil,
344+
"test-identity",
344345
)
345346

346347
got := temporalClient.hasMTLSVault()

0 commit comments

Comments
 (0)