You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/04/09 07:04:22 UTC

[GitHub] [apisix-dashboard] bisakhmondal commented on pull request #1667: feat: run manager-api as a systemd daemon/service

bisakhmondal commented on pull request #1667:
URL: https://github.com/apache/apisix-dashboard/pull/1667#issuecomment-816462583


   Hii @membphis, @nic-chen, I have checked and found these few packages that look good and have cross-platform support. On the way, found a lot of packages that run as a bg process using `Exec()` and are obviously not immune to SIGHUP when its controlling terminal sends it.
   
   1. https://github.com/takama/daemon [supports linux, win, osx] (easy APIs, uses OS-specific service manager, under the hood, no nasty multiple forks)
   2. https://github.com/sevlyar/go-daemon [Doesn't supports windows till now]
   3. https://github.com/kardianos/service [more stars :)]
   
   Let me know which one you like :)
   If you ask me, I'd say going with option 1 is a good choice. It's really simple, and no overhead of keeping the package up-to-date as it's kind of an overlay on top of the os specific system managers. Tested with a gin server
   ```go
   package main
   
   import (
   	"fmt"
   	"github.com/gin-gonic/gin"
   	"github.com/takama/daemon"
   	"net/http"
   	"os"
   	"os/signal"
   	"syscall"
   	"time"
   )
   
   type Service struct {
   	daemon.Daemon
   }
   
   func (service* Service) Manage() (string, error){
   	if len(os.Args) > 1 {
   		command := os.Args[1]
   		switch command {
   		case "install":
   			return service.Install()
   		case "remove":
   			return service.Remove()
   		case "start":
   			return service.Start()
   		case "stop":
   			return service.Stop()
   		case "status":
   			return service.Status()
   		default:
   			return "Usage: myservice install | remove | start | stop | status", nil
   		}
   	}
   
   	r := gin.Default()
   
   	fmt.Println("id: ",os.Getpid())
   	fmt.Println("ppid: ", os.Getppid())
   
   	r.GET("/", func(g *gin.Context) {
   		g.JSON(http.StatusOK, gin.H{
   			"message": "Server running successfully",
   		})
   	})
   
   
   	go r.Run(":8080")
   
   	sig := make(chan os.Signal, 1)
   
   	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
   
   	sis := <- sig
   
   	fmt.Println("signal received: ", sis.String())
   	time.Sleep(2*time.Second)
   	fmt.Println("Shutted down")
   	return "Closed", nil
   }
   func main() {
   	srv, err := daemon.New("my-gin-app", "my gin app", daemon.SystemDaemon)
   
   	if err !=nil{
   		panic(err)
   	}
   	service := &Service{srv}
   	status, err := service.Manage()
   	if err != nil{
   		panic(err)
   	}
   	fmt.Println(status)
   }
   ```
   
   commands
   ```shell
   go build main.go
   sudo ./main install 
   sudo ./main start
   ```
   ![image](https://user-images.githubusercontent.com/41498427/114141510-4da36a80-992f-11eb-9ead-6293446ec2cd.png)
   
    Thanks😄


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org