示例#1
0
func startP2PServer() {
	p2pListener, err := net.Listen("tcp", ":35800")
	if err != nil {
		log.Fatal(err)
	}

	p2psl, err = tcpNew(p2pListener)
	if err != nil {
		log.Fatal(err)
	}

	P2pRouter := httprouter.New()
	P2pRouter.GET("/", sayhello)
	P2pRouter.GET("/pull/:repo/:dataitem/:tag", p2p_pull)
	P2pRouter.GET("/health", p2pHealthyCheckHandler)

	p2pserver := http.Server{Handler: P2pRouter}

	wg.Add(1)
	defer wg.Done()

	log.Info("p2p server start")
	p2pserver.Serve(p2psl)
	log.Info("p2p server stop")

}
示例#2
0
// Prepares and returns command for starting daemonized process.
//
// This function can also be used when writing your own daemon actions.
func StartCommand() (*exec.Cmd, error) {
	const errLoc = "daemonigo.StartCommand()"
	//path, err := filepath.Abs(os.Args[0])
	path, err := osext.Executable()
	if err != nil {
		log.Fatal(err)
	}
	//log.Println("exec path", path, Token)
	if err != nil {
		return nil, fmt.Errorf(
			"%s: failed to resolve absolute path of %s, reason -> %s",
			errLoc, AppName, err.Error(),
		)
	}

	cmd := exec.Command(path, "--daemon")
	if len(Token) > 0 {
		cmd = exec.Command(path, "--daemon", "--token", Token)
	}
	cmd.Env = append(
		os.Environ(), fmt.Sprintf("%s=%s", EnvVarName, EnvVarValue),
	)
	cmd.Stdin = os.Stdin //给新进程设置文件描述符,可以重定向到文件中
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd, nil
}
示例#3
0
func testserver() {
	var tsl = new(StoppabletcpListener)

	Listener, err := net.Listen("tcp", ":35888")
	if err != nil {
		log.Fatal(err)
	}

	tsl, err = tcpNew(Listener)
	if err != nil {
		log.Fatal(err)
	}

	tRouter := httprouter.New()
	tRouter.GET("/", sayhello)

	http.Handle("/", tRouter)

	server := http.Server{Handler: tRouter}

	log.Info("p2p server start")
	server.Serve(tsl)
	log.Info("p2p server stop")
}
示例#4
0
func RunDaemon() {
	//fmt.Println("Run daemon..")
	// Daemonizing echo server application.

	switch isDaemon, err := daemonigo.Daemonize(); {
	case !isDaemon:
		return
	case err != nil:
		log.Fatal("main(): could not start daemon, reason -> %s", err.Error())
	}
	//fmt.Printf("server := http.Server{}\n")

	if false == isDirExists(g_strDpPath) {
		err := os.MkdirAll(g_strDpPath, 0755)
		if err != nil {
			log.Printf("mkdir %s error! %v ", g_strDpPath, err)
		}

	}

	DaemonAuthrization = utils.Getguid()
	log.Println("DaemonAuthrization", DaemonAuthrization)

	dbinit()

	if len(DaemonID) == 40 {
		log.Println("daemonid", DaemonID)
		saveDaemonID(DaemonID)
	} else {
		log.Println("get daemonid from db")
		DaemonID = getDaemonid()
	}

	LoadJobFromDB()

	os.Chdir(g_strDpPath)
	originalListener, err := net.Listen("tcp", DaemonCliServer)
	if err != nil {
		log.Fatal(err)
	} //else {
	// 	if err = os.Chmod(cmd.UnixSock, os.ModePerm); err != nil {
	// 		l := log.Error(err)
	// 		logq.LogPutqueue(l)
	// 	}
	// }

	sl, err = tcpNew(originalListener)
	if err != nil {
		panic(err)
	}

	router := httprouter.New()
	router.GET("/", serverFileHandler)
	router.POST("/api/datapools", dpPostOneHandler)
	router.GET("/api/datapools", dpGetAllHandler)
	router.GET("/api/datapools/:dpname", dpGetOneHandler)
	router.DELETE("/api/datapools/:dpname", dpDeleteOneHandler)

	router.GET("/api/ep", epGetHandler)
	router.POST("/api/ep", epPostHandler)
	router.DELETE("/api/ep", epDeleteHandler)

	router.GET("/api/repositories/:repo/:item/:tag", repoTagHandler)
	router.GET("/api/repositories/:repo/:item", repoItemHandler)
	router.GET("/api/repositories/:repo", repoRepoNameHandler)
	router.GET("/api/repositories", repoHandler)
	router.GET("/api/repositories/:repo/:item/:tag/judge", judgeTagExistHandler)
	router.DELETE("/api/repositories/:repo/:item", repoDelOneItemHandler)
	router.DELETE("/api/repositories/:repo/:item/:tag", repoDelTagHandler)

	router.GET("/api/subscriptions/dataitems", subsHandler)
	router.GET("/api/subscriptions/pull/:repo/:item", subsHandler)

	router.POST("/api/repositories/:repo/:item", pubItemHandler)
	router.POST("/api/repositories/:repo/:item/:tag", newPubTagHandler)

	router.POST("/api/subscriptions/:repo/:item/pull", pullHandler)

	router.GET("/api/job", jobHandler)
	router.GET("/api/job/:id", jobDetailHandler)
	router.DELETE("/api/job/:id", jobRmHandler)
	router.DELETE("/api/job", jobRmAllHandler)

	router.GET("/api/daemon/:repo/:item/:tag", tagStatusHandler)
	router.GET("/api/daemon/:repo/:item", tagOfItemStatusHandler)

	router.GET("/api/heartbeat/status/:user", userStatusHandler)

	http.Handle("/", router)
	http.HandleFunc("/api/stop", stopHttp)
	http.HandleFunc("/api/users/auth", loginHandler)
	http.HandleFunc("/api/users/logout", logoutHandler)

	router.GET("/api/users/whoami", whoamiHandler)
	router.GET("/api/pulled/:repo/:item", itemPulledHandler)

	router.GET("/api/datapool/published/:dpname", publishedOfDatapoolHandler)
	router.GET("/api/datapool/pulled/:dpname", pulledOfDatapoolHandler)
	router.GET("/api/datapool/published/:dpname/:repo", publishedOfRepoHandler)
	router.GET("/api/datapool/pulled/:dpname/:repo", pulledOfRepoHandler)
	router.POST("/api/datapool/check", checkDpConnectHandler)
	router.GET("/api/datapool/other/:dpname", dpGetOtherDataHandler)
	router.GET("/api/datapool/pulled/:dpname/:repo/:item", pulledTagOfItemHandler)
	router.GET("/api/datapool/published/:dpname/:repo/:item", publishedTagOfItemHandler)
	//router.POST("/api/datapool/newpublishtag", newPublishTagHandler)

	router.NotFound = &mux{}

	server := http.Server{}

	go func() {

		stop := make(chan os.Signal)
		signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

		select {
		case signal := <-stop:
			log.Printf("Got signal:%v", signal)
		}

		sl.Stop()
		if len(DaemonID) > 0 {
			p2psl.Stop()
		}

	}()

	if len(DaemonID) > 0 {
		go startP2PServer()
		go HeartBeat()
		go CheckHealthClock()
		//go datapoolMonitor()  //Temporarily not use
		go GetMessages()
		go PullTagAutomatic()
	} else {
		l := log.Error("no daemonid specificed.")
		logq.LogPutqueue(l)
		fmt.Println("You don't have a daemonid specificed.")
	}

	/*
		wg.Add(1)
		defer wg.Done()
	*/
	log.Info("starting daemon listener...")
	server.Serve(sl)
	log.Info("Stopping daemon listener...")

	if len(DaemonID) > 0 {
		wg.Wait()
	}

	daemonigo.UnlockPidFile()
	g_ds.Db.Close()

	log.Info("daemon exit....")
	log.CloseLogFile()

}