Esempio n. 1
0
func Init(db_addr, certs, host_addr, data_path string, insert_fixture bool) error {
	url := fmt.Sprintf("postgresql://root@%s:26257?sslmode=disable", db_addr)
	if len(certs) > 0 {
		url = fmt.Sprintf("postgresql://root@%s:26257?sslmode=verify-full&sslcert=%s&sslrootcert=%s&sslkey=%s", db_addr,
			fmt.Sprint("%s/ca.crt", certs),
			fmt.Sprintf("%s/root.client.crt", certs),
			fmt.Sprintf("%s/ca.key", certs))
	}
	db, err := sql.Open("postgres", url)
	if err != nil {
		return err
	}
	//setup database
	dbm = Database{
		gorp.DbMap{Db: db, Dialect: gorp.NewCockroachDialect()},
		"channer", insert_fixture,
	}
	//add model
	InitAccount()
	InitRescue()
	InitDevice()
	InitService()
	InitPersona()
	InitChannel()
	InitTopic()
	InitReaction()
	InitPost()
	log.Printf("initialize db")
	//create table according to model definition. TODO: how to do migration with roach?
	if err := dbm.CreateTablesIfNotExists(); err != nil {
		log.Printf("CreateTablesIfNotExists: %v", err)
		return err
	}
	log.Printf("create databases")
	if _, err := dbm.Exec("set database = channer;"); err != nil {
		log.Printf("exec set database: %v", err)
		return err
	}
	if err := dbm.CreateIndex(); err != nil {
		if strings.Contains(err.Error(), "duplicate index name") {
			log.Printf("index already created: ignore error %v", err)
		} else {
			log.Printf("CreateIndex: %v", err)
			return err
		}
	}
	log.Printf("create indexes")
	//import data
	if err := Import(data_path); err != nil {
		log.Printf("Import data: %v", err)
		return err
	}
	log.Printf("model initialized")
	utils.DumpMemUsage()
	return nil
}
Esempio n. 2
0
func (a *HotActor) task() {
	t := time.Now()
	//do first update
	a.update(t)
	a.updateQueryCache(t)
	for {
		select {
		case t := <-a.tick.C:
			a.update(t)
			go a.updateQueryCache(t)
			utils.DumpMemUsage()
		}
	}
}