imitater/task.go

112 lines
2.6 KiB
Go
Raw Normal View History

2018-12-07 10:21:23 +00:00
package imitater
2018-11-27 09:46:29 +00:00
import (
2018-12-06 10:34:10 +00:00
"time"
2018-12-07 11:09:38 +00:00
"474420502.top/eson/structure/circular_linked"
2018-12-07 10:21:23 +00:00
"474420502.top/eson/crontabex"
2018-11-27 09:46:29 +00:00
"474420502.top/eson/curl2info"
"474420502.top/eson/requests"
)
// Task 任务
type Task struct {
2018-11-30 10:46:49 +00:00
ITask
2018-12-07 10:21:23 +00:00
name string
2018-12-06 10:34:10 +00:00
crontab *crontab.Crontab
2018-11-30 10:46:49 +00:00
curl *curl2info.CURL
workflow *requests.Workflow
session *requests.Session
2018-12-07 11:09:38 +00:00
proxies clinked.CircularLinked
2018-11-27 09:46:29 +00:00
}
2018-12-07 10:21:23 +00:00
// SetName 任务的名字
func (task *Task) SetName(name string) {
task.name = name
}
// GetName 获取任务的名字
func (task *Task) GetName() string {
return task.name
}
// SetCurl 设置任务的curl信息类
2018-12-06 10:34:10 +00:00
func (task *Task) SetCurl(curl *curl2info.CURL) {
task.curl = curl
}
2018-11-27 09:46:29 +00:00
// GetCurl 获取任务的curl信息类
2018-12-07 10:21:23 +00:00
func (task *Task) GetCurl() *curl2info.CURL {
return task.curl
}
2018-11-27 09:46:29 +00:00
2018-12-07 11:09:38 +00:00
// AddProxies 添加代理集合
func (task *Task) AddProxies(proxy string) {
2018-12-09 13:39:44 +00:00
task.proxies.Append(proxy)
2018-12-06 10:34:10 +00:00
}
// GetProxies 获取代理的字符串
2018-12-07 11:09:38 +00:00
func (task *Task) GetProxies() *clinked.CircularLinked {
return &task.proxies
2018-12-06 10:34:10 +00:00
}
// SetCrontab 设置crontab的控制规则字符串
2018-12-06 10:34:10 +00:00
func (task *Task) SetCrontab(cron string) {
task.crontab = crontab.NewCrontab(cron)
}
// SetLastStatus 设置上次执行的状态 成功true 失败false
func (task *Task) SetLastStatus(status bool) {
task.crontab.SetStatus(status)
}
// TimeUp 判断是否到了下次执行的时间点
2018-12-06 20:47:22 +00:00
func (task *Task) TimeUp() bool {
return task.crontab.TimeUp()
}
// NextTime 下次执行的时间点
2018-12-06 20:47:22 +00:00
func (task *Task) NextTime() time.Time {
return task.crontab.NextTime()
}
2018-12-07 10:21:23 +00:00
// Workflow 根据persistent 是否返回现有session(or 新建 session),创建Workflow的信息, 便于设置或者更改参数, Session持久化
func (task *Task) Workflow(persistent bool) *requests.Workflow {
2018-12-06 20:47:22 +00:00
if persistent {
2018-12-09 13:39:44 +00:00
task.workflow = task.curl.CreateWorkflow(task.Session())
return task.workflow
}
2018-12-07 11:09:38 +00:00
proxies := task.GetProxies()
ses := task.curl.CreateSession()
if proxies.Size() > 0 {
2018-12-09 13:39:44 +00:00
proxy := proxies.Cursor().GetValue().(string)
ses.SetConfig(requests.CProxy, proxy)
2018-12-07 11:09:38 +00:00
proxies.MoveNext()
}
2018-12-09 13:39:44 +00:00
return task.curl.CreateWorkflow(ses)
}
// Request 根据curl信息执行,没持久化
func (task *Task) Request() (*requests.Response, error) {
return task.Workflow(false).Execute()
2018-12-06 20:47:22 +00:00
}
// Session 获取Session的信息 只保留session的数据和url参数.
func (task *Task) Session() *requests.Session {
if task.session == nil {
task.session = task.curl.CreateSession()
}
2018-12-07 11:09:38 +00:00
if task.proxies.Size() > 0 {
2018-12-09 13:39:44 +00:00
proxy := task.proxies.Cursor().GetValue().(string)
task.session.SetConfig(requests.CProxy, proxy)
2018-12-07 11:09:38 +00:00
task.proxies.MoveNext()
}
return task.session
}