func TestCleanWithZeroThreshold(t *testing.T) {
	p := startAndWaitTestProcess(testCmd, t)
	defer cleanupLogsDir()

	process.NewCleaner(0, 0).CleanOnce()

	_, err := process.Get(p.Pid)
	if err == nil {
		t.Fatal("Must not exist")
	}
	if _, ok := err.(*process.NoProcessError); !ok {
		t.Fatal(err)
	}
}
Exemple #2
0
func main() {
	flag.Parse()

	log.SetOutput(os.Stdout)

	// print configuration
	fmt.Println("Exec-agent configuration")
	fmt.Println("  Server")
	fmt.Printf("    - Address: %s\n", serverAddress)
	fmt.Printf("    - Static content: %s\n", staticDir)
	fmt.Printf("    - Base path: '%s'\n", basePath)
	fmt.Println("  Terminal")
	fmt.Printf("    - Slave command: '%s'\n", term.Cmd)
	fmt.Printf("    - Activity tracking enabled: %t\n", term.ActivityTrackingEnabled)
	if authEnabled {
		fmt.Println("  Authentication")
		fmt.Printf("    - Enabled: %t\n", authEnabled)
		fmt.Printf("    - Tokens expiration timeout: %dm\n", tokensExpirationTimeoutInMinutes)
	}
	fmt.Println("  Process executor")
	fmt.Printf("    - Logs dir: %s\n", process.LogsDir)
	if processCleanupPeriodInMinutes > 0 {
		fmt.Printf("    - Cleanup job period: %dm\n", processCleanupPeriodInMinutes)
		fmt.Printf("    - Not used & dead processes stay for: %dm\n", processCleanupThresholdInMinutes)
	}
	if authEnabled || term.ActivityTrackingEnabled {
		fmt.Println("  Workspace master server")
		fmt.Printf("    - API endpoint: %s\n", apiEndpoint)
	}
	fmt.Println()

	term.ApiEndpoint = apiEndpoint

	// process configuration
	if err := os.RemoveAll(process.LogsDir); err != nil {
		log.Fatal(err)
	}

	if processCleanupPeriodInMinutes > 0 {
		if processCleanupThresholdInMinutes < 0 {
			log.Fatal("Expected process cleanup threshold to be non negative value")
		}
		cleaner := process.NewCleaner(processCleanupPeriodInMinutes, processCleanupThresholdInMinutes)
		cleaner.CleanPeriodically()
	}

	// terminal configuration
	if term.ActivityTrackingEnabled {
		go term.Activity.StartTracking()
	}

	// register routes and http handlers
	router := httprouter.New()
	router.NotFound = http.FileServer(http.Dir(staticDir))

	fmt.Print("⇩ Registered HttpRoutes:\n\n")
	for _, routesGroup := range AppHttpRoutes {
		fmt.Printf("%s:\n", routesGroup.Name)
		for _, route := range routesGroup.Items {
			router.Handle(
				route.Method,
				route.Path,
				toHandle(route.HandleFunc),
			)
			fmt.Printf("✓ %s\n", &route)
		}
		fmt.Println()
	}

	fmt.Print("\n⇩ Registered RpcRoutes:\n\n")
	for _, routesGroup := range AppOpRoutes {
		fmt.Printf("%s:\n", routesGroup.Name)
		for _, route := range routesGroup.Items {
			fmt.Printf("✓ %s\n", route.Method)
			rpc.RegisterRoute(route)
		}
	}

	var handler http.Handler = router

	// required authentication for all the requests, if it is configured
	if authEnabled {
		cache := auth.NewCache(time.Minute*time.Duration(tokensExpirationTimeoutInMinutes), time.Minute*5)

		handler = auth.Handler{
			Delegate:    handler,
			ApiEndpoint: apiEndpoint,
			Cache:       cache,
			UnauthorizedHandler: func(w http.ResponseWriter, req *http.Request) {
				dropChannelsWithExpiredToken(req.URL.Query().Get("token"))
				http.Error(w, "Unauthorized", http.StatusUnauthorized)
			},
		}
	}

	// cut base path on requests, if it is configured
	if basePath != "" {
		if rx, err := regexp.Compile(basePath); err == nil {
			handler = basePathChopper{rx, handler}
		} else {
			log.Fatal(err)
		}
	}

	http.Handle("/", handler)

	server := &http.Server{
		Handler:      handler,
		Addr:         serverAddress,
		WriteTimeout: 10 * time.Second,
		ReadTimeout:  10 * time.Second,
	}
	log.Fatal(server.ListenAndServe())
}