mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-18 11:26:10 +08:00
feat: The script library synchronizes and adds task logs (#8584)
This commit is contained in:
parent
f9a035f380
commit
123a2e8998
8 changed files with 39 additions and 15 deletions
|
@ -33,11 +33,15 @@ func (u *ScriptRepo) Get(opts ...DBOption) (model.ScriptLibrary, error) {
|
|||
|
||||
func (u *ScriptRepo) List(opts ...DBOption) ([]model.ScriptLibrary, error) {
|
||||
var ops []model.ScriptLibrary
|
||||
itemDB := global.DB
|
||||
if global.IsMaster {
|
||||
itemDB = global.CoreDB
|
||||
db := global.CoreDB.Model(&model.ScriptLibrary{})
|
||||
for _, opt := range opts {
|
||||
db = opt(db)
|
||||
}
|
||||
err := db.Where("is_interactive = ?", false).Find(&ops).Error
|
||||
return ops, err
|
||||
}
|
||||
db := itemDB.Model(&model.ScriptLibrary{})
|
||||
db := global.DB.Model(&model.ScriptLibrary{})
|
||||
for _, opt := range opts {
|
||||
db = opt(db)
|
||||
}
|
||||
|
|
|
@ -90,13 +90,19 @@ func (b *BaseApi) DeleteScript(c *gin.Context) {
|
|||
|
||||
// @Tags ScriptLibrary
|
||||
// @Summary Sync script from remote
|
||||
// @Accept json
|
||||
// @Param request body dto.OperateByTaskID true "request"
|
||||
// @Success 200
|
||||
// @Security ApiKeyAuth
|
||||
// @Security Timestamp
|
||||
// @Router /script/sync [post]
|
||||
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"同步脚本库脚本","formatEN":"sync scripts"}
|
||||
func (b *BaseApi) SyncScript(c *gin.Context) {
|
||||
if err := scriptService.Sync(); err != nil {
|
||||
var req dto.OperateByTaskID
|
||||
if err := helper.CheckBindAndValidate(&req, c); err != nil {
|
||||
return
|
||||
}
|
||||
if err := scriptService.Sync(req); err != nil {
|
||||
helper.InternalServer(c, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -51,3 +51,6 @@ type OperateByID struct {
|
|||
type OperateByIDs struct {
|
||||
IDs []uint `json:"ids"`
|
||||
}
|
||||
type OperateByTaskID struct {
|
||||
TaskID string `json:"taskID"`
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ type IScriptService interface {
|
|||
Create(req dto.ScriptOperate) error
|
||||
Update(req dto.ScriptOperate) error
|
||||
Delete(ids dto.OperateByIDs) error
|
||||
Sync() error
|
||||
Sync(req dto.OperateByTaskID) error
|
||||
}
|
||||
|
||||
func NewIScriptService() IScriptService {
|
||||
|
@ -173,8 +173,8 @@ func LoadScriptInfo(id uint) (model.ScriptLibrary, error) {
|
|||
return scriptRepo.Get(repo.WithByID(id))
|
||||
}
|
||||
|
||||
func (u *ScriptService) Sync() error {
|
||||
syncTask, err := task.NewTaskWithOps(i18n.GetMsgByKey("ScriptLibrary"), task.TaskSync, task.TaskScopeScript, "", 0)
|
||||
func (u *ScriptService) Sync(req dto.OperateByTaskID) error {
|
||||
syncTask, err := task.NewTaskWithOps(i18n.GetMsgByKey("ScriptLibrary"), task.TaskSync, task.TaskScopeScript, req.TaskID, 0)
|
||||
if err != nil {
|
||||
global.LOG.Errorf("create sync task failed %v", err)
|
||||
return err
|
||||
|
@ -189,7 +189,7 @@ func (u *ScriptService) Sync() error {
|
|||
var scriptSetting model.Setting
|
||||
_ = global.DB.Where("key = ?", "ScriptVersion").First(&scriptSetting).Error
|
||||
if scriptSetting.Value == string(versionRes) {
|
||||
syncTask.Log("The local and remote versions are detected to be consistent. Skip...")
|
||||
syncTask.Logf("The local-%s and remote-%s versions are detected to be consistent. Skip...", scriptSetting.Value, versionRes)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package job
|
||||
|
||||
import (
|
||||
"github.com/1Panel-dev/1Panel/core/app/dto"
|
||||
"github.com/1Panel-dev/1Panel/core/app/service"
|
||||
"github.com/1Panel-dev/1Panel/core/global"
|
||||
)
|
||||
|
@ -12,7 +13,7 @@ func NewScriptJob() *script {
|
|||
}
|
||||
|
||||
func (s *script) Run() {
|
||||
if err := service.NewIScriptService().Sync(); err != nil {
|
||||
if err := service.NewIScriptService().Sync(dto.OperateByTaskID{}); err != nil {
|
||||
global.LOG.Errorf("sync scripts from remote failed, err: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,8 +71,8 @@ export const searchScript = (params: SearchWithPage) => {
|
|||
export const addScript = (params: Cronjob.ScriptOperate) => {
|
||||
return http.post(`/core/script`, params);
|
||||
};
|
||||
export const syncScript = () => {
|
||||
return http.post(`/core/script/sync`, {}, TimeoutEnum.T_60S);
|
||||
export const syncScript = (taskID: string) => {
|
||||
return http.post(`/core/script/sync`, { taskID: taskID }, TimeoutEnum.T_60S);
|
||||
};
|
||||
export const editScript = (params: Cronjob.ScriptOperate) => {
|
||||
return http.post(`/core/script/update`, params);
|
||||
|
|
|
@ -27,6 +27,6 @@ const prop = defineProps({
|
|||
});
|
||||
|
||||
const toDoc = () => {
|
||||
router.push({ name: 'Library' });
|
||||
router.push({ name: 'Library', query: { uncached: 'true' } });
|
||||
};
|
||||
</script>
|
||||
|
|
|
@ -98,12 +98,14 @@
|
|||
<GroupDialog @search="loadGroupOptions" ref="dialogGroupRef" />
|
||||
<CodemirrorDrawer ref="myDetail" />
|
||||
<TerminalDialog ref="runRef" />
|
||||
<TaskLog ref="taskLogRef" width="70%" @close="search" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { dateFormat, deepCopy, getCurrentDateFormatted } from '@/utils/util';
|
||||
import { dateFormat, deepCopy, getCurrentDateFormatted, newUUID } from '@/utils/util';
|
||||
import GroupDialog from '@/components/group/index.vue';
|
||||
import TaskLog from '@/components/log/task/index.vue';
|
||||
import OperateDialog from '@/views/cronjob/library/operate/index.vue';
|
||||
import TerminalDialog from '@/views/cronjob/library/run/index.vue';
|
||||
import { deleteScript, searchScript, syncScript } from '@/api/modules/cronjob';
|
||||
|
@ -113,6 +115,7 @@ import i18n from '@/lang';
|
|||
import { GlobalStore } from '@/store';
|
||||
import { getGroupList } from '@/api/modules/group';
|
||||
import CodemirrorDrawer from '@/components/codemirror-pro/drawer.vue';
|
||||
import { MsgSuccess } from '@/utils/message';
|
||||
|
||||
const globalStore = GlobalStore();
|
||||
const mobile = computed(() => {
|
||||
|
@ -125,6 +128,7 @@ const selects = ref<any>([]);
|
|||
const opRef = ref();
|
||||
|
||||
const runRef = ref();
|
||||
const taskLogRef = ref();
|
||||
|
||||
const data = ref();
|
||||
const paginationConfig = reactive({
|
||||
|
@ -196,10 +200,12 @@ const onSync = async () => {
|
|||
type: 'info',
|
||||
}).then(async () => {
|
||||
loading.value = true;
|
||||
await syncScript()
|
||||
let taskID = newUUID();
|
||||
await syncScript(taskID)
|
||||
.then(() => {
|
||||
loading.value = false;
|
||||
search();
|
||||
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
|
||||
openTaskLog(taskID);
|
||||
})
|
||||
.catch(() => {
|
||||
loading.value = false;
|
||||
|
@ -207,6 +213,10 @@ const onSync = async () => {
|
|||
});
|
||||
};
|
||||
|
||||
const openTaskLog = (taskID: string) => {
|
||||
taskLogRef.value.openWithTaskID(taskID);
|
||||
};
|
||||
|
||||
const search = async () => {
|
||||
let params = {
|
||||
info: searchInfo.value,
|
||||
|
|
Loading…
Add table
Reference in a new issue