简单的定时任务--ok
This commit is contained in:
@@ -30,3 +30,16 @@ export type EditableNotificationRecipient = {
|
||||
deviceKey: string
|
||||
enabled: boolean
|
||||
}
|
||||
|
||||
export type EditableScheduledJob = {
|
||||
id: string
|
||||
type: string
|
||||
enabled: boolean
|
||||
intervalSeconds: number
|
||||
intervalSecondsAmount: number
|
||||
intervalSecondsUnit: number
|
||||
cooldownSeconds: number
|
||||
cooldownSecondsAmount: number
|
||||
cooldownSecondsUnit: number
|
||||
assetThreshold: number
|
||||
}
|
||||
|
||||
+111
-1
@@ -2,15 +2,20 @@ import { computed, ref } from 'vue'
|
||||
|
||||
import { showError, showSuccess } from '@/lib/feedback'
|
||||
import {
|
||||
runAdminScheduledJob,
|
||||
saveAdminNotificationConfig,
|
||||
saveAdminScheduledJobsConfig,
|
||||
testAdminNotification,
|
||||
} from '@/services/admin'
|
||||
import type {
|
||||
AdminNotificationConfig,
|
||||
AdminNotificationTestResult,
|
||||
AdminScheduledJobRuntimeState,
|
||||
AdminScheduledJobsConfig,
|
||||
AdminScheduledJobsResponse,
|
||||
} from '@/types/admin'
|
||||
|
||||
import type { EditableNotificationRecipient } from './types'
|
||||
import type { EditableNotificationRecipient, EditableScheduledJob } from './types'
|
||||
|
||||
export function useAdminNotificationPlatform() {
|
||||
const notificationFilePath = ref('')
|
||||
@@ -27,6 +32,14 @@ export function useAdminNotificationPlatform() {
|
||||
const notificationTesting = ref(false)
|
||||
const notificationResultError = ref('')
|
||||
const notificationTestResult = ref<AdminNotificationTestResult | null>(null)
|
||||
const scheduledJobsFilePath = ref('')
|
||||
const scheduledJobsForm = ref({
|
||||
enabled: true,
|
||||
})
|
||||
const scheduledJobs = ref<EditableScheduledJob[]>([])
|
||||
const scheduledJobRuntime = ref<AdminScheduledJobRuntimeState[]>([])
|
||||
const scheduledJobsSaving = ref(false)
|
||||
const scheduledJobRunningId = ref('')
|
||||
|
||||
const notificationStats = computed(() => {
|
||||
const enabledRecipients = notificationRecipients.value.filter((item) => item.enabled && item.deviceKey.trim())
|
||||
@@ -37,6 +50,7 @@ export function useAdminNotificationPlatform() {
|
||||
barkReady: notificationForm.value.enabled && notificationForm.value.barkEnabled && enabledRecipients.length > 0,
|
||||
lastSuccessCount: notificationTestResult.value?.successCount || 0,
|
||||
lastFailedCount: notificationTestResult.value?.failedCount || 0,
|
||||
scheduledJobEnabledCount: scheduledJobs.value.filter((item) => item.enabled).length,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -56,6 +70,24 @@ export function useAdminNotificationPlatform() {
|
||||
}))
|
||||
}
|
||||
|
||||
function hydrateScheduledJobsConfig(data: AdminScheduledJobsResponse) {
|
||||
scheduledJobsFilePath.value = data.filePath
|
||||
scheduledJobsForm.value.enabled = data.source.enabled !== false
|
||||
scheduledJobs.value = data.source.jobs.map((item) => ({
|
||||
id: item.id,
|
||||
type: item.type,
|
||||
enabled: item.enabled === true,
|
||||
intervalSeconds: Number(item.intervalSeconds || 300),
|
||||
intervalSecondsAmount: resolveTimeValue(Number(item.intervalSeconds || 300)).amount,
|
||||
intervalSecondsUnit: resolveTimeValue(Number(item.intervalSeconds || 300)).unit,
|
||||
cooldownSeconds: Number(item.cooldownSeconds || 1800),
|
||||
cooldownSecondsAmount: resolveTimeValue(Number(item.cooldownSeconds || 1800)).amount,
|
||||
cooldownSecondsUnit: resolveTimeValue(Number(item.cooldownSeconds || 1800)).unit,
|
||||
assetThreshold: Number(item.config?.assetThreshold || 500),
|
||||
}))
|
||||
scheduledJobRuntime.value = data.runtime || []
|
||||
}
|
||||
|
||||
function addNotificationRecipient() {
|
||||
notificationRecipients.value.unshift({
|
||||
id: crypto.randomUUID(),
|
||||
@@ -88,6 +120,22 @@ export function useAdminNotificationPlatform() {
|
||||
}
|
||||
}
|
||||
|
||||
function buildScheduledJobsConfigPayload(): AdminScheduledJobsConfig {
|
||||
return {
|
||||
enabled: scheduledJobsForm.value.enabled,
|
||||
jobs: scheduledJobs.value.map((item) => ({
|
||||
id: item.id,
|
||||
type: item.type,
|
||||
enabled: item.enabled,
|
||||
intervalSeconds: Number(item.intervalSeconds || 300),
|
||||
cooldownSeconds: Number(item.cooldownSeconds || 1800),
|
||||
config: {
|
||||
assetThreshold: Number(item.assetThreshold || 500),
|
||||
},
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
async function handleNotificationSaveConfig() {
|
||||
notificationSaving.value = true
|
||||
notificationResultError.value = ''
|
||||
@@ -134,6 +182,58 @@ export function useAdminNotificationPlatform() {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleScheduledJobsSaveConfig() {
|
||||
scheduledJobsSaving.value = true
|
||||
notificationResultError.value = ''
|
||||
|
||||
try {
|
||||
const response = await saveAdminScheduledJobsConfig(buildScheduledJobsConfigPayload())
|
||||
hydrateScheduledJobsConfig(response.data)
|
||||
showSuccess('监控任务配置已保存')
|
||||
} catch (error) {
|
||||
notificationResultError.value = error instanceof Error ? error.message : '监控任务配置保存失败'
|
||||
showError(notificationResultError.value)
|
||||
} finally {
|
||||
scheduledJobsSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handleScheduledJobRunNow(jobId: string) {
|
||||
scheduledJobRunningId.value = jobId
|
||||
notificationResultError.value = ''
|
||||
|
||||
try {
|
||||
const saved = await saveAdminScheduledJobsConfig(buildScheduledJobsConfigPayload())
|
||||
hydrateScheduledJobsConfig(saved.data)
|
||||
const response = await runAdminScheduledJob(jobId)
|
||||
scheduledJobRuntime.value = response.data.runtime || []
|
||||
const message = String(response.data.result?.message || '监控任务已执行')
|
||||
showSuccess(message)
|
||||
} catch (error) {
|
||||
notificationResultError.value = error instanceof Error ? error.message : '监控任务执行失败'
|
||||
showError(notificationResultError.value)
|
||||
} finally {
|
||||
scheduledJobRunningId.value = ''
|
||||
}
|
||||
}
|
||||
|
||||
function getScheduledJobRuntime(jobId: string) {
|
||||
return scheduledJobRuntime.value.find((item) => item.id === jobId) || null
|
||||
}
|
||||
|
||||
function resolveTimeValue(seconds: number) {
|
||||
const normalized = Math.max(0, Number(seconds || 0))
|
||||
if (normalized >= 3600 && normalized % 3600 === 0) {
|
||||
return { amount: normalized / 3600, unit: 3600 }
|
||||
}
|
||||
|
||||
if (normalized >= 60 && normalized % 60 === 0) {
|
||||
return { amount: normalized / 60, unit: 60 }
|
||||
}
|
||||
|
||||
return { amount: normalized || 1, unit: 1 }
|
||||
}
|
||||
|
||||
return {
|
||||
notificationFilePath,
|
||||
notificationForm,
|
||||
@@ -142,11 +242,21 @@ export function useAdminNotificationPlatform() {
|
||||
notificationTesting,
|
||||
notificationResultError,
|
||||
notificationTestResult,
|
||||
scheduledJobsFilePath,
|
||||
scheduledJobsForm,
|
||||
scheduledJobs,
|
||||
scheduledJobRuntime,
|
||||
scheduledJobsSaving,
|
||||
scheduledJobRunningId,
|
||||
notificationStats,
|
||||
hydrateNotificationConfig,
|
||||
hydrateScheduledJobsConfig,
|
||||
addNotificationRecipient,
|
||||
removeNotificationRecipient,
|
||||
handleNotificationSaveConfig,
|
||||
handleNotificationTest,
|
||||
handleScheduledJobsSaveConfig,
|
||||
handleScheduledJobRunNow,
|
||||
getScheduledJobRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user