feat: refact scheduler
This commit is contained in:
@@ -5,6 +5,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import DirTreeSelect from '@/components/DirTreeSelect.vue'
|
||||
import { api, type Task, type RepoConfig, type Agent } from '@/api'
|
||||
@@ -48,15 +49,23 @@ const repoConfig = ref<RepoConfig>({
|
||||
branch: '',
|
||||
sparse_path: '',
|
||||
single_file: false,
|
||||
proxy: 'none',
|
||||
proxy_url: '',
|
||||
auth_token: ''
|
||||
auth_token: '',
|
||||
concurrency: 1,
|
||||
proxy: ''
|
||||
})
|
||||
const cleanType = ref('none')
|
||||
const cleanKeep = ref(30)
|
||||
const allAgents = ref<Agent[]>([])
|
||||
const selectedAgentId = ref<string>('local')
|
||||
|
||||
const concurrencyEnabled = computed({
|
||||
get: () => repoConfig.value.concurrency === 1,
|
||||
set: (val: boolean) => {
|
||||
repoConfig.value.concurrency = val ? 1 : 0
|
||||
}
|
||||
})
|
||||
|
||||
const cleanConfig = computed(() => {
|
||||
if (!cleanType.value || cleanType.value === 'none' || cleanKeep.value <= 0) return ''
|
||||
return JSON.stringify({ type: cleanType.value, keep: cleanKeep.value })
|
||||
@@ -80,14 +89,34 @@ watch(() => props.open, async (val) => {
|
||||
cleanKeep.value = 30
|
||||
}
|
||||
// 解析仓库配置
|
||||
if (props.task?.config) {
|
||||
// 解析仓库配置
|
||||
const defaultConfig: RepoConfig = {
|
||||
source_type: 'git',
|
||||
source_url: '',
|
||||
target_path: '',
|
||||
branch: '',
|
||||
sparse_path: '',
|
||||
single_file: false,
|
||||
proxy: 'none',
|
||||
proxy_url: '',
|
||||
auth_token: '',
|
||||
concurrency: 1
|
||||
}
|
||||
const configStr = props.task?.config
|
||||
if (configStr) {
|
||||
try {
|
||||
repoConfig.value = JSON.parse(props.task.config)
|
||||
const parsed = JSON.parse(configStr)
|
||||
// 兼容旧字段: 优先使用 $task_concurrency, 若无则默认 1
|
||||
let concurrency = 1
|
||||
if (parsed['$task_concurrency'] !== undefined) {
|
||||
concurrency = parsed['$task_concurrency'] === 1 ? 1 : 0
|
||||
}
|
||||
repoConfig.value = { ...defaultConfig, ...parsed, concurrency }
|
||||
} catch {
|
||||
repoConfig.value = { source_type: 'git', source_url: '', target_path: '', branch: '', sparse_path: '', single_file: false, proxy: 'none', proxy_url: '', auth_token: '' }
|
||||
repoConfig.value = defaultConfig
|
||||
}
|
||||
} else {
|
||||
repoConfig.value = { source_type: 'git', source_url: '', target_path: '', branch: '', sparse_path: '', single_file: false, proxy: 'none', proxy_url: '', auth_token: '' }
|
||||
repoConfig.value = defaultConfig
|
||||
}
|
||||
// 仓库任务暂时仅支持本地执行
|
||||
selectedAgentId.value = 'local'
|
||||
@@ -106,7 +135,15 @@ async function save() {
|
||||
try {
|
||||
form.value.clean_config = cleanConfig.value
|
||||
form.value.type = 'repo'
|
||||
form.value.config = JSON.stringify(repoConfig.value)
|
||||
// 确保 concurrency 字段被正确保存到 config 中
|
||||
// 注意:我们将 concurrency 存储在 config 的 $task_concurrency 字段中
|
||||
// 同时也保留在 repoConfig 对象中以便回显
|
||||
const configToSave: any = {
|
||||
...repoConfig.value,
|
||||
'$task_concurrency': repoConfig.value.concurrency !== undefined ? repoConfig.value.concurrency : 1
|
||||
}
|
||||
|
||||
form.value.config = JSON.stringify(configToSave)
|
||||
form.value.command = `[${repoConfig.value.source_type}] ${repoConfig.value.source_url}`
|
||||
form.value.agent_id = selectedAgentId.value === 'local' ? null : Number(selectedAgentId.value)
|
||||
if (props.isEdit && form.value.id) {
|
||||
@@ -204,6 +241,14 @@ async function save() {
|
||||
<Label class="sm:text-right text-sm">认证Token</Label>
|
||||
<Input v-model="repoConfig.auth_token" type="text" placeholder="可选,用于私有仓库" class="sm:col-span-3 h-8 text-sm" autocomplete="new-password" />
|
||||
</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 flex items-center gap-2">
|
||||
<Switch v-model:checked="concurrencyEnabled" />
|
||||
<span class="text-sm text-muted-foreground">允许并发</span>
|
||||
<span class="text-xs text-muted-foreground ml-2">(如果任务未执行完成,是否允许再次执行)</span>
|
||||
</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 0 0 * * *" class="sm:col-span-3 h-8 text-sm font-mono" />
|
||||
|
||||
@@ -4,6 +4,7 @@ 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'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||
import DirTreeSelect from '@/components/DirTreeSelect.vue'
|
||||
@@ -44,6 +45,13 @@ const selectedAgentId = ref<string>('local')
|
||||
const envSearchQuery = ref('')
|
||||
// 为每个执行位置保存独立的工作目录配置
|
||||
const workDirCache = ref<Record<string, string>>({})
|
||||
const concurrency = ref(0)
|
||||
const concurrencyEnabled = ref(false)
|
||||
|
||||
// 监听 concurrencyEnabled 的变化,同步到 concurrency
|
||||
watch(concurrencyEnabled, (val) => {
|
||||
concurrency.value = val ? 1 : 0
|
||||
})
|
||||
|
||||
// 当前显示的工作目录(根据选择的执行位置)
|
||||
const currentWorkDir = computed({
|
||||
@@ -93,6 +101,36 @@ watch(() => props.open, async (val) => {
|
||||
cleanType.value = 'none'
|
||||
cleanKeep.value = 30
|
||||
}
|
||||
// 解析任务配置
|
||||
try {
|
||||
// 确保 config 是有效的 JSON 对象字符串
|
||||
let configStr = props.task?.config
|
||||
// 如果是 null/undefined 或者空字符串,初始化为 '{}'
|
||||
if (!configStr) {
|
||||
configStr = '{}'
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
// 解析环境变量
|
||||
if (props.task?.envs) {
|
||||
selectedEnvIds.value = props.task.envs.split(',').map(s => parseInt(s.trim())).filter(n => !isNaN(n))
|
||||
@@ -140,8 +178,31 @@ async function save() {
|
||||
form.value.envs = selectedEnvIds.value.join(',')
|
||||
form.value.type = 'task'
|
||||
form.value.agent_id = selectedAgentId.value === 'local' ? null : Number(selectedAgentId.value)
|
||||
|
||||
// 保存配置 - 确保 concurrency 字段被正确保存
|
||||
let config: Record<string, any> = {}
|
||||
|
||||
// 如果 form.value.config 存在,先解析它以保留其他配置
|
||||
if (form.value.config) {
|
||||
try {
|
||||
const parsed = JSON.parse(form.value.config)
|
||||
if (parsed && typeof parsed === 'object') {
|
||||
config = parsed
|
||||
}
|
||||
} catch {
|
||||
config = {}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新并发控制字段 (1: 开启, 0: 关闭)
|
||||
config['$task_concurrency'] = concurrency.value
|
||||
|
||||
// 重新序列化配置
|
||||
form.value.config = JSON.stringify(config)
|
||||
|
||||
// 保存当前选择的执行位置对应的工作目录
|
||||
form.value.work_dir = currentWorkDir.value
|
||||
|
||||
if (props.isEdit && form.value.id) {
|
||||
await api.tasks.update(form.value.id, form.value)
|
||||
toast.success('任务已更新')
|
||||
@@ -151,7 +212,9 @@ async function save() {
|
||||
}
|
||||
emit('update:open', false)
|
||||
emit('saved')
|
||||
} catch { toast.error('保存失败') }
|
||||
} catch (error) {
|
||||
toast.error('保存失败')
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -185,7 +248,7 @@ async function save() {
|
||||
<SelectValue placeholder="选择执行位置" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="local">本地执行</SelectItem>
|
||||
<SelectItem value="local">本地执行</SelectItem>
|
||||
<SelectItem v-for="agent in onlineAgents" :key="agent.id" :value="String(agent.id)">
|
||||
{{ agent.name }} ({{ agent.status === 'online' ? '在线' : '离线' }})
|
||||
</SelectItem>
|
||||
@@ -235,6 +298,16 @@ async function save() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<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>
|
||||
<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">
|
||||
|
||||
@@ -6,7 +6,7 @@ import { Input } from '@/components/ui/input'
|
||||
import Pagination from '@/components/Pagination.vue'
|
||||
import TaskDialog from './TaskDialog.vue'
|
||||
import RepoDialog from './RepoDialog.vue'
|
||||
import { Plus, Play, Pencil, Trash2, Search, ScrollText, GitBranch, Terminal, Server, Monitor, X } from 'lucide-vue-next'
|
||||
import { Plus, Play, Pencil, Trash2, Search, ScrollText, GitBranch, Terminal, Server, Monitor, X, Loader2 } from 'lucide-vue-next'
|
||||
import { api, type Task, type Agent } from '@/api'
|
||||
import { toast } from 'vue-sonner'
|
||||
import { useSiteSettings } from '@/composables/useSiteSettings'
|
||||
@@ -137,8 +137,22 @@ async function deleteTask() {
|
||||
deleteTaskId.value = null
|
||||
}
|
||||
|
||||
const executingTaskId = ref<number | null>(null)
|
||||
|
||||
async function runTask(id: number) {
|
||||
try { await api.tasks.execute(id); toast.success('任务已执行') } catch { toast.error('执行失败') }
|
||||
executingTaskId.value = id
|
||||
toast.message('正在执行...', { id: 'executing' })
|
||||
try {
|
||||
const res = await api.tasks.execute(id)
|
||||
if (res.Success === false) {
|
||||
throw new Error(res.Error || '执行失败')
|
||||
}
|
||||
toast.success('触发成功', { id: 'executing' })
|
||||
} catch (error: any) {
|
||||
toast.error(error?.message || '执行失败', { id: 'executing' })
|
||||
} finally {
|
||||
executingTaskId.value = null
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleTask(task: Task, enabled: boolean) {
|
||||
@@ -252,8 +266,9 @@ watch(() => route.query.agent_id, (newVal) => {
|
||||
</span>
|
||||
</span>
|
||||
<span class="w-20 sm:w-36 shrink-0 flex justify-center gap-0.5 sm:gap-1">
|
||||
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="runTask(task.id)" title="执行">
|
||||
<Play class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
|
||||
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="runTask(task.id)" title="执行" :disabled="executingTaskId === task.id">
|
||||
<Loader2 v-if="executingTaskId === task.id" class="h-3 w-3 sm:h-3.5 sm:w-3.5 animate-spin" />
|
||||
<Play v-else class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
|
||||
</Button>
|
||||
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="viewLogs(task.id)" title="日志">
|
||||
<ScrollText class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
|
||||
|
||||
Reference in New Issue
Block a user