fix: add new crontask module
This commit is contained in:
@@ -20,6 +20,15 @@ tasks:
|
||||
unit: Seconds #Seconds/Minutes/Hours/Days/Weeks, Monday/Tuesday/.../Sunday,
|
||||
at: 00:10:00 # do at time such as xx:xx:xx when unit such as Day/Days/Mondays...
|
||||
do: TaskHelloWorld # (Do what: callback function)
|
||||
- name: Cron user login OMC as startup
|
||||
status: Inactive
|
||||
uri: /login
|
||||
params:
|
||||
body: '{"username":"cronuser","password":"tcu@1000OMC!","code":"","uuid":""}'
|
||||
interval: 0
|
||||
unit: Startup
|
||||
at: 00:00:00
|
||||
do: TaskCronUserLoginOMC
|
||||
- name: clear expired history alarm
|
||||
uri: /api/rest/databaseManagement/v1/omc_db/alarm
|
||||
params: WHERE=now()+>+ADDDATE(event_time,+interval+(SELECT+`value`+FROM+config+WHERE+config_tag='historyDuration')+day)+and+alarm_status='0'
|
||||
@@ -46,8 +55,8 @@ tasks:
|
||||
params: WHERE=now()+>+ADDDATE(`create_time`,+interval+IFNULL((SELECT+`value`+FROM+config+WHERE+config_tag='BackUpSaveTime'),30)+day)
|
||||
interval: 1
|
||||
unit: Days
|
||||
at: 20:12:00
|
||||
do: TaskDeleteExpiredRecord
|
||||
at: 15:02:00
|
||||
do: TaskRemoveExpiredFile
|
||||
- name: update expired user session
|
||||
uri: /api/rest/databaseManagement/v1/omc_db/session
|
||||
params: WHERE=NOW()+>+ADDDATE(shake_time,+interval+expires+second)+and+status='online'
|
||||
|
||||
@@ -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