66 "fmt"
77 "io/ioutil"
88 "log"
9+ "sync"
10+ "time"
911
1012 "./api"
1113 "./task"
@@ -14,9 +16,10 @@ import (
1416)
1517
1618type taskConfig struct {
17- TOKEN string `yaml:"token"`
18- APIURL string `yaml:"api"`
19- TASKNUM int `yaml:"taskNum"`
19+ TOKEN string `yaml:"token"`
20+ APIURL string `yaml:"api"`
21+ TASKNUM int `yaml:"taskNum"`
22+ DURATION int `yaml:"Duration"`
2023}
2124
2225func (c * taskConfig ) getConf () (* taskConfig , error ) {
@@ -42,23 +45,48 @@ func main() {
4245 var config taskConfig
4346 _ , err := config .getConf ()
4447 if err == nil {
48+
4549 log .Printf ("[INFO] Config information: %v " , config )
46- api := api.ApiInfo {TOKEN : config .TOKEN , APIURL : config .APIURL }
50+ api := api.ApiInfo {
51+ TOKEN : config .TOKEN ,
52+ APIURL : config .APIURL ,
53+ Lock : new (sync.Mutex ),
54+ }
4755 basicInfo := api .GetBasicInfo ()
56+
4857 if basicInfo != "" {
58+
4959 log .Printf ("[INFO] Basic Info: %v " , basicInfo )
5060 var siteInfo map [string ]string
5161 err := json .Unmarshal ([]byte (basicInfo ), & siteInfo )
5262 if err != nil {
5363 log .Printf ("[ERROR] Failed to decode basic infomation, %v " , err .Error ())
5464 }
55- for {
56- taskListContent := api .GetTaskList (config .TASKNUM )
57- if taskListContent != "none" {
58- task .Init (taskListContent , api , siteInfo )
59- }
60- break
65+
66+ var wg sync.WaitGroup
67+ for i := 0 ; i < config .TASKNUM ; i ++ {
68+ wg .Add (1 )
69+ log .Printf ("[Info] Thread %d start" , i + 1 )
70+ threadID := i
71+ go func () {
72+ for {
73+
74+ api .Lock .Lock ()
75+ taskListContent := api .GetTaskList (1 )
76+ api .Lock .Unlock ()
77+
78+ if taskListContent != "none" {
79+ task .Init (taskListContent , api , siteInfo , threadID )
80+ }
81+ time .Sleep (time .Duration (config .DURATION ) * time .Second )
82+ }
83+
84+ }()
85+ time .Sleep (time .Duration (1 ) * time .Second )
6186 }
87+
88+ wg .Wait ()
89+
6290 }
6391
6492 }
0 commit comments