mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-31 14:08:06 +08:00
feat: 定时访问网站功能实现
This commit is contained in:
parent
ada58ecb8c
commit
082ef77a61
@ -2,7 +2,10 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
@ -82,14 +85,21 @@ func (u *CronjobService) Create(cronjobDto dto.CronjobCreate) error {
|
|||||||
if err := cronjobRepo.Create(&cronjob); err != nil {
|
if err := cronjobRepo.Create(&cronjob); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
var (
|
||||||
|
entryID int
|
||||||
|
err error
|
||||||
|
)
|
||||||
switch cronjobDto.Type {
|
switch cronjobDto.Type {
|
||||||
case "shell":
|
case "shell":
|
||||||
entryID, err := u.AddShellJob(&cronjob)
|
entryID, err = u.AddShellJob(&cronjob)
|
||||||
if err != nil {
|
case "curl":
|
||||||
return err
|
entryID, err = u.AddCurlJob(&cronjob)
|
||||||
}
|
|
||||||
_ = cronjobRepo.Update(cronjob.ID, map[string]interface{}{"entry_id": entryID})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_ = cronjobRepo.Update(cronjob.ID, map[string]interface{}{"entry_id": entryID})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,15 +146,47 @@ func (u *CronjobService) AddShellJob(cronjob *model.Cronjob) (int, error) {
|
|||||||
return int(entryID), nil
|
return int(entryID), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *CronjobService) AddCurlJob(cronjob *model.Cronjob) (int, error) {
|
||||||
|
addFunc := func() {
|
||||||
|
record := cronjobRepo.StartRecords(cronjob.ID, "")
|
||||||
|
if len(cronjob.URL) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
client := &http.Client{Timeout: 1 * time.Second, Transport: tr}
|
||||||
|
request, _ := http.NewRequest("GET", cronjob.URL, nil)
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
cronjobRepo.EndRecords(record, constant.StatusFailed, err.Error(), "")
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
stdout, _ := ioutil.ReadAll(response.Body)
|
||||||
|
|
||||||
|
record.Records, err = mkdirAndWriteFile(cronjob.ID, cronjob.Name, record.StartTime, stdout)
|
||||||
|
if err != nil {
|
||||||
|
record.Records = "ERR_CREATE_FILE"
|
||||||
|
global.LOG.Errorf("save file %s failed, err: %v", record.Records, err)
|
||||||
|
}
|
||||||
|
cronjobRepo.EndRecords(record, constant.StatusSuccess, "", record.Records)
|
||||||
|
}
|
||||||
|
entryID, err := global.Cron.AddFunc(cronjob.Spec, addFunc)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int(entryID), nil
|
||||||
|
}
|
||||||
|
|
||||||
func mkdirAndWriteFile(id uint, name string, startTime time.Time, msg []byte) (string, error) {
|
func mkdirAndWriteFile(id uint, name string, startTime time.Time, msg []byte) (string, error) {
|
||||||
dir := fmt.Sprintf("/opt/1Panel/data/cron/%s%v", name, id)
|
dir := fmt.Sprintf("/opt/1Panel/task/%s%v/log", name, id)
|
||||||
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
path := fmt.Sprintf("%s/%s", dir, startTime.Format("20060102150405"))
|
path := fmt.Sprintf("%s/%s.log", dir, startTime.Format("20060102150405"))
|
||||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -95,11 +95,11 @@
|
|||||||
trigger="click"
|
trigger="click"
|
||||||
style="white-space: pre-wrap"
|
style="white-space: pre-wrap"
|
||||||
>
|
>
|
||||||
<div style="margin-left: 20px">
|
<div style="margin-left: 20px; height: 400px; overflow: auto">
|
||||||
<span style="white-space: pre-wrap">{{ dialogData.rowData!.script }}</span>
|
<span style="white-space: pre-wrap">{{ dialogData.rowData!.script }}</span>
|
||||||
</div>
|
</div>
|
||||||
<template #reference>
|
<template #reference>
|
||||||
<el-button link>{{ $t('commons.button.expand') }}</el-button>
|
<el-button type="primary" link>{{ $t('commons.button.expand') }}</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-popover>
|
</el-popover>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
@ -130,7 +130,7 @@
|
|||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :span="8" v-if="dialogData.rowData!.type === 'curl'">
|
<el-col :span="8" v-if="dialogData.rowData!.type === 'curl'">
|
||||||
<el-form-item :label="$t('cronjob.url') + 'URL'">
|
<el-form-item :label="$t('cronjob.url')">
|
||||||
{{ dialogData.rowData!.url }}
|
{{ dialogData.rowData!.url }}
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
@ -182,13 +182,13 @@
|
|||||||
trigger="click"
|
trigger="click"
|
||||||
style="white-space: pre-wrap"
|
style="white-space: pre-wrap"
|
||||||
>
|
>
|
||||||
<div style="margin-left: 20px">
|
<div style="margin-left: 20px; height: 400px; overflow: auto">
|
||||||
<span style="white-space: pre-wrap">
|
<span style="white-space: pre-wrap">
|
||||||
{{ currentRecordDetail }}
|
{{ currentRecordDetail }}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<template #reference>
|
<template #reference>
|
||||||
<el-button link @click="loadRecord(currentRecord?.records!)">
|
<el-button type="primary" link @click="loadRecord(currentRecord?.records!)">
|
||||||
{{ $t('commons.button.expand') }}
|
{{ $t('commons.button.expand') }}
|
||||||
</el-button>
|
</el-button>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user