feat: 重构视频下载任务的触发逻辑,由简单的 tokio::sleep 迁移至调度器调度 (#529)
This commit is contained in:
@@ -240,6 +240,11 @@ class ApiClient {
|
||||
async getDashboard(): Promise<ApiResponse<DashBoardResponse>> {
|
||||
return this.get<DashBoardResponse>('/dashboard');
|
||||
}
|
||||
|
||||
async triggerDownloadTask(): Promise<ApiResponse<boolean>> {
|
||||
return this.post<boolean>('/task/download');
|
||||
}
|
||||
|
||||
subscribeToLogs(onMessage: (data: string) => void) {
|
||||
return wsManager.subscribeToLogs(onMessage);
|
||||
}
|
||||
@@ -281,6 +286,7 @@ const api = {
|
||||
getConfig: () => apiClient.getConfig(),
|
||||
updateConfig: (config: Config) => apiClient.updateConfig(config),
|
||||
getDashboard: () => apiClient.getDashboard(),
|
||||
triggerDownloadTask: () => apiClient.triggerDownloadTask(),
|
||||
subscribeToSysInfo: (onMessage: (data: SysInfo) => void) =>
|
||||
apiClient.subscribeToSysInfo(onMessage),
|
||||
|
||||
|
||||
@@ -286,6 +286,8 @@ export interface WebhookNotifier {
|
||||
|
||||
export type Notifier = TelegramNotifier | WebhookNotifier;
|
||||
|
||||
export type Trigger = number | string;
|
||||
|
||||
export interface Config {
|
||||
auth_token: string;
|
||||
bind_address: string;
|
||||
@@ -299,7 +301,7 @@ export interface Config {
|
||||
favorite_default_path: string;
|
||||
collection_default_path: string;
|
||||
submission_default_path: string;
|
||||
interval: number;
|
||||
interval: Trigger;
|
||||
upper_path: string;
|
||||
nfo_time_type: string;
|
||||
concurrent_limit: ConcurrentLimit;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '$lib/components/ui/card/index.js';
|
||||
import { Progress } from '$lib/components/ui/progress/index.js';
|
||||
import { Badge } from '$lib/components/ui/badge/index.js';
|
||||
import { Button } from '$lib/components/ui/button/index.js';
|
||||
import * as Chart from '$lib/components/ui/chart/index.js';
|
||||
import MyChartTooltip from '$lib/components/custom/my-chart-tooltip.svelte';
|
||||
import { curveNatural } from 'd3-shape';
|
||||
@@ -24,11 +25,13 @@
|
||||
import PlayIcon from '@lucide/svelte/icons/play';
|
||||
import CheckCircleIcon from '@lucide/svelte/icons/check-circle';
|
||||
import CalendarIcon from '@lucide/svelte/icons/calendar';
|
||||
import DownloadIcon from '@lucide/svelte/icons/download';
|
||||
|
||||
let dashboardData: DashBoardResponse | null = null;
|
||||
let sysInfo: SysInfo | null = null;
|
||||
let taskStatus: TaskStatus | null = null;
|
||||
let loading = false;
|
||||
let triggering = false;
|
||||
let unsubscribeSysInfo: (() => void) | null = null;
|
||||
let unsubscribeTasks: (() => void) | null = null;
|
||||
|
||||
@@ -59,6 +62,23 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function handleTriggerDownload() {
|
||||
triggering = true;
|
||||
try {
|
||||
await api.triggerDownloadTask();
|
||||
toast.success('已触发下载任务', {
|
||||
description: '任务将立即开始执行'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('触发下载任务失败:', error);
|
||||
toast.error('触发下载任务失败', {
|
||||
description: (error as ApiError).message
|
||||
});
|
||||
} finally {
|
||||
triggering = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
setBreadcrumb([{ label: '仪表盘' }]);
|
||||
|
||||
@@ -295,6 +315,8 @@
|
||||
<span class="text-muted-foreground text-sm">
|
||||
{taskStatus.last_run
|
||||
? new Date(taskStatus.last_run).toLocaleString('en-US', {
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
@@ -311,6 +333,8 @@
|
||||
<span class="text-muted-foreground text-sm">
|
||||
{taskStatus.last_finish
|
||||
? new Date(taskStatus.last_finish).toLocaleString('en-US', {
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
@@ -327,6 +351,8 @@
|
||||
<span class="text-muted-foreground text-sm">
|
||||
{taskStatus.next_run
|
||||
? new Date(taskStatus.next_run).toLocaleString('en-US', {
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
@@ -336,6 +362,21 @@
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-6 border-t pt-4">
|
||||
<Button
|
||||
class="w-full"
|
||||
size="sm"
|
||||
onclick={handleTriggerDownload}
|
||||
disabled={triggering || (taskStatus?.is_running ?? false)}
|
||||
>
|
||||
<DownloadIcon class="h-4 w-4" />
|
||||
{triggering
|
||||
? '触发中...'
|
||||
: taskStatus?.is_running
|
||||
? '任务运行中'
|
||||
: '立即执行下载任务'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="text-muted-foreground text-sm">加载中...</div>
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
import { Separator } from '$lib/components/ui/separator/index.js';
|
||||
import { Badge } from '$lib/components/ui/badge/index.js';
|
||||
import * as Dialog from '$lib/components/ui/dialog/index.js';
|
||||
import * as Tooltip from '$lib/components/ui/tooltip/index.js';
|
||||
import PasswordInput from '$lib/components/custom/password-input.svelte';
|
||||
import NotifierDialog from './NotifierDialog.svelte';
|
||||
import InfoIcon from '@lucide/svelte/icons/info';
|
||||
import api from '$lib/api';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { setBreadcrumb } from '$lib/stores/breadcrumb';
|
||||
@@ -21,6 +23,8 @@
|
||||
let saving = false;
|
||||
let loading = false;
|
||||
|
||||
let intervalInput: string = '1200';
|
||||
|
||||
// Notifier 管理相关
|
||||
let showNotifierDialog = false;
|
||||
let editingNotifier: Notifier | null = null;
|
||||
@@ -76,6 +80,13 @@
|
||||
const response = await api.getConfig();
|
||||
config = response.data;
|
||||
formData = { ...config };
|
||||
|
||||
// 根据 interval 的类型初始化输入框
|
||||
if (typeof formData.interval === 'number') {
|
||||
intervalInput = String(formData.interval);
|
||||
} else {
|
||||
intervalInput = formData.interval;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载配置失败:', error);
|
||||
toast.error('加载配置失败', {
|
||||
@@ -108,11 +119,32 @@
|
||||
toast.error('配置未加载');
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存前根据输入内容判断类型
|
||||
const trimmed = intervalInput.trim();
|
||||
const asNumber = Number(trimmed);
|
||||
|
||||
if (!isNaN(asNumber) && trimmed !== '') {
|
||||
// 纯数字,作为 Interval
|
||||
formData.interval = asNumber;
|
||||
} else {
|
||||
// 非数字,作为 Cron 表达式
|
||||
formData.interval = trimmed;
|
||||
}
|
||||
|
||||
saving = true;
|
||||
try {
|
||||
let resp = await api.updateConfig(formData);
|
||||
formData = resp.data;
|
||||
config = { ...formData };
|
||||
|
||||
// 更新输入框显示
|
||||
if (typeof formData.interval === 'number') {
|
||||
intervalInput = String(formData.interval);
|
||||
} else {
|
||||
intervalInput = formData.interval;
|
||||
}
|
||||
|
||||
toast.success('配置已保存');
|
||||
} catch (error) {
|
||||
console.error('保存配置失败:', error);
|
||||
@@ -214,8 +246,27 @@
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<Label for="interval">同步间隔(秒)</Label>
|
||||
<Input id="interval" type="number" min="60" bind:value={formData.interval} />
|
||||
<div class="flex items-center gap-1">
|
||||
<Label for="interval">任务触发条件</Label>
|
||||
<Tooltip.Root>
|
||||
<Tooltip.Trigger>
|
||||
<InfoIcon class="text-muted-foreground h-3.5 w-3.5" />
|
||||
</Tooltip.Trigger>
|
||||
<Tooltip.Content>
|
||||
<p class="text-xs">
|
||||
视频下载任务的触发条件,支持两种格式:<br />
|
||||
1. 输入数字表示间隔秒数,例如 1200 表示每隔 20 分钟触发一次; <br />
|
||||
2. 输入 Cron 表达式,格式为“秒 分 时 日 月 周”,例如“0 0 2 * * *”表示每天凌晨2点触发一次。
|
||||
</p>
|
||||
</Tooltip.Content>
|
||||
</Tooltip.Root>
|
||||
</div>
|
||||
<Input
|
||||
id="interval"
|
||||
type="text"
|
||||
bind:value={intervalInput}
|
||||
placeholder="1200 或 0 0 2 * * *"
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<Label for="video-name">视频名称模板</Label>
|
||||
|
||||
Reference in New Issue
Block a user