Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement systemd-notify protocol #21140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/systemd/gitea.service
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ After=network.target
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
RestartSec=2s
Type=simple
Type=notify
User=git
Group=git
WorkingDirectory=/var/lib/gitea/
Expand Down
50 changes: 50 additions & 0 deletions modules/graceful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,54 @@ func (srv *Server) ListenAndServeTLSConfig(tlsConfig *tls.Config, serve ServeFun
return srv.Serve(serve)
}

type NotifyMsg int8

const {
ReadyMsg NotifyMsg = iota
StoppingMsg
}

func (msg *NotifyMsg) Bytes() []byte {
switch msg {
case ReadyMsg:
return []byte("READY=1")
case StoppingMsg:
return []byte("STOPPING=1")
}
}
Comment on lines +155 to +169
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, only the bytes are needed in your implementation.
So, why not skip the ints and make them strings/ byte arrays immediately?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to pretend that golang has abstract data types but I probably should just give up and use byte strings.


Copy link
Member

@delvh delvh Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func SystemdNotificationImpossible(action string, err error) {
log.Warn("Failed to %s NOTIFY_SOCKET (%v). Systemd might show some weird states: %v", action, os.Getenv("NOTIFY_SOCKET"), err)
}

// systemd notify protocol
Copy link
Member

@delvh delvh Sep 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// systemd notify protocol
// Notify notifies systemd (if present) about the given startup status of Gitea

func (srv *Server) Notify(msg NotifyMsg) {
notifySocket := Os.Getenv("NOTIFY_SOCKET")
if notifySocket == "" {
log.Debug("no NOTIFY_SOCKET given")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Debug("no NOTIFY_SOCKET given")
log.Debug("no NOTIFY_SOCKET given, Gitea is probably not deployed using systemd")

return
}

socketAddr := &net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
}

err := os.unsetenv("NOTIFY_SOCKET")
if err != nil {
log.Warn("failed to unsetenv NOTIFY_SOCKET: %v", err)
return
}

conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
log.Warn("failed to dial NOTIFY_SOCKET: %v", err)
return
}
defer conn.close()

if _, err = conn.Write(msg.Bytes()) {
log.Warn("failed to notify NOTIFY_SOCKET: %v", err)
Comment on lines +186 to +198
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Warn("failed to unsetenv NOTIFY_SOCKET: %v", err)
return
}
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
log.Warn("failed to dial NOTIFY_SOCKET: %v", err)
return
}
defer conn.close()
if _, err = conn.Write(msg.Bytes()) {
log.Warn("failed to notify NOTIFY_SOCKET: %v", err)
SystemdNotificationImpossible("unsetenv", err)
return
}
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
SystemdNotificationImpossible("dial", err)
return
}
defer conn.close()
if _, err = conn.Write(msg.Bytes()) {
SystemdNotificationImpossible("notify", err)

return
}
}

// Serve accepts incoming HTTP connections on the wrapped listener l, creating a new
// service goroutine for each. The service goroutines read requests and then call
// handler to reply to them. Handler is typically nil, in which case the
Expand All @@ -164,7 +212,9 @@ func (srv *Server) Serve(serve ServeFunction) error {
defer log.Debug("Serve() returning... (PID: %d)", syscall.Getpid())
srv.setState(stateRunning)
GetManager().RegisterServer()
Notify(ReadyMsg)
err := serve(srv.listener)
Notify(StoppingMsg)
log.Debug("Waiting for connections to finish... (PID: %d)", syscall.Getpid())
srv.wg.Wait()
srv.setState(stateTerminate)
Expand Down