fix: add new crontask module
This commit is contained in:
@@ -127,6 +127,8 @@ func initCronTasks() {
|
||||
} else {
|
||||
gocron.Every(t.Interval).Sunday().At(t.At).DoSafely(taskFunc, t.Uri, t.Params, t.Body)
|
||||
}
|
||||
case "Startup":
|
||||
gocron.Every(0).DoSafely(taskFunc, t.Uri, t.Params, t.Body)
|
||||
default:
|
||||
log.Error("Error config:", t)
|
||||
}
|
||||
@@ -161,6 +163,14 @@ func initCronTabs() {
|
||||
job.Start()
|
||||
}
|
||||
|
||||
type LoginRespone struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
} `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type TaskJob struct {
|
||||
Do interface{}
|
||||
Uri string
|
||||
@@ -200,6 +210,43 @@ func (t *TaskFunc) TaskWithParams(a int, b string) {
|
||||
log.Trace(a, b)
|
||||
}
|
||||
|
||||
func (t *TaskFunc) TaskCronUserLoginOMC(uri, params, body string) {
|
||||
log.Debug("TaskCronUserLoginOMC processing... ")
|
||||
|
||||
var response *resty.Response
|
||||
requestURI := fmt.Sprintf("%s?%s", uri, params)
|
||||
requestURL := fmt.Sprintf("%s%s", yamlConfig.OMC.HostUri, requestURI)
|
||||
log.Debug("requestURL: POST ", requestURL)
|
||||
var loginBody string
|
||||
if body != "" {
|
||||
loginBody = body
|
||||
} else {
|
||||
loginBody = "{\"username\": \"cronuser\",\"password\": \"tcu@1000OMC!\",\"code\": \"\", \"uuid\": \"\"}"
|
||||
}
|
||||
client := resty.New()
|
||||
response, err := client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{"User-Agent": GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
SetBody(loginBody).
|
||||
Post(requestURL)
|
||||
if err != nil {
|
||||
log.Error("Failed to post:", err)
|
||||
}
|
||||
|
||||
log.Debug("StatusCode: ", response.StatusCode())
|
||||
switch response.StatusCode() {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
default:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TaskFunc) TaskDeleteExpiredRecord(uri, params, body string) {
|
||||
log.Debug("TaskDeleteExpiredRecord processing... ")
|
||||
|
||||
@@ -267,6 +314,77 @@ func (t *TaskFunc) TaskUpdateTable(uri, params, body string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TaskFunc) TaskRemoveExpiredFile(uri, params, body string) {
|
||||
log.Debug("TaskRemoveExpiredFile processing... ")
|
||||
|
||||
var response *resty.Response
|
||||
loginUri := "/login"
|
||||
loginBody := "{\"username\": \"cronuser\",\"password\": \"tcu@1000OMC!\",\"code\": \"\", \"uuid\": \"\"}"
|
||||
t.TaskCronUserLoginOMC(loginUri, "", loginBody)
|
||||
loginURI := fmt.Sprintf("%s?%s", loginUri, "")
|
||||
loginURL := fmt.Sprintf("%s%s", yamlConfig.OMC.HostUri, loginURI)
|
||||
log.Debug("requestURL: Post ", loginURL)
|
||||
client := resty.New()
|
||||
loginResponse, err := client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{"User-Agent": GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
SetBody(loginBody).
|
||||
Post(loginURL)
|
||||
if err != nil {
|
||||
log.Error("Failed to post:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var accessToken string
|
||||
log.Debug("StatusCode: ", loginResponse.StatusCode())
|
||||
switch loginResponse.StatusCode() {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
|
||||
log.Debug("response body:", string(loginResponse.Body()))
|
||||
var loginResp LoginRespone
|
||||
err = json.Unmarshal(loginResponse.Body(), &loginResp)
|
||||
if err != nil {
|
||||
log.Error("Failed to unmarshal:", err)
|
||||
return
|
||||
}
|
||||
if loginResp.Code == 1 {
|
||||
accessToken = loginResp.Data.AccessToken
|
||||
} else {
|
||||
log.Error("Failed to login: %s", loginResp.Msg)
|
||||
return
|
||||
}
|
||||
default:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
return
|
||||
}
|
||||
|
||||
requestURI := fmt.Sprintf("%s?%s", uri, params)
|
||||
requestURL := fmt.Sprintf("%s%s", yamlConfig.OMC.HostUri, requestURI)
|
||||
log.Debug("requestURL: DELETE ", requestURL)
|
||||
response, err = client.R().
|
||||
EnableTrace().
|
||||
SetHeaders(map[string]string{"Authorization": "Bearer " + accessToken}).
|
||||
SetHeaders(map[string]string{"User-Agent": GetDefaultUserAgent()}).
|
||||
SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
|
||||
Delete(requestURL)
|
||||
if err != nil {
|
||||
log.Error("Failed to delete:", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("StatusCode: ", response.StatusCode())
|
||||
switch response.StatusCode() {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
default:
|
||||
log.Debug("response body:", string(response.Body()))
|
||||
body := new(map[string]interface{})
|
||||
_ = json.Unmarshal(response.Body(), &body)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TaskFunc) GetTableNameFromUri(uri string) string {
|
||||
sa := global.SplitString(uri, "/")
|
||||
n := len(sa)
|
||||
|
||||
Reference in New Issue
Block a user