Use sdnotify for systemd (#768)

* Use sdnotify for systemd

This is useful if a configuration is long to load.
Systemd will continue dependency chain only when server have finish to start.

https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=

* Extract the waiting behavior from Start()
This commit is contained in:
Guilhem Lettron 2016-10-25 17:59:39 +02:00 committed by Vincent Demeester
parent 870f378782
commit 649cb548d0
5 changed files with 24 additions and 4 deletions

View file

@ -2,6 +2,7 @@
Description=Traefik Description=Traefik
[Service] [Service]
Type=notify
ExecStart=/usr/bin/traefik --configFile=/etc/traefik.toml ExecStart=/usr/bin/traefik --configFile=/etc/traefik.toml
Restart=on-failure Restart=on-failure

10
glide.lock generated
View file

@ -1,5 +1,5 @@
hash: 39ff28cc1d13d5915a870b14491ece1849c4eaf5a56cecd50a7676ecee6c6143 hash: 61a63d2dd37e5a9bf32c83fa520f54458c3fe6b520f89b9615d8b26f5d3f317f
updated: 2016-10-06T14:06:39.455848971+02:00 updated: 2016-10-24T15:09:49.3944218+02:00
imports: imports:
- name: github.com/abbot/go-http-auth - name: github.com/abbot/go-http-auth
version: cb4372376e1e00e9f6ab9ec142e029302c9e7140 version: cb4372376e1e00e9f6ab9ec142e029302c9e7140
@ -31,6 +31,10 @@ imports:
- client - client
- pkg/pathutil - pkg/pathutil
- pkg/types - pkg/types
- name: github.com/coreos/go-systemd
version: 43e4800a6165b4e02bb2a36673c54b230d6f7b26
subpackages:
- daemon
- name: github.com/davecgh/go-spew - name: github.com/davecgh/go-spew
version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9
subpackages: subpackages:
@ -340,7 +344,7 @@ testImports:
- name: github.com/libkermit/docker-check - name: github.com/libkermit/docker-check
version: cbe0ef03b3d23070eac4d00ba8828f2cc7f7e5a3 version: cbe0ef03b3d23070eac4d00ba8828f2cc7f7e5a3
- name: github.com/spf13/pflag - name: github.com/spf13/pflag
version: 08b1a584251b5b62f458943640fc8ebd4d50aaa5 version: 5644820622454e71517561946e3d94b9f9db6842
- name: github.com/vbatts/tar-split - name: github.com/vbatts/tar-split
version: bd4c5d64c3e9297f410025a3b1bd0c58f659e721 version: bd4c5d64c3e9297f410025a3b1bd0c58f659e721
subpackages: subpackages:

View file

@ -102,3 +102,7 @@ import:
- package: github.com/docker/leadership - package: github.com/docker/leadership
- package: github.com/satori/go.uuid - package: github.com/satori/go.uuid
version: ^1.1.0 version: ^1.1.0
- package: github.com/coreos/go-systemd
version: v12
subpackages:
- daemon

View file

@ -91,7 +91,7 @@ func NewServer(globalConfiguration GlobalConfiguration) *Server {
return server return server
} }
// Start starts the server and blocks until server is shutted down. // Start starts the server.
func (server *Server) Start() { func (server *Server) Start() {
server.startHTTPServers() server.startHTTPServers()
server.startLeadership() server.startLeadership()
@ -104,6 +104,10 @@ func (server *Server) Start() {
server.configureProviders() server.configureProviders()
server.startProviders() server.startProviders()
go server.listenSignals() go server.listenSignals()
}
// Wait blocks until server is shutted down.
func (server *Server) Wait() {
<-server.stopChan <-server.stopChan
} }

View file

@ -24,6 +24,8 @@ import (
"github.com/containous/traefik/version" "github.com/containous/traefik/version"
"github.com/docker/libkv/store" "github.com/docker/libkv/store"
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
"github.com/coreos/go-systemd/daemon"
) )
var versionTemplate = `Version: {{.Version}} var versionTemplate = `Version: {{.Version}}
@ -268,6 +270,11 @@ func run(traefikConfiguration *TraefikConfiguration) {
server := NewServer(globalConfiguration) server := NewServer(globalConfiguration)
server.Start() server.Start()
defer server.Close() defer server.Close()
sent, err := daemon.SdNotify("READY=1")
if !sent && err != nil {
log.Error("Fail to notify", err)
}
server.Wait()
log.Info("Shutting down") log.Info("Shutting down")
} }