71 lines
1.1 KiB
Go
Raw Normal View History

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