Example #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flushDb := flag.Bool("flush", false, "Clears all redis dbs")
	configPath := flag.String("config", "", "Config path")
	isProd := flag.Bool("prod", false, "Prod mode")
	flag.Parse()

	output := Util.Output{StartTime: time.Now()}
	output.Write("Starting...")

	// init
	var (
		regionRealms map[int64][]Entity.Realm
		err          error
		cacheClient  Cache.Client
	)
	if cacheClient, _, regionRealms, err = Misc.Init(*configPath, *flushDb); err != nil {
		output.Write(fmt.Sprintf("Misc.Init() fail: %s", err.Error()))
		return
	}

	// formatting the realms to be evenly distributed
	largestRegion := 0
	for _, realms := range regionRealms {
		if len(realms) > largestRegion {
			largestRegion = len(realms)
		}
	}
	formattedRealms := make([]map[int64]Entity.Realm, largestRegion)
	for regionId, realms := range regionRealms {
		for i, realm := range realms {
			if formattedRealms[int64(i)] == nil {
				formattedRealms[int64(i)] = map[int64]Entity.Realm{}
			}
			formattedRealms[int64(i)][regionId] = realm

			if !*isProd {
				break
			}
		}
	}

	if !*isProd {
		output.Write("Running it once due to non-prod mode...")
		_, err = work(formattedRealms, cacheClient)
		if err != nil {
			output.Write(fmt.Sprintf("work() fail: %s", err.Error()))
			return
		}
	} else {
		output.Write("Starting up the timed rotation...")
		c := time.Tick(15 * time.Minute)
		for {
			output.Write("Running work()...")
			startTime := time.Now()
			formattedRealms, err = work(formattedRealms, cacheClient)
			if err != nil {
				output.Write(fmt.Sprintf("work() fail: %s", err.Error()))
				return
			}
			output.Write(fmt.Sprintf("Done work() in %.2fs!", time.Since(startTime).Seconds()))

			<-c
		}
	}

	output.Conclude()
}
Example #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	configPath := flag.String("config", "", "Config path")
	flag.Parse()

	output := Util.Output{StartTime: time.Now()}
	output.Write("Starting...")

	// init
	var (
		err         error
		cacheClient Cache.Client
	)
	if cacheClient, _, _, err = Misc.Init(*configPath, false); err != nil {
		output.Write(fmt.Sprintf("Misc.Init() fail: %s", err.Error()))
		return
	}

	regionManager := Entity.NewRegionManager(cacheClient)
	var regions []Entity.Region
	if regions, err = regionManager.FindAll(); err != nil {
		output.Write(fmt.Sprintf("RegionManager.FindAll() fail: %s", err.Error()))
		return
	}

	characterCount := 0
	for _, region := range regions {
		realmManager := Entity.NewRealmManager(region, cacheClient)
		var realms []Entity.Realm
		if realms, err = realmManager.FindAll(); err != nil {
			output.Write(fmt.Sprintf("RealmManager.FindAll() fail: %s", err.Error()))
			return
		}

		for _, realm := range realms {
			characterManager := Character.NewManager(realm, cacheClient)
			var names []string
			if names, err = characterManager.GetNames(); err != nil {
				output.Write(fmt.Sprintf("CharacterManager.GetNames() fail: %s", err.Error()))
				return
			}
			characterCount += len(names)
		}
	}
	output.Write(fmt.Sprintf("Characters in the world: %d", characterCount))

	output.Conclude()
}