Skip to content
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

Perform health check of MQTT on startup #305

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (c *Config) ConfigureProducers(airbrakeHandler *airbrake.Handler, logger *l

if _, ok := requiredDispatchers[telemetry.MQTT]; ok {
if c.MQTT == nil {
return nil, nil, errors.New("Expected MQTT to be configured")
return nil, nil, errors.New("expected MQTT to be configured")
}
mqttProducer, err := mqtt.NewProducer(context.Background(), c.MQTT, c.MetricCollector, c.Namespace, airbrakeHandler, c.AckChan, reliableAckSources[telemetry.MQTT], logger)
if err != nil {
Expand Down Expand Up @@ -352,6 +352,11 @@ func (c *Config) ConfigureProducers(airbrakeHandler *airbrake.Handler, logger *l
return nil, nil, err
}
}
if !test && producers[telemetry.MQTT] != nil {
if err := producers[telemetry.MQTT].(*mqtt.Producer).Connect(); err != nil {
return nil, nil, err
}
}

return producers, dispatchProducerRules, nil
}
Expand Down
18 changes: 17 additions & 1 deletion datastore/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mqtt

import (
"context"
"fmt"
"sync"
"time"

Expand All @@ -14,6 +15,10 @@ import (
"github.com/teslamotors/fleet-telemetry/telemetry"
)

var (
defaultTimeout = 5 * time.Second
)

// Producer is a telemetry.Producer that sends records to an MQTT broker.
type Producer struct {
client pahomqtt.Client
Expand Down Expand Up @@ -105,7 +110,6 @@ func NewProducer(ctx context.Context, config *Config, metrics metrics.MetricColl
SetKeepAlive(time.Duration(config.KeepAlive) * time.Second)

client := PahoNewClient(opts)
client.Connect()

return &Producer{
client: client,
Expand All @@ -119,6 +123,18 @@ func NewProducer(ctx context.Context, config *Config, metrics metrics.MetricColl
}, nil
}

// Connect performs health check and returns error if connection is not established
func (p *Producer) Connect() error {
token := p.client.Connect()
if !token.WaitTimeout(defaultTimeout) {
return fmt.Errorf("connection attempt timed out after %v", defaultTimeout)
}
if err := token.Error(); err != nil {
return err
}
return nil
}

// Produce sends a record to the MQTT broker.
func (p *Producer) Produce(rec *telemetry.Record) {
if p.ctx.Err() != nil {
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3'
services:
app:
build:
Expand Down
Loading