Ejemplo n.º 1
0
func RunDaemon() {
	log.Println("Run daemon..")
	// Daemonizing echo server application.
	switch isDaemon, err := daemonigo.Daemonize(); {
	case !isDaemon:
		return
	case err != nil:
		log.Fatalf("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)
		}

	}

	dbinit()

	os.Chdir(g_strDpPath)
	originalListener, err := net.Listen("unix", cmd.UnixSock)
	if err != nil {
		panic(err)
	}

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

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

	router.GET("/repositories/:repo/:item", repoDetailHandler)
	router.GET("/repositories", repoHandler)
	router.GET("/subscriptions", subsHandler)
	router.POST("/subscriptions/:repo/:item/pull", pullHandler)

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

	server := http.Server{}

	stop := make(chan os.Signal)
	signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
	var wg sync.WaitGroup
	go func() {
		wg.Add(1)
		defer wg.Done()
		server.Serve(sl)
	}()

	//p2p server
	P2pRouter := httprouter.New()
	P2pRouter.GET("/", sayhello)
	P2pRouter.GET("/pull/:repo/:dataitem/:tag", p2p_pull)
	go func() {
		wg.Add(1)
		defer wg.Done()
		http.ListenAndServe(":35800", P2pRouter)
	}()

	log.Printf("Serving HTTP\n")
	select {
	case signal := <-stop:
		log.Printf("Got signal:%v\n", signal)
	}
	log.Printf("Stopping listener\n")
	sl.Stop()
	log.Printf("Waiting on server\n")
	wg.Wait()
	daemonigo.UnlockPidFile()
	g_ds.Db.Close()

}
Ejemplo n.º 2
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()

}