81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package plugin
|
|
|
|
import (
|
|
"go-account-register/config"
|
|
"go-account-register/models"
|
|
"go-account-register/services"
|
|
paramsTypes "go-account-register/types"
|
|
"log"
|
|
"runtime/debug"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type TaskGetAddressPlugin struct{}
|
|
|
|
func (*TaskGetAddressPlugin) Run(task *paramsTypes.Task) {
|
|
|
|
// 查询当前所有账号
|
|
userService := services.InitTwitterAccountService()
|
|
// 判断是否是补偿任务
|
|
userList := userService.GetAvailableAccount(&models.TwitterAccount{
|
|
Type: "get",
|
|
})
|
|
log.Println("可用账号数量:", len(userList))
|
|
// 获取并发数
|
|
appConfig, _ := config.LoadConfig()
|
|
sem := make(chan struct{}, appConfig.Limit.BrowserRunMax) // 全局并发上限
|
|
var globalWait sync.WaitGroup
|
|
for j := 0; j < appConfig.Limit.BrowserRunMax; j++ {
|
|
select {
|
|
case <-task.Ctx.Done():
|
|
log.Println("中断任务")
|
|
return
|
|
default:
|
|
}
|
|
log.Println(j)
|
|
taskIndex := j
|
|
if taskIndex >= len(userList) {
|
|
break
|
|
}
|
|
time.Sleep(5 * time.Second)
|
|
globalWait.Add(1)
|
|
sem <- struct{}{} // 阻塞直到有空位
|
|
go func() {
|
|
defer func() {
|
|
<-sem // 释放信号量
|
|
globalWait.Done()
|
|
}()
|
|
getAddressWork(task, userList[taskIndex].ID)
|
|
}()
|
|
}
|
|
|
|
globalWait.Wait()
|
|
|
|
task.Message <- "Success"
|
|
}
|
|
|
|
func getAddressWork(task *paramsTypes.Task, id int64) {
|
|
UserService := services.InitTwitterAccountService()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Printf("Goroutine panic: %v\nStack: %s", r, debug.Stack())
|
|
UserService.ChangeAccountLoginStatus(int(id), "账号下线")
|
|
TwitterService.Logout(int(id))
|
|
}
|
|
}()
|
|
|
|
// 开始登录
|
|
|
|
res := TwitterService.Login(int(id))
|
|
UserService.ChangeAccountLoginStatus(int(id), res.Msg)
|
|
if res.Code == 200 {
|
|
// 开始获取
|
|
TwitterService.GetAccountAddress(int(id))
|
|
TwitterService.Logout(int(id))
|
|
UserService.ChangeAccountLoginStatus(int(id), "账号下线")
|
|
}
|
|
|
|
log.Println(task.ID)
|
|
}
|