Browse code

Separate a) initialization of the http api and b) actually serving the api into 2 distinct jobs

Solomon Hykes authored on 2013/10/27 09:19:35
Showing 3 changed files
... ...
@@ -9,7 +9,6 @@ import (
9 9
 type DaemonConfig struct {
10 10
 	Pidfile                     string
11 11
 	Root                        string
12
-	ProtoAddresses              []string
13 12
 	AutoRestart                 bool
14 13
 	EnableCors                  bool
15 14
 	Dns                         []string
... ...
@@ -36,7 +35,6 @@ func ConfigFromJob(job *engine.Job) *DaemonConfig {
36 36
 	} else {
37 37
 		config.BridgeIface = DefaultNetworkBridge
38 38
 	}
39
-	config.ProtoAddresses = job.GetenvList("ProtoAddresses")
40 39
 	config.DefaultIp = net.ParseIP(job.Getenv("DefaultIp"))
41 40
 	config.InterContainerCommunication = job.GetenvBool("InterContainerCommunication")
42 41
 	return &config
... ...
@@ -71,7 +71,8 @@ func main() {
71 71
 		if err != nil {
72 72
 			log.Fatal(err)
73 73
 		}
74
-		job := eng.Job("serveapi")
74
+		// Load plugin: httpapi
75
+		job := eng.Job("initapi")
75 76
 		job.Setenv("Pidfile", *pidfile)
76 77
 		job.Setenv("Root", *flRoot)
77 78
 		job.SetenvBool("AutoRestart", *flAutoRestart)
... ...
@@ -79,12 +80,15 @@ func main() {
79 79
 		job.Setenv("Dns", *flDns)
80 80
 		job.SetenvBool("EnableIptables", *flEnableIptables)
81 81
 		job.Setenv("BridgeIface", *bridgeName)
82
-		job.SetenvList("ProtoAddresses", flHosts)
83 82
 		job.Setenv("DefaultIp", *flDefaultIp)
84 83
 		job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
85 84
 		if err := job.Run(); err != nil {
86 85
 			log.Fatal(err)
87 86
 		}
87
+		// Serve api
88
+		if err := eng.Job("serveapi", flHosts...).Run(); err != nil {
89
+			log.Fatal(err)
90
+		}
88 91
 	} else {
89 92
 		if len(flHosts) > 1 {
90 93
 			log.Fatal("Please specify only one -H")
... ...
@@ -33,30 +33,20 @@ func (srv *Server) Close() error {
33 33
 }
34 34
 
35 35
 func init() {
36
-	engine.Register("serveapi", JobServeApi)
36
+	engine.Register("initapi", jobInitApi)
37 37
 }
38 38
 
39
-func JobServeApi(job *engine.Job) string {
39
+// jobInitApi runs the remote api server `srv` as a daemon,
40
+// Only one api server can run at the same time - this is enforced by a pidfile.
41
+// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup.
42
+func jobInitApi(job *engine.Job) string {
40 43
 	srv, err := NewServer(ConfigFromJob(job))
41 44
 	if err != nil {
42 45
 		return err.Error()
43 46
 	}
44
-	defer srv.Close()
45
-	if err := srv.Daemon(); err != nil {
46
-		return err.Error()
47
-	}
48
-	return "0"
49
-}
50
-
51
-// Daemon runs the remote api server `srv` as a daemon,
52
-// Only one api server can run at the same time - this is enforced by a pidfile.
53
-// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup.
54
-func (srv *Server) Daemon() error {
55 47
 	if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil {
56 48
 		log.Fatal(err)
57 49
 	}
58
-	defer utils.RemovePidFile(srv.runtime.config.Pidfile)
59
-
60 50
 	c := make(chan os.Signal, 1)
61 51
 	signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
62 52
 	go func() {
... ...
@@ -66,8 +56,17 @@ func (srv *Server) Daemon() error {
66 66
 		srv.Close()
67 67
 		os.Exit(0)
68 68
 	}()
69
+	err = engine.Register("serveapi", func(job *engine.Job) string {
70
+		return srv.ListenAndServe(job.Args...).Error()
71
+	})
72
+	if err != nil {
73
+		return err.Error()
74
+	}
75
+	return "0"
76
+}
77
+
69 78
 
70
-	protoAddrs := srv.runtime.config.ProtoAddresses
79
+func (srv *Server) ListenAndServe(protoAddrs ...string) error {
71 80
 	chErrors := make(chan error, len(protoAddrs))
72 81
 	for _, protoAddr := range protoAddrs {
73 82
 		protoAddrParts := strings.SplitN(protoAddr, "://", 2)