mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 13:23:03 +01:00
* refactor: rename providers to Provider * refactor folders * fix build cmd * fix build cmd * fix build cmd * fix cmd start
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package dockerswarm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/docker/docker/api/types/events"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"io"
|
|
"log/slog"
|
|
)
|
|
|
|
func (p *Provider) NotifyInstanceStopped(ctx context.Context, instance chan<- string) {
|
|
msgs, errs := p.Client.Events(ctx, events.ListOptions{
|
|
Filters: filters.NewArgs(
|
|
filters.Arg("scope", "swarm"),
|
|
filters.Arg("type", "service"),
|
|
),
|
|
})
|
|
|
|
go func() {
|
|
for {
|
|
select {
|
|
case msg, ok := <-msgs:
|
|
if !ok {
|
|
p.l.ErrorContext(ctx, "event stream closed")
|
|
return
|
|
}
|
|
if msg.Actor.Attributes["replicas.new"] == "0" {
|
|
instance <- msg.Actor.Attributes["name"]
|
|
} else if msg.Action == "remove" {
|
|
instance <- msg.Actor.Attributes["name"]
|
|
}
|
|
case err, ok := <-errs:
|
|
if !ok {
|
|
p.l.ErrorContext(ctx, "event stream closed")
|
|
return
|
|
}
|
|
if errors.Is(err, io.EOF) {
|
|
p.l.ErrorContext(ctx, "event stream closed")
|
|
return
|
|
}
|
|
p.l.ErrorContext(ctx, "event stream error", slog.Any("error", err))
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|