feat: 采集任务可以立刻运行一次
This commit is contained in:
+11
-1
@@ -21,7 +21,7 @@ from app.nqi.adhoc import TIME_MODES, catalog_tree, categories, table_fields, ta
|
|||||||
from app.nqi.client import NqiAuthError, NqiClient, NqiError
|
from app.nqi.client import NqiAuthError, NqiClient, NqiError
|
||||||
from app.nqi.flows import drop, purge, remember, take
|
from app.nqi.flows import drop, purge, remember, take
|
||||||
from app.security import hash_password, issue_token, verify_password
|
from app.security import hash_password, issue_token, verify_password
|
||||||
from app.tasks.runner import check_concurrency, check_refresh, enqueue_export
|
from app.tasks.runner import check_concurrency, check_refresh, enqueue_collection, enqueue_export
|
||||||
|
|
||||||
router = APIRouter(prefix="/api")
|
router = APIRouter(prefix="/api")
|
||||||
# 在响应发出前提交,客户端紧接着发出的请求才能读到刚写入的内容。
|
# 在响应发出前提交,客户端紧接着发出的请求才能读到刚写入的内容。
|
||||||
@@ -623,6 +623,16 @@ def update_source(source_id: int, body: dict, actor: Actor = Depends(current_act
|
|||||||
return {"source": _public_source(source)}
|
return {"source": _public_source(source)}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/admin/sources/{source_id}/run")
|
||||||
|
def run_source(source_id: int, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||||
|
require_admin(actor)
|
||||||
|
source = session.get(CollectionSource, source_id)
|
||||||
|
if source is None:
|
||||||
|
raise _bad("采集不存在", 404)
|
||||||
|
enqueue_collection(source.id, force=True)
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/admin/sources/{source_id}")
|
@router.delete("/admin/sources/{source_id}")
|
||||||
def delete_source(source_id: int, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
def delete_source(source_id: int, actor: Actor = Depends(current_actor), session: Session = db_session) -> dict:
|
||||||
require_admin(actor)
|
require_admin(actor)
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ def decom_export(task_id: int) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@queue.task(name="decom_collect", queue="decom")
|
@queue.task(name="decom_collect", queue="decom")
|
||||||
def decom_collect(source_id: int) -> None:
|
def decom_collect(source_id: int, force: bool = False) -> None:
|
||||||
execute_collection(source_id)
|
execute_collection(source_id, force=force)
|
||||||
|
|
||||||
|
|
||||||
def defer(name: str, **kwargs) -> None:
|
def defer(name: str, **kwargs) -> None:
|
||||||
|
|||||||
@@ -148,10 +148,10 @@ def execute_export(task_id: int) -> None:
|
|||||||
task.finished_at = utcnow()
|
task.finished_at = utcnow()
|
||||||
|
|
||||||
|
|
||||||
def execute_collection(source_id: int) -> None:
|
def execute_collection(source_id: int, force: bool = False) -> None:
|
||||||
with session_scope() as session:
|
with session_scope() as session:
|
||||||
source = session.get(CollectionSource, source_id)
|
source = session.get(CollectionSource, source_id)
|
||||||
if source is None or not source.enabled:
|
if source is None or (not source.enabled and not force):
|
||||||
return
|
return
|
||||||
kind = source.kind
|
kind = source.kind
|
||||||
config = dict(source.config or {})
|
config = dict(source.config or {})
|
||||||
@@ -306,10 +306,13 @@ def enqueue_export(task_id: int) -> None:
|
|||||||
_run_inline(execute_export, task_id)
|
_run_inline(execute_export, task_id)
|
||||||
|
|
||||||
|
|
||||||
def enqueue_collection(source_id: int) -> None:
|
def enqueue_collection(source_id: int, *, force: bool = False) -> None:
|
||||||
if _defer("decom_collect", source_id=source_id):
|
if _defer("decom_collect", source_id=source_id, force=force):
|
||||||
return
|
return
|
||||||
_run_inline(execute_collection, source_id)
|
if not inline_worker_allowed():
|
||||||
|
logger.info("没有排队数据库,任务 %s 留在排队中", source_id)
|
||||||
|
return
|
||||||
|
threading.Thread(target=execute_collection, args=(source_id, force), daemon=True).start()
|
||||||
|
|
||||||
|
|
||||||
def _run_inline(target, item_id: int) -> None:
|
def _run_inline(target, item_id: int) -> None:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { computed, h, onMounted, ref } from "vue";
|
import { computed, h, onMounted, ref } from "vue";
|
||||||
import { NButton, NIcon, NInput, NInputNumber, NModal, NSelect, NSwitch, NTimePicker, NTooltip, useDialog } from "naive-ui";
|
import { NButton, NIcon, NInput, NInputNumber, NModal, NSelect, NSwitch, NTimePicker, NTooltip, useDialog } from "naive-ui";
|
||||||
import type { SelectRenderLabel, SelectRenderTag } from "naive-ui";
|
import type { SelectRenderLabel, SelectRenderTag } from "naive-ui";
|
||||||
import { CreateOutline, InformationCircleOutline, TrashOutline } from "@vicons/ionicons5";
|
import { CreateOutline, InformationCircleOutline, PlayOutline, TrashOutline } from "@vicons/ionicons5";
|
||||||
import Shell from "../../shell/Shell.vue";
|
import Shell from "../../shell/Shell.vue";
|
||||||
import { api } from "../../shared/http";
|
import { api } from "../../shared/http";
|
||||||
import { useToast } from "../../shared/toast";
|
import { useToast } from "../../shared/toast";
|
||||||
@@ -58,6 +58,7 @@ const weekday = ref(1);
|
|||||||
const monthDay = ref(1);
|
const monthDay = ref(1);
|
||||||
const clock = ref("08:00");
|
const clock = ref("08:00");
|
||||||
const catalogLoading = ref(false);
|
const catalogLoading = ref(false);
|
||||||
|
const runningId = ref<number | null>(null);
|
||||||
const dialog = useDialog();
|
const dialog = useDialog();
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const timeOptions = [
|
const timeOptions = [
|
||||||
@@ -271,6 +272,18 @@ async function remove(id: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function runNow(source: Source) {
|
||||||
|
runningId.value = source.id;
|
||||||
|
try {
|
||||||
|
await api(`/api/admin/sources/${source.id}/run`, { method: "POST" });
|
||||||
|
toast.success("已开始运行");
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(error instanceof Error ? error.message : "运行失败");
|
||||||
|
} finally {
|
||||||
|
runningId.value = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function confirmRemove(source: Source) {
|
function confirmRemove(source: Source) {
|
||||||
dialog.warning({
|
dialog.warning({
|
||||||
title: "删除采集",
|
title: "删除采集",
|
||||||
@@ -322,6 +335,14 @@ onMounted(load);
|
|||||||
</td>
|
</td>
|
||||||
<td class="col-actions">
|
<td class="col-actions">
|
||||||
<div class="row-actions">
|
<div class="row-actions">
|
||||||
|
<n-tooltip>
|
||||||
|
<template #trigger>
|
||||||
|
<n-button size="small" quaternary circle aria-label="立刻运行" :loading="runningId === source.id" @click="runNow(source)">
|
||||||
|
<template #icon><n-icon :component="PlayOutline" /></template>
|
||||||
|
</n-button>
|
||||||
|
</template>
|
||||||
|
立刻运行
|
||||||
|
</n-tooltip>
|
||||||
<n-tooltip>
|
<n-tooltip>
|
||||||
<template #trigger>
|
<template #trigger>
|
||||||
<n-button size="small" quaternary circle aria-label="编辑" @click="reset(source)">
|
<n-button size="small" quaternary circle aria-label="编辑" @click="reset(source)">
|
||||||
|
|||||||
Reference in New Issue
Block a user