示例#1
0
func CheckIn(path string) {
	log.Info("Starting db checking background script: " + path)
	ticker := time.NewTicker(10 * time.Second)
	quit := make(chan struct{})
	go func() {
		for {
			select {
			case <-ticker.C:
				nodeinfo.UpdateConnection()
				log.Info("Checking all changed stuff in db for: " + path)
				listener := utils.GetListenerFromDir(path)
				items, err := datastore.CheckIn(listener)
				if err != nil {
					log.Infof("Error occurred getting data for %s (%s): %+v", listener, err.Error(), err)
				}
				cfg := utils.GetConfig()
				handleDataChanges(items, cfg.Listeners[listener], listener)
			// @TODO: check that db knows Im alive.
			case <-quit:
				ticker.Stop()
				return
			}
		}
	}()
}
示例#2
0
func getLoggerConfig() string {
	cfg := utils.GetConfig()
	var loggerConfig = ""
	if cfg.ServerConfig.LogLocation != "stdout" {
		if _, err := os.Stat(filepath.Dir(cfg.ServerConfig.LogLocation)); os.IsNotExist(err) {
			os.Mkdir(filepath.Dir(cfg.ServerConfig.LogLocation), 0775)
		}
		loggerConfig = `<seelog type="asynctimer" asyncinterval="1000">
    <outputs formatid="main">
        <filter levels="` + cfg.ServerConfig.LogLevel + `">
          <file path="` + cfg.ServerConfig.LogLocation + `" />
        </filter>
    </outputs>
    <formats>
        <format id="main" format="%Date %Time [%LEVEL] %Msg%n"/>
    </formats>
    </seelog>`
	} else {
		loggerConfig = `<seelog type="asynctimer" asyncinterval="1000">
    <outputs formatid="main">
        <console/>
    </outputs>
    <formats>
        <format id="main" format="%Date %Time [%LEVEL] %Msg (%RelFile:%Func)%n"/>
    </formats>
    </seelog>`
	}

	return loggerConfig
}
示例#3
0
func InitialSync() {
	cfg := utils.GetConfig()
	log.Info("Verifying DB Tables")
	datastore.CreateDB()
	log.Info("Initial sync starting...")

	for key, listener := range cfg.Listeners {

		// First check to see if the table is empty and do a full import false == not empty
		if datastore.CheckEmpty(key) == false {
			// Database is not empty so pull the updates and match locally
			items := datastore.FetchAll(key)
			handleDataChanges(items, listener, key)
		} else {
			// Database is empty so lets import
			fsItems := utils.ListFilesInDir(listener.Directory)
			for _, item := range fsItems {
				success := datastore.Insert(key, item)
				if success != true {
					log.Infof("An error occurred inserting %x to database", item)
				}
				if !item.IsDir {
					storage.PutFile(item.Filename, key)
				}

			}
		}

	}

	log.Info("Initial sync completed...")
}
示例#4
0
func GetNodeCopy(item utils.DataTable, listener string, uid, gid int, perms string) bool {
	cfg := utils.GetConfig()
	aliveNodes := nodeinfo.GetNodes()
	for _, node := range aliveNodes {
		log.Infof("Trying download from: %s", node.NodeIPs)
		nIPs := strings.Split(node.NodeIPs, ",")
		for _, ipAddress := range nIPs {
			resp, err := getData(ipAddress, cfg.ServerConfig.ListenPort, listener, utils.GetRelativePath(listener, item.Path))
			if err == nil {
				defer resp.Body.Close()
				if resp.Status == "404" {
					log.Infof("File not found: %s", item.Path)
					return false
				}
				size, err := utils.FileWrite(item.Path, resp.Body, uid, gid, perms)
				if err != nil {
					log.Infof("Cannot write file: %s", item.Path)
					return false
				} else {
					log.Infof("%s with %v bytes downloaded", item.Path, size)
					return true
				}
				return false
			} else {
				return false
			}
		}
	}
	return false
}
示例#5
0
func setdbstoreEngine() {
	cfg := utils.GetConfig()
	var engine = cfg.Database.Type
	switch engine {
	case "mysql":
		dbstore = &MySQLDB{config: cfg}
	}
}
示例#6
0
func setdbstoreEngine() {
	cfg := utils.GetConfig()
	var engine = cfg.Database.Type
	switch engine {
	case "mysql":
		nodeStore = &MySQLNodeDB{config: cfg}
		nodeStore.initDB()
	}
}
示例#7
0
func getListener(dir string) string {
	cfg := utils.GetConfig()
	var listener = ""
	for lname, ldata := range cfg.Listeners {
		if ldata.Directory == dir {
			listener = lname
		}
	}
	return listener
}
示例#8
0
func setStorageEngine(listener string) {
	cfg := utils.GetConfig()
	var engine = cfg.Listeners[listener].StorageType
	switch engine {
	case "gdrive":
		storage = &GDrive{config: cfg, listener: listener}
	case "s3":
		storage = &S3{config: cfg, listener: listener}
	}
}
示例#9
0
func main() {

	cfg := utils.GetConfig()
	replicator.InitialSync()
	for _, item := range cfg.Listeners {
		log.Info("Working with: " + item.Directory)
		go replicator.CheckIn(item.Directory)
		go fswatcher.SysPathWatcher(item.Directory)
	}
	StartWebFileServer(cfg)

}