71 lines
1.1 KiB
Go
Raw Normal View History

2017-06-12 18:06:31 +00:00
package main
import (
"os"
"path"
2017-06-12 18:06:31 +00:00
"runtime"
2017-07-08 14:10:22 +00:00
"sync"
2017-06-12 18:06:31 +00:00
2017-06-13 12:47:17 +00:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-06-15 14:56:06 +00:00
"github.com/aerogo/log"
2017-06-12 18:06:31 +00:00
"github.com/animenotifier/arn"
2017-07-18 05:23:48 +00:00
"github.com/animenotifier/avatar/lib"
2017-06-13 11:23:54 +00:00
"github.com/fatih/color"
2017-06-12 22:06:35 +00:00
)
2017-07-08 14:10:22 +00:00
var wg sync.WaitGroup
2017-06-13 11:23:54 +00:00
// Main
2017-06-12 18:06:31 +00:00
func main() {
2017-06-14 15:16:03 +00:00
color.Yellow("Generating user avatars")
2017-06-13 11:23:54 +00:00
// Switch to main directory
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
os.Chdir(path.Join(root, "../../"))
2017-06-12 22:06:35 +00:00
2017-06-15 14:56:06 +00:00
// Log
2017-07-18 05:23:48 +00:00
lib.Log.AddOutput(log.File("logs/avatar.log"))
defer lib.Log.Flush()
2017-06-13 12:47:17 +00:00
if InvokeShellArgs() {
return
}
2017-06-13 11:23:54 +00:00
// Worker queue
2017-07-08 14:10:22 +00:00
usersQueue := make(chan *arn.User, runtime.NumCPU())
2017-07-18 05:23:48 +00:00
StartWorkers(usersQueue, lib.RefreshAvatar)
2017-06-12 18:06:31 +00:00
2017-07-13 07:20:12 +00:00
allUsers, _ := arn.AllUsers()
2017-06-13 11:23:54 +00:00
// We'll send each user to one of the worker threads
2017-07-13 07:20:12 +00:00
for _, user := range allUsers {
2017-07-08 14:10:22 +00:00
wg.Add(1)
2017-06-13 11:23:54 +00:00
usersQueue <- user
}
2017-06-14 15:16:03 +00:00
2017-07-08 14:10:22 +00:00
wg.Wait()
2017-06-14 15:16:03 +00:00
color.Green("Finished.")
2017-06-13 11:23:54 +00:00
}
2017-06-12 22:06:35 +00:00
2017-06-13 11:23:54 +00:00
// StartWorkers creates multiple workers to handle a user each.
2017-06-13 15:06:30 +00:00
func StartWorkers(queue chan *arn.User, work func(*arn.User)) {
2017-06-13 11:23:54 +00:00
for w := 0; w < runtime.NumCPU(); w++ {
go func() {
for user := range queue {
work(user)
2017-07-08 14:19:31 +00:00
wg.Done()
2017-06-12 18:06:31 +00:00
}
2017-06-13 11:23:54 +00:00
}()
2017-06-12 18:06:31 +00:00
}
2017-06-13 11:23:54 +00:00
}