add Stop function to module

This commit is contained in:
Joel Wetzell
2026-02-07 09:53:38 -06:00
parent 8f5091cf9b
commit 33ecc94097
19 changed files with 156 additions and 18 deletions

View File

@@ -19,6 +19,7 @@ type MQTTClient struct {
Topic string
client mqtt.Client
logger *slog.Logger
cancel context.CancelFunc
}
func init() {
@@ -82,7 +83,9 @@ func (mc *MQTTClient) Run(ctx context.Context) error {
return errors.New("mqtt.client unable to get router from context")
}
mc.router = router
mc.ctx = ctx
moduleContext, cancel := context.WithCancel(ctx)
mc.ctx = moduleContext
mc.cancel = cancel
opts := mqtt.NewClientOptions()
opts.AddBroker(mc.Broker)
@@ -98,6 +101,7 @@ func (mc *MQTTClient) Run(ctx context.Context) error {
}
mc.client = mqtt.NewClient(opts)
defer mc.client.Disconnect(250)
token := mc.client.Connect()
@@ -133,3 +137,7 @@ func (mc *MQTTClient) Output(ctx context.Context, payload any) error {
return token.Error()
}
func (mc *MQTTClient) Stop() {
mc.cancel()
}