示例#1
0
func main() {
	flag.Parse()
	pgConfig.LoadAgeTrigger = time.Second * time.Duration(loadAgeSeconds)

	log.SetOutput(os.Stdout)
	stats, err := lib.InitStats(statsPrefix)
	if err != nil {
		log.Fatalln("Failed to setup statter", err)
	}
	scoopConnection, err := loadclient.NewScoopLoader(scoopURL, manifestBucketPrefix, stats)
	if err != nil {
		log.Fatalln("Failed to setup scoop client for postgres", err)
	}

	pgBackend, err := metadata.NewPostgresLoader(&pgConfig, scoopConnection)
	if err != nil {
		log.Fatalln("Failed to setup postgres backend", err)
	}

	_, err = StartWorkers(pgBackend, stats)
	if err != nil {
		log.Fatalln("Failed to start workers", err)
	}

	hcBackend := healthcheck.BuildHealthCheckBackend(scoopConnection, pgBackend)
	hcHandler := healthcheck.BuildHealthCheckHandler(hcBackend)

	hcServeMux := http.NewServeMux()
	hcServeMux.Handle("/health", healthcheck.MakeHealthRouter(hcHandler))

	go func() {
		if err := http.ListenAndServe(net.JoinHostPort("localhost", "8080"), hcServeMux); err != nil {
			log.Fatal("Health Check (HTTP) failed: ", err)
		}
	}()

	wait := make(chan struct{})
	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGINT)
	log.Println("Loader is set up")
	go func() {
		<-sigc
		log.Println("Sigint received -- shutting down")
		pgBackend.Close()
		// Cause flush
		stats.Close()
		workerGroup.Wait()
		close(wait)
	}()

	<-wait
}
示例#2
0
func StartWorkers(b metadata.MetadataBackend, stats lib.Stats) ([]LoadWorker, error) {
	workers := make([]LoadWorker, poolSize)
	for i := 0; i < poolSize; i++ {
		loadclient, err := loadclient.NewScoopLoader(scoopURL, manifestBucketPrefix, stats)
		if err != nil {
			return workers, err
		}
		workers[i] = LoadWorker{MetadataBackend: b, Loader: loadclient}
		go workers[i].Work()
		workerGroup.Add(1)
	}
	return workers, nil
}