func (wp *WorkersPool) GetWorkerForMerchant(merchant string) *Worker {
	pos := wp.workerPosition(merchant)

	if -1 == pos {
		config := config.GetConfig()
		worker := newWorker(merchant, config.RequestLogDir)
		wp.appendWorker(worker)

		pos = wp.workerPosition(merchant)
		go worker.Serve()
	}

	return (*wp)[pos]
}
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	defer glog.Flush()

	var environment string
	var configFile string
	var pidfile string

	flag.StringVar(&environment, "environment", "", "Setup environment: production, development")
	flag.StringVar(&configFile, "config-file", "", "Configuration file")
	flag.StringVar(&pidfile, "pidfile", "", "PID file")
	flag.Parse()

	pid := syscall.Getpid()
	glog.Info(pid)
	spid := fmt.Sprintf("%v", pid)
	err := ioutil.WriteFile(pidfile, []byte(spid), 0644)
	if err != nil {
		glog.Fatalf("Could not open pidfile. %v", err)
		panic(err)
	}

	config.ParseConfig(environment, configFile)

	waitForStop := make(chan bool, 1)

	config := config.GetConfig()

	server := server.NewServer(config)
	glog.Infof("New Server is starting with configuration %+v", config)
	glog.Flush()
	go server.Serve()

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

	signal := <-signalChan

	glog.V(2).Infof("Received %v signal. Stopping...", signal)
	glog.Flush()

	server.Stop(waitForStop)

	<-waitForStop
	glog.Info("EXIT")
}
Пример #3
0
func NewConnection() *sql.DB {
	config := config.GetConfig()

	connectionString := fmt.Sprintf("user=%v password=%v dbname=%v sslmode=disable", config.DatabaseUser, config.DatabasePassword, config.DatabaseName)

	db, ok := sql.Open("postgres", connectionString)
	if ok != nil {
		panic(ok)
	}

	err := db.Ping()
	if err != nil {
		panic(err.Error())
	}

	return db
}