2025-12-25 20:28:20 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref, computed, watch } from 'vue'
|
|
|
|
|
import { Button } from '@/components/ui/button'
|
|
|
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'
|
|
|
|
|
import { Input } from '@/components/ui/input'
|
|
|
|
|
import { Label } from '@/components/ui/label'
|
2026-02-07 21:44:09 +08:00
|
|
|
import { Switch } from '@/components/ui/switch'
|
2025-12-25 20:28:20 +08:00
|
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
|
|
|
|
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
|
|
|
|
import DirTreeSelect from '@/components/DirTreeSelect.vue'
|
|
|
|
|
import { Plus, ChevronDown, X } from 'lucide-vue-next'
|
2025-12-28 11:42:58 +08:00
|
|
|
import { api, type Task, type EnvVar, type Agent } from '@/api'
|
2025-12-25 20:28:20 +08:00
|
|
|
import { toast } from 'vue-sonner'
|
|
|
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
|
open: boolean
|
|
|
|
|
task?: Partial<Task>
|
|
|
|
|
isEdit: boolean
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
'update:open': [value: boolean]
|
|
|
|
|
'saved': []
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
const cronPresets = [
|
|
|
|
|
{ label: '每5秒', value: '*/5 * * * * *' },
|
|
|
|
|
{ label: '每30秒', value: '*/30 * * * * *' },
|
|
|
|
|
{ label: '每分钟', value: '0 * * * * *' },
|
|
|
|
|
{ label: '每5分钟', value: '0 */5 * * * *' },
|
|
|
|
|
{ label: '每小时', value: '0 0 * * * *' },
|
|
|
|
|
{ label: '每天0点', value: '0 0 0 * * *' },
|
|
|
|
|
{ label: '每天8点', value: '0 0 8 * * *' },
|
|
|
|
|
{ label: '每周一', value: '0 0 0 * * 1' },
|
|
|
|
|
{ label: '每月1号', value: '0 0 0 1 * *' },
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const form = ref<Partial<Task>>({})
|
|
|
|
|
const cleanType = ref('none')
|
|
|
|
|
const cleanKeep = ref(30)
|
|
|
|
|
const allEnvVars = ref<EnvVar[]>([])
|
2025-12-28 11:42:58 +08:00
|
|
|
const allAgents = ref<Agent[]>([])
|
2025-12-25 20:28:20 +08:00
|
|
|
const selectedEnvIds = ref<number[]>([])
|
2025-12-28 11:42:58 +08:00
|
|
|
const selectedAgentId = ref<string>('local')
|
2025-12-25 20:28:20 +08:00
|
|
|
const envSearchQuery = ref('')
|
2026-01-02 11:45:29 +08:00
|
|
|
// 为每个执行位置保存独立的工作目录配置
|
|
|
|
|
const workDirCache = ref<Record<string, string>>({})
|
2026-02-07 21:44:09 +08:00
|
|
|
const concurrency = ref(0)
|
|
|
|
|
const concurrencyEnabled = ref(false)
|
|
|
|
|
|
|
|
|
|
// 监听 concurrencyEnabled 的变化,同步到 concurrency
|
|
|
|
|
watch(concurrencyEnabled, (val) => {
|
|
|
|
|
concurrency.value = val ? 1 : 0
|
|
|
|
|
})
|
2026-01-02 11:45:29 +08:00
|
|
|
|
|
|
|
|
// 当前显示的工作目录(根据选择的执行位置)
|
|
|
|
|
const currentWorkDir = computed({
|
|
|
|
|
get: () => workDirCache.value[selectedAgentId.value] || '',
|
|
|
|
|
set: (val) => {
|
|
|
|
|
workDirCache.value[selectedAgentId.value] = val
|
|
|
|
|
}
|
|
|
|
|
})
|
2025-12-25 20:28:20 +08:00
|
|
|
|
|
|
|
|
const cleanConfig = computed(() => {
|
|
|
|
|
if (!cleanType.value || cleanType.value === 'none' || cleanKeep.value <= 0) return ''
|
|
|
|
|
return JSON.stringify({ type: cleanType.value, keep: cleanKeep.value })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const filteredEnvVars = computed(() => {
|
|
|
|
|
return allEnvVars.value.filter(env => {
|
|
|
|
|
const matchSearch = !envSearchQuery.value || env.name.toLowerCase().includes(envSearchQuery.value.toLowerCase())
|
|
|
|
|
const notSelected = !selectedEnvIds.value.includes(env.id)
|
|
|
|
|
return matchSearch && notSelected
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const selectedEnvs = computed(() => {
|
|
|
|
|
return selectedEnvIds.value
|
|
|
|
|
.map(id => allEnvVars.value.find(e => e.id === id))
|
|
|
|
|
.filter((e): e is EnvVar => e !== undefined)
|
|
|
|
|
})
|
|
|
|
|
|
2025-12-28 11:42:58 +08:00
|
|
|
const onlineAgents = computed(() => {
|
|
|
|
|
return allAgents.value.filter(a => a.enabled)
|
|
|
|
|
})
|
|
|
|
|
|
2025-12-25 20:28:20 +08:00
|
|
|
watch(() => props.open, async (val) => {
|
|
|
|
|
if (val) {
|
|
|
|
|
form.value = { ...props.task }
|
|
|
|
|
// 解析清理配置
|
|
|
|
|
if (props.task?.clean_config) {
|
|
|
|
|
try {
|
|
|
|
|
const config = JSON.parse(props.task.clean_config)
|
|
|
|
|
cleanType.value = config.type || 'none'
|
|
|
|
|
cleanKeep.value = config.keep || 30
|
|
|
|
|
} catch {
|
|
|
|
|
cleanType.value = 'none'
|
|
|
|
|
cleanKeep.value = 30
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
cleanType.value = 'none'
|
|
|
|
|
cleanKeep.value = 30
|
|
|
|
|
}
|
2026-02-07 21:44:09 +08:00
|
|
|
// 解析任务配置
|
|
|
|
|
try {
|
|
|
|
|
// 确保 config 是有效的 JSON 对象字符串
|
|
|
|
|
let configStr = props.task?.config
|
|
|
|
|
// 如果是 null/undefined 或者空字符串,初始化为 '{}'
|
|
|
|
|
if (!configStr) {
|
|
|
|
|
configStr = '{}'
|
|
|
|
|
}
|
2026-02-10 18:58:21 +08:00
|
|
|
|
2026-02-07 21:44:09 +08:00
|
|
|
const parsed = JSON.parse(configStr)
|
|
|
|
|
// 确保解析结果是对象
|
|
|
|
|
if (parsed && typeof parsed === 'object') {
|
|
|
|
|
const val = parsed['$task_concurrency']
|
|
|
|
|
if (typeof val === 'number') {
|
|
|
|
|
// 如果已存在并发配置,直接使用(0 或 1)
|
|
|
|
|
concurrency.value = val
|
|
|
|
|
concurrencyEnabled.value = val === 1
|
|
|
|
|
} else {
|
|
|
|
|
// 默认值:允许并发
|
|
|
|
|
concurrency.value = 1
|
|
|
|
|
concurrencyEnabled.value = true
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
concurrency.value = 1
|
|
|
|
|
concurrencyEnabled.value = true
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
concurrency.value = 1
|
|
|
|
|
concurrencyEnabled.value = true
|
|
|
|
|
}
|
2025-12-25 20:28:20 +08:00
|
|
|
// 解析环境变量
|
|
|
|
|
if (props.task?.envs) {
|
|
|
|
|
selectedEnvIds.value = props.task.envs.split(',').map(s => parseInt(s.trim())).filter(n => !isNaN(n))
|
|
|
|
|
} else {
|
|
|
|
|
selectedEnvIds.value = []
|
|
|
|
|
}
|
2026-01-02 11:45:29 +08:00
|
|
|
// 解析 Agent 和工作目录
|
|
|
|
|
const agentId = props.task?.agent_id ? String(props.task.agent_id) : 'local'
|
|
|
|
|
selectedAgentId.value = agentId
|
|
|
|
|
// 初始化工作目录缓存,将当前任务的工作目录保存到对应的执行位置
|
|
|
|
|
workDirCache.value = {
|
|
|
|
|
[agentId]: props.task?.work_dir || ''
|
|
|
|
|
}
|
2025-12-25 20:28:20 +08:00
|
|
|
envSearchQuery.value = ''
|
2025-12-28 11:42:58 +08:00
|
|
|
// 加载数据
|
|
|
|
|
await loadData()
|
2025-12-25 20:28:20 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-12-28 11:42:58 +08:00
|
|
|
async function loadData() {
|
|
|
|
|
try {
|
|
|
|
|
const [envs, agents] = await Promise.all([
|
|
|
|
|
api.env.all(),
|
|
|
|
|
api.agents.list()
|
|
|
|
|
])
|
|
|
|
|
allEnvVars.value = envs
|
|
|
|
|
allAgents.value = agents
|
|
|
|
|
} catch { /* ignore */ }
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-25 20:28:20 +08:00
|
|
|
function addEnv(id: number) {
|
|
|
|
|
if (!selectedEnvIds.value.includes(id)) {
|
|
|
|
|
selectedEnvIds.value.push(id)
|
|
|
|
|
}
|
|
|
|
|
envSearchQuery.value = ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeEnv(id: number) {
|
2026-01-15 19:18:50 +08:00
|
|
|
selectedEnvIds.value = selectedEnvIds.value.filter(envId => envId !== id)
|
2025-12-25 20:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function save() {
|
|
|
|
|
try {
|
|
|
|
|
form.value.clean_config = cleanConfig.value
|
|
|
|
|
form.value.envs = selectedEnvIds.value.join(',')
|
|
|
|
|
form.value.type = 'task'
|
2025-12-28 11:42:58 +08:00
|
|
|
form.value.agent_id = selectedAgentId.value === 'local' ? null : Number(selectedAgentId.value)
|
2026-02-10 18:58:21 +08:00
|
|
|
|
2026-02-07 21:44:09 +08:00
|
|
|
// 保存配置 - 确保 concurrency 字段被正确保存
|
|
|
|
|
let config: Record<string, any> = {}
|
2026-02-10 18:58:21 +08:00
|
|
|
|
2026-02-07 21:44:09 +08:00
|
|
|
// 如果 form.value.config 存在,先解析它以保留其他配置
|
|
|
|
|
if (form.value.config) {
|
|
|
|
|
try {
|
|
|
|
|
const parsed = JSON.parse(form.value.config)
|
|
|
|
|
if (parsed && typeof parsed === 'object') {
|
|
|
|
|
config = parsed
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
config = {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-10 18:58:21 +08:00
|
|
|
|
2026-02-07 21:44:09 +08:00
|
|
|
// 更新并发控制字段 (1: 开启, 0: 关闭)
|
|
|
|
|
config['$task_concurrency'] = concurrency.value
|
2026-02-10 18:58:21 +08:00
|
|
|
|
2026-02-07 21:44:09 +08:00
|
|
|
// 重新序列化配置
|
|
|
|
|
form.value.config = JSON.stringify(config)
|
|
|
|
|
|
2026-01-02 11:45:29 +08:00
|
|
|
// 保存当前选择的执行位置对应的工作目录
|
|
|
|
|
form.value.work_dir = currentWorkDir.value
|
2026-02-10 18:58:21 +08:00
|
|
|
|
2025-12-25 20:28:20 +08:00
|
|
|
if (props.isEdit && form.value.id) {
|
|
|
|
|
await api.tasks.update(form.value.id, form.value)
|
|
|
|
|
toast.success('任务已更新')
|
|
|
|
|
} else {
|
|
|
|
|
await api.tasks.create(form.value)
|
|
|
|
|
toast.success('任务已创建')
|
|
|
|
|
}
|
|
|
|
|
emit('update:open', false)
|
|
|
|
|
emit('saved')
|
2026-02-10 18:58:21 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
toast.error('保存失败')
|
2026-02-07 21:44:09 +08:00
|
|
|
}
|
2025-12-25 20:28:20 +08:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<Dialog :open="open" @update:open="emit('update:open', $event)">
|
2026-02-10 18:58:21 +08:00
|
|
|
<DialogContent class="sm:max-w-[480px]" @openAutoFocus.prevent>
|
2025-12-25 20:28:20 +08:00
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>{{ isEdit ? '编辑任务' : '新建任务' }}</DialogTitle>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<div class="space-y-3 py-3">
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm">任务名称</Label>
|
|
|
|
|
<Input v-model="form.name" placeholder="我的任务" class="sm:col-span-3 h-8 text-sm" />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm">执行命令</Label>
|
|
|
|
|
<Input v-model="form.command" placeholder="node script.js" class="sm:col-span-3 h-8 text-sm font-mono" />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm">工作目录</Label>
|
|
|
|
|
<div class="sm:col-span-3">
|
2026-01-02 11:45:29 +08:00
|
|
|
<DirTreeSelect v-if="selectedAgentId === 'local'" v-model="currentWorkDir" />
|
|
|
|
|
<Input v-else v-model="currentWorkDir" placeholder="工作目录(可选)" class="h-8 text-sm" />
|
2025-12-28 11:42:58 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm">执行位置</Label>
|
|
|
|
|
<div class="sm:col-span-3">
|
|
|
|
|
<Select v-model="selectedAgentId">
|
|
|
|
|
<SelectTrigger class="h-8 text-sm">
|
|
|
|
|
<SelectValue placeholder="选择执行位置" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
2026-02-10 18:58:21 +08:00
|
|
|
<SelectItem value="local">本地执行</SelectItem>
|
2025-12-28 11:42:58 +08:00
|
|
|
<SelectItem v-for="agent in onlineAgents" :key="agent.id" :value="String(agent.id)">
|
|
|
|
|
{{ agent.name }} ({{ agent.status === 'online' ? '在线' : '离线' }})
|
|
|
|
|
</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
2025-12-25 20:28:20 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm">定时规则</Label>
|
|
|
|
|
<Input v-model="form.schedule" placeholder="0 * * * * *" class="sm:col-span-3 h-8 text-sm font-mono" />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-start gap-2 sm:gap-3">
|
|
|
|
|
<span></span>
|
|
|
|
|
<div class="sm:col-span-3">
|
|
|
|
|
<p class="text-xs text-muted-foreground mb-1.5">格式: 秒 分 时 日 月 周</p>
|
|
|
|
|
<div class="flex flex-wrap gap-1">
|
2026-02-10 18:58:21 +08:00
|
|
|
<span v-for="preset in cronPresets" :key="preset.value"
|
2025-12-25 20:28:20 +08:00
|
|
|
class="px-1.5 py-0.5 text-xs rounded bg-muted hover:bg-accent cursor-pointer transition-colors"
|
2026-02-10 18:58:21 +08:00
|
|
|
@click="form.schedule = preset.value">
|
2025-12-25 20:28:20 +08:00
|
|
|
{{ preset.label }}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm">超时/清理</Label>
|
2026-01-08 21:29:26 +08:00
|
|
|
<div class="sm:col-span-3 flex flex-wrap items-center gap-2">
|
2025-12-25 20:28:20 +08:00
|
|
|
<div class="flex items-center gap-1.5">
|
2026-01-08 21:29:26 +08:00
|
|
|
<Input v-model.number="form.timeout" type="number" placeholder="30" class="w-20 h-9 text-sm" />
|
|
|
|
|
<span class="text-sm text-muted-foreground whitespace-nowrap">分钟</span>
|
2025-12-25 20:28:20 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="flex items-center gap-1.5">
|
|
|
|
|
<Select :model-value="cleanType" @update:model-value="(v) => cleanType = String(v || 'none')">
|
2026-01-08 21:29:26 +08:00
|
|
|
<SelectTrigger class="w-24 h-9 text-sm">
|
2025-12-25 20:28:20 +08:00
|
|
|
<SelectValue placeholder="不清理" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
<SelectItem value="none">不清理</SelectItem>
|
|
|
|
|
<SelectItem value="day">按天数</SelectItem>
|
|
|
|
|
<SelectItem value="count">按条数</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
2026-02-10 18:58:21 +08:00
|
|
|
<Input v-if="cleanType && cleanType !== 'none'" v-model.number="cleanKeep" type="number"
|
|
|
|
|
:placeholder="cleanType === 'day' ? '7' : '100'" class="w-20 h-9 text-sm" />
|
2025-12-25 20:28:20 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-02-07 21:44:09 +08:00
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-start gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm pt-2">并发控制</Label>
|
|
|
|
|
<div class="sm:col-span-3 space-y-1.5">
|
|
|
|
|
<div class="flex items-center gap-2">
|
|
|
|
|
<Switch v-model="concurrencyEnabled" />
|
|
|
|
|
<span class="text-sm text-muted-foreground">允许并发</span>
|
|
|
|
|
</div>
|
|
|
|
|
<p class="text-xs text-muted-foreground">如果任务未执行完成,是否允许再次执行</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-12-25 20:28:20 +08:00
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-4 items-start gap-2 sm:gap-3">
|
|
|
|
|
<Label class="sm:text-right text-sm pt-1.5">环境变量</Label>
|
|
|
|
|
<div class="sm:col-span-3 space-y-1.5">
|
|
|
|
|
<Popover>
|
|
|
|
|
<PopoverTrigger as-child>
|
|
|
|
|
<Button variant="outline" class="w-full justify-between font-normal h-8 text-sm">
|
|
|
|
|
<span class="text-muted-foreground text-xs">搜索并添加环境变量...</span>
|
|
|
|
|
<ChevronDown class="h-3.5 w-3.5 shrink-0 opacity-50" />
|
|
|
|
|
</Button>
|
|
|
|
|
</PopoverTrigger>
|
|
|
|
|
<PopoverContent class="w-[260px] p-2" align="start">
|
|
|
|
|
<Input v-model="envSearchQuery" placeholder="搜索环境变量..." class="mb-2 h-7 text-sm" />
|
|
|
|
|
<div v-if="filteredEnvVars.length === 0" class="text-xs text-muted-foreground text-center py-2">
|
|
|
|
|
{{ allEnvVars.length === 0 ? '暂无环境变量' : '无匹配结果' }}
|
|
|
|
|
</div>
|
|
|
|
|
<div v-else class="max-h-[140px] overflow-y-auto space-y-0.5">
|
2026-02-10 18:58:21 +08:00
|
|
|
<div v-for="env in filteredEnvVars" :key="env.id"
|
2025-12-25 20:28:20 +08:00
|
|
|
class="flex items-center gap-2 px-2 py-1 rounded hover:bg-muted cursor-pointer text-xs"
|
2026-02-10 18:58:21 +08:00
|
|
|
@click="addEnv(env.id)">
|
2025-12-25 20:28:20 +08:00
|
|
|
<Plus class="h-3 w-3 text-muted-foreground" />
|
|
|
|
|
<span class="truncate">{{ env.name }}</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
</Popover>
|
|
|
|
|
<div v-if="selectedEnvs.length > 0" class="flex flex-wrap gap-1">
|
2026-02-10 18:58:21 +08:00
|
|
|
<div v-for="env in selectedEnvs" :key="env.id"
|
|
|
|
|
class="inline-flex items-center gap-0.5 px-2 py-0.5 text-xs h-5 rounded-full bg-secondary text-secondary-foreground">
|
2026-01-15 19:18:50 +08:00
|
|
|
<span>{{ env.name }}</span>
|
|
|
|
|
<X class="h-2.5 w-2.5 cursor-pointer hover:text-destructive" @click="removeEnv(env.id)" />
|
|
|
|
|
</div>
|
2025-12-25 20:28:20 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" size="sm" @click="emit('update:open', false)">取消</Button>
|
|
|
|
|
<Button size="sm" @click="save">保存</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</template>
|