43 lines
994 B
Go
43 lines
994 B
Go
|
package docker
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"time"
|
||
|
|
||
|
"github.com/Neur0toxine/sshpoke/internal/config"
|
||
|
"github.com/Neur0toxine/sshpoke/internal/logger"
|
||
|
"github.com/docker/docker/api/types"
|
||
|
"github.com/docker/docker/api/types/events"
|
||
|
"github.com/docker/docker/client"
|
||
|
)
|
||
|
|
||
|
func ListenToEvents(ctx context.Context) (chan events.Message, error) {
|
||
|
cli, err := client.NewClientWithOpts(config.DefaultConfig.Docker.Opts)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
output := make(chan events.Message)
|
||
|
go func() {
|
||
|
for {
|
||
|
eventSource, errSource := cli.Events(ctx, types.EventsOptions{})
|
||
|
select {
|
||
|
case event := <-eventSource:
|
||
|
logger.Sugar.Debugf("docker event: %#v", event)
|
||
|
output <- event
|
||
|
case err := <-errSource:
|
||
|
if errors.Is(err, context.Canceled) {
|
||
|
logger.Sugar.Debug("stopping docker event listener...")
|
||
|
return
|
||
|
}
|
||
|
logger.Sugar.Errorf("docker error, reconnect in 1s: %s", err)
|
||
|
time.Sleep(time.Second)
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return output, nil
|
||
|
}
|