This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from lucaslorentz/improvements
Improvements
- Loading branch information
Showing
9 changed files
with
111 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,5 @@ echo { | |
args "Hello World {{.Replica}}" | ||
redirect_stdout stdout | ||
restart_policy on_failure | ||
replicas 5 | ||
replicas 3 | ||
} |
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 |
---|---|---|
@@ -1,63 +1,44 @@ | ||
package httpplugin | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/caddyserver/caddy" | ||
"github.com/lucaslorentz/caddy-supervisor/supervisor" | ||
) | ||
|
||
func init() { | ||
caddy.RegisterPlugin("supervisor", caddy.Plugin{ | ||
ServerType: "http", | ||
Action: setup, | ||
Action: setupDirective, | ||
}) | ||
} | ||
|
||
var supervisors []*supervisor.Supervisor | ||
|
||
func setup(c *caddy.Controller) error { | ||
setupEventsOnlyOnce(c) | ||
|
||
func setupDirective(c *caddy.Controller) error { | ||
return c.OncePerServerBlock(func() error { | ||
optionsList, err := parseHTTPDirectives(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, options := range optionsList { | ||
newSupervisors := supervisor.CreateSupervisors(options) | ||
supervisors = append(supervisors, newSupervisors...) | ||
for _, supervisor := range newSupervisors { | ||
go supervisor.Start() | ||
supervisors := supervisor.CreateSupervisors(options) | ||
for _, sup := range supervisors { | ||
func(s *supervisor.Supervisor) { | ||
c.OnStartup(func() error { | ||
go s.Run() | ||
return nil | ||
}) | ||
// Use OnRestart to shutdown supervisors before new instances starts | ||
c.OnRestart(func() error { | ||
s.Stop() | ||
return nil | ||
}) | ||
c.OnFinalShutdown(func() error { | ||
s.Stop() | ||
return nil | ||
}) | ||
}(sup) | ||
} | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
var didSetupEvents = false | ||
|
||
func setupEventsOnlyOnce(c *caddy.Controller) { | ||
if didSetupEvents { | ||
return | ||
} | ||
c.OnShutdown(shutdownExecutions) | ||
didSetupEvents = true | ||
} | ||
|
||
func shutdownExecutions() error { | ||
var wg sync.WaitGroup | ||
|
||
for _, s := range supervisors { | ||
wg.Add(1) | ||
go func(s *supervisor.Supervisor) { | ||
defer wg.Done() | ||
s.Stop() | ||
}(s) | ||
} | ||
|
||
wg.Wait() | ||
|
||
return nil | ||
} |
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