-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This release adds the following features: - Adds environment variables `DF_SERVICE_POLLING_INTERVAL` and `DF_USE_DOCKER_SERVICE_EVENTS` to configure service polling. Details can be found at the [Configuration documentation](http://swarmlistener.dockerflow.com/config/). - Adds environment variables `DF_NODE_POLLING_INTERVAL` and `DF_USE_DOCKER_NODE_EVENTS` to configure node polling. Details can be found at the [Configuration documentation](http://swarmlistener.dockerflow.com/config/). - More robust handling of events and sending out notifications.
- Loading branch information
1 parent
cf38abd
commit 6d47a88
Showing
11 changed files
with
434 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types/swarm" | ||
) | ||
|
||
// NodePolling provides an interface for polling node changes | ||
type NodePolling interface { | ||
Run(eventChan chan<- Event) | ||
} | ||
|
||
// NodePoller implements `NodePolling` | ||
type NodePoller struct { | ||
Client NodeInspector | ||
Cache NodeCacher | ||
PollingInterval int | ||
MinifyFunc func(swarm.Node) NodeMini | ||
Log *log.Logger | ||
} | ||
|
||
// NewNodePoller creates a new `NodePoller` | ||
func NewNodePoller( | ||
client NodeInspector, | ||
cache NodeCacher, | ||
pollingInterval int, | ||
minifyFunc func(swarm.Node) NodeMini, | ||
log *log.Logger, | ||
) *NodePoller { | ||
return &NodePoller{ | ||
Client: client, | ||
Cache: cache, | ||
PollingInterval: pollingInterval, | ||
MinifyFunc: minifyFunc, | ||
Log: log, | ||
} | ||
} | ||
|
||
// Run starts poller and places events onto `eventChan` | ||
func (n NodePoller) Run(eventChan chan<- Event) { | ||
|
||
if n.PollingInterval <= 0 { | ||
return | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
n.Log.Printf("Polling for Node Changes") | ||
time.Sleep(time.Duration(n.PollingInterval) * time.Second) | ||
|
||
for { | ||
nodes, err := n.Client.NodeList(ctx) | ||
if err != nil { | ||
n.Log.Printf("ERROR (NodePoller): %v", err) | ||
} else { | ||
nowTimeNano := time.Now().UTC().UnixNano() | ||
keys := n.Cache.Keys() | ||
for _, node := range nodes { | ||
delete(keys, node.ID) | ||
|
||
nodeMini := n.MinifyFunc(node) | ||
if n.Cache.IsNewOrUpdated(nodeMini) { | ||
eventChan <- Event{ | ||
Type: EventTypeCreate, | ||
ID: node.ID, | ||
TimeNano: nowTimeNano, | ||
UseCache: true, | ||
} | ||
} | ||
} | ||
|
||
// Remaining key sare removal events | ||
for k := range keys { | ||
eventChan <- Event{ | ||
Type: EventTypeRemove, | ||
ID: k, | ||
TimeNano: nowTimeNano, | ||
UseCache: true, | ||
} | ||
} | ||
} | ||
time.Sleep(time.Duration(n.PollingInterval) * time.Second) | ||
} | ||
} |
Oops, something went wrong.