Skip to content

Terminate daemon command when parent process exits; added "--daemonize" flag to keep old behaviour #488

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

Merged
merged 2 commits into from
Nov 20, 2019
Merged
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
42 changes: 28 additions & 14 deletions cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package daemon

import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand All @@ -39,41 +41,53 @@ const (

// NewCommand created a new `daemon` command
func NewCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "daemon",
Short: fmt.Sprintf("Run as a daemon on port %s", port),
Long: "Running as a daemon the initialization of cores and libraries is done only once.",
Example: " " + os.Args[0] + " daemon",
Args: cobra.NoArgs,
Run: runDaemonCommand,
}
cmd.Flags().BoolVar(&daemonize, "daemonize", false, "Do not terminate daemon process if the parent process dies")
return cmd
}

var daemonize bool

func runDaemonCommand(cmd *cobra.Command, args []string) {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()

userAgentValue := fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s", globals.VersionInfo.Application,
globals.VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), globals.VersionInfo.Commit)
headers := http.Header{"User-Agent": []string{userAgentValue}}

// register the commands service
coreServer := daemon.ArduinoCoreServerImpl{
headers := http.Header{"User-Agent": []string{
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
globals.VersionInfo.Application,
globals.VersionInfo.VersionString,
runtime.GOARCH, runtime.GOOS,
runtime.Version(), globals.VersionInfo.Commit)}}
srv_commands.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{
DownloaderHeaders: headers,
VersionString: globals.VersionInfo.VersionString,
Config: globals.Config,
}
srv_commands.RegisterArduinoCoreServer(s, &coreServer)
})

// register the monitors service
srv_monitor.RegisterMonitorServer(s, &daemon.MonitorService{})

if !daemonize {
// When parent process ends terminate also the daemon
go func() {
// stdin is closed when the controlling parent process ends
_, _ = io.Copy(ioutil.Discard, os.Stdin)
os.Exit(0)
}()
}

lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}

fmt.Println("Done serving")
}