Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
fc6afe2a
Commit
fc6afe2a
authored
Nov 10, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #333 from mholt/firststartup
startup: Only run commands at first startup
parents
51d2ff4e
5cced604
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
5 deletions
+49
-5
caddy/caddy.go
caddy/caddy.go
+17
-0
caddy/setup/startupshutdown.go
caddy/setup/startupshutdown.go
+1
-1
caddy/setup/startupshutdown_test.go
caddy/setup/startupshutdown_test.go
+1
-1
server/config.go
server/config.go
+13
-3
server/server.go
server/server.go
+17
-0
No files found.
caddy/caddy.go
View file @
fc6afe2a
...
...
@@ -71,6 +71,10 @@ var (
// index in the list of inherited file descriptors. This
// variable is not safe for concurrent access.
loadedGob
caddyfileGob
// startedBefore should be set to true if caddy has been
// started at least once.
startedBefore
bool
)
const
(
...
...
@@ -128,6 +132,7 @@ func Start(cdyfile Input) (err error) {
if
err
!=
nil
{
return
err
}
startedBefore
=
true
// Close remaining file descriptors we may have inherited that we don't need
if
IsRestart
()
{
...
...
@@ -203,6 +208,18 @@ func startServers(groupings bindingGroup) error {
wg
.
Add
(
1
)
go
func
(
s
*
server
.
Server
,
ln
server
.
ListenerFile
)
{
defer
wg
.
Done
()
// run startup functions that should only execute when
// the original parent process is starting.
if
!
IsRestart
()
&&
!
startedBefore
{
err
:=
s
.
RunFirstStartupFuncs
()
if
err
!=
nil
{
errChan
<-
err
return
}
}
// start the server
if
ln
!=
nil
{
errChan
<-
s
.
Serve
(
ln
)
}
else
{
...
...
caddy/setup/startupshutdown.go
View file @
fc6afe2a
...
...
@@ -10,7 +10,7 @@ import (
// Startup registers a startup callback to execute during server start.
func
Startup
(
c
*
Controller
)
(
middleware
.
Middleware
,
error
)
{
return
nil
,
registerCallback
(
c
,
&
c
.
Startup
)
return
nil
,
registerCallback
(
c
,
&
c
.
First
Startup
)
}
// Shutdown registers a shutdown callback to execute during process exit.
...
...
caddy/setup/startupshutdown_test.go
View file @
fc6afe2a
...
...
@@ -45,7 +45,7 @@ func TestStartup(t *testing.T) {
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, got: %v"
,
err
)
}
err
=
c
.
Startup
[
0
]()
err
=
c
.
First
Startup
[
0
]()
if
err
!=
nil
&&
!
test
.
shouldExecutionErr
{
t
.
Errorf
(
"Test %d recieved an error of:
\n
%v"
,
i
,
err
)
}
...
...
server/config.go
View file @
fc6afe2a
...
...
@@ -26,11 +26,21 @@ type Config struct {
// Middleware stack; map of path scope to middleware -- TODO: Support path scope?
Middleware
map
[
string
][]
middleware
.
Middleware
// Functions (or methods) to execute at server start; these
// are executed before any parts of the server are configured,
// and the functions are blocking
// Startup is a list of functions (or methods) to execute at
// server startup and restart; these are executed before any
// parts of the server are configured, and the functions are
// blocking. These are good for setting up middlewares and
// starting goroutines.
Startup
[]
func
()
error
// FirstStartup is like Startup but these functions only execute
// during the initial startup, not on subsequent restarts.
//
// (Note: The server does not ever run these on its own; it is up
// to the calling application to do so, and do so only once, as the
// server itself has no notion whether it's a restart or not.)
FirstStartup
[]
func
()
error
// Functions (or methods) to execute when the server quits;
// these are executed in response to SIGINT and are blocking
Shutdown
[]
func
()
error
...
...
server/server.go
View file @
fc6afe2a
...
...
@@ -371,6 +371,23 @@ func setupClientAuth(tlsConfigs []TLSConfig, config *tls.Config) error {
return
nil
}
// RunFirstStartupFuncs runs all of the server's FirstStartup
// callback functions unless one of them returns an error first.
// It is up the caller's responsibility to call this only once and
// at the correct time. The functions here should not be executed
// at restarts or where the user does not explicitly start a new
// instance of the server.
func
(
s
*
Server
)
RunFirstStartupFuncs
()
error
{
for
_
,
vh
:=
range
s
.
vhosts
{
for
_
,
f
:=
range
vh
.
config
.
FirstStartup
{
if
err
:=
f
();
err
!=
nil
{
return
err
}
}
}
return
nil
}
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment