/
events.go
79 lines (65 loc) · 1.86 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package docker
import (
"context"
d "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
"github.com/lastbackend/lastbackend/pkg/distribution/types"
"github.com/lastbackend/lastbackend/pkg/log"
)
func (r *Runtime) Subscribe(ctx context.Context) (chan *types.Image, error) {
log.V(logLevel).Debug("Create new event listener subscribe")
var cs = make(chan *types.Image)
go func() {
if _, err := r.client.Ping(ctx); err != nil {
log.Errorf("Can not ping docker client")
return
}
es, errr := r.client.Events(ctx, d.EventsOptions{})
for {
select {
case e := <-es:
if e.Type != events.ImageEventType {
continue
}
log.V(logLevel).Debugf("Image %s", e.ID)
if e.Action == types.StateDestroy {
c := new(types.Image)
c.Meta.ID = e.ID
c.Status.State = types.StateDestroyed
cs <- c
break
}
c, err := r.Inspect(ctx, e.ID)
if err != nil {
log.Errorf("Container inspect err: %s", err.Error())
continue
}
if c == nil {
log.Errorf("Container: container not found")
break
}
break
case err := <-errr:
log.Errorf("Event listening error: %s", err)
}
}
}()
return cs, nil
}