Improved avatar background job

This commit is contained in:
2017-06-13 13:23:54 +02:00
parent 4145f86e84
commit bdb8d983d2
8 changed files with 139 additions and 26 deletions

@ -1,45 +1,86 @@
package main
import (
"fmt"
"os"
"runtime"
"time"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
const (
avatarsDirectoryOriginal = "images/avatars/original/"
avatarsDirectoryWebP = "images/avatars/webp/"
networkRateLimit = 100 * time.Millisecond
avatarsDirectoryOriginal = "images/avatars/large/original/"
avatarsDirectoryWebP = "images/avatars/large/webp/"
)
var avatarSources []AvatarSource
var avatarWriters []AvatarWriter
// Main
func main() {
// Switch to main directory
os.Chdir("../../")
users, _ := arn.AllUsers()
usersQueue := make(chan *arn.User)
rateLimiter := time.NewTicker(100 * time.Millisecond)
defer rateLimiter.Stop()
for w := 0; w < runtime.NumCPU(); w++ {
go func(workerID int) {
for user := range usersQueue {
<-rateLimiter.C
if downloadAvatar(user) {
makeWebPAvatar(user)
user.Avatar = "/+" + user.Nick + "/avatar"
} else {
user.Avatar = ""
}
user.Save()
}
}(w)
// Define the avatar sources
avatarSources = []AvatarSource{
&Gravatar{},
}
// Stream of all users
users, _ := arn.AllUsers()
// Worker queue
usersQueue := make(chan *arn.User)
StartWorkers(usersQueue, networkRateLimit, Work)
// We'll send each user to one of the worker threads
for user := range users {
usersQueue <- user
}
}
// StartWorkers creates multiple workers to handle a user each.
func StartWorkers(queue chan *arn.User, rateLimit time.Duration, work func(*arn.User)) {
rateLimiter := time.NewTicker(rateLimit)
for w := 0; w < runtime.NumCPU(); w++ {
go func() {
for user := range queue {
<-rateLimiter.C
work(user)
}
}()
}
}
// Work handles a single user.
func Work(user *arn.User) {
for _, source := range avatarSources {
avatar := source.GetAvatar(user)
if avatar == nil {
fmt.Println(color.RedString("✘"), user.Nick)
continue
}
fmt.Println(color.GreenString("✔"), user.Nick, "|", avatar.Format, avatar.Image.Bounds().Dx(), avatar.Image.Bounds().Dy())
for _, writer := range avatarWriters {
writer.SaveAvatar(avatar)
}
return
}
// if downloadAvatar(user) {
// makeWebPAvatar(user)
// user.Avatar = "/+" + user.Nick + "/avatar"
// } else {
// user.Avatar = ""
// }
// user.Save()
}