Пример #1
0
func UpdateDB(title string) error {
	cfg.GetLogger().Info("Trying to update", "DB", title)

	var dbData cfg.DB
	var ok bool
	if dbData, ok = cfg.GetConfig().GetDBByTitle(title); !ok {
		return setDBStatusFailed(title, "Failed to get db data from config")
	}

	adapter, err := adapter.New(dbData)
	if err != nil {
		return err
	}
	tables, err := adapter.ListTables()
	if err != nil {
		return err
	}
	for _, table := range tables {
		table.State, err = adapter.GetState(table)
		if err != nil {
			return err
		}

		cfg.GetLogger().Debug("Got table status", "table", table, "filling", fmt.Sprintf("%.10f", table.State.Filling))
		if cfg.GetConfig().Debug {
			pp.Println(table)
		}
	}

	return err
}
Пример #2
0
func InitMetaData() error {
	cfg.GetLogger().Info("Initializing meta data...")
	err := db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists([]byte(BUCKET_META))
		if err != nil {
			return err
		}

		var data []Meta
		jsonData := bucket.Get([]byte(META_DATA))
		if jsonData != nil {
			json.Unmarshal(jsonData, &data)
		}

		dbsMeta := make(map[string]string)
		for _, meta := range data {
			dbsMeta[meta.Title] = meta.Title
			if _, ok := cfg.GetConfig().GetDBByTitle(meta.Title); !ok {
				cfg.GetLogger().Info("DB not found in config, cleaning up", "title", meta.Title)
				// TODO: remove unused DB data
			}
		}

		for _, dbData := range cfg.GetConfig().Dbs {
			if _, ok := dbsMeta[dbData.Title]; !ok {
				cfg.GetLogger().Info("New DB to monitor", "title", dbData.Title)
			}
		}

		return nil
	})
	return err
}
Пример #3
0
func init() {
	dbPath := fmt.Sprintf("%s/data/bolt.db", cfg.RootDir())
	cfg.GetLogger().Debug("Initializing storage", "path", dbPath)
	os.MkdirAll(path.Dir(dbPath), 0755)

	var err error
	if db, err = bolt.Open(dbPath, 0644, nil); err != nil {
		panic(err)
	}
}
Пример #4
0
func (self *adapter) GetState(table do.Table) (do.Snapshot, error) {
	cfg.GetLogger().Debug("Getting state", "table", table.Name)

	var err error
	switch strings.ToLower(self.db.Type) {
	case "mysql":
		return self.mysqlAdapter.GetState(table)

	case "postgres":
		err = errors.New("Support for PostgreSQL not implemented yet")

	default:
		err = errors.New(fmt.Sprintf("Unknown db type '%s'. Known types are 'mysql', 'postgres'", self.db.Type))
	}

	if err != nil {
		cfg.GetLogger().Warn("Failed to get state", "table", table.Name, "error", err)
	}

	return do.Snapshot{UpdatedAt: time.Now()}, err
}
Пример #5
0
func (self *adapter) ListTables() ([]do.Table, error) {
	cfg.GetLogger().Debug("Listing tables...")

	var err error
	switch strings.ToLower(self.db.Type) {
	case "mysql":
		return self.mysqlAdapter.ListTables()

	case "postgres":
		err = errors.New("Support for PostgreSQL not implemented yet")

	default:
		err = errors.New(fmt.Sprintf("Unknown db type '%s'. Known types are 'mysql', 'postgres'", self.db.Type))
	}

	if err != nil {
		cfg.GetLogger().Warn("Failed to list tables", "error", err)
	}

	return nil, err
}
Пример #6
0
func (self *mysqlAdapter) ListTables() ([]do.Table, error) {
	var (
		tableName string
		tables    []do.Table
	)

	rows, err := self.connection.Query("SHOW tables;")
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {
		err := rows.Scan(&tableName)
		if err != nil {
			cfg.GetLogger().Warn("Failed to scan table name from row", "error", err)
			return nil, err
		}

		has, fieldName, fieldMax, err := self.hasAutoIncrementField(tableName)
		if err != nil {
			cfg.GetLogger().Warn("Failed to get info on autoincrement field", "table", tableName, "error", err)
			return nil, err
		}

		if has {
			table := do.Table{Name: tableName, Field: fieldName, Max: fieldMax}
			tables = append(tables, table)
		}
	}

	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return tables, nil
}
Пример #7
0
func newMySQL(db cfg.DB) (*mysqlAdapter, error) {
	a := new(mysqlAdapter)
	a.db = db

	var err error
	a.connection, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", db.User, db.Pass, db.Host, db.Port, db.Name))
	if err != nil {
		dbString := fmt.Sprintf("mysql://%s:***@tcp(%s:%d)/%s", db.User, db.Host, db.Port, db.Name)
		cfg.GetLogger().Error("Error while openning connection", "DB", dbString)
		return nil, err
	}

	return a, nil
}
Пример #8
0
func setDBStatusFailed(name string, reason string) error {
	cfg.GetLogger().Warn("DB update status failed", "name", name, "reason", reason)
	return db.Update(func(tx *bolt.Tx) error {
		bucket, err := tx.CreateBucketIfNotExists([]byte(BUCKET_DBS))
		if err != nil {
			return err
		}

		dbUpdate := new(do.DB)
		jsonData := bucket.Get([]byte(name))
		if jsonData != nil {
			json.Unmarshal(jsonData, &dbUpdate)
		}

		dbUpdate.UpdatedAt = time.Now()
		dbUpdate.Status = false
		dbUpdate.Error = reason

		jsonData, _ = json.Marshal(dbUpdate)
		return bucket.Put([]byte(name), []byte(jsonData))
	})
}
Пример #9
0
func New(db cfg.DB) (*adapter, error) {
	a := new(adapter)
	a.db = db

	var err error
	switch strings.ToLower(a.db.Type) {
	case "mysql":
		a.mysqlAdapter, err = newMySQL(db)

	case "postgres":
		err = errors.New("Support for PostgreSQL not implemented yet")

	default:
		err = errors.New(fmt.Sprintf("Unknown db type '%s'. Known types are 'mysql', 'postgres'", db.Type))
	}

	if err != nil {
		cfg.GetLogger().Error("Failed to instantiate adapter", "error", err)
	}

	return a, err
}