Skip to content

Commit b853b69

Browse files
committed
add TaskPool
1 parent 7706060 commit b853b69

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

conf.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
token: sadsadasdasdasd
2+
api: http://127.0.0.1/Queue
3+
taskNum: 4

main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
10+
"./task"
11+
12+
"gopkg.in/yaml.v2"
13+
)
14+
15+
type taskConfig struct {
16+
TOKEN string `yaml:"token"`
17+
APIURL string `yaml:"api"`
18+
TASKNUM int `yaml:"taskNum"`
19+
}
20+
21+
func (c *taskConfig) getConf() (*taskConfig, error) {
22+
23+
yamlFile, err := ioutil.ReadFile("conf.yaml")
24+
if err != nil {
25+
log.Printf("[ERROR] yamlFile.Get err #%v ", err)
26+
return nil, errors.New("Cant not read config.yaml")
27+
}
28+
err = yaml.Unmarshal(yamlFile, c)
29+
if err != nil {
30+
return nil, errors.New("Cant not prase config.yaml")
31+
}
32+
33+
return c, nil
34+
}
35+
36+
func main() {
37+
38+
fmt.Println("Cloudreve Queue Go Version")
39+
fmt.Println("Author: AaronLiu <abslant@foxmail.com>")
40+
41+
var config taskConfig
42+
_, err := config.getConf()
43+
if err == nil {
44+
log.Printf("[INFO] Config information: %v ", config)
45+
api := task.ApiInfo{TOKEN: config.TOKEN, APIURL: config.APIURL}
46+
basicInfo := api.GetBasicInfo()
47+
if basicInfo != "" {
48+
log.Printf("[INFO] Basic Info: %v ", basicInfo)
49+
var siteInfo map[string]string
50+
err := json.Unmarshal([]byte(basicInfo), &siteInfo)
51+
if err != nil {
52+
log.Printf("[ERROR] Failed to decode basic infomation, %v ", err.Error())
53+
}
54+
for {
55+
taskListContent := api.GetTaskList(config.TASKNUM)
56+
task.Init(taskListContent, api)
57+
break
58+
}
59+
}
60+
61+
}
62+
63+
}

task/CloudreveApi.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package task
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"net/http"
7+
"strconv"
8+
)
9+
10+
//ApiInfo Api请求配置
11+
type ApiInfo struct {
12+
TOKEN string
13+
APIURL string
14+
BASEPATH string
15+
}
16+
17+
//GetBasicInfo 获取目标站点基本信息
18+
func (apiInfo *ApiInfo) GetBasicInfo() string {
19+
return apiInfo.apiGet("basicInfo")
20+
}
21+
22+
//GetTaskList 获取待处理任务列表
23+
func (apiInfo *ApiInfo) GetTaskList(num int) string {
24+
return apiInfo.apiGet("getList?num=" + strconv.Itoa(num))
25+
}
26+
27+
//apiGet 发送GET请求
28+
func (apiInfo *ApiInfo) apiGet(controller string) string {
29+
client := &http.Client{}
30+
request, err := http.NewRequest("GET", apiInfo.APIURL+"/"+controller, nil)
31+
if err != nil {
32+
log.Printf("[ERROR] Failed to create GET requetst, #%v ", err)
33+
}
34+
request.Header.Set("Authorization", "Bearer "+apiInfo.TOKEN)
35+
response, err := client.Do(request)
36+
if err != nil {
37+
log.Printf("[ERROR] Failed to send GET requetst, #%v ", err)
38+
return ""
39+
}
40+
defer response.Body.Close()
41+
if response.StatusCode == 200 {
42+
r, err := ioutil.ReadAll(response.Body)
43+
if err != nil {
44+
log.Printf("[ERROR] Failed to get GET requetst body, #%v ", err)
45+
}
46+
return string(r)
47+
} else if response.StatusCode == 403 {
48+
log.Printf("[ERROR] Auth failed, please verify your token, HTTP ERROR %v ", response.StatusCode)
49+
return ""
50+
} else {
51+
log.Printf("[ERROR] Failed to get respond, HTTP ERROR %v ", response.StatusCode)
52+
return ""
53+
}
54+
55+
}

task/TaskPool.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package task
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
)
8+
9+
//SingleTaskInfo 单个任务
10+
type SingleTaskInfo struct {
11+
ID int `json:"id"`
12+
TaskName string `json:"task_name"`
13+
Attr interface{} `json:"attr"`
14+
TaskType string `json:"task"`
15+
Status string `json:"status"`
16+
Addtime string `json:"addtime"`
17+
}
18+
19+
//Init 初始化任务线程池
20+
func Init(taskListContent string, apiInfo ApiInfo) {
21+
var taskStringList []SingleTaskInfo
22+
err := json.Unmarshal([]byte(taskListContent), &taskStringList)
23+
if err != nil {
24+
log.Printf("[ERROR] Failed to decode basic infomation, %v ", err.Error())
25+
}
26+
for _, v := range taskStringList {
27+
fmt.Println(v)
28+
}
29+
}

0 commit comments

Comments
 (0)