-
Notifications
You must be signed in to change notification settings - Fork 8
feat(messaging): implement NATS messaging integration (topic 7) #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package main | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "net" | ||
| "net/http" | ||
|
|
@@ -19,21 +20,12 @@ import ( | |
| "github.com/dcm-project/environment-agent/internal/health" | ||
| "github.com/dcm-project/environment-agent/internal/health/monitor" | ||
| "github.com/dcm-project/environment-agent/internal/httperror" | ||
| "github.com/dcm-project/environment-agent/internal/messaging" | ||
| "github.com/dcm-project/environment-agent/internal/provider" | ||
| "github.com/dcm-project/environment-agent/internal/provider/service" | ||
| "github.com/dcm-project/environment-agent/internal/provider/store" | ||
| ) | ||
|
|
||
| // TODO: replace with real MessagingStatus from the NATS/messaging subsystem. | ||
| type messagingStatus struct{} | ||
|
|
||
| func (messagingStatus) IsConnected() bool { return true } | ||
|
|
||
| // stubConsumerLagProvider returns 0 until Topic 7 provides real NATS consumer lag. | ||
| type stubConsumerLagProvider struct{} | ||
|
|
||
| func (stubConsumerLagProvider) ConsumerLag() int64 { return 0 } | ||
|
|
||
| // serviceTypeLister adapts ProviderService to dcm.ServiceTypeLister. | ||
| type serviceTypeLister struct { | ||
| providerSvc *service.ProviderService | ||
|
|
@@ -107,27 +99,35 @@ func run(ctx context.Context) int { | |
| } | ||
| providerSvc.RegisterEmbedded(cfg.Provider.EmbeddedSPs) | ||
|
|
||
| // Messaging client — must start before registrar (provides ConsumerLagProvider) | ||
| msgClient, topicMain, err := setupMessaging(cfg, logger) | ||
| if err != nil { | ||
| logger.Error("invalid topic name", "error", err) | ||
| return 1 | ||
| } | ||
| if err := msgClient.Start(ctx); err != nil { | ||
| logger.Error("failed to start messaging client", "error", err) | ||
| return 1 | ||
| } | ||
| defer msgClient.Stop() | ||
|
|
||
| // DCM Registrar — created before monitor starts so callbacks can be wired | ||
| // before any health transitions fire. Deferred after monitor so LIFO shuts | ||
| // registrar down first. | ||
| topicName := cfg.Messaging.TopicName | ||
| if topicName == "" { | ||
| topicName = cfg.Agent.Name | ||
| } | ||
| registrar, err := dcm.NewRegistrar( | ||
| dcm.RegistrarConfig{ | ||
| AgentName: cfg.Agent.Name, | ||
| Environment: cfg.Agent.Environment, | ||
| Cost: cfg.Agent.Cost, | ||
| TopicName: topicName, | ||
| TopicName: topicMain, | ||
| RegistrationURL: cfg.DCM.RegistrationURL, | ||
| InitialBackoff: cfg.DCM.InitialBackoff, | ||
| MaxBackoff: cfg.DCM.MaxBackoff, | ||
| HeartbeatInterval: cfg.Heartbeat.Interval, | ||
| PrerequisiteRetryInterval: cfg.DCM.PrerequisiteRetryInterval, | ||
| }, | ||
| &serviceTypeLister{providerSvc: providerSvc, logger: logger}, | ||
| stubConsumerLagProvider{}, | ||
| msgClient, | ||
| nil, | ||
| logger, | ||
| ) | ||
|
|
@@ -159,7 +159,7 @@ func run(ctx context.Context) int { | |
| <-registrar.Done() | ||
| }() | ||
|
|
||
| healthSvc := health.NewService(messagingStatus{}) | ||
| healthSvc := health.NewService(msgClient) | ||
| strictHandler := handler.New(healthSvc, providerSvc) | ||
| h := oapigen.NewStrictHandlerWithOptions(strictHandler, nil, oapigen.StrictHTTPServerOptions{ | ||
| ResponseErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) { | ||
|
|
@@ -177,3 +177,16 @@ func run(ctx context.Context) int { | |
| logger.Info("Environment Agent stopped") | ||
| return 0 | ||
| } | ||
|
|
||
| func setupMessaging(cfg *config.Config, logger *slog.Logger) (*messaging.Client, string, error) { | ||
| topics := messaging.DeriveTopicNames(cfg.Agent.Name, cfg.Messaging.TopicName) | ||
| if err := messaging.ValidateTopicName(topics.Main); err != nil { | ||
| return nil, "", fmt.Errorf("invalid topic name: %w", err) | ||
| } | ||
|
Comment on lines
+181
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Derived topics unvalidated setupMessaging validates only the base topic (topics.Main), but DeriveTopicNames appends ".retry"/".cancel" which can exceed ValidateTopicName’s 255-character limit. This allows a configuration that passes validation to later fail JetStream stream initialization at runtime. Agent Prompt
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6cf52ed and 8555d06 — added |
||
| client := messaging.NewClient(messaging.ClientConfig{ | ||
| URL: cfg.Messaging.URL, | ||
| TopicName: topics.Main, | ||
| AgentName: cfg.Agent.Name, | ||
| }, logger) | ||
| return client, topics.Main, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.