-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
94 lines (74 loc) · 1.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"sync"
"time"
"./api"
"./task"
"gopkg.in/yaml.v2"
)
type taskConfig struct {
TOKEN string `yaml:"token"`
APIURL string `yaml:"api"`
TASKNUM int `yaml:"taskNum"`
DURATION int `yaml:"Duration"`
}
func (c *taskConfig) getConf() (*taskConfig, error) {
yamlFile, err := ioutil.ReadFile("conf.yaml")
if err != nil {
log.Printf("[ERROR] yamlFile.Get err #%v ", err)
return nil, errors.New("Cant not read config.yaml")
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
return nil, errors.New("Cant not prase config.yaml")
}
return c, nil
}
func main() {
fmt.Println("Cloudreve Queue Go Version 1.0")
fmt.Println("Author: AaronLiu <abslant@foxmail.com>")
fmt.Println("")
var config taskConfig
_, err := config.getConf()
if err == nil {
log.Printf("[INFO] Config information: %v ", config)
api := api.ApiInfo{
TOKEN: config.TOKEN,
APIURL: config.APIURL,
Lock: new(sync.Mutex),
}
basicInfo := api.GetBasicInfo()
if basicInfo != "" {
log.Printf("[INFO] Basic Info: %v ", basicInfo)
var siteInfo map[string]string
err := json.Unmarshal([]byte(basicInfo), &siteInfo)
if err != nil {
log.Printf("[ERROR] Failed to decode basic infomation, %v ", err.Error())
}
var wg sync.WaitGroup
for i := 0; i < config.TASKNUM; i++ {
wg.Add(1)
log.Printf("[Info] Thread %d start", i+1)
threadID := i
go func() {
for {
api.Lock.Lock()
taskListContent := api.GetTaskList(1)
api.Lock.Unlock()
if taskListContent != "none" {
task.Init(taskListContent, api, siteInfo, threadID)
}
time.Sleep(time.Duration(config.DURATION) * time.Second)
}
}()
time.Sleep(time.Duration(1) * time.Second)
}
wg.Wait()
}
}
}