Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Task Run Monitoring (#68116)

Nicola Mometto2e3746de5573
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index a908fccfa55..dfb12f47737 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1451,6 +1451,8 @@
metabase.task-history.core
metabase.task-history.init}
:uses #{api
+ app-db
+ config
models
permissions
premium-features
diff --git a/e2e/test/scenarios/admin/tools/tools.cy.spec.ts b/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
index a7dec55f94f..4fa98c21f9f 100644
--- a/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
+++ b/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
@@ -79,7 +79,7 @@ describe("issue 14636", () => {
});
it("pagination should work (metabase#14636)", () => {
- cy.visit("/admin/tools/tasks");
+ cy.visit("/admin/tools/tasks/list");
cy.wait("@first");
cy.location("search").should("eq", "");
@@ -115,14 +115,14 @@ describe("issue 14636", () => {
cy.log("pagination should affect browser history");
cy.go("back");
- cy.location("pathname").should("eq", "/admin/tools/tasks");
+ cy.location("pathname").should("eq", "/admin/tools/tasks/list");
cy.location("search").should("eq", "?page=1");
cy.go("back");
- cy.location("pathname").should("eq", "/admin/tools/tasks");
+ cy.location("pathname").should("eq", "/admin/tools/tasks/list");
cy.location("search").should("eq", "");
cy.log("it should respect page query param on page load");
- cy.visit("/admin/tools/tasks?page=1");
+ cy.visit("/admin/tools/tasks/list?page=1");
cy.wait("@second");
cy.findByLabelText("pagination")
@@ -131,7 +131,9 @@ describe("issue 14636", () => {
});
it("filtering should work", () => {
- cy.visit("/admin/tools/tasks?status=success&task=field+values+scanning");
+ cy.visit(
+ "/admin/tools/tasks/list?status=success&task=field+values+scanning",
+ );
cy.findByPlaceholderText("Filter by task").should(
"have.value",
@@ -181,13 +183,13 @@ describe("issue 14636", () => {
cy.findByLabelText("pagination").findByText("1 - 50").should("be.visible");
cy.log("should reset pagination when changing filters");
- cy.visit("/admin/tools/tasks?page=1");
+ cy.visit("/admin/tools/tasks/list?page=1");
cy.findByPlaceholderText("Filter by status").click();
H.popover().findByText("Success").click();
cy.location("search").should("eq", "?status=success");
cy.log("should remove invalid query params");
- cy.visit("/admin/tools/tasks?status=foobar");
+ cy.visit("/admin/tools/tasks/list?status=foobar");
cy.location("search").should("eq", "");
cy.findByPlaceholderText("Filter by status").should("have.value", "");
});
@@ -222,20 +224,16 @@ describe("scenarios > admin > tools > tasks", () => {
}).as("getTask");
});
- it("shows task modal", () => {
- cy.visit("/admin/tools/tasks");
+ it("shows task details page", () => {
+ cy.visit("/admin/tools/tasks/list");
cy.wait("@getTasks");
cy.findByTestId("tasks-table").findByText("A task").click();
cy.wait("@getTask");
- cy.location("pathname").should("eq", `/admin/tools/tasks/${task.id}`);
+ cy.location("pathname").should("eq", `/admin/tools/tasks/list/${task.id}`);
cy.log("task details");
- H.modal()
- .get(".cm-content")
- .should("be.visible")
- .get(".cm-line")
- .as("lines");
+ cy.get(".cm-content").should("be.visible").get(".cm-line").as("lines");
cy.get("@lines").eq(0).should("have.text", "{");
cy.get("@lines").eq(1).should("have.text", ' "useful": {');
cy.get("@lines").eq(2).should("have.text", ' "information": true');
@@ -260,6 +258,62 @@ describe("scenarios > admin > tools > tasks", () => {
task.task_details,
);
});
+
+ it("should render logs when they are present", () => {
+ const taskWithLogs = {
+ ...task,
+ logs: [
+ {
+ timestamp: "2024-01-10T10:00:00.000Z",
+ process_uuid: "test-uuid-1234",
+ fqns: "metabase.sync.sync",
+ msg: "Starting database sync",
+ level: "INFO",
+ exception: null,
+ },
+ {
+ timestamp: "2024-01-10T10:00:01.000Z",
+ process_uuid: "test-uuid-1234",
+ fqns: "metabase.sync.sync",
+ msg: "Sync completed successfully",
+ level: "DEBUG",
+ exception: null,
+ },
+ ],
+ };
+
+ cy.intercept("GET", `/api/task/${task.id}`, {
+ body: taskWithLogs,
+ }).as("getTaskWithLogs");
+
+ cy.visit(`/admin/tools/tasks/list/${task.id}`);
+ cy.wait("@getTaskWithLogs");
+
+ cy.findByTestId("task-logs").scrollIntoView().should("be.visible");
+ cy.findByTestId("task-logs").within(() => {
+ cy.findByText(new RegExp(taskWithLogs.logs[0].msg)).should("be.visible");
+ cy.findByText(new RegExp(taskWithLogs.logs[1].msg)).should("be.visible");
+ });
+ });
+
+ it("should show empty state when no logs are present", () => {
+ const taskWithoutLogs = {
+ ...task,
+ logs: [],
+ };
+
+ cy.intercept("GET", `/api/task/${task.id}`, {
+ body: taskWithoutLogs,
+ }).as("getTaskWithoutLogs");
+
+ cy.visit(`/admin/tools/tasks/list/${task.id}`);
+ cy.wait("@getTaskWithoutLogs");
+
+ cy.findByTestId("task-logs").should("not.exist");
+ cy.findByTestId("admin-layout-content")
+ .findByText("There are no captured logs")
+ .should("be.visible");
+ });
});
describe("scenarios > admin > tools > logs", () => {
@@ -480,17 +534,17 @@ describe("admin > tools", () => {
});
describe("issue 57113", () => {
- it("should navigate to /admin/tools/tasks when closing Task details modal even with no browser history", () => {
- cy.visit("/admin/tools/tasks");
+ it("should navigate to /admin/tools/tasks/list when clicking Back to Tasks even with no browser history", () => {
+ cy.visit("/admin/tools/tasks/list");
cy.log("Pick an existing task url");
cy.findAllByTestId("task").should("be.visible").first().click();
cy.location("pathname")
- .should("match", /\/admin\/tools\/tasks\/[0-9]+$/)
+ .should("match", /\/admin\/tools\/tasks\/list\/[0-9]+$/)
.then((pathname) => {
- // Clear all history and navigate to the task detail modal page
+ // Clear all history and navigate to the task detail page
cy.window().then((window) => {
window.history.replaceState(null, "", pathname);
// Clear the entire history stack by going to about:blank first
@@ -498,9 +552,381 @@ describe("admin > tools", () => {
});
cy.visit(pathname);
- H.modal().findByRole("img", { name: "close icon" }).click();
- cy.location("pathname").should("eq", "/admin/tools/tasks");
+ cy.findByText("Back to Tasks").click();
+ cy.location("pathname").should("eq", "/admin/tools/tasks/list");
});
});
});
});
+
+describe("scenarios > admin > tools > task runs", () => {
+ const taskRun = {
+ id: 1,
+ run_type: "sync",
+ entity_type: "database",
+ entity_id: 1,
+ entity_name: "Sample Database",
+ started_at: "2024-01-10T10:00:00Z",
+ ended_at: "2024-01-10T10:05:00Z",
+ status: "success",
+ task_count: 3,
+ success_count: 2,
+ failed_count: 1,
+ };
+
+ const taskRunExtended = {
+ ...taskRun,
+ tasks: [
+ {
+ id: 101,
+ task: "sync-database",
+ status: "success",
+ db_id: 1,
+ duration: 100,
+ started_at: "2024-01-10T10:00:00Z",
+ ended_at: "2024-01-10T10:01:00Z",
+ task_details: null,
+ logs: null,
+ run_id: 1,
+ },
+ {
+ id: 102,
+ task: "analyze",
+ status: "success",
+ db_id: 1,
+ duration: 200,
+ started_at: "2024-01-10T10:01:00Z",
+ ended_at: "2024-01-10T10:03:00Z",
+ task_details: null,
+ logs: null,
+ run_id: 1,
+ },
+ {
+ id: 103,
+ task: "fingerprint",
+ status: "failed",
+ db_id: 1,
+ duration: 50,
+ started_at: "2024-01-10T10:03:00Z",
+ ended_at: "2024-01-10T10:05:00Z",
+ task_details: null,
+ logs: null,
+ run_id: 1,
+ },
+ ],
+ };
+
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+
+ cy.intercept("GET", "/api/task/runs?*", (request) => {
+ request.reply((response) => {
+ response.body = {
+ data: [taskRun],
+ total: 1,
+ limit: 50,
+ offset: 0,
+ };
+ });
+ }).as("getTaskRuns");
+
+ cy.intercept("GET", /\/api\/task\/runs\/\d+$/, {
+ body: taskRunExtended,
+ }).as("getTaskRun");
+ });
+
+ it("should switch between Tasks and Runs tabs", () => {
+ cy.visit("/admin/tools/tasks/list");
+
+ cy.findByTestId("tasks-table").should("be.visible");
+
+ cy.findByRole("tab", { name: /Runs/i }).click();
+ cy.location("pathname").should("eq", "/admin/tools/tasks/runs");
+ cy.findByTestId("task-runs-table").should("be.visible");
+
+ cy.findByRole("tab", { name: /Tasks/i }).click();
+ cy.location("pathname").should("eq", "/admin/tools/tasks/list");
+ cy.findByTestId("tasks-table").should("be.visible");
+ });
+
+ it("should navigate to task run details and show associated tasks", () => {
+ cy.visit("/admin/tools/tasks/runs");
+ cy.wait("@getTaskRuns");
+
+ cy.findByTestId("task-runs-table")
+ .findAllByTestId("task-run")
+ .first()
+ .click();
+ cy.wait("@getTaskRun");
+
+ cy.location("pathname").should(
+ "eq",
+ `/admin/tools/tasks/runs/${taskRun.id}`,
+ );
+
+ cy.findByTestId("admin-layout-content").within(() => {
+ cy.findByText("Run type").should("be.visible");
+ cy.findByText("Entity").should("be.visible");
+ cy.findByText("Sample Database").should("be.visible");
+ });
+
+ cy.findByTestId("task-run-tasks-table").should("be.visible");
+ cy.findAllByTestId("task-run-task").should("have.length", 3);
+ });
+
+ it("should navigate back to runs list from run details", () => {
+ cy.visit(`/admin/tools/tasks/runs/${taskRun.id}`);
+ cy.wait("@getTaskRun");
+
+ cy.findByRole("link", { name: /Back to Runs/i }).click();
+ cy.location("pathname").should("eq", "/admin/tools/tasks/runs");
+ });
+
+ it("should have clickable entity link in task run details", () => {
+ cy.visit(`/admin/tools/tasks/runs/${taskRun.id}`);
+ cy.wait("@getTaskRun");
+
+ cy.findByRole("link", { name: /Sample Database/i }).click();
+ cy.location("pathname").should("eq", "/admin/databases/1");
+ });
+
+ it("should navigate to task details from task run details", () => {
+ cy.visit(`/admin/tools/tasks/runs/${taskRun.id}`);
+ cy.wait("@getTaskRun");
+
+ cy.findByTestId("task-run-tasks-table")
+ .findAllByTestId("task-run-task")
+ .first()
+ .click();
+
+ cy.location("pathname").should(
+ "eq",
+ `/admin/tools/tasks/list/${taskRunExtended.tasks[0].id}`,
+ );
+ });
+});
+
+describe("scenarios > admin > tools > task runs pagination", () => {
+ const total = 57;
+ const limit = 50;
+
+ function stubRunsPageResponses({
+ page,
+ alias,
+ }: {
+ page: number;
+ alias: string;
+ }) {
+ const offset = page * limit;
+
+ cy.intercept("GET", `/api/task/runs?limit=${limit}&offset=${offset}`, {
+ status: 200,
+ body: {
+ data: stubRunsPageRows(page),
+ limit,
+ offset,
+ total,
+ },
+ }).as(alias);
+ }
+
+ function stubRunsPageRows(page: number) {
+ const runTypes = ["sync", "fingerprint"];
+
+ const row = {
+ id: 1,
+ run_type: runTypes[page],
+ entity_type: "database",
+ entity_id: 1,
+ entity_name: "Sample Database",
+ started_at: "2024-01-10T10:00:00Z",
+ ended_at: "2024-01-10T10:05:00Z",
+ status: "success",
+ task_count: 3,
+ success_count: 2,
+ failed_count: 1,
+ };
+
+ const pageRows = [limit, total - limit];
+ const length = pageRows[page];
+
+ return Array.from({ length }, (_, index) => ({
+ ...row,
+ id: index + 1,
+ }));
+ }
+
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+
+ stubRunsPageResponses({ page: 0, alias: "firstRunsPage" });
+ stubRunsPageResponses({ page: 1, alias: "secondRunsPage" });
+ });
+
+ it("pagination should work for task runs", () => {
+ cy.visit("/admin/tools/tasks/runs");
+ cy.wait("@firstRunsPage");
+
+ cy.location("search").should("eq", "");
+
+ cy.findByLabelText("pagination").findByText("1 - 50").should("be.visible");
+ cy.findByTestId("task-runs-table").should("contain.text", "Sync");
+
+ cy.findByLabelText("Previous page").should("be.disabled");
+ cy.findByLabelText("Next page").should("not.be.disabled").click();
+ cy.wait("@secondRunsPage");
+
+ cy.location("search").should("eq", "?page=1");
+
+ cy.findByLabelText("pagination")
+ .scrollIntoView()
+ .findByText(`51 - ${total}`)
+ .should("be.visible");
+ cy.findByTestId("task-runs-table").should("contain.text", "Fingerprint");
+
+ cy.findByLabelText("Next page").should("be.disabled");
+ cy.findByLabelText("Previous page").should("not.be.disabled").click();
+
+ cy.location("search").should("eq", "");
+ });
+});
+
+describe("scenarios > admin > tools > task runs filtering", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+
+ cy.intercept("GET", "/api/task/runs?*", (request) => {
+ request.reply((response) => {
+ response.body = {
+ data: [],
+ total: 0,
+ limit: 50,
+ offset: 0,
+ };
+ });
+ }).as("getTaskRuns");
+
+ cy.intercept("GET", "/api/task/runs/entities?*", (request) => {
+ request.reply((response) => {
+ response.body = [
+ {
+ entity_type: "database",
+ entity_id: 1,
+ entity_name: "Sample Database",
+ },
+ {
+ entity_type: "database",
+ entity_id: 2,
+ entity_name: "Test Database",
+ },
+ ];
+ });
+ }).as("getEntities");
+ });
+
+ it("filtering should work for task runs", () => {
+ cy.visit("/admin/tools/tasks/runs");
+ cy.wait("@getTaskRuns");
+
+ cy.log("Filter by run type");
+ cy.findByPlaceholderText("Filter by run type").click();
+ H.popover().findByText("Sync").click();
+ cy.location("search").should("contain", "run-type=sync");
+ cy.wait("@getTaskRuns");
+
+ cy.wait("@getEntities");
+ cy.log("Filter by entity");
+ cy.findByPlaceholderText("Filter by entity").click();
+ H.popover().findByText("Sample Database").click();
+ cy.location("search").should("contain", "entity-type=database");
+ cy.location("search").should("contain", "entity-id=1");
+ cy.wait("@getTaskRuns");
+
+ cy.log("Filter by status");
+ cy.findByPlaceholderText("Filter by status").click();
+ H.popover().findByText("Success").click();
+ cy.location("search").should("contain", "status=success");
+ cy.wait("@getTaskRuns");
+
+ cy.log("Clear all filters");
+ cy.findByPlaceholderText("Filter by run type")
+ .parent()
+ .findByLabelText("Clear")
+ .click();
+ cy.findByPlaceholderText("Filter by status")
+ .parent()
+ .findByLabelText("Clear")
+ .click();
+ cy.location("search").should("eq", "");
+ });
+
+ it("entity picker should be disabled/enabled based on run type and entities availability", () => {
+ cy.visit("/admin/tools/tasks/runs");
+ cy.wait("@getTaskRuns");
+
+ cy.log("Should be disabled when no run type is selected");
+ cy.findByPlaceholderText("Filter by entity").should("be.disabled");
+
+ cy.log("Should show tooltip 'Select a run type first' when hovering");
+ cy.findByPlaceholderText("Filter by entity").trigger("mouseenter", {
+ force: true,
+ });
+ cy.findByRole("tooltip").should("have.text", "Select a run type first");
+ cy.findByPlaceholderText("Filter by entity").trigger("mouseleave", {
+ force: true,
+ });
+
+ cy.log("Should show loader while loading entities");
+ cy.intercept("GET", "/api/task/runs/entities?*", {
+ body: [
+ {
+ entity_type: "database",
+ entity_id: 1,
+ entity_name: "Sample Database",
+ },
+ ],
+ delay: 500,
+ }).as("getEntitiesDelayed");
+
+ cy.findByPlaceholderText("Filter by run type").click();
+ H.popover().findByText("Sync").click();
+
+ cy.findByPlaceholderText("Filter by entity")
+ .should("be.disabled")
+ .closest(".mb-mantine-Select-root")
+ .find(".mb-mantine-Loader-root")
+ .should("exist");
+
+ cy.wait("@getEntitiesDelayed");
+
+ cy.log("Should be enabled after entities are loaded");
+ cy.findByPlaceholderText("Filter by entity").should("not.be.disabled");
+
+ cy.log("Should clear and disable entity filter when run type is cleared");
+ cy.findByPlaceholderText("Filter by run type")
+ .parent()
+ .findByLabelText("Clear")
+ .click();
+
+ cy.findByPlaceholderText("Filter by entity").should("be.disabled");
+ cy.findByPlaceholderText("Filter by entity").should("have.value", "");
+
+ cy.log("Should show tooltip 'No entities available' when no entities");
+ cy.intercept("GET", "/api/task/runs/entities?*", {
+ body: [],
+ }).as("getEmptyEntities");
+
+ cy.findByPlaceholderText("Filter by run type").click();
+ H.popover().findByText("Alert").click();
+ cy.wait("@getEmptyEntities");
+
+ cy.findByPlaceholderText("Filter by entity").should("be.disabled");
+ cy.findByPlaceholderText("Filter by entity").trigger("mouseenter", {
+ force: true,
+ });
+ cy.findByRole("tooltip").should("have.text", "No entities available");
+ });
+});
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
index 6289bc5385e..c2f2093bb22 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
@@ -118,6 +118,7 @@
"Session"
"SupportAccessGrantLog"
"TaskHistory"
+ "TaskRun"
"Tenant"
"TransformJobRun"
"TransformRun"
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj
index bc14274fb2c..c658b050f02 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj
@@ -91,6 +91,7 @@
:model/Session
:model/SupportAccessGrantLog
:model/TaskHistory
+ :model/TaskRun
:model/Tenant
:model/TimelineEvent
;; TODO we should remove these models from here once serialization is supported
diff --git a/frontend/src/metabase-types/api/mocks/task.ts b/frontend/src/metabase-types/api/mocks/task.ts
index 40d0a09fb3e..a8b86eaefda 100644
--- a/frontend/src/metabase-types/api/mocks/task.ts
+++ b/frontend/src/metabase-types/api/mocks/task.ts
@@ -9,5 +9,7 @@ export const createMockTask = (task?: Partial<Task>): Task => ({
task: "A task",
task_details: null,
status: "success",
+ logs: task?.logs ?? null,
+ run_id: task?.run_id ?? null,
...task,
});
diff --git a/frontend/src/metabase-types/api/task.ts b/frontend/src/metabase-types/api/task.ts
index 9e57d421de2..0b33c129480 100644
--- a/frontend/src/metabase-types/api/task.ts
+++ b/frontend/src/metabase-types/api/task.ts
@@ -1,3 +1,5 @@
+import type { Log } from "metabase-types/api/util";
+
import type { DatabaseId } from "./database";
import type { PaginationRequest, PaginationResponse } from "./pagination";
import type { SortingOptions } from "./sorting";
@@ -14,6 +16,8 @@ export interface Task {
task: string;
task_details: Record<string, unknown> | null;
status: TaskStatus;
+ logs: Log[] | null;
+ run_id: number | null;
}
export type ListTasksSortColumn = "started_at" | "ended_at" | "duration";
@@ -59,3 +63,46 @@ export type TaskInfo = {
scheduler: string[];
jobs: Job[];
};
+
+export type TaskRunType = "subscription" | "alert" | "sync" | "fingerprint";
+export type TaskRunEntityType = "database" | "card" | "dashboard";
+export type TaskRunStatus = "started" | "success" | "failed" | "abandoned";
+
+export interface TaskRun {
+ id: number;
+ run_type: TaskRunType;
+ entity_type: TaskRunEntityType;
+ entity_id: number;
+ started_at: string;
+ ended_at: string | null;
+ status: TaskRunStatus;
+ entity_name?: string;
+ task_count: number;
+ success_count: number;
+ failed_count: number;
+}
+
+export interface TaskRunExtended extends TaskRun {
+ tasks: Task[];
+}
+
+export interface RunEntity {
+ entity_type: TaskRunEntityType;
+ entity_id: number;
+ entity_name?: string;
+}
+
+export type ListTaskRunsRequest = {
+ "run-type"?: TaskRunType;
+ "entity-type"?: TaskRunEntityType;
+ "entity-id"?: number;
+ status?: TaskRunStatus;
+} & PaginationRequest;
+
+export type ListTaskRunsResponse = {
+ data: TaskRun[];
+} & PaginationResponse;
+
+export type ListTaskRunEntitiesRequest = {
+ "run-type": TaskRunType;
+};
diff --git a/frontend/src/metabase/admin/routes.jsx b/frontend/src/metabase/admin/routes.jsx
index e866797d5e8..da3a9a10987 100644
--- a/frontend/src/metabase/admin/routes.jsx
+++ b/frontend/src/metabase/admin/routes.jsx
@@ -35,9 +35,6 @@ import {
ModelCachePage,
ModelCacheRefreshJobModal,
} from "metabase/admin/tools/components/ModelCacheRefreshJobs";
-import { TaskModal } from "metabase/admin/tools/components/TaskModal";
-import { TasksApp } from "metabase/admin/tools/components/TasksApp";
-import { ToolsApp } from "metabase/admin/tools/components/ToolsApp";
import { EmbeddingHubAdminSettingsPage } from "metabase/embedding/embedding-hub";
import { ModalRoute } from "metabase/hoc/ModalRoute";
import { DataModelV1 } from "metabase/metadata/pages/DataModelV1";
@@ -57,7 +54,9 @@ import { ModelPersistenceConfiguration } from "./performance/components/ModelPer
import { StrategyEditorForDatabases } from "./performance/components/StrategyEditorForDatabases";
import { PerformanceTabId } from "./performance/types";
import { getSettingsRoutes } from "./settingsRoutes";
+import { ToolsApp } from "./tools/components/ToolsApp";
import { ToolsUpsell } from "./tools/components/ToolsUpsell";
+import { getTasksRoutes } from "./tools/routes";
import {
RedirectToAllowedSettings,
createAdminRouteGuard,
@@ -290,16 +289,7 @@ const getRoutes = (store, CanAccessSettings, IsAdmin) => {
/>
)}
</Route>
- <Route path="tasks" component={TasksApp}>
- <ModalRoute
- path=":taskId"
- modal={TaskModal}
- modalProps={{
- // EventSandbox interferes with mouse text selection in CodeMirror editor
- disableEventSandbox: true,
- }}
- />
- </Route>
+ <Route path="tasks">{getTasksRoutes()}</Route>
<Route path="jobs" component={JobInfoApp}>
<ModalRoute
path=":jobKey"
diff --git a/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx b/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx
new file mode 100644
index 00000000000..b73dc256e1b
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/Logs/LogsViewer.tsx
@@ -0,0 +1,25 @@
+import * as React from "react";
+import { useMemo } from "react";
+import reactAnsiStyle from "react-ansi-style";
+
+import { AnsiLogs } from "metabase/common/components/AnsiLogs";
+import type { Log } from "metabase-types/api";
+
+import { LogsContent } from "./Logs.styled";
+import { formatLog } from "./utils";
+
+type LogsViewerProps = React.ComponentPropsWithoutRef<"div"> & {
+ logs: Log[];
+};
+
+// TODO(egorgrushin): use it in frontend/src/metabase/admin/tools/components/Logs/Logs.tsx
+export const LogsViewer = ({ logs, ...rest }: LogsViewerProps) => {
+ const logText = useMemo(() => logs.map(formatLog).join("\n"), [logs]);
+ const displayLogs = useMemo(() => reactAnsiStyle(React, logText), [logText]);
+
+ return (
+ <AnsiLogs component={LogsContent} {...rest}>
+ {displayLogs}
+ </AnsiLogs>
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/RunTypePicker/TaskRunTypePicker.tsx b/frontend/src/metabase/admin/tools/components/RunTypePicker/TaskRunTypePicker.tsx
new file mode 100644
index 00000000000..1c4c6c6cf86
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/RunTypePicker/TaskRunTypePicker.tsx
@@ -0,0 +1,47 @@
+import { t } from "ttag";
+
+import { Select, type SelectProps } from "metabase/ui";
+import type { SelectData } from "metabase/ui/components/inputs/Select/Select";
+import type { TaskRunType } from "metabase-types/api";
+
+type TaskRunTypePickerProps = Omit<
+ SelectProps,
+ "data" | "value" | "onChange"
+> & {
+ value: TaskRunType | null;
+ onChange: (value: TaskRunType | null) => void;
+};
+
+export const TaskRunTypePicker = ({
+ value,
+ onChange,
+ ...props
+}: TaskRunTypePickerProps) => {
+ const data: SelectData<TaskRunType> = [
+ { label: t`Subscription`, value: "subscription" },
+ { label: t`Alert`, value: "alert" },
+ { label: t`Sync`, value: "sync" },
+ { label: t`Fingerprint`, value: "fingerprint" },
+ ];
+
+ return (
+ <Select
+ comboboxProps={{
+ middlewares: {
+ flip: true,
+ size: {
+ padding: 6,
+ },
+ },
+ position: "bottom-start",
+ width: 300,
+ }}
+ clearable
+ data={data}
+ placeholder={t`Filter by run type`}
+ value={value}
+ onChange={onChange}
+ {...props}
+ />
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/RunTypePicker/index.ts b/frontend/src/metabase/admin/tools/components/RunTypePicker/index.ts
new file mode 100644
index 00000000000..7882f33364f
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/RunTypePicker/index.ts
@@ -0,0 +1 @@
+export { TaskRunTypePicker } from "./TaskRunTypePicker";
diff --git a/frontend/src/metabase/admin/tools/components/TaskModal/TaskModal.module.css b/frontend/src/metabase/admin/tools/components/TaskDetailsPage/TaskDetailsPage.module.css
similarity index 100%
rename from frontend/src/metabase/admin/tools/components/TaskModal/TaskModal.module.css
rename to frontend/src/metabase/admin/tools/components/TaskDetailsPage/TaskDetailsPage.module.css
diff --git a/frontend/src/metabase/admin/tools/components/TaskDetailsPage/TaskDetailsPage.tsx b/frontend/src/metabase/admin/tools/components/TaskDetailsPage/TaskDetailsPage.tsx
new file mode 100644
index 00000000000..773e722f134
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskDetailsPage/TaskDetailsPage.tsx
@@ -0,0 +1,159 @@
+import { useMemo } from "react";
+import { Link } from "react-router";
+import { t } from "ttag";
+import _ from "underscore";
+
+import { useGetTaskQuery, useListDatabasesQuery } from "metabase/api";
+import { CodeEditor } from "metabase/common/components/CodeEditor";
+import { CopyButton } from "metabase/common/components/CopyButton";
+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
+import { openSaveDialog } from "metabase/lib/dom";
+import * as Urls from "metabase/lib/urls";
+import {
+ Anchor,
+ Box,
+ Button,
+ Flex,
+ Icon,
+ Loader,
+ Stack,
+ Text,
+ Title,
+} from "metabase/ui";
+import type { Database } from "metabase-types/api";
+
+import { SettingsSection } from "../../../components/SettingsSection";
+import { formatTaskDetails, getFilename } from "../../utils";
+import { LogsViewer } from "../Logs/LogsViewer";
+import { TaskStatusBadge } from "../TaskStatusBadge";
+
+import S from "./TaskDetailsPage.module.css";
+
+type TaskDetailsPageProps = {
+ params: { taskId: number };
+};
+
+export const TaskDetailsPage = ({ params }: TaskDetailsPageProps) => {
+ const { data: task, error, isLoading } = useGetTaskQuery(params.taskId);
+ const code = formatTaskDetails(task);
+ const linesCount = useMemo(() => code.split("\n").length, [code]);
+ const { data: databasesData, isLoading: isLoadingDatabases } =
+ useListDatabasesQuery();
+
+ if (!task || error || isLoading) {
+ return <LoadingAndErrorWrapper error={error} loading={isLoading} />;
+ }
+ const hasLogs = Boolean(task.logs?.length);
+ const databases = databasesData?.data ?? [];
+ const databaseByID: Record<number, Database> = _.indexBy(databases, "id");
+ const db = task.db_id ? databaseByID[task.db_id] : null;
+ const dbName = db?.name ?? t`Unknown name`;
+ const dbEngine = db?.engine ?? t`Unknown engine`;
+
+ const handleDownload = () => {
+ const filename = getFilename(task);
+ const blob = new Blob([code], { type: "text/json" });
+ openSaveDialog(filename, blob);
+ };
+
+ return (
+ <SettingsSection>
+ <Flex align="center" gap="sm">
+ <Link to={Urls.adminToolsTasksList()}>
+ <Flex align="center" gap="xs" c="text-secondary">
+ <Icon name="chevronleft" />
+ {t`Back to Tasks`}
+ </Flex>
+ </Link>
+ </Flex>
+
+ <Stack>
+ <Title order={3}>{t`Task details`}</Title>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`ID`}</Text>
+ <Text>{task.id}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Task`}</Text>
+ <Text>{task.task}</Text>
+ </Flex>
+ <Flex gap="md" align="center">
+ <Text fw="bold" w={120}>{t`Status`}</Text>
+ <TaskStatusBadge task={task} />
+ </Flex>
+ {task.run_id !== null && (
+ <Flex gap="md" align="baseline">
+ <Text fw="bold" w={120}>{t`Task run`}</Text>
+ <Anchor
+ component={Link}
+ to={Urls.adminToolsTaskRunDetails(task.run_id)}
+ >
+ {t`Go to the corresponding run`}
+ </Anchor>
+ </Flex>
+ )}
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`DB Name`}</Text>
+ <Text>
+ {isLoadingDatabases ? <Loader size="xs" /> : db ? dbName : "—"}
+ </Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`DB Engine`}</Text>
+ <Text>
+ {isLoadingDatabases ? <Loader size="xs" /> : db ? dbEngine : "—"}
+ </Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Started at`}</Text>
+ <Text>{task.started_at}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Ended at`}</Text>
+ <Text>{task.ended_at ?? "—"}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Duration (ms)`}</Text>
+ <Text>{task.duration}</Text>
+ </Flex>
+ <Flex justify="space-between" align="flex-end">
+ <Title order={3}>{t`JSON details`}</Title>
+ <Button
+ leftSection={<Icon name="download" />}
+ variant="filled"
+ onClick={handleDownload}
+ >{t`Download`}</Button>
+ </Flex>
+ <Box
+ className={S.codeContainer}
+ p={linesCount > 1 ? 0 : "xs"}
+ pos="relative"
+ mt="md"
+ >
+ <CodeEditor
+ language="json"
+ lineNumbers={linesCount > 1}
+ readOnly
+ value={code}
+ />
+
+ <Box p="sm" pos="absolute" right={0} top={0}>
+ <CopyButton value={code} />
+ </Box>
+ </Box>
+
+ <Title order={3}>{t`Logs`}</Title>
+ {hasLogs ? (
+ <Box className={S.codeContainer}>
+ <LogsViewer
+ logs={task?.logs ?? []}
+ data-testid="task-logs"
+ ></LogsViewer>
+ </Box>
+ ) : (
+ <Text>{t`There are no captured logs`}</Text>
+ )}
+ </Stack>
+ </SettingsSection>
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskDetailsPage/index.ts b/frontend/src/metabase/admin/tools/components/TaskDetailsPage/index.ts
new file mode 100644
index 00000000000..c8565cdb7e6
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskDetailsPage/index.ts
@@ -0,0 +1 @@
+export { TaskDetailsPage } from "./TaskDetailsPage";
diff --git a/frontend/src/metabase/admin/tools/components/TaskListPage/TaskListPage.tsx b/frontend/src/metabase/admin/tools/components/TaskListPage/TaskListPage.tsx
new file mode 100644
index 00000000000..ff8438418b6
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskListPage/TaskListPage.tsx
@@ -0,0 +1,91 @@
+import type { WithRouterProps } from "react-router";
+
+import { useListDatabasesQuery, useListTasksQuery } from "metabase/api";
+import { PaginationControls } from "metabase/common/components/PaginationControls";
+import { useUrlState } from "metabase/common/hooks/use-url-state";
+import { Flex } from "metabase/ui";
+
+import { TaskPicker } from "../TaskPicker";
+import { TaskStatusPicker } from "../TaskStatusPicker";
+import { TasksTabs } from "../TasksTabs";
+
+import { TasksTable } from "./TasksTable";
+import { urlStateConfig } from "./utils";
+
+const PAGE_SIZE = 50;
+
+export const TaskListPage = ({ location }: WithRouterProps) => {
+ const [
+ { page, sort_column, sort_direction, status, task },
+ { patchUrlState },
+ ] = useUrlState(location, urlStateConfig);
+ const sortingOptions = { sort_column, sort_direction };
+
+ const {
+ data: tasksData,
+ isLoading: isLoadingTasks,
+ error: tasksError,
+ } = useListTasksQuery(
+ {
+ limit: PAGE_SIZE,
+ offset: page * PAGE_SIZE,
+ sort_column,
+ sort_direction,
+ status: status ?? undefined,
+ task: task ?? undefined,
+ },
+ {
+ refetchOnMountOrArgChange: true,
+ },
+ );
+
+ const {
+ data: databasesData,
+ isLoading: isLoadingDatabases,
+ error: databasesError,
+ } = useListDatabasesQuery();
+
+ const tasks = tasksData?.data ?? [];
+ const total = tasksData?.total ?? 0;
+ const databases = databasesData?.data ?? [];
+ const isLoading = isLoadingTasks || isLoadingDatabases;
+ const error = tasksError || databasesError;
+
+ return (
+ <TasksTabs>
+ <Flex gap="md" justify="space-between">
+ <Flex gap="md">
+ <TaskPicker
+ value={task}
+ onChange={(task) => patchUrlState({ task, page: 0 })}
+ />
+
+ <TaskStatusPicker
+ value={status}
+ onChange={(status) => patchUrlState({ status, page: 0 })}
+ />
+ </Flex>
+
+ <PaginationControls
+ onPreviousPage={() => patchUrlState({ page: page - 1 })}
+ onNextPage={() => patchUrlState({ page: page + 1 })}
+ page={page}
+ pageSize={PAGE_SIZE}
+ itemsLength={tasks.length}
+ total={total}
+ />
+ </Flex>
+
+ <TasksTable
+ databases={databases}
+ error={error}
+ isLoading={isLoading}
+ sortingOptions={sortingOptions}
+ tasks={tasks}
+ onSortingOptionsChange={(sortingOptions) =>
+ patchUrlState({ ...sortingOptions, page: 0 })
+ }
+ />
+ </TasksTabs>
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/TasksApp/TasksApp.unit.spec.tsx b/frontend/src/metabase/admin/tools/components/TaskListPage/TaskListPage.unit.spec.tsx
similarity index 98%
rename from frontend/src/metabase/admin/tools/components/TasksApp/TasksApp.unit.spec.tsx
rename to frontend/src/metabase/admin/tools/components/TaskListPage/TaskListPage.unit.spec.tsx
index 19cbb5e00e1..f902b34f0d9 100644
--- a/frontend/src/metabase/admin/tools/components/TasksApp/TasksApp.unit.spec.tsx
+++ b/frontend/src/metabase/admin/tools/components/TaskListPage/TaskListPage.unit.spec.tsx
@@ -17,12 +17,13 @@ import {
within,
} from "__support__/ui";
import { URL_UPDATE_DEBOUNCE_DELAY } from "metabase/common/hooks/use-url-state";
+import * as Urls from "metabase/lib/urls";
import type { ListTasksResponse } from "metabase-types/api";
import { createMockTask } from "metabase-types/api/mocks";
import { createSampleDatabase } from "metabase-types/api/mocks/presets";
import { createMockLocation } from "metabase-types/store/mocks";
-import { TasksApp } from "./TasksApp";
+import { TaskListPage } from "./TaskListPage";
interface SetupOpts {
error?: boolean;
@@ -30,7 +31,7 @@ interface SetupOpts {
tasksResponse?: ListTasksResponse;
}
-const PATHNAME = "/admin/tools/tasks";
+const PATHNAME = Urls.adminToolsTasksList();
const setup = ({
error,
@@ -48,13 +49,16 @@ const setup = ({
setupTasksEndpoints(tasksResponse, { delay: 10 });
}
- return renderWithProviders(<Route path={PATHNAME} component={TasksApp} />, {
- initialRoute: `${location.pathname}${location.search}`,
- withRouter: true,
- });
+ return renderWithProviders(
+ <Route path={PATHNAME} component={TaskListPage} />,
+ {
+ initialRoute: `${location.pathname}${location.search}`,
+ withRouter: true,
+ },
+ );
};
-describe("TasksApp", () => {
+describe("TaskListPage", () => {
beforeEach(() => {
jest.useFakeTimers({ advanceTimers: true });
});
diff --git a/frontend/src/metabase/admin/tools/components/TasksApp/TasksTable.tsx b/frontend/src/metabase/admin/tools/components/TaskListPage/TasksTable.tsx
similarity index 90%
rename from frontend/src/metabase/admin/tools/components/TasksApp/TasksTable.tsx
rename to frontend/src/metabase/admin/tools/components/TaskListPage/TasksTable.tsx
index aa0cb710b4c..80e09fe8ae6 100644
--- a/frontend/src/metabase/admin/tools/components/TasksApp/TasksTable.tsx
+++ b/frontend/src/metabase/admin/tools/components/TaskListPage/TasksTable.tsx
@@ -1,15 +1,16 @@
import cx from "classnames";
import { push } from "react-router-redux";
-import { match } from "ts-pattern";
import { t } from "ttag";
import _ from "underscore";
+import { TaskStatusBadge } from "metabase/admin/tools/components/TaskStatusBadge";
import { Ellipsified } from "metabase/common/components/Ellipsified";
import { SortableColumnHeader } from "metabase/common/components/ItemsTable/BaseItemsTable";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
import AdminS from "metabase/css/admin.module.css";
import CS from "metabase/css/core/index.css";
import { useDispatch } from "metabase/lib/redux";
+import * as Urls from "metabase/lib/urls";
import { Box, Flex } from "metabase/ui";
import type { Database, ListTasksSortColumn, Task } from "metabase-types/api";
import type { SortingOptions } from "metabase-types/api/sorting";
@@ -39,7 +40,7 @@ export const TasksTable = ({
const dispatch = useDispatch();
const onClickTask = (task: Task) => {
- dispatch(push(`/admin/tools/tasks/${task.id}`));
+ dispatch(push(Urls.adminToolsTaskDetails(task.id)));
};
return (
@@ -75,7 +76,7 @@ export const TasksTable = ({
<tbody>
{showLoadingAndErrorWrapper && (
<tr>
- <td colSpan={8}>
+ <td colSpan={7}>
<LoadingAndErrorWrapper loading={isLoading} error={error} />
</td>
</tr>
@@ -121,14 +122,8 @@ export const TasksTable = ({
</td>
<td>{task.duration}</td>
<td>
- {match(task.status)
- .with("failed", () => t`Failed`)
- .with("started", () => t`Started`)
- .with("success", () => t`Success`)
- .with("unknown", () => t`Unknown`)
- .exhaustive()}
+ <TaskStatusBadge task={task} />
</td>
- <td></td>
</tr>
);
})}
diff --git a/frontend/src/metabase/admin/tools/components/TaskListPage/index.ts b/frontend/src/metabase/admin/tools/components/TaskListPage/index.ts
new file mode 100644
index 00000000000..85a6624a070
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskListPage/index.ts
@@ -0,0 +1 @@
+export { TaskListPage } from "./TaskListPage";
diff --git a/frontend/src/metabase/admin/tools/components/TasksApp/utils.ts b/frontend/src/metabase/admin/tools/components/TaskListPage/utils.ts
similarity index 100%
rename from frontend/src/metabase/admin/tools/components/TasksApp/utils.ts
rename to frontend/src/metabase/admin/tools/components/TaskListPage/utils.ts
diff --git a/frontend/src/metabase/admin/tools/components/TaskModal/TaskModal.tsx b/frontend/src/metabase/admin/tools/components/TaskModal/TaskModal.tsx
deleted file mode 100644
index ff517c7465d..00000000000
--- a/frontend/src/metabase/admin/tools/components/TaskModal/TaskModal.tsx
+++ /dev/null
@@ -1,83 +0,0 @@
-import { useMemo } from "react";
-import { Link } from "react-router";
-import { t } from "ttag";
-
-import { useGetTaskQuery } from "metabase/api";
-import { CodeEditor } from "metabase/common/components/CodeEditor";
-import { CopyButton } from "metabase/common/components/CopyButton";
-import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
-import ModalContent from "metabase/common/components/ModalContent";
-import { openSaveDialog } from "metabase/lib/dom";
-import { Box, Button, Flex, Icon } from "metabase/ui";
-import type { Task } from "metabase-types/api";
-
-import S from "./TaskModal.module.css";
-
-interface Props {
- params: { taskId: number };
- onClose: VoidFunction;
-}
-
-export const TaskModal = ({ params, onClose }: Props) => {
- const { data: task, error, isLoading } = useGetTaskQuery(params.taskId);
- const code = formatTaskDetails(task);
- const linesCount = useMemo(() => code.split("\n").length, [code]);
-
- const handleDownload = () => {
- const filename = getFilename(task);
- const blob = new Blob([code], { type: "text/json" });
- openSaveDialog(filename, blob);
- };
-
- if (error || isLoading) {
- return <LoadingAndErrorWrapper error={error} loading={isLoading} />;
- }
-
- return (
- <ModalContent title={t`Task details`} onClose={onClose}>
- <Box
- className={S.codeContainer}
- p={linesCount > 1 ? 0 : "xs"}
- pos="relative"
- >
- <CodeEditor
- language="json"
- /**
- * Hide line numbers when there's only one line:
- * - Not useful in this case
- * - Prevents confusion about whether it's part of the log output
- */
- lineNumbers={linesCount > 1}
- readOnly
- value={code}
- />
-
- <Box p="sm" pos="absolute" right={0} top={0}>
- <CopyButton value={code} />
- </Box>
- </Box>
-
- <Flex gap="md" justify="space-between" mt="xl">
- <Button
- component={Link}
- leftSection={<Icon name="audit" />}
- to="/admin/tools/logs"
- >{t`See logs`}</Button>
-
- <Button
- leftSection={<Icon name="download" />}
- variant="filled"
- onClick={handleDownload}
- >{t`Download`}</Button>
- </Flex>
- </ModalContent>
- );
-};
-
-function getFilename(task: Task | undefined) {
- return task ? `task-${task.id}.json` : "task.json";
-}
-
-function formatTaskDetails(task: Task | undefined): string {
- return task ? JSON.stringify(task.task_details, null, 2) : "";
-}
diff --git a/frontend/src/metabase/admin/tools/components/TaskModal/index.ts b/frontend/src/metabase/admin/tools/components/TaskModal/index.ts
deleted file mode 100644
index 34a08f7d742..00000000000
--- a/frontend/src/metabase/admin/tools/components/TaskModal/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./TaskModal";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.tsx b/frontend/src/metabase/admin/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.tsx
new file mode 100644
index 00000000000..5b63757bcc9
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunDetailsPage/TaskRunDetailsPage.tsx
@@ -0,0 +1,142 @@
+import cx from "classnames";
+import { Link } from "react-router";
+import { push } from "react-router-redux";
+import { t } from "ttag";
+
+import { SettingsSection } from "metabase/admin/components/SettingsSection";
+import { useGetTaskRunQuery } from "metabase/api";
+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
+import AdminS from "metabase/css/admin.module.css";
+import CS from "metabase/css/core/index.css";
+import { useDispatch } from "metabase/lib/redux";
+import * as Urls from "metabase/lib/urls";
+import { Anchor, Box, Flex, Grid, Icon, Stack, Text, Title } from "metabase/ui";
+import type { Task } from "metabase-types/api";
+
+import {
+ formatTaskRunEntityType,
+ formatTaskRunType,
+ getEntityUrl,
+ renderTaskRunCounters,
+} from "../../utils";
+import { TaskRunStatusBadge } from "../TaskRunStatusBadge";
+import { TaskStatusBadge } from "../TaskStatusBadge";
+
+type TaskRunDetailsPageProps = {
+ params: { runId: number };
+};
+
+export const TaskRunDetailsPage = ({ params }: TaskRunDetailsPageProps) => {
+ const { data: taskRun, error, isLoading } = useGetTaskRunQuery(params.runId);
+ const dispatch = useDispatch();
+
+ const onClickTask = (task: Task) => {
+ dispatch(push(Urls.adminToolsTaskDetails(task.id)));
+ };
+
+ if (!taskRun || error || isLoading) {
+ return <LoadingAndErrorWrapper error={error} loading={isLoading} />;
+ }
+
+ return (
+ <SettingsSection>
+ <Flex align="center" gap="sm">
+ <Link to={Urls.adminToolsTasksRuns()}>
+ <Flex align="center" gap="xs" c="text-secondary">
+ <Icon name="chevronleft" />
+ {t`Back to Runs`}
+ </Flex>
+ </Link>
+ </Flex>
+
+ <Grid mt="md">
+ <Grid.Col span={{ base: 12, lg: "content" }} maw="50%">
+ <Title order={3}>{t`Run details`}</Title>
+ <Stack gap="xs" mt="sm">
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`ID`}</Text>
+ <Text>{taskRun.id}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Run type`}</Text>
+ <Text>{formatTaskRunType(taskRun.run_type)}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Entity type`}</Text>
+ <Text>{formatTaskRunEntityType(taskRun.entity_type)}</Text>
+ </Flex>
+ <Flex gap="md" align="baseline">
+ <Text fw="bold" w={120}>{t`Entity`}</Text>
+ <Anchor
+ component={Link}
+ to={getEntityUrl(
+ taskRun.entity_type,
+ taskRun.entity_id,
+ taskRun.entity_name,
+ )}
+ >
+ {taskRun.entity_name ?? taskRun.entity_id}
+ </Anchor>
+ </Flex>
+ <Flex gap="md" align="center">
+ <Text fw="bold" w={120}>{t`Status`}</Text>
+ <TaskRunStatusBadge taskRun={taskRun} />
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Started at`}</Text>
+ <Text>{taskRun.started_at}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Ended at`}</Text>
+ <Text>{taskRun.ended_at ?? "—"}</Text>
+ </Flex>
+ <Flex gap="md">
+ <Text fw="bold" w={120}>{t`Task count`}</Text>
+ <Text>{renderTaskRunCounters(taskRun)}</Text>
+ </Flex>
+ </Stack>
+ </Grid.Col>
+
+ <Grid.Col span={{ base: 12, lg: "auto" }}>
+ <Title order={3}>{t`Associated tasks`}</Title>
+ <table
+ className={cx(AdminS.ContentTable)}
+ data-testid="task-run-tasks-table"
+ >
+ <thead>
+ <tr>
+ <Box component="th" w={200}>{t`Task`}</Box>
+ <th>{t`Status`}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {taskRun.tasks.length === 0 && (
+ <tr>
+ <td colSpan={2}>
+ <Flex
+ c="text-tertiary"
+ justify="center"
+ >{t`No tasks`}</Flex>
+ </td>
+ </tr>
+ )}
+ {taskRun.tasks.map((task) => (
+ <tr
+ key={task.id}
+ className={CS.cursorPointer}
+ data-testid="task-run-task"
+ onClick={() => onClickTask(task)}
+ >
+ <td className={CS.textBold}>{task.task}</td>
+ <td>
+ <TaskStatusBadge task={task} />
+ </td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </Grid.Col>
+ </Grid>
+ </SettingsSection>
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunDetailsPage/index.ts b/frontend/src/metabase/admin/tools/components/TaskRunDetailsPage/index.ts
new file mode 100644
index 00000000000..9026239d425
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunDetailsPage/index.ts
@@ -0,0 +1 @@
+export { TaskRunDetailsPage } from "./TaskRunDetailsPage";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx
new file mode 100644
index 00000000000..4d2adfea31e
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx
@@ -0,0 +1,85 @@
+import { useMemo } from "react";
+import { t } from "ttag";
+
+import { skipToken, useListTaskRunEntitiesQuery } from "metabase/api";
+import { Loader, Select, type SelectProps, Tooltip } from "metabase/ui";
+import type { TaskRunType } from "metabase-types/api";
+
+import type { EntityValue } from "./types";
+import {
+ convertEntitiesToSelectOptions,
+ parseValue,
+ serializeValue,
+} from "./utils";
+
+type TaskRunEntityPickerProps = Omit<
+ SelectProps,
+ "data" | "value" | "onChange"
+> & {
+ runType: TaskRunType | null;
+ value: EntityValue | null;
+ onChange: (value: EntityValue | null) => void;
+};
+
+export const TaskRunEntityPicker = ({
+ runType,
+ value,
+ onChange,
+ ...props
+}: TaskRunEntityPickerProps) => {
+ const { data: entities, isLoading } = useListTaskRunEntitiesQuery(
+ runType ? { "run-type": runType } : skipToken,
+ );
+
+ const data = useMemo(
+ () => convertEntitiesToSelectOptions(entities),
+ [entities],
+ );
+
+ const serializedValue = value ? serializeValue(value) : null;
+
+ const handleChange = (serialized: string | null) => {
+ if (!serialized) {
+ onChange(null);
+ return;
+ }
+ const parsed = parseValue(serialized);
+ onChange(parsed);
+ };
+
+ const isDisabled = isLoading || !runType || (!isLoading && data.length === 0);
+
+ const select = (
+ <Select
+ comboboxProps={{
+ middlewares: {
+ flip: true,
+ size: {
+ padding: 6,
+ },
+ },
+ position: "bottom-start",
+ width: 300,
+ }}
+ clearable
+ data={data}
+ disabled={isDisabled}
+ placeholder={t`Filter by entity`}
+ rightSection={isLoading ? <Loader size="xs" /> : undefined}
+ searchable
+ value={serializedValue}
+ onChange={handleChange}
+ {...props}
+ />
+ );
+
+ if (!runType) {
+ return <Tooltip label={t`Select a run type first`}>{select}</Tooltip>;
+ }
+
+ if (!isLoading && data.length === 0) {
+ return <Tooltip label={t`No entities available`}>{select}</Tooltip>;
+ }
+
+ return select;
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/index.ts b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/index.ts
new file mode 100644
index 00000000000..9f0be03f334
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/index.ts
@@ -0,0 +1,2 @@
+export { TaskRunEntityPicker } from "./TaskRunEntityPicker";
+export type { EntityValue } from "./types";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/types.ts b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/types.ts
new file mode 100644
index 00000000000..b53992b9a82
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/types.ts
@@ -0,0 +1,6 @@
+import type { TaskRunEntityType } from "metabase-types/api";
+
+export type EntityValue = {
+ entityType: TaskRunEntityType;
+ entityId: number;
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/utils.ts b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/utils.ts
new file mode 100644
index 00000000000..8f410e14d3f
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/utils.ts
@@ -0,0 +1,32 @@
+import type { RunEntity } from "metabase-types/api";
+
+import { guardTaskRunEntityType } from "../../utils";
+
+import type { EntityValue } from "./types";
+
+export const convertEntitiesToSelectOptions = (entities?: RunEntity[]) =>
+ entities?.map(({ entity_name, entity_type, entity_id }) => ({
+ label: entity_name ?? `${entity_type} ${entity_id}`,
+ value: serializeValue({
+ entityId: entity_id,
+ entityType: entity_type,
+ }),
+ })) ?? [];
+
+export const serializeValue = ({ entityType, entityId }: EntityValue): string =>
+ `${entityType}:${entityId}`;
+
+export const parseValue = (serialized: string): EntityValue | null => {
+ const [entityType, entityIdStr] = serialized.split(":");
+ const entityId = parseInt(entityIdStr, 10);
+
+ if (
+ !entityType ||
+ !Number.isFinite(entityId) ||
+ !guardTaskRunEntityType(entityType)
+ ) {
+ return null;
+ }
+
+ return { entityType, entityId };
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunStatusBadge/TaskRunStatusBadge.tsx b/frontend/src/metabase/admin/tools/components/TaskRunStatusBadge/TaskRunStatusBadge.tsx
new file mode 100644
index 00000000000..0a63315216d
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunStatusBadge/TaskRunStatusBadge.tsx
@@ -0,0 +1,14 @@
+import { Badge } from "metabase/ui";
+import type { TaskRun } from "metabase-types/api";
+
+import { formatTaskRunStatus, getTaskRunStatusColor } from "../../utils";
+
+type TaskRunStatusBadgeProps = {
+ taskRun: TaskRun;
+};
+
+export const TaskRunStatusBadge = ({ taskRun }: TaskRunStatusBadgeProps) => (
+ <Badge color={getTaskRunStatusColor(taskRun.status)}>
+ {formatTaskRunStatus(taskRun.status)}
+ </Badge>
+);
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunStatusBadge/index.ts b/frontend/src/metabase/admin/tools/components/TaskRunStatusBadge/index.ts
new file mode 100644
index 00000000000..b66d864b3fd
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunStatusBadge/index.ts
@@ -0,0 +1 @@
+export { TaskRunStatusBadge } from "./TaskRunStatusBadge";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunStatusPicker/TaskRunStatusPicker.tsx b/frontend/src/metabase/admin/tools/components/TaskRunStatusPicker/TaskRunStatusPicker.tsx
new file mode 100644
index 00000000000..058b503bc6d
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunStatusPicker/TaskRunStatusPicker.tsx
@@ -0,0 +1,44 @@
+import { t } from "ttag";
+
+import { Select, type SelectProps } from "metabase/ui";
+import type { SelectData } from "metabase/ui/components/inputs/Select/Select";
+import type { TaskRunStatus } from "metabase-types/api";
+
+type TaskRunStatusPicker = Omit<SelectProps, "data" | "value" | "onChange"> & {
+ value: TaskRunStatus | null;
+ onChange: (value: TaskRunStatus | null) => void;
+};
+
+export const TaskRunStatusPicker = ({
+ value,
+ onChange,
+ ...props
+}: TaskRunStatusPicker) => {
+ const data: SelectData<TaskRunStatus> = [
+ { label: t`Started`, value: "started" },
+ { label: t`Success`, value: "success" },
+ { label: t`Failed`, value: "failed" },
+ { label: t`Abandoned`, value: "abandoned" },
+ ];
+
+ return (
+ <Select
+ comboboxProps={{
+ middlewares: {
+ flip: true,
+ size: {
+ padding: 6,
+ },
+ },
+ position: "bottom-start",
+ width: 300,
+ }}
+ clearable
+ data={data}
+ placeholder={t`Filter by status`}
+ value={value}
+ onChange={onChange}
+ {...props}
+ />
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunStatusPicker/index.ts b/frontend/src/metabase/admin/tools/components/TaskRunStatusPicker/index.ts
new file mode 100644
index 00000000000..36a67dd2245
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunStatusPicker/index.ts
@@ -0,0 +1 @@
+export { TaskRunStatusPicker } from "./TaskRunStatusPicker";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx b/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx
new file mode 100644
index 00000000000..4997a8aef2a
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx
@@ -0,0 +1,101 @@
+import { type WithRouterProps, withRouter } from "react-router";
+
+import { useListTaskRunsQuery } from "metabase/api";
+import { PaginationControls } from "metabase/common/components/PaginationControls";
+import { useUrlState } from "metabase/common/hooks/use-url-state";
+import { Flex } from "metabase/ui";
+
+import { TaskRunTypePicker } from "../RunTypePicker";
+import { TaskRunEntityPicker } from "../TaskRunEntityPicker";
+import { TaskRunStatusPicker } from "../TaskRunStatusPicker";
+import { TasksTabs } from "../TasksTabs";
+
+import { TaskRunsTable } from "./TaskRunsTable";
+import { PAGE_SIZE } from "./constants";
+import { urlStateConfig } from "./utils";
+
+const TaskRunsPageBase = ({ location }: WithRouterProps) => {
+ const [
+ {
+ page,
+ "run-type": runType,
+ "entity-type": entityType,
+ "entity-id": entityId,
+ status,
+ },
+ { patchUrlState },
+ ] = useUrlState(location, urlStateConfig);
+
+ const {
+ data: taskRunsData,
+ isLoading,
+ error,
+ } = useListTaskRunsQuery(
+ {
+ limit: PAGE_SIZE,
+ offset: page * PAGE_SIZE,
+ "run-type": runType ?? undefined,
+ "entity-type": entityType ?? undefined,
+ "entity-id": entityId ?? undefined,
+ status: status ?? undefined,
+ },
+ {
+ refetchOnMountOrArgChange: true,
+ },
+ );
+
+ const taskRuns = taskRunsData?.data ?? [];
+ const total = taskRunsData?.total ?? 0;
+
+ const entityValue = entityType && entityId ? { entityType, entityId } : null;
+
+ return (
+ <TasksTabs>
+ <Flex gap="md" justify="space-between">
+ <Flex gap="md">
+ <TaskRunTypePicker
+ value={runType}
+ onChange={(runType) =>
+ patchUrlState({
+ "run-type": runType,
+ "entity-type": null,
+ "entity-id": null,
+ page: 0,
+ })
+ }
+ />
+
+ <TaskRunEntityPicker
+ runType={runType}
+ value={entityValue}
+ onChange={(entity) =>
+ patchUrlState({
+ "entity-type": entity?.entityType ?? null,
+ "entity-id": entity?.entityId ?? null,
+ page: 0,
+ })
+ }
+ />
+
+ <TaskRunStatusPicker
+ value={status}
+ onChange={(status) => patchUrlState({ status, page: 0 })}
+ />
+ </Flex>
+
+ <PaginationControls
+ onPreviousPage={() => patchUrlState({ page: page - 1 })}
+ onNextPage={() => patchUrlState({ page: page + 1 })}
+ page={page}
+ pageSize={PAGE_SIZE}
+ itemsLength={taskRuns.length}
+ total={total}
+ />
+ </Flex>
+
+ <TaskRunsTable taskRuns={taskRuns} isLoading={isLoading} error={error} />
+ </TasksTabs>
+ );
+};
+
+export const TaskRunsPage = withRouter(TaskRunsPageBase);
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsTable.tsx b/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsTable.tsx
new file mode 100644
index 00000000000..8c19febd597
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsTable.tsx
@@ -0,0 +1,109 @@
+import cx from "classnames";
+import { push } from "react-router-redux";
+import { t } from "ttag";
+
+import { Ellipsified } from "metabase/common/components/Ellipsified";
+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
+import AdminS from "metabase/css/admin.module.css";
+import CS from "metabase/css/core/index.css";
+import { useDispatch } from "metabase/lib/redux";
+import * as Urls from "metabase/lib/urls";
+import { Box, Flex } from "metabase/ui";
+import type { TaskRun } from "metabase-types/api";
+
+import { formatTaskRunType, renderTaskRunCounters } from "../../utils";
+import { TaskRunStatusBadge } from "../TaskRunStatusBadge";
+
+type TaskRunsTableProps = {
+ error: unknown;
+ isLoading: boolean;
+ taskRuns: TaskRun[];
+};
+
+export const TaskRunsTable = ({
+ error,
+ isLoading,
+ taskRuns,
+}: TaskRunsTableProps) => {
+ const showLoadingAndErrorWrapper = isLoading || error != null;
+ const dispatch = useDispatch();
+
+ const onClickTaskRun = (taskRun: TaskRun) => {
+ dispatch(push(Urls.adminToolsTaskRunDetails(taskRun.id)));
+ };
+
+ return (
+ <table
+ className={cx(AdminS.ContentTable, CS.mt2)}
+ data-testid="task-runs-table"
+ >
+ <thead>
+ <tr>
+ <Box component="th" w={200}>{t`Run Type`}</Box>
+ <Box component="th" w={200}>{t`Entity`}</Box>
+ <th>{t`Started at`}</th>
+ <th>{t`Ended at`}</th>
+ <th>{t`Status`}</th>
+ <th>{t`Task Count`}</th>
+ </tr>
+ </thead>
+
+ <tbody>
+ {showLoadingAndErrorWrapper && (
+ <tr>
+ <td colSpan={6}>
+ <LoadingAndErrorWrapper loading={isLoading} error={error} />
+ </td>
+ </tr>
+ )}
+
+ {!showLoadingAndErrorWrapper && (
+ <>
+ {taskRuns.length === 0 && (
+ <tr>
+ <td colSpan={6}>
+ <Flex
+ c="text-tertiary"
+ justify="center"
+ >{t`No results`}</Flex>
+ </td>
+ </tr>
+ )}
+
+ {taskRuns.map((taskRun) => (
+ <tr
+ data-testid="task-run"
+ key={taskRun.id}
+ className={CS.cursorPointer}
+ onClick={() => onClickTaskRun(taskRun)}
+ >
+ <td className={CS.textBold}>
+ {formatTaskRunType(taskRun.run_type)}
+ </td>
+ <td>
+ <Ellipsified style={{ maxWidth: 200 }}>
+ {taskRun.entity_name}
+ </Ellipsified>
+ </td>
+ <td>
+ <Ellipsified style={{ maxWidth: 150 }}>
+ {taskRun.started_at}
+ </Ellipsified>
+ </td>
+ <td>
+ <Ellipsified style={{ maxWidth: 150 }}>
+ {taskRun.ended_at}
+ </Ellipsified>
+ </td>
+ <td>
+ <TaskRunStatusBadge taskRun={taskRun} />
+ </td>
+ <td>{renderTaskRunCounters(taskRun)}</td>
+ </tr>
+ ))}
+ </>
+ )}
+ </tbody>
+ </table>
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/constants.ts b/frontend/src/metabase/admin/tools/components/TaskRunsPage/constants.ts
new file mode 100644
index 00000000000..c0b65ab6b8e
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/constants.ts
@@ -0,0 +1 @@
+export const PAGE_SIZE = 50;
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/index.ts b/frontend/src/metabase/admin/tools/components/TaskRunsPage/index.ts
new file mode 100644
index 00000000000..497a934739d
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/index.ts
@@ -0,0 +1 @@
+export { TaskRunsPage } from "./TaskRunsPage";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts b/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts
new file mode 100644
index 00000000000..d5262f645f5
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts
@@ -0,0 +1,77 @@
+import {
+ type QueryParam,
+ type UrlStateConfig,
+ getFirstParamValue,
+} from "metabase/common/hooks/use-url-state";
+import type {
+ TaskRunEntityType,
+ TaskRunStatus,
+ TaskRunType,
+} from "metabase-types/api";
+
+import {
+ guardTaskRunEntityType,
+ guardTaskRunRunType,
+ guardTaskRunStatus,
+} from "../../utils";
+
+type UrlState = {
+ page: number;
+ "run-type": TaskRunType | null;
+ "entity-type": TaskRunEntityType | null;
+ "entity-id": number | null;
+ status: TaskRunStatus | null;
+};
+
+export const urlStateConfig: UrlStateConfig<UrlState> = {
+ parse: (query) => ({
+ page: parsePage(query.page),
+ "run-type": parseTaskRunRunType(query["run-type"]),
+ "entity-type": parseTaskRunEntityType(query["entity-type"]),
+ "entity-id": parseTaskRunEntityId(query["entity-id"]),
+ status: parseTaskRunStatus(query.status),
+ }),
+ serialize: ({
+ page,
+ "run-type": runType,
+ "entity-type": entityType,
+ "entity-id": entityId,
+ status,
+ }) => ({
+ page: page === 0 ? undefined : String(page),
+ "run-type": runType === null ? undefined : runType,
+ "entity-type": entityType === null ? undefined : entityType,
+ "entity-id": entityId === null ? undefined : String(entityId),
+ status: status === null ? undefined : status,
+ }),
+};
+
+const parsePage = (param: QueryParam): UrlState["page"] => {
+ const value = getFirstParamValue(param);
+ const parsed = parseInt(value || "0", 10);
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0;
+};
+
+const parseTaskRunRunType = (param: QueryParam): UrlState["run-type"] => {
+ const value = getFirstParamValue(param);
+ return value && guardTaskRunRunType(value) ? value : null;
+};
+
+const parseTaskRunEntityType = (param: QueryParam): UrlState["entity-type"] => {
+ const value = getFirstParamValue(param);
+ return value && guardTaskRunEntityType(value) ? value : null;
+};
+
+const parseTaskRunEntityId = (param: QueryParam): UrlState["entity-id"] => {
+ const value = getFirstParamValue(param);
+ if (!value) {
+ return null;
+ }
+ const parsed = parseInt(value, 10);
+ return Number.isFinite(parsed) ? parsed : null;
+};
+
+const parseTaskRunStatus = (param: QueryParam): UrlState["status"] => {
+ const value = getFirstParamValue(param);
+ return value && guardTaskRunStatus(value) ? value : null;
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskStatusBadge/TaskStatusBadge.tsx b/frontend/src/metabase/admin/tools/components/TaskStatusBadge/TaskStatusBadge.tsx
new file mode 100644
index 00000000000..33db6f5b683
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskStatusBadge/TaskStatusBadge.tsx
@@ -0,0 +1,14 @@
+import { Badge } from "metabase/ui";
+import type { Task } from "metabase-types/api";
+
+import { formatTaskStatus, getTaskStatusColor } from "../../utils";
+
+type TaskStatusProps = {
+ task: Task;
+};
+
+export const TaskStatusBadge = ({ task }: TaskStatusProps) => (
+ <Badge color={getTaskStatusColor(task.status)}>
+ {formatTaskStatus(task.status)}
+ </Badge>
+);
diff --git a/frontend/src/metabase/admin/tools/components/TaskStatusBadge/index.ts b/frontend/src/metabase/admin/tools/components/TaskStatusBadge/index.ts
new file mode 100644
index 00000000000..8a017cc1073
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskStatusBadge/index.ts
@@ -0,0 +1 @@
+export { TaskStatusBadge } from "./TaskStatusBadge";
diff --git a/frontend/src/metabase/admin/tools/components/TasksApp/TasksApp.tsx b/frontend/src/metabase/admin/tools/components/TasksApp/TasksApp.tsx
deleted file mode 100644
index 1c8f41c79e2..00000000000
--- a/frontend/src/metabase/admin/tools/components/TasksApp/TasksApp.tsx
+++ /dev/null
@@ -1,118 +0,0 @@
-import type { Location } from "history";
-import type { ReactNode } from "react";
-import { withRouter } from "react-router";
-import { t } from "ttag";
-
-import {
- SettingsPageWrapper,
- SettingsSection,
-} from "metabase/admin/components/SettingsSection";
-import { useListDatabasesQuery, useListTasksQuery } from "metabase/api";
-import { PaginationControls } from "metabase/common/components/PaginationControls";
-import { useUrlState } from "metabase/common/hooks/use-url-state";
-import { Flex, Icon, Title, Tooltip } from "metabase/ui";
-
-import { TaskPicker } from "../../components/TaskPicker";
-import { TaskStatusPicker } from "../../components/TaskStatusPicker";
-
-import { TasksTable } from "./TasksTable";
-import { urlStateConfig } from "./utils";
-
-type TasksAppProps = {
- children: ReactNode;
- location: Location;
-};
-
-const PAGE_SIZE = 50;
-
-const TasksAppBase = ({ children, location }: TasksAppProps) => {
- const [
- { page, sort_column, sort_direction, status, task },
- { patchUrlState },
- ] = useUrlState(location, urlStateConfig);
- const sortingOptions = { sort_column, sort_direction };
-
- const {
- data: tasksData,
- isLoading: isLoadingTasks,
- error: tasksError,
- } = useListTasksQuery(
- {
- limit: PAGE_SIZE,
- offset: page * PAGE_SIZE,
- sort_column,
- sort_direction,
- status: status ?? undefined,
- task: task ?? undefined,
- },
- {
- refetchOnMountOrArgChange: true,
- },
- );
-
- const {
- data: databasesData,
- isLoading: isLoadingDatabases,
- error: databasesError,
- } = useListDatabasesQuery();
-
- const tasks = tasksData?.data ?? [];
- const total = tasksData?.total ?? 0;
- const databases = databasesData?.data ?? [];
- const isLoading = isLoadingTasks || isLoadingDatabases;
- const error = tasksError || databasesError;
-
- return (
- <SettingsPageWrapper>
- <Flex align="center" gap="sm">
- <Title order={1}>{t`Troubleshooting logs`} </Title>
- <Tooltip
- label={t`Trying to get to the bottom of something? This section shows logs of Metabase's background tasks, which can help shed light on what's going on.`}
- >
- <Icon name="info" />
- </Tooltip>
- </Flex>
- <SettingsSection>
- <Flex gap="md" justify="space-between">
- <Flex gap="md">
- <TaskPicker
- value={task}
- onChange={(task) => patchUrlState({ task, page: 0 })}
- />
-
- <TaskStatusPicker
- value={status}
- onChange={(status) => patchUrlState({ status, page: 0 })}
- />
- </Flex>
-
- <PaginationControls
- onPreviousPage={() => patchUrlState({ page: page - 1 })}
- onNextPage={() => patchUrlState({ page: page + 1 })}
- page={page}
- pageSize={50}
- itemsLength={tasks.length}
- total={total}
- />
- </Flex>
-
- <TasksTable
- databases={databases}
- error={error}
- isLoading={isLoading}
- sortingOptions={sortingOptions}
- tasks={tasks}
- onSortingOptionsChange={(sortingOptions) =>
- patchUrlState({ ...sortingOptions, page: 0 })
- }
- />
- </SettingsSection>
- {
- // render 'children' so that the individual task modals show up
- children
- }
- </SettingsPageWrapper>
- );
-};
-
-export const TasksApp = withRouter(TasksAppBase);
diff --git a/frontend/src/metabase/admin/tools/components/TasksApp/index.ts b/frontend/src/metabase/admin/tools/components/TasksApp/index.ts
deleted file mode 100644
index fe1afa55930..00000000000
--- a/frontend/src/metabase/admin/tools/components/TasksApp/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./TasksApp";
diff --git a/frontend/src/metabase/admin/tools/components/TasksTabs/TasksTabs.tsx b/frontend/src/metabase/admin/tools/components/TasksTabs/TasksTabs.tsx
new file mode 100644
index 00000000000..883473dc72d
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TasksTabs/TasksTabs.tsx
@@ -0,0 +1,65 @@
+import { type WithRouterProps, withRouter } from "react-router";
+import { push } from "react-router-redux";
+import { t } from "ttag";
+
+import { useDispatch } from "metabase/lib/redux";
+import * as Urls from "metabase/lib/urls";
+import { Flex, Icon, Tabs, Title, Tooltip } from "metabase/ui";
+
+import {
+ SettingsPageWrapper,
+ SettingsSection,
+} from "../../../components/SettingsSection";
+
+type TabConfig = {
+ value: string;
+ label: string;
+};
+
+type TasksTabsProps = WithRouterProps & {
+ children: React.ReactNode;
+};
+
+const TasksTabsBase = ({ children, location }: TasksTabsProps) => {
+ const tabs: TabConfig[] = [
+ { value: Urls.adminToolsTasksList(), label: t`Tasks` },
+ { value: Urls.adminToolsTasksRuns(), label: t`Runs` },
+ ];
+ const DEFAULT_TAB = tabs[0].value;
+ const dispatch = useDispatch();
+ const activeTab =
+ tabs.find(({ value }) => value === location.pathname)?.value ?? DEFAULT_TAB;
+
+ const handleTabChange = (value: string | null) => {
+ if (value) {
+ dispatch(push(value));
+ }
+ };
+
+ return (
+ <SettingsPageWrapper>
+ <SettingsSection>
+ <Flex align="center" gap="sm">
+ <Title order={1}>{t`Troubleshooting logs`}</Title>
+ <Tooltip
+ label={t`Trying to get to the bottom of something? This section shows logs of Metabase's background tasks, which can help shed light on what's going on.`}
+ >
+ <Icon name="info" />
+ </Tooltip>
+ </Flex>
+ <Tabs value={activeTab} onChange={handleTabChange}>
+ <Tabs.List>
+ {tabs.map((tab) => (
+ <Tabs.Tab key={tab.value} value={tab.value}>
+ {tab.label}
+ </Tabs.Tab>
+ ))}
+ </Tabs.List>
+ </Tabs>
+ {children}
+ </SettingsSection>
+ </SettingsPageWrapper>
+ );
+};
+
+export const TasksTabs = withRouter(TasksTabsBase);
diff --git a/frontend/src/metabase/admin/tools/components/TasksTabs/index.ts b/frontend/src/metabase/admin/tools/components/TasksTabs/index.ts
new file mode 100644
index 00000000000..75f604b7611
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TasksTabs/index.ts
@@ -0,0 +1 @@
+export { TasksTabs } from "./TasksTabs";
diff --git a/frontend/src/metabase/admin/tools/components/ToolsApp.tsx b/frontend/src/metabase/admin/tools/components/ToolsApp.tsx
index 1b0f75ebde8..510c35e1be5 100644
--- a/frontend/src/metabase/admin/tools/components/ToolsApp.tsx
+++ b/frontend/src/metabase/admin/tools/components/ToolsApp.tsx
@@ -30,7 +30,7 @@ export function ToolsApp({ location, children }: ToolsAppProps) {
/>
<ToolsNavItem
label={t`Tasks`}
- path={Urls.adminToolsTasks()}
+ path={Urls.adminToolsTasksBase()}
icon="clipboard"
location={location}
/>
diff --git a/frontend/src/metabase/admin/tools/routes.tsx b/frontend/src/metabase/admin/tools/routes.tsx
new file mode 100644
index 00000000000..097af1e873e
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/routes.tsx
@@ -0,0 +1,16 @@
+import { IndexRedirect, Route } from "react-router";
+
+import { TaskDetailsPage } from "./components/TaskDetailsPage";
+import { TaskListPage } from "./components/TaskListPage";
+import { TaskRunDetailsPage } from "./components/TaskRunDetailsPage";
+import { TaskRunsPage } from "./components/TaskRunsPage";
+
+export const getTasksRoutes = () => (
+ <>
+ <IndexRedirect to="list" />
+ <Route path="list" component={TaskListPage} />
+ <Route path="list/:taskId" component={TaskDetailsPage} />
+ <Route path="runs" component={TaskRunsPage} />
+ <Route path="runs/:runId" component={TaskRunDetailsPage} />
+ </>
+);
diff --git a/frontend/src/metabase/admin/tools/utils.ts b/frontend/src/metabase/admin/tools/utils.ts
new file mode 100644
index 00000000000..3c9cb49cdd5
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/utils.ts
@@ -0,0 +1,105 @@
+import { match } from "ts-pattern";
+import { t } from "ttag";
+
+import * as Urls from "metabase/lib/urls";
+import type {
+ Task,
+ TaskRun,
+ TaskRunEntityType,
+ TaskRunStatus,
+ TaskRunType,
+ TaskStatus,
+} from "metabase-types/api";
+
+export const formatTaskRunType = (runType: TaskRunType) =>
+ match(runType)
+ .with("subscription", () => t`Subscription`)
+ .with("alert", () => t`Alert`)
+ .with("sync", () => t`Sync`)
+ .with("fingerprint", () => t`Fingerprint`)
+ .exhaustive();
+
+export const formatTaskRunEntityType = (entityType: TaskRunEntityType) =>
+ match(entityType)
+ .with("database", () => t`Database`)
+ .with("card", () => t`Card`)
+ .with("dashboard", () => t`Dashboard`)
+ .exhaustive();
+
+export const formatTaskRunStatus = (status: TaskRunStatus) =>
+ match(status)
+ .with("started", () => t`Started`)
+ .with("success", () => t`Success`)
+ .with("failed", () => t`Failed`)
+ .with("abandoned", () => t`Abandoned`)
+ .exhaustive();
+
+export const formatTaskStatus = (status: TaskStatus) =>
+ match(status)
+ .with("failed", () => t`Failed`)
+ .with("started", () => t`Started`)
+ .with("success", () => t`Success`)
+ .with("unknown", () => t`Unknown`)
+ .exhaustive();
+
+export const getEntityUrl = (
+ entityType: TaskRunEntityType,
+ entityId: number,
+ entityName?: string,
+): string =>
+ match(entityType)
+ .with("database", () => Urls.viewDatabase(entityId))
+ .with("card", () => Urls.question({ id: entityId, name: entityName }))
+ .with("dashboard", () => Urls.dashboard({ id: entityId }))
+ .exhaustive();
+
+export const getTaskRunStatusColor = (status: TaskRunStatus) =>
+ match(status)
+ .with("success", () => "success" as const)
+ .with("failed", () => "error" as const)
+ .with("abandoned", () => "warning" as const)
+ .otherwise(() => "text-primary" as const);
+
+export const getTaskStatusColor = (status: TaskStatus) =>
+ match(status)
+ .with("success", () => "success" as const)
+ .with("failed", () => "error" as const)
+ .otherwise(() => "text-primary" as const);
+
+export const getFilename = (task: Task | undefined) =>
+ task ? `task-${task.id}.json` : "task.json";
+
+export const formatTaskDetails = (task: Task | undefined): string => {
+ try {
+ return JSON.stringify(task?.task_details, null, 2) ?? "";
+ } catch {
+ return "";
+ }
+};
+
+export const renderTaskRunCounters = ({
+ task_count,
+ success_count,
+ failed_count,
+}: TaskRun) => {
+ const success = t`Success`;
+ const failed = t`Failed`;
+ return `${task_count} (${success}: ${success_count} / ${failed}: ${failed_count})`;
+};
+
+export const guardTaskRunRunType = (value: string): value is TaskRunType =>
+ (
+ ["subscription", "alert", "sync", "fingerprint"] satisfies TaskRunType[]
+ ).includes(value as TaskRunType);
+
+export const guardTaskRunEntityType = (
+ value: string,
+): value is TaskRunEntityType =>
+ (["database", "card", "dashboard"] satisfies TaskRunEntityType[]).includes(
+ value as TaskRunEntityType,
+ );
+
+export const guardTaskRunStatus = (value: string): value is TaskRunStatus =>
+ (
+ ["started", "success", "failed", "abandoned"] satisfies TaskRunStatus[]
+ ).includes(value as TaskRunStatus);
diff --git a/frontend/src/metabase/api/tags/constants.ts b/frontend/src/metabase/api/tags/constants.ts
index d3d87871cd4..2cb60623905 100644
--- a/frontend/src/metabase/api/tags/constants.ts
+++ b/frontend/src/metabase/api/tags/constants.ts
@@ -41,6 +41,7 @@ export const TAG_TYPES = [
"transform",
"table",
"task",
+ "task-run",
"timeline",
"timeline-event",
"transform",
diff --git a/frontend/src/metabase/api/tags/utils.ts b/frontend/src/metabase/api/tags/utils.ts
index 8099c9df706..36592d29071 100644
--- a/frontend/src/metabase/api/tags/utils.ts
+++ b/frontend/src/metabase/api/tags/utils.ts
@@ -39,6 +39,7 @@ import type {
Segment,
Table,
Task,
+ TaskRun,
Timeline,
TimelineEvent,
UserInfo,
@@ -621,6 +622,18 @@ export function provideTaskTags(task: Task): TagDescription<TagType>[] {
return [idTag("task", task.id)];
}
+export function provideTaskRunListTags(
+ taskRuns: TaskRun[],
+): TagDescription<TagType>[] {
+ return [listTag("task-run"), ...taskRuns.flatMap(provideTaskRunTags)];
+}
+
+export function provideTaskRunTags(
+ taskRun: TaskRun,
+): TagDescription<TagType>[] {
+ return [idTag("task-run", taskRun.id)];
+}
+
export function provideTimelineEventListTags(
events: TimelineEvent[],
): TagDescription<TagType>[] {
diff --git a/frontend/src/metabase/api/task.ts b/frontend/src/metabase/api/task.ts
index b0eb01a936a..a4d54aad9c7 100644
--- a/frontend/src/metabase/api/task.ts
+++ b/frontend/src/metabase/api/task.ts
@@ -1,13 +1,20 @@
import type {
+ ListTaskRunEntitiesRequest,
+ ListTaskRunsRequest,
+ ListTaskRunsResponse,
ListTasksRequest,
ListTasksResponse,
+ RunEntity,
Task,
TaskInfo,
+ TaskRunExtended,
} from "metabase-types/api";
import { Api } from "./api";
import {
provideTaskListTags,
+ provideTaskRunListTags,
+ provideTaskRunTags,
provideTaskTags,
provideUniqueTasksListTags,
} from "./tags";
@@ -44,6 +51,34 @@ export const taskApi = Api.injectEndpoints({
url: "/api/task/info",
}),
}),
+ listTaskRuns: builder.query<
+ ListTaskRunsResponse,
+ ListTaskRunsRequest | void
+ >({
+ query: (params) => ({
+ method: "GET",
+ url: "/api/task/runs",
+ params,
+ }),
+ providesTags: (response) =>
+ response ? provideTaskRunListTags(response.data) : [],
+ }),
+ getTaskRun: builder.query<TaskRunExtended, number>({
+ query: (id) => ({
+ method: "GET",
+ url: `/api/task/runs/${id}`,
+ }),
+ providesTags: (taskRun) => (taskRun ? provideTaskRunTags(taskRun) : []),
+ }),
+ listTaskRunEntities: builder.query<RunEntity[], ListTaskRunEntitiesRequest>(
+ {
+ query: (params) => ({
+ method: "GET",
+ url: "/api/task/runs/entities",
+ params,
+ }),
+ },
+ ),
}),
});
@@ -52,4 +87,7 @@ export const {
useListUniqueTasksQuery,
useGetTaskQuery,
useGetTasksInfoQuery,
+ useListTaskRunsQuery,
+ useGetTaskRunQuery,
+ useListTaskRunEntitiesQuery,
} = taskApi;
diff --git a/frontend/src/metabase/lib/urls/admin.ts b/frontend/src/metabase/lib/urls/admin.ts
index 6fdffa9b15a..945941f61af 100644
--- a/frontend/src/metabase/lib/urls/admin.ts
+++ b/frontend/src/metabase/lib/urls/admin.ts
@@ -138,9 +138,24 @@ export function adminToolsHelp() {
return "/admin/tools/help";
}
-export function adminToolsTasks() {
+export function adminToolsTasksBase() {
return "/admin/tools/tasks";
}
+export function adminToolsTasksList() {
+ return `${adminToolsTasksBase()}/list`;
+}
+
+export function adminToolsTaskDetails(taskId: number) {
+ return `${adminToolsTasksList()}/${taskId}`;
+}
+
+export function adminToolsTasksRuns() {
+ return `${adminToolsTasksBase()}/runs`;
+}
+
+export function adminToolsTaskRunDetails(runId: number) {
+ return `${adminToolsTasksRuns()}/${runId}`;
+}
export function adminToolsJobs() {
return "/admin/tools/jobs";
diff --git a/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx b/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx
index 999168f0d73..e3072ac4db4 100644
--- a/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx
+++ b/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx
@@ -40,7 +40,6 @@ export const SearchInputContainer = styled.div<{
}
return css`
background-color: var(--mb-color-background-primary);
-
&:hover {
background-color: var(--mb-color-background-secondary);
}
diff --git a/resources/migrations/059_update_migrations.yaml b/resources/migrations/059_update_migrations.yaml
index a5f05a7fb2b..d935d2430eb 100644
--- a/resources/migrations/059_update_migrations.yaml
+++ b/resources/migrations/059_update_migrations.yaml
@@ -239,7 +239,6 @@ databaseChangeLog:
columns:
- column:
name: result
-
- changeSet:
id: v59.2026-01-12T22:43:43
author: galdre
@@ -253,7 +252,7 @@ databaseChangeLog:
value: null
where: query_type = 'native' AND source_card_id IS NOT NULL
rollback:
- # No rollback - we can't know what the original source_card_id values were
+ # No rollback - we can't know what the original source_card_id values were
- changeSet:
id: v59.2026-01-13T12:00:00
@@ -418,6 +417,211 @@ databaseChangeLog:
constraints:
nullable: true
+ - changeSet:
+ id: v59.2026-01-16T09:30:00
+ author: bronsa
+ comment: Create task_run table for grouping related tasks
+ changes:
+ - createTable:
+ tableName: task_run
+ remarks: Groups related tasks from a single operation (subscription, alert, sync, fingerprint)
+ columns:
+ - column:
+ name: id
+ type: int
+ autoIncrement: true
+ constraints:
+ primaryKey: true
+ nullable: false
+ - column:
+ name: run_type
+ type: varchar(32)
+ remarks: "Type of run: subscription, alert, sync, fingerprint"
+ constraints:
+ nullable: false
+ - column:
+ name: entity_type
+ type: varchar(32)
+ remarks: "Type of associated entity: database, card, dashboard"
+ constraints:
+ nullable: false
+ - column:
+ name: entity_id
+ type: int
+ remarks: ID of associated entity
+ constraints:
+ nullable: false
+ - column:
+ name: started_at
+ type: ${timestamp_type}
+ remarks: When the run started
+ defaultValueComputed: current_timestamp
+ constraints:
+ nullable: false
+ - column:
+ name: ended_at
+ type: ${timestamp_type}
+ remarks: When the run completed (null if still running)
+ constraints:
+ nullable: true
+ - column:
+ name: status
+ type: varchar(21)
+ remarks: "Overall status: started, success, failed"
+ constraints:
+ nullable: false
+ rollback:
+ - dropTable:
+ tableName: task_run
+
+ - changeSet:
+ id: v59.2026-01-16T09:30:01
+ author: bronsa
+ comment: Add composite index on task_run for type/entity queries
+ changes:
+ - createIndex:
+ tableName: task_run
+ indexName: idx_task_run_type_entity
+ columns:
+ - column:
+ name: run_type
+ - column:
+ name: entity_type
+ - column:
+ name: entity_id
+ rollback:
+ - dropIndex:
+ tableName: task_run
+ indexName: idx_task_run_type_entity
+
+ - changeSet:
+ id: v59.2026-01-16T09:30:02
+ author: bronsa
+ comment: Add index on task_run.started_at for ordering
+ changes:
+ - createIndex:
+ tableName: task_run
+ indexName: idx_task_run_started_at
+ columns:
+ - column:
+ name: started_at
+ descending: true
+ rollback:
+ - dropIndex:
+ tableName: task_run
+ indexName: idx_task_run_started_at
+
+ - changeSet:
+ id: v59.2026-01-16T09:30:03
+ author: bronsa
+ comment: Add run_id column to task_history
+ changes:
+ - addColumn:
+ tableName: task_history
+ columns:
+ - column:
+ name: run_id
+ type: int
+ remarks: FK to task_run - groups tasks belonging to the same run
+ constraints:
+ nullable: true
+ rollback:
+ - dropColumn:
+ tableName: task_history
+ columnName: run_id
+
+ - changeSet:
+ id: v59.2026-01-16T09:30:04
+ author: bronsa
+ comment: Add index on task_history.run_id
+ changes:
+ - createIndex:
+ tableName: task_history
+ indexName: idx_task_history_run_id
+ columns:
+ - column:
+ name: run_id
+ rollback:
+ - dropIndex:
+ tableName: task_history
+ indexName: idx_task_history_run_id
+
+ - changeSet:
+ id: v59.2026-01-16T09:30:05
+ author: bronsa
+ comment: Add foreign key from task_history.run_id to task_run
+ changes:
+ - addForeignKeyConstraint:
+ baseTableName: task_history
+ baseColumnNames: run_id
+ referencedTableName: task_run
+ referencedColumnNames: id
+ constraintName: fk_task_history_run_id
+ onDelete: SET NULL
+ rollback:
+ - dropForeignKeyConstraint:
+ baseTableName: task_history
+ constraintName: fk_task_history_run_id
+
+ - changeSet:
+ id: v59.2026-01-16T09:30:06
+ author: bronsa
+ comment: Add logs column to task_history for captured execution logs
+ changes:
+ - addColumn:
+ tableName: task_history
+ columns:
+ - column:
+ name: logs
+ type: ${text.type}
+ remarks: JSON array of captured log messages during task execution
+ constraints:
+ nullable: true
+ rollback:
+ - dropColumn:
+ tableName: task_history
+ columnName: logs
+
+ - changeSet:
+ id: v59.2026-01-16T10:00:00
+ author: bronsa
+ comment: Add process_uuid to task_run for orphan detection
+ changes:
+ - addColumn:
+ tableName: task_run
+ columns:
+ - column:
+ name: process_uuid
+ type: varchar(36)
+ remarks: UUID of the Metabase instance that created this run
+ defaultValue: "unknown"
+ constraints:
+ nullable: false
+ rollback:
+ - dropColumn:
+ tableName: task_run
+ columnName: process_uuid
+
+ - changeSet:
+ id: v59.2026-01-16T10:00:01
+ author: bronsa
+ comment: Add updated_at to task_run for heartbeat tracking
+ changes:
+ - addColumn:
+ tableName: task_run
+ columns:
+ - column:
+ name: updated_at
+ type: ${timestamp_type}
+ remarks: Last heartbeat timestamp - updated periodically for running tasks
+ defaultValueComputed: current_timestamp
+ constraints:
+ nullable: false
+ rollback:
+ - dropColumn:
+ tableName: task_run
+ columnName: updated_at
+
# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<
########################################################################################################################
diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.clj
index a08cbff78c0..754d86f2b82 100644
--- a/src/metabase/models/resolution.clj
+++ b/src/metabase/models/resolution.clj
@@ -97,6 +97,7 @@
:model/SupportAccessGrantLog metabase-enterprise.support-access-grants.models.support-access-grant-log
:model/Table metabase.warehouse-schema.models.table
:model/TaskHistory metabase.task-history.models.task-history
+ :model/TaskRun metabase.task-history.models.task-run
:model/Tenant metabase-enterprise.tenants.model
:model/Timeline metabase.timeline.models.timeline
:model/TimelineEvent metabase.timeline.models.timeline-event
diff --git a/src/metabase/notification/api/notification.clj b/src/metabase/notification/api/notification.clj
index 506031cb011..1d4a1abab52 100644
--- a/src/metabase/notification/api/notification.clj
+++ b/src/metabase/notification/api/notification.clj
@@ -222,14 +222,11 @@
[{:keys [id]} :- [:map [:id ms/PositiveInt]]
_query
{:keys [handler_ids]} :- [:map [:handler_ids {:optional true} [:sequential ms/PositiveInt]]]]
- (let [notification (get-notification id)]
+ (let [notification (cond-> (get-notification id)
+ (seq handler_ids)
+ (update :handlers (fn [handlers] (filter (comp (set handler_ids) :id) handlers))))]
(api/read-check notification)
- (cond-> notification
- (seq handler_ids)
- (update :handlers (fn [handlers] (filter (comp (set handler_ids) :id) handlers)))
-
- true
- (notification/send-notification! :notification/sync? true))))
+ (notification/send-notification! notification :notification/sync? true)))
(defn- promote-to-t2-instance
[notification]
@@ -251,10 +248,10 @@
[_route _query body :- ::models.notification/FullyHydratedNotification]
(api/create-check :model/Notification body)
(models.notification/validate-email-handlers! (:handlers body))
- (-> body
- (assoc :creator_id api/*current-user-id*)
- promote-to-t2-instance
- (notification/send-notification! :notification/sync? true)))
+ (let [notification (-> body
+ (assoc :creator_id api/*current-user-id*)
+ promote-to-t2-instance)]
+ (notification/send-notification! notification :notification/sync? true)))
(defn unsubscribe-user!
"Unsubscribe a user from a notification."
diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.clj
index fc71f2a6103..8a95d35f0f7 100644
--- a/src/metabase/notification/send.clj
+++ b/src/metabase/notification/send.clj
@@ -176,7 +176,9 @@
(prometheus/inc! :metabase-notification/send-error {:payload-type payload_type})
(throw e))
(finally
- (prometheus/dec! :metabase-notification/concurrent-tasks)))
+ (prometheus/dec! :metabase-notification/concurrent-tasks)
+ (when-let [run-id (task-history/current-run-id)]
+ (task-history/complete-task-run! run-id))))
(prometheus/observe! :metabase-notification/send-duration-ms {:payload-type payload_type} (duration-ms-fn))
(when-let [total-time (since-trigger-ms notification-info)]
(prometheus/observe! :metabase-notification/total-duration-ms {:payload-type payload_type} total-time))
@@ -362,7 +364,8 @@
(try
(when-let [notification (take-notification-with-timeout! queue 1000)]
(log/with-restored-context-from-meta notification
- (send-notification-sync! notification)))
+ (task-history/with-restored-run-id notification
+ (send-notification-sync! notification))))
(catch InterruptedException _
(log/warn "Notification worker interrupted, shutting down")
(throw (InterruptedException.)))
@@ -378,7 +381,9 @@
(if-not (.get shutdown-flag)
(do
(ensure-enough-workers!)
- (put-notification! queue (log/with-context-meta notification))
+ (put-notification! queue (-> notification
+ log/with-context-meta
+ task-history/with-run-id-meta))
::ok)
(do
(log/infof "Rejecting notification with id %d as the workers are being shutdown" (:id notification))
@@ -429,17 +434,43 @@
"The default options for sending a notification."
{:notification/sync? false})
+(defn notification->task-run-info
+ "Extract task run info from a notification for use with [[metabase.task-history.core/with-task-run]].
+ - Card notifications (alerts): run_type :alert, entity_type :card
+ - Dashboard notifications (subscriptions): run_type :subscription, entity_type :dashboard
+ - Returns nil for other notification types or if entity_id would be nil.
+ Handles both hydrated notifications (with :payload) and non-hydrated (with :payload_id)."
+ [{:keys [payload_type payload payload_id]}]
+ (case payload_type
+ :notification/card (when-let [card-id (or (:card_id payload)
+ (some->> payload_id
+ (t2/select-one-fn :card_id :model/NotificationCard :id)))]
+ {:run_type :alert
+ :entity_type :card
+ :entity_id card-id})
+ :notification/dashboard (when-let [dashboard-id (or (:dashboard_id payload)
+ (some->> payload_id
+ (t2/select-one-fn :dashboard_id :model/NotificationDashboard :id)))]
+ {:run_type :subscription
+ :entity_type :dashboard
+ :entity_id dashboard-id})
+ nil))
+
(mu/defn send-notification!
"The function to send a notification. Defaults to `notification.send/send-notification-async!`."
[notification & {:keys [] :as options} :- [:maybe Options]]
- (log/with-context {:notification_id (:id notification)
- :payload_type (:payload_type notification)}
- (let [options (merge *default-options* options)
- notification (with-meta notification {:notification/triggered-at-ns (u/start-timer)})]
- (log/debugf "Will be send %s" (if (:notification/sync? options) "synchronously" "asynchronously"))
- (if (:notification/sync? options)
- (send-notification-sync! notification)
- (send-notification-async! notification)))))
+ (let [options (merge *default-options* options)
+ sync? (:notification/sync? options)]
+ ;; with-task-run is a no-op if already nested (e.g., from scheduler)
+ (task-history/with-task-run (some-> (notification->task-run-info notification)
+ (assoc :auto-complete sync?))
+ (log/with-context {:notification_id (:id notification)
+ :payload_type (:payload_type notification)}
+ (let [notification (with-meta notification {:notification/triggered-at-ns (u/start-timer)})]
+ (log/debugf "Will be send %s" (if sync? "synchronously" "asynchronously"))
+ (if sync?
+ (send-notification-sync! notification)
+ (send-notification-async! notification)))))))
(defn shutdown!
"Shutdown all notification workers with wait up to [[timeout-ms]] milliseconds for each workers."
diff --git a/src/metabase/notification/task/send.clj b/src/metabase/notification/task/send.clj
index 82a690e205c..55774c820e6 100644
--- a/src/metabase/notification/task/send.clj
+++ b/src/metabase/notification/task/send.clj
@@ -107,18 +107,19 @@
(cond
(:active notification)
- (try
- (log/info "Submitting to the notification queue")
- (task-history/with-task-history {:task "notification-trigger"
- :task_details {:trigger_type :notification-subscription/cron
- :notification_subscription_id subscription-id
- :cron_schedule (:cron_schedule subscription)
- :notification_ids [notification-id]}}
- (notification.send/send-notification! (assoc notification :triggering_subscription subscription)))
- (log/info "Submitted to the notification queue")
- (catch Exception e
- (log/error e "Failed to submit to the notification queue")
- (throw e)))
+ (task-history/with-task-run (some-> (notification.send/notification->task-run-info notification) (assoc :auto-complete false))
+ (try
+ (log/info "Submitting to the notification queue")
+ (task-history/with-task-history {:task "notification-trigger"
+ :task_details {:trigger_type :notification-subscription/cron
+ :notification_subscription_id subscription-id
+ :cron_schedule (:cron_schedule subscription)
+ :notification_ids [notification-id]}}
+ (notification.send/send-notification! (assoc notification :triggering_subscription subscription)))
+ (log/info "Submitted to the notification queue")
+ (catch Exception e
+ (log/error e "Failed to submit to the notification queue")
+ (throw e))))
(nil? notification)
(do
diff --git a/src/metabase/pulse/api/pulse.clj b/src/metabase/pulse/api/pulse.clj
index 09505ca87f6..7ce618dac45 100644
--- a/src/metabase/pulse/api/pulse.clj
+++ b/src/metabase/pulse/api/pulse.clj
@@ -457,12 +457,13 @@
;; make sure any email addresses that are specified are allowed before sending the test Pulse.
(doseq [channel channels]
(pulse-channel/validate-email-domains channel))
- (notification/with-default-options {:notification/sync? true}
- (pulse.send/send-pulse! (-> body
- (assoc :creator_id api/*current-user-id*)
- (assoc :disable_links
- (embed.util/modular-embedding-or-modular-embedding-sdk-context?
- (get-in request [:headers "x-metabase-client"]))))))
+ (let [pulse (-> body
+ (assoc :creator_id api/*current-user-id*)
+ (assoc :disable_links
+ (embed.util/modular-embedding-or-modular-embedding-sdk-context?
+ (get-in request [:headers "x-metabase-client"]))))]
+ (notification/with-default-options {:notification/sync? true}
+ (pulse.send/send-pulse! pulse)))
{:ok true})
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
diff --git a/src/metabase/pulse/send.clj b/src/metabase/pulse/send.clj
index 15dac27c6c7..a82fa88265d 100644
--- a/src/metabase/pulse/send.clj
+++ b/src/metabase/pulse/send.clj
@@ -3,6 +3,7 @@
(:require
[metabase.models.interface :as mi]
[metabase.pulse.models.pulse :as models.pulse]
+ [metabase.task-history.core :as task-history]
[metabase.util.cron :as u.cron]
[metabase.util.log :as log]
[toucan2.core :as t2]))
@@ -107,6 +108,25 @@
(log/errorf e "[Pulse %d] Error sending to %s channel" (:id pulse) (:channel_type pulse-channel)))))
nil))
+(defn pulse->task-run-info
+ "Extract task run info from a pulse for use with [[metabase.task-history.core/with-task-run]].
+ - Dashboard subscriptions: entity_type :dashboard
+ - Legacy pulses (no dashboard): entity_type :card (first card)
+ - Returns nil if neither dashboard_id nor cards are present."
+ [{:keys [dashboard_id cards]}]
+ (cond
+ dashboard_id
+ {:run_type :subscription
+ :entity_type :dashboard
+ :entity_id dashboard_id}
+
+ (seq cards)
+ {:run_type :subscription
+ :entity_type :card
+ :entity_id (:id (first cards))}
+
+ :else nil))
+
(defn send-pulse!
"Execute and Send a `Pulse`, optionally specifying the specific `PulseChannels`. This includes running each
`PulseCard`, formatting the content, and sending the content to any specified destination.
@@ -121,11 +141,14 @@
[{:keys [dashboard_id], :as pulse} & {:keys [channel-ids async?]
:or {async? false}}]
{:pre [(map? pulse) (integer? (:creator_id pulse))]}
- (let [dashboard (t2/select-one :model/Dashboard :id dashboard_id)
- pulse (-> (mi/instance :model/Pulse pulse)
- ;; This is usually already done by this step, in the `send-pulses` task which uses `retrieve-pulse`
- ;; to fetch the Pulse.
- models.pulse/hydrate-notification
- (merge (when channel-ids {:channel-ids channel-ids})))]
- (when (not (:archived dashboard))
- (send-pulse!* pulse dashboard async?))))
+ ;; with-task-run is a no-op if already nested (e.g., from scheduler)
+ (task-history/with-task-run (some-> (pulse->task-run-info pulse)
+ (assoc :auto-complete (not async?)))
+ (let [dashboard (t2/select-one :model/Dashboard :id dashboard_id)
+ pulse (-> (mi/instance :model/Pulse pulse)
+ ;; This is usually already done by this step, in the `send-pulses` task which uses `retrieve-pulse`
+ ;; to fetch the Pulse.
+ models.pulse/hydrate-notification
+ (merge (when channel-ids {:channel-ids channel-ids})))]
+ (when (not (:archived dashboard))
+ (send-pulse!* pulse dashboard async?)))))
diff --git a/src/metabase/pulse/task/send_pulses.clj b/src/metabase/pulse/task/send_pulses.clj
index d047a33fa27..ec77baabb5b 100644
--- a/src/metabase/pulse/task/send_pulses.clj
+++ b/src/metabase/pulse/task/send_pulses.clj
@@ -49,19 +49,19 @@
(defn- send-pulse!
[pulse-id channel-ids]
(try
- (task-history/with-task-history {:task "send-pulse"
- :task_details {:pulse-id pulse-id
- :channel-ids (seq channel-ids)}}
- (if-let [pulse (models.pulse/retrieve-notification pulse-id
- :archived false
- ;; alerts should all be migrated to notifications by now
- :alert_condition nil)]
- (do
+ (if-let [pulse (models.pulse/retrieve-notification pulse-id
+ :archived false
+ ;; alerts should all be migrated to notifications by now
+ :alert_condition nil)]
+ (task-history/with-task-run (some-> (pulse.send/pulse->task-run-info pulse) (assoc :auto-complete false))
+ (task-history/with-task-history {:task "send-pulse"
+ :task_details {:pulse-id pulse-id
+ :channel-ids (seq channel-ids)}}
(log/debugf "Starting Pulse Execution: %d" pulse-id)
(pulse.send/send-pulse! pulse :channel-ids channel-ids :async? true)
(log/debugf "Finished Pulse Execution: %d" pulse-id)
- :done)
- (log/debugf "Pulse %d not found, Skipping." pulse-id)))
+ :done))
+ (log/debugf "Pulse %d not found, Skipping." pulse-id))
(catch Throwable e
(log/errorf e "Error sending Pulse %d to channel ids: %s" pulse-id (str/join ", " channel-ids)))))
diff --git a/src/metabase/sync/util.clj b/src/metabase/sync/util.clj
index d3ddd788117..645d6ea8809 100644
--- a/src/metabase/sync/util.clj
+++ b/src/metabase/sync/util.clj
@@ -231,6 +231,14 @@
[message & body]
`(do-with-returning-throwable ~message (^:once fn* [] ~@body)))
+(def ^:private operation->run-type
+ "Map sync operation keywords to task run types."
+ {:sync :sync
+ :sync-metadata :sync
+ :cache-field-values :sync
+ :analyze :fingerprint
+ :refingerprint :fingerprint})
+
(mu/defn do-sync-operation
"Internal implementation of [[sync-operation]]; use that instead of calling this directly."
[operation :- :keyword ; something like `:sync-metadata` or `:refingerprint`
@@ -238,15 +246,20 @@
message :- ms/NonBlankString
f :- fn?]
(when (database/should-sync? database)
- ((with-duplicate-ops-prevented
- operation database
- (with-sync-events
- operation database
- (with-start-and-finish-logging
- message
- (with-db-logging-disabled
- (sync-in-context database
- (partial do-with-error-handling (format "Error in sync step %s" message) f)))))))))
+ (let [run-type (operation->run-type operation)]
+ (task-history/with-task-run (when run-type
+ {:run_type run-type
+ :entity_type :database
+ :entity_id (u/the-id database)})
+ ((with-duplicate-ops-prevented
+ operation database
+ (with-sync-events
+ operation database
+ (with-start-and-finish-logging
+ message
+ (with-db-logging-disabled
+ (sync-in-context database
+ (partial do-with-error-handling (format "Error in sync step %s" message) f)))))))))))
(defmacro sync-operation
"Perform the operations in `body` as a sync operation, which wraps the code in several special macros that do things
diff --git a/src/metabase/task_history/api.clj b/src/metabase/task_history/api.clj
index 955338302c3..d2d797054fa 100644
--- a/src/metabase/task_history/api.clj
+++ b/src/metabase/task_history/api.clj
@@ -6,8 +6,11 @@
[metabase.permissions.core :as perms]
[metabase.request.core :as request]
[metabase.task-history.models.task-history :as task-history]
+ [metabase.task-history.models.task-run :as task-run]
[metabase.task.core :as task]
- [metabase.util.malli.schema :as ms]))
+ [metabase.util.malli.registry :as mr]
+ [metabase.util.malli.schema :as ms]
+ [toucan2.core :as t2]))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -57,3 +60,153 @@
[] :- [:vector string?]
(perms/check-has-application-permission :monitoring)
(task-history/unique-tasks))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Task Runs endpoints |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(mr/def ::RunFilterParams
+ [:map
+ [:run-type {:optional true} (into [:enum] (map name task-run/run-types))]
+ [:entity-type {:optional true} (into [:enum] (map name task-run/entity-types))]
+ [:entity-id {:optional true} ms/PositiveInt]
+ [:status {:optional true} [:enum "started" "success" "failed" "abandoned"]]])
+
+(mr/def ::TaskRun
+ [:map
+ [:id ms/PositiveInt]
+ [:run_type (into [:enum] task-run/run-types)]
+ [:entity_type (into [:enum] task-run/entity-types)]
+ [:entity_id ms/PositiveInt]
+ [:started_at :any]
+ [:ended_at [:maybe :any]]
+ [:status [:enum :started :success :failed :abandoned]]
+ [:entity_name {:optional true} [:maybe :string]]
+ [:task_count {:optional true} :int]
+ [:success_count {:optional true} :int]
+ [:failed_count {:optional true} :int]])
+
+(mr/def ::TaskRunsResponse
+ [:map
+ [:total ms/IntGreaterThanOrEqualToZero]
+ [:limit [:maybe ms/PositiveInt]]
+ [:offset [:maybe ms/IntGreaterThanOrEqualToZero]]
+ [:data [:sequential ::TaskRun]]])
+
+(mr/def ::TaskHistory
+ [:map
+ [:id ms/PositiveInt]
+ [:task :string]
+ [:started_at :any]
+ [:ended_at [:maybe :any]]
+ [:duration [:maybe :int]]
+ [:status [:enum :started :success :failed :unknown]]
+ [:task_details {:optional true} [:maybe :map]]
+ [:run_id {:optional true} [:maybe ms/PositiveInt]]
+ [:logs {:optional true} [:maybe :any]]])
+
+(mr/def ::TaskRunWithTasks
+ [:merge ::TaskRun
+ [:map
+ [:tasks [:sequential ::TaskHistory]]]])
+
+(mr/def ::RunEntity
+ [:map
+ [:entity_type (into [:enum] task-run/entity-types)]
+ [:entity_id ms/PositiveInt]
+ [:entity_name {:optional true} [:maybe :string]]])
+
+(def ^:private entity-type->model
+ {:database :model/Database
+ :card :model/Card
+ :dashboard :model/Dashboard})
+
+(defn- hydrate-entity-names
+ "Hydrate entity names based on entity_type and entity_id."
+ [runs]
+ (if (empty? runs)
+ runs
+ (let [grouped (group-by :entity_type runs)
+ name-lookup (into {}
+ (mapcat (fn [[entity-type model]]
+ (when-let [entity-runs (grouped entity-type)]
+ (let [ids (map :entity_id entity-runs)
+ names (t2/select-pk->fn :name model :id [:in ids])]
+ (map (fn [[id name]] [[entity-type id] name]) names))))
+ entity-type->model))]
+ (map #(assoc % :entity_name (get name-lookup [(:entity_type %) (:entity_id %)])) runs))))
+
+(defn- hydrate-task-counts
+ "Add task_count, success_count, failed_count to runs."
+ [runs]
+ (if (empty? runs)
+ runs
+ (let [run-ids (map :id runs)
+ counts (t2/query {:select [:run_id
+ [[:count :id] :task_count]
+ [[:raw "SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END)"] :success_count]
+ [[:raw "SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END)"] :failed_count]]
+ :from :task_history
+ :where [:in :run_id run-ids]
+ :group-by [:run_id]})
+ ;; Coerce counts to int (MySQL may return BigDecimal)
+ counts-by-id (into {} (map (fn [{:keys [run_id task_count success_count failed_count]}]
+ [run_id {:task_count (int task_count)
+ :success_count (int success_count)
+ :failed_count (int failed_count)}])
+ counts))]
+ (map #(merge {:task_count 0 :success_count 0 :failed_count 0}
+ %
+ (get counts-by-id (:id %)))
+ runs))))
+
+(defn- build-run-where-clause
+ [{:keys [run-type entity-type entity-id status]}]
+ (let [clause (cond-> {}
+ run-type (assoc :run_type run-type)
+ entity-type (assoc :entity_type entity-type)
+ entity-id (assoc :entity_id entity-id)
+ status (assoc :status status))]
+ (if (seq clause)
+ {:where (into [:and] (map (fn [[k v]] [:= k v])) clause)}
+ {})))
+
+(api.macros/defendpoint :get "/runs" :- ::TaskRunsResponse
+ "List task runs with optional filters. Returns runs with hydrated entity names and task counts."
+ [_
+ params :- [:maybe ::RunFilterParams]]
+ (perms/check-has-application-permission :monitoring)
+ (let [where-clause (build-run-where-clause params)
+ limit (request/limit)
+ offset (request/offset)
+ runs (t2/select :model/TaskRun (merge where-clause
+ (when limit {:limit limit})
+ (when offset {:offset offset})
+ {:order-by [[:started_at :desc]]}))]
+ {:total (t2/count :model/TaskRun where-clause)
+ :limit limit
+ :offset offset
+ :data (-> runs hydrate-entity-names hydrate-task-counts)}))
+
+(api.macros/defendpoint :get "/runs/:id" :- ::TaskRunWithTasks
+ "Get a single task run with all its child tasks."
+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]]
+ (perms/check-has-application-permission :monitoring)
+ (let [run (api/check-404 (t2/select-one :model/TaskRun :id id))
+ tasks (t2/select :model/TaskHistory :run_id id {:order-by [[:started_at :asc]]})]
+ (-> [run]
+ hydrate-entity-names
+ hydrate-task-counts
+ first
+ (assoc :tasks tasks))))
+
+(api.macros/defendpoint :get "/runs/entities" :- [:sequential ::RunEntity]
+ "Get distinct entities that have task runs for a given run type. Used for populating entity filter picker."
+ [_
+ params :- [:map [:run-type (into [:enum] (map name task-run/run-types))]]]
+ (perms/check-has-application-permission :monitoring)
+ (->> (t2/query {:select-distinct [:entity_type :entity_id]
+ :from :task_run
+ :where [:= :run_type (:run-type params)]})
+ (map #(update % :entity_type keyword))
+ hydrate-entity-names))
diff --git a/src/metabase/task_history/core.clj b/src/metabase/task_history/core.clj
index 7c803389596..18d22164d74 100644
--- a/src/metabase/task_history/core.clj
+++ b/src/metabase/task_history/core.clj
@@ -1,10 +1,18 @@
(ns metabase.task-history.core
(:require
[metabase.task-history.models.task-history]
+ [metabase.task-history.models.task-run]
[potemkin :as p]))
-(comment metabase.task-history.models.task-history/keep-me)
+(comment metabase.task-history.models.task-history/keep-me
+ metabase.task-history.models.task-run/keep-me)
(p/import-vars
[metabase.task-history.models.task-history
- with-task-history])
+ with-task-history]
+ [metabase.task-history.models.task-run
+ complete-task-run!
+ current-run-id
+ with-restored-run-id
+ with-run-id-meta
+ with-task-run])
diff --git a/src/metabase/task_history/init.clj b/src/metabase/task_history/init.clj
index 3dbce228aa8..a56fd6ff3bc 100644
--- a/src/metabase/task_history/init.clj
+++ b/src/metabase/task_history/init.clj
@@ -1,3 +1,4 @@
(ns metabase.task-history.init
(:require
- [metabase.task-history.task.task-history-cleanup]))
+ [metabase.task-history.task.task-history-cleanup]
+ [metabase.task-history.task.task-run-heartbeat]))
diff --git a/src/metabase/task_history/models/task_history.clj b/src/metabase/task_history/models/task_history.clj
index 82a59343bbd..420ff66d7bb 100644
--- a/src/metabase/task_history/models/task_history.clj
+++ b/src/metabase/task_history/models/task_history.clj
@@ -1,15 +1,23 @@
(ns metabase.task-history.models.task-history
(:require
+ ^{:clj-kondo/ignore [:discouraged-namespace]}
+ [clojure.tools.logging]
+ [clojure.tools.logging.impl]
[java-time.api :as t]
+ [metabase.config.core :as config]
[metabase.models.interface :as mi]
[metabase.permissions.core :as perms]
[metabase.premium-features.core :as premium-features]
+ [metabase.task-history.models.task-run :as task-run]
[metabase.util :as u]
[metabase.util.json :as json]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[methodical.core :as methodical]
- [toucan2.core :as t2]))
+ [toucan2.core :as t2])
+ (:import (clojure.lang PersistentQueue)
+ (java.time Clock)
+ (org.apache.commons.lang3.exception ExceptionUtils)))
(set! *warn-on-reflection* true)
@@ -62,6 +70,7 @@
(t2/deftransforms :model/TaskHistory
{:task_details mi/transform-json-eliding
+ :logs mi/transform-json
:status mi/transform-keyword})
(defn- params->where
@@ -82,13 +91,13 @@
dir :sort_direction}]
{:order-by [[col dir]]})
-(def ^:private avaialble-sort-columns
+(def ^:private available-sort-columns
#{:duration :ended_at :started_at})
(def SortParams
"Sorting map schema."
[:map
- [:sort_column {:default :started_at} (into [:enum] avaialble-sort-columns)]
+ [:sort_column {:default :started_at} (into [:enum] available-sort-columns)]
[:sort_direction {:default :desc} [:enum :asc :desc]]])
(mu/defn all
@@ -142,6 +151,73 @@
info)]
(t2/update! :model/TaskHistory th-id updated-info)))
+(def ^:dynamic ^Clock *log-capture-clock*
+ "The java.time.Clock used for captured log message `:timestamp` values. Can be overriden for tests."
+ (Clock/systemUTC))
+
+(def ^:private log-capture-truncation-threshold 100)
+
+(defn- log-capture-atom []
+ (atom {:queue PersistentQueue/EMPTY
+ :trunc {:start-timestamp nil, :last-timestamp nil :levels {}}}))
+
+(defn- elide-string
+ "Elides the string to the specified length, adding '...' if it exceeds that length."
+ [s max-length]
+ (if (> (count s) max-length)
+ (str (subs s 0 (- max-length 3)) "...")
+ s))
+
+(defn- format-timestamp
+ "Format a timestamp from the clock as an ISO instant string."
+ [^Clock clock]
+ (t/format :iso-instant (t/instant clock)))
+
+(defn- log-capture-entries [{:keys [queue trunc]}]
+ (if (nil? (:last-timestamp trunc))
+ (vec queue)
+ (into [{:level :info
+ :timestamp (:last-timestamp trunc)
+ :fqns "metabase.task-history"
+ :msg (format "[truncated] %d messages" (apply + (vals (:levels trunc))))
+ :trunc trunc}]
+ queue)))
+
+(defn- log-capture-entry [fqns level msg ^Throwable e]
+ (cond->
+ {:level level
+ :timestamp (format-timestamp *log-capture-clock*)
+ :fqns (str fqns)
+ :msg (elide-string (str msg) 4000)
+ :process_uuid config/local-process-uuid}
+ e (assoc :exception
+ (take 20 (map #(elide-string (str %) 500)
+ (seq (ExceptionUtils/getStackFrames e)))))))
+
+(defn- add-log-capture-entry [{:keys [queue, trunc]} entry]
+ (if (< (count queue) log-capture-truncation-threshold)
+ {:queue (conj queue entry), :trunc trunc}
+ (let [removed (peek queue)
+ {:keys [start-timestamp, levels]} trunc]
+ {:queue (conj (pop queue) entry)
+ :trunc {:levels (update levels (:level removed) (fnil inc 0))
+ :start-timestamp (or start-timestamp (:timestamp removed))
+ :last-timestamp (:timestamp removed)}})))
+
+(defn- log-capture-factory [base-factory logs-atom]
+ (reify clojure.tools.logging.impl/LoggerFactory
+ (name [_] "metabase.task_history")
+ (get-logger [_ logger-ns]
+ (let [base-logger (clojure.tools.logging.impl/get-logger base-factory logger-ns)]
+ (reify clojure.tools.logging.impl/Logger
+ (enabled? [_ level] (clojure.tools.logging.impl/enabled? base-logger level))
+ (write! [_ level ex msg]
+ (case level
+ (:fatal :error :warn :info)
+ (swap! logs-atom add-log-capture-entry (log-capture-entry logger-ns level msg ex))
+ nil)
+ (clojure.tools.logging.impl/write! base-logger level ex msg)))))))
+
(mu/defn do-with-task-history
"Impl for `with-task-history` macro; see documentation below."
[info :- TaskHistoryInfo f]
@@ -149,26 +225,33 @@
on-fail-info (or (:on-fail-info info) (fn [& args] (first args)))
info (dissoc info :on-success-info :on-fail-info)
start-time-ns (System/nanoTime)
+ run-id (task-run/current-run-id)
th-id (t2/insert-returning-pk! :model/TaskHistory
- (assoc info
- :status :started
- :started_at (t/instant)))]
- (try
- (u/prog1 (f)
- (update-task-history! th-id start-time-ns (on-success-info {:status :success
- :task_details (:task_details info)}
- <>)))
- (catch Throwable e
- (update-task-history! th-id start-time-ns
- (on-fail-info {:task_details {:status :failed
- :exception (class e)
- :message (.getMessage e)
- :stacktrace (u/filtered-stacktrace e)
- :ex-data (ex-data e)
- :original-info (:task_details info)}
- :status :failed}
- e))
- (throw e)))))
+ (cond-> (assoc info
+ :status :started
+ :started_at (t/instant))
+ run-id (assoc :run_id run-id)))
+ logs-atom (log-capture-atom)]
+ (binding [clojure.tools.logging/*logger-factory*
+ (log-capture-factory clojure.tools.logging/*logger-factory* logs-atom)]
+ (try
+ (u/prog1 (f)
+ (update-task-history! th-id start-time-ns (on-success-info {:status :success
+ :task_details (:task_details info)
+ :logs (log-capture-entries @logs-atom)}
+ <>)))
+ (catch Throwable e
+ (update-task-history! th-id start-time-ns
+ (on-fail-info {:task_details {:status :failed
+ :exception (class e)
+ :message (.getMessage e)
+ :stacktrace (u/filtered-stacktrace e)
+ :ex-data (ex-data e)
+ :original-info (:task_details info)}
+ :logs (log-capture-entries @logs-atom)
+ :status :failed}
+ e))
+ (throw e))))))
(defmacro with-task-history
"Record a TaskHistory before executing the body, updating TaskHistory accordingly when the body completes.
diff --git a/src/metabase/task_history/models/task_run.clj b/src/metabase/task_history/models/task_run.clj
new file mode 100644
index 00000000000..2e3112d68b7
--- /dev/null
+++ b/src/metabase/task_history/models/task_run.clj
@@ -0,0 +1,138 @@
+(ns metabase.task-history.models.task-run
+ "Model for TaskRun - groups related tasks from a single operation (subscription, alert, sync, fingerprint)."
+ (:require
+ [java-time.api :as t]
+ [metabase.config.core :as config]
+ [metabase.models.interface :as mi]
+ [metabase.permissions.core :as perms]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]
+ [metabase.util.malli.schema :as ms]
+ [methodical.core :as methodical]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+(methodical/defmethod t2/table-name :model/TaskRun [_model] :task_run)
+
+(doto :model/TaskRun
+ (derive :metabase/model)
+ (derive ::mi/read-policy.full-perms-for-perms-set)
+ (derive ::mi/write-policy.full-perms-for-perms-set))
+
+;;; Permissions to read or write TaskRun. Same as TaskHistory - requires superusers or monitoring permission.
+(defmethod mi/perms-objects-set :model/TaskRun
+ [_task-run _read-or-write]
+ #{(if (premium-features/enable-advanced-permissions?)
+ (perms/application-perms-path :monitoring)
+ "/")})
+
+(def run-types
+ "Valid run types."
+ #{:subscription :alert :sync :fingerprint})
+
+(def entity-types
+ "Valid entity types for task runs."
+ #{:database :card :dashboard})
+
+(def ^:private task-run-status #{:started :success :failed :abandoned})
+
+(defn- assert-task-run-status
+ [status]
+ (assert (task-run-status (keyword status)) "Invalid task run status"))
+
+(t2/define-after-insert :model/TaskRun
+ [task-run]
+ (assert-task-run-status (:status task-run))
+ task-run)
+
+(t2/define-before-update :model/TaskRun
+ [task-run]
+ (when (contains? task-run :status)
+ (assert-task-run-status (:status task-run)))
+ task-run)
+
+(t2/deftransforms :model/TaskRun
+ {:run_type mi/transform-keyword
+ :entity_type mi/transform-keyword
+ :status mi/transform-keyword})
+
+(def ^:dynamic *run-id*
+ "The ID of the current task run. Set by [[with-task-run]]."
+ nil)
+
+(defn current-run-id
+ "Returns the current task run ID, or nil if not inside a [[with-task-run]] block."
+ []
+ *run-id*)
+
+(defn with-run-id-meta
+ "Given a map, returns the map with `*run-id*` attached to metadata.
+ Used for propagation of run context across threads (e.g., async notification dispatch)."
+ [m]
+ (vary-meta m assoc ::run-id *run-id*))
+
+(defmacro with-restored-run-id
+ "Given a map presumably containing metadata from [[with-run-id-meta]], restores `*run-id*` and executes body."
+ [m & body]
+ `(binding [*run-id* (::run-id (meta ~m))]
+ ~@body))
+
+(mr/def ::TaskRunInfo
+ [:map {:closed true}
+ [:run_type (into [:enum] run-types)]
+ [:entity_type (into [:enum] entity-types)]
+ [:entity_id ms/PositiveInt]
+ [:auto-complete {:optional true} [:maybe :boolean]]])
+
+(mu/defn create-task-run! :- ms/PositiveInt
+ "Create a new task run record. Returns the run ID."
+ [{:keys [run_type entity_type entity_id]} :- ::TaskRunInfo]
+ (let [now (mi/now)]
+ (t2/insert-returning-pk! :model/TaskRun
+ {:run_type run_type
+ :entity_type entity_type
+ :entity_id entity_id
+ :status :started
+ :started_at now
+ :updated_at now
+ :process_uuid config/local-process-uuid})))
+
+(mu/defn complete-task-run!
+ "Mark a task run as complete, deriving status from child tasks.
+ Must be called manually for async flows, or automatically via [[with-task-run]].
+ Idempotent - only completes if status is still :started."
+ [run-id :- ms/PositiveInt]
+ (let [task-statuses (t2/select-fn-set :status :model/TaskHistory :run_id run-id)
+ status (if (= #{:success} task-statuses)
+ :success
+ :failed)]
+ (t2/update! :model/TaskRun {:id run-id :status :started}
+ {:status status
+ :ended_at (t/instant)})))
+
+(defmacro with-task-run
+ "Wrap a root flow to group all tasks under a single run.
+ `run-info` should contain :run_type, :entity_type, :entity_id.
+
+ If `run-info` is nil or already inside a task run, just executes body without
+ creating a new task run (prevents nesting).
+
+ For async flows (e.g., async notifications), use :auto-complete false and
+ call [[complete-task-run!]] manually when all async work is done."
+ {:style/indent 1}
+ [run-info & body]
+ `(if (or *run-id* (nil? ~run-info))
+ (do ~@body)
+ (let [info# ~run-info
+ auto-complete# (get info# :auto-complete true)
+ run-id# (create-task-run! info#)]
+ (binding [*run-id* run-id#]
+ (if auto-complete#
+ (try
+ ~@body
+ (finally
+ (complete-task-run! run-id#)))
+ ;; Async: caller is responsible for calling complete-task-run!
+ (do ~@body))))))
diff --git a/src/metabase/task_history/task/task_run_heartbeat.clj b/src/metabase/task_history/task/task_run_heartbeat.clj
new file mode 100644
index 00000000000..65fa4da5c93
--- /dev/null
+++ b/src/metabase/task_history/task/task_run_heartbeat.clj
@@ -0,0 +1,99 @@
+(ns metabase.task-history.task.task-run-heartbeat
+ "Scheduled task to:
+ 1. Send heartbeat for running task runs owned by this process
+ 2. Mark orphaned task runs (no heartbeat for threshold hours) as :abandoned
+ 3. Mark orphaned tasks (in :started status with no heartbeat) as :unknown"
+ (:require
+ [clojurewerkz.quartzite.jobs :as jobs]
+ [clojurewerkz.quartzite.schedule.cron :as cron]
+ [clojurewerkz.quartzite.triggers :as triggers]
+ [metabase.app-db.core :as mdb]
+ [metabase.config.core :as config]
+ [metabase.models.interface :as mi]
+ [metabase.task.core :as task]
+ [metabase.util.honey-sql-2 :as h2x]
+ [metabase.util.log :as log]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private orphan-threshold-hours
+ "Runs with no heartbeat for this many hours are marked as orphaned."
+ 1)
+
+(def ^:private max-run-duration-hours
+ "Runs older than this many hours are marked as orphaned regardless of heartbeat."
+ 24)
+
+(defn send-heartbeat!
+ "Update updated_at for all :started runs belonging to this process."
+ []
+ (let [updated (t2/update! :model/TaskRun
+ {:status :started
+ :process_uuid config/local-process-uuid}
+ {:updated_at (mi/now)})]
+ (when (pos? updated)
+ (log/debugf "Sent heartbeat for %d running task runs" updated))
+ updated))
+
+(defn mark-orphaned-runs!
+ "Mark runs as :abandoned if:
+ 1. updated_at is older than threshold (no heartbeat), OR
+ 2. started_at is older than max run duration (stuck run)
+ Returns the set of run IDs that were marked as orphaned."
+ []
+ (let [heartbeat-cutoff (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- orphan-threshold-hours) :hour)
+ duration-cutoff (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- max-run-duration-hours) :hour)
+ orphaned-run-ids (t2/select-fn-set :id :model/TaskRun
+ {:where [:and
+ [:= :status "started"]
+ [:or
+ [:< :updated_at heartbeat-cutoff]
+ [:< :started_at duration-cutoff]]]})]
+ (when (seq orphaned-run-ids)
+ (t2/update! :model/TaskRun {:id [:in orphaned-run-ids]}
+ {:status :abandoned
+ :ended_at (mi/now)})
+ (log/infof "Marked %d abandoned task runs" (count orphaned-run-ids)))
+ orphaned-run-ids))
+
+(defn mark-orphaned-tasks!
+ "Mark tasks as :unknown if they belong to the given orphaned runs."
+ [orphaned-run-ids]
+ (when (seq orphaned-run-ids)
+ (let [orphaned (t2/update! :model/TaskHistory
+ {:status :started
+ :run_id [:in orphaned-run-ids]}
+ {:status :unknown
+ :ended_at (mi/now)})]
+ (when (pos? orphaned)
+ (log/infof "Marked %d orphaned tasks as :unknown" orphaned))
+ orphaned)))
+
+(defn- task-run-heartbeat!
+ "Send heartbeat for running tasks and mark orphaned runs/tasks."
+ []
+ (log/debug "Running task run heartbeat")
+ (send-heartbeat!)
+ (let [orphaned-run-ids (mark-orphaned-runs!)]
+ (mark-orphaned-tasks! orphaned-run-ids)))
+
+(task/defjob
+ ^{:doc "Send heartbeat for running task runs and mark orphaned runs as :unknown"}
+ TaskRunHeartbeat [_]
+ (task-run-heartbeat!))
+
+(def ^:private job-key "metabase.task.task-run-heartbeat.job")
+(def ^:private trigger-key "metabase.task.task-run-heartbeat.trigger")
+
+(defmethod task/init! ::TaskRunHeartbeat [_]
+ (let [job (jobs/build
+ (jobs/of-type TaskRunHeartbeat)
+ (jobs/with-identity (jobs/key job-key)))
+ trigger (triggers/build
+ (triggers/with-identity (triggers/key trigger-key))
+ (triggers/start-now)
+ (triggers/with-schedule
+ ;; run every 10 minutes
+ (cron/cron-schedule "0 */10 * * * ? *")))]
+ (task/schedule-task! job trigger)))
diff --git a/test/metabase/cmd/copy_test.clj b/test/metabase/cmd/copy_test.clj
index b90f4734eac..0f2317d9d62 100644
--- a/test/metabase/cmd/copy_test.clj
+++ b/test/metabase/cmd/copy_test.clj
@@ -47,6 +47,7 @@
:model/SemanticSearchTokenTracking
:model/SupportAccessGrantLog
:model/TaskHistory
+ :model/TaskRun
;; TODO we should remove these models from here once serialization is supported
:model/Transform
:model/TransformRun
diff --git a/test/metabase/sync/util_test.clj b/test/metabase/sync/util_test.clj
index 82ea461ad02..6e262d0affc 100644
--- a/test/metabase/sync/util_test.clj
+++ b/test/metabase/sync/util_test.clj
@@ -127,14 +127,14 @@
(is (= [step-1-name step-2-name]
(map first (:steps results)))))
(testing "operation history"
- (is (= (merge default-task-history {:task process-name, :task_details nil})
- (fetch-task-history-row process-name))))
+ (is (=? (merge default-task-history {:task process-name, :task_details nil})
+ (fetch-task-history-row process-name))))
(testing "step 1 history"
- (is (= (merge default-task-history {:task step-1-name, :task_details {:foo "bar"}})
- (fetch-task-history-row step-1-name))))
+ (is (=? (merge default-task-history {:task step-1-name, :task_details {:foo "bar"}})
+ (fetch-task-history-row step-1-name))))
(testing "step 2 history"
- (is (= (merge default-task-history {:task step-2-name, :task_details nil})
- (fetch-task-history-row step-2-name))))))
+ (is (=? (merge default-task-history {:task step-2-name, :task_details nil})
+ (fetch-task-history-row step-2-name))))))
(deftest run-sync-operation-record-failed-task-history-test
(let [process-name (mt/random-name)
@@ -150,8 +150,8 @@
(throw (ex-info "Sorry" {})))))]]
(call-with-operation-info! #(sync-util/run-sync-operation process-name mock-db sync-steps))
(testing "operation history"
- (is (= (merge default-task-history {:task process-name, :task_details nil})
- (fetch-task-history-row process-name))))
+ (is (=? (merge default-task-history {:task process-name, :task_details nil})
+ (fetch-task-history-row process-name))))
(testing "step history should has status is failed"
(is (=? (merge default-task-history
{:task step-name-1
diff --git a/test/metabase/task_history/api_test.clj b/test/metabase/task_history/api_test.clj
index 830dfb03705..20e55e1fd68 100644
--- a/test/metabase/task_history/api_test.clj
+++ b/test/metabase/task_history/api_test.clj
@@ -7,7 +7,7 @@
[toucan2.core :as t2]))
(def ^:private default-task-history
- {:id true, :db_id true, :started_at true, :ended_at true, :duration 10, :task_details nil :status "success"})
+ {:id true, :db_id true, :started_at true, :ended_at true, :duration 10, :task_details nil :status "success" :logs nil :run_id false})
(defn- generate-tasks
"Creates `n` task history maps with guaranteed increasing `:ended_at` times. This means that when stored and queried
@@ -496,3 +496,190 @@
(is (= 1 (-> response :data count)))
(is [{:duration 10000}]
(-> response :data vec)))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Task Runs API tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest runs-list-perms-test
+ (testing "Only superusers can query for TaskRuns"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :get 403 "task/runs")))))
+
+(deftest ^:synchronized runs-list-test
+ (t2/delete! :model/TaskRun)
+ (testing "Superusers can list TaskRuns"
+ (mt/with-temp [:model/Database db {:name "Test DB"}
+ :model/TaskRun _run1 {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db)
+ :status :success
+ :started_at (t/minus (t/zoned-date-time) (t/hours 2))
+ :ended_at (t/minus (t/zoned-date-time) (t/hours 1))}
+ :model/TaskRun run2 {:run_type :fingerprint
+ :entity_type :database
+ :entity_id (:id db)
+ :status :started
+ :started_at (t/zoned-date-time)}]
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs")]
+ (is (= 2 (:total response)))
+ (is (= 2 (count (:data response))))
+ (testing "runs are sorted by started_at desc"
+ (is (= (:id run2) (-> response :data first :id))))
+ (testing "entity_name is hydrated"
+ (is (= "Test DB" (-> response :data first :entity_name))))))))
+
+(deftest ^:synchronized runs-filter-test
+ (t2/delete! :model/TaskRun)
+ (mt/with-temp [:model/Database db1 {:name "DB1"}
+ :model/Database db2 {:name "DB2"}
+ :model/Dashboard dash {:name "My Dashboard"}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db1)
+ :status :success
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db2)
+ :status :failed
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}
+ :model/TaskRun _ {:run_type :subscription
+ :entity_type :dashboard
+ :entity_id (:id dash)
+ :status :success
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}]
+ (testing "filter by run-type"
+ (is (= 2 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :run-type "sync"))))
+ (is (= 1 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :run-type "subscription")))))
+ (testing "filter by entity-type"
+ (is (= 2 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :entity-type "database"))))
+ (is (= 1 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :entity-type "dashboard")))))
+ (testing "filter by entity-id"
+ (is (= 1 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :entity-id (:id db1))))))
+ (testing "filter by status"
+ (is (= 2 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :status "success"))))
+ (is (= 1 (:total (mt/user-http-request :crowberto :get 200 "task/runs" :status "failed")))))
+ (testing "combined filters"
+ (is (= 1 (:total (mt/user-http-request :crowberto :get 200 "task/runs"
+ :run-type "sync"
+ :status "failed")))))))
+
+(deftest runs-get-single-test
+ (testing "non-superuser cannot access single run"
+ (mt/with-temp [:model/TaskRun run {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :success
+ :started_at (t/zoned-date-time)}]
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :get 403 (format "task/runs/%d" (:id run)))))))
+
+ (testing "404 for non-existent run"
+ (is (= "Not found."
+ (mt/user-http-request :crowberto :get 404 (format "task/runs/%d" Integer/MAX_VALUE)))))
+
+ (testing "superuser can get single run with tasks"
+ (t2/delete! :model/TaskRun)
+ (t2/delete! :model/TaskHistory)
+ (mt/with-temp [:model/Database db {:name "Test DB"}
+ :model/TaskRun run {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db)
+ :status :success
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}
+ :model/TaskHistory th1 {:task "task1"
+ :status :success
+ :run_id (:id run)
+ :started_at (t/minus (t/zoned-date-time) (t/seconds 2))
+ :ended_at (t/minus (t/zoned-date-time) (t/seconds 1))}
+ :model/TaskHistory _th2 {:task "task2"
+ :status :success
+ :run_id (:id run)
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}]
+ (let [response (mt/user-http-request :crowberto :get 200 (format "task/runs/%d" (:id run)))]
+ (is (= (:id run) (:id response)))
+ (is (= "Test DB" (:entity_name response)))
+ (is (= 2 (count (:tasks response))))
+ (testing "tasks are sorted by started_at asc"
+ (is (= (:id th1) (-> response :tasks first :id))))))))
+
+(deftest runs-entities-perms-test
+ (testing "non-superuser cannot access runs/entities"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :get 403 "task/runs/entities" :run-type "sync")))))
+
+(deftest ^:synchronized runs-task-counts-test
+ (t2/delete! :model/TaskRun)
+ (t2/delete! :model/TaskHistory)
+ (testing "task counts are hydrated correctly"
+ (mt/with-temp [:model/Database db {:name "Test DB"}
+ :model/TaskRun run {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db)
+ :status :failed
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}
+ :model/TaskHistory _ {:task "task1"
+ :status :success
+ :run_id (:id run)
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}
+ :model/TaskHistory _ {:task "task2"
+ :status :success
+ :run_id (:id run)
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}
+ :model/TaskHistory _ {:task "task3"
+ :status :failed
+ :run_id (:id run)
+ :started_at (t/zoned-date-time)
+ :ended_at (t/zoned-date-time)}]
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs")
+ run-data (first (:data response))]
+ (is (= 3 (:task_count run-data)))
+ (is (= 2 (:success_count run-data)))
+ (is (= 1 (:failed_count run-data)))))))
+
+(deftest ^:synchronized runs-entities-test
+ (t2/delete! :model/TaskRun)
+ (testing "returns distinct entities for run type with hydrated names"
+ (mt/with-temp [:model/Database db1 {:name "DB1"}
+ :model/Database db2 {:name "DB2"}
+ :model/Dashboard dash {:name "My Dashboard"}
+ ;; Multiple sync runs for same database
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db1)
+ :status :success
+ :started_at (t/zoned-date-time)}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db1)
+ :status :success
+ :started_at (t/zoned-date-time)}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db2)
+ :status :success
+ :started_at (t/zoned-date-time)}
+ :model/TaskRun _ {:run_type :subscription
+ :entity_type :dashboard
+ :entity_id (:id dash)
+ :status :success
+ :started_at (t/zoned-date-time)}]
+ (testing "sync entities"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities" :run-type "sync")]
+ (is (= 2 (count response)))
+ (is (every? #(= "database" (:entity_type %)) response))
+ (is (= #{"DB1" "DB2"} (set (map :entity_name response))))))
+ (testing "subscription entities"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities" :run-type "subscription")]
+ (is (= 1 (count response)))
+ (is (= "dashboard" (-> response first :entity_type)))
+ (is (= "My Dashboard" (-> response first :entity_name))))))))
diff --git a/test/metabase/task_history/models/task_history_test.clj b/test/metabase/task_history/models/task_history_test.clj
index 4084963ac6c..bf13e827489 100644
--- a/test/metabase/task_history/models/task_history_test.clj
+++ b/test/metabase/task_history/models/task_history_test.clj
@@ -5,7 +5,10 @@
[metabase.task-history.models.task-history :as task-history]
[metabase.test :as mt]
[metabase.util :as u]
- [toucan2.core :as t2]))
+ [metabase.util.log :as log]
+ [toucan2.core :as t2])
+ (:import
+ (java.time Clock Instant ZoneId)))
(set! *warn-on-reflection* true)
@@ -108,10 +111,11 @@
(task-history/with-task-history {:task task-name
:task_details {:id 1}
:on-success-info (fn [info result]
- (testing "info should have task_details and updated status"
- (is (= {:task_details {:id 1}
- :status :success}
- info)))
+ (testing "info should have task_details, logs, and updated status"
+ (is (=? {:task_details {:id 1}
+ :logs (mt/malli=? vector?)
+ :status :success}
+ info)))
(update info :task_details assoc :result result))}
42)
(is (= {:status :success
@@ -144,3 +148,97 @@
:original-info {:id 1}
:reason "test"}}
(t2/select-one [:model/TaskHistory :status :task_details] :task task-name)))))))
+
+(deftest log-capture-test
+ (mt/with-model-cleanup [:model/TaskHistory]
+ (testing "logs are captured on success"
+ (let [task-name (mt/random-name)]
+ (binding [task-history/*log-capture-clock* (Clock/fixed (Instant/ofEpochMilli 1000) (ZoneId/of "UTC"))]
+ (task-history/with-task-history {:task task-name}
+ (log/info "info message")
+ (log/warn "warning message")
+ (log/error "error message")))
+ (let [{:keys [logs status]} (t2/select-one :model/TaskHistory :task task-name)]
+ (is (= :success status))
+ (is (=? [{:level "info", :msg "info message", :timestamp "1970-01-01T00:00:01Z", :fqns string?}
+ {:level "warn", :msg "warning message", :timestamp "1970-01-01T00:00:01Z", :fqns string?}
+ {:level "error", :msg "error message", :timestamp "1970-01-01T00:00:01Z", :fqns string?}]
+ logs)))))
+
+ (testing "logs are captured on failure"
+ (let [task-name (mt/random-name)]
+ (u/ignore-exceptions
+ (task-history/with-task-history {:task task-name}
+ (log/info "before exception")
+ (throw (ex-info "Test failure" {:reason :test}))))
+ (let [{:keys [logs status]} (t2/select-one :model/TaskHistory :task task-name)]
+ (is (= :failed status))
+ (is (=? [{:level "info", :msg "before exception", :timestamp string?, :fqns string?}] logs)))))
+
+ (testing "exception details are captured in logs"
+ (let [task-name (mt/random-name)]
+ (u/ignore-exceptions
+ (task-history/with-task-history {:task task-name}
+ (log/error (Exception. "Test exception") "error message")))
+ (let [{:keys [logs status]} (t2/select-one :model/TaskHistory :task task-name)]
+ (is (= :success status))
+ (is (=? [{:level "error"
+ :msg "error message"
+ :timestamp string?
+ :fqns string?
+ :exception (mt/malli=? [:sequential string?])}]
+ logs)))))
+
+ (testing "debug/trace are elided"
+ (let [task-name (mt/random-name)]
+ (task-history/with-task-history {:task task-name}
+ (log/error "error")
+ (log/fatal "fatal"))
+ (let [{:keys [logs status]} (t2/select-one :model/TaskHistory :task task-name)]
+ (is (= :success status))
+ (is (=? [{:level "error"} {:level "fatal"}] logs)))))
+
+ (testing "task with no logs"
+ (let [task-name (mt/random-name)]
+ (task-history/with-task-history {:task task-name})
+ (let [{:keys [logs status]} (t2/select-one :model/TaskHistory :task task-name)]
+ (is (= :success status))
+ (is (= [] logs)))))))
+
+(deftest log-truncation-test
+ (mt/with-model-cleanup [:model/TaskHistory]
+ (testing "logs are truncated when threshold is exceeded"
+ (let [task-name (mt/random-name)
+ threshold 5]
+ (with-redefs [task-history/log-capture-truncation-threshold threshold]
+ (binding [task-history/*log-capture-clock* (Clock/fixed (Instant/ofEpochMilli 1000) (ZoneId/of "UTC"))]
+ (task-history/with-task-history {:task task-name}
+ ;; Generate more messages than the threshold
+ (dotimes [i 10]
+ (set! task-history/*log-capture-clock* (Clock/fixed (Instant/ofEpochMilli (+ 1000 i)) (ZoneId/of "UTC")))
+ (log/info (str "message " i))
+ (log/warn (str "warning " i))))))
+ (let [{:keys [logs status]} (t2/select-one :model/TaskHistory :task task-name)]
+ (is (= :success status))
+ (testing "logs include truncation message plus threshold entries"
+ (is (= (inc threshold) (count logs))))
+ (testing "first entry is truncation message"
+ (let [{:keys [level timestamp msg trunc]} (first logs)]
+ (is (= "info" level))
+ (is (= "1970-01-01T00:00:01.007Z" timestamp) ":timestamp is of the last removed message")
+ (is (= "[truncated] 15 messages" msg))
+ (testing "truncation metadata tracks removed messages by level"
+ (let [{:keys [start-timestamp last-timestamp levels]} trunc]
+ (is (= "1970-01-01T00:00:01Z" start-timestamp))
+ (is (= "1970-01-01T00:00:01.007Z" last-timestamp))
+ (is (= {:info 8, :warn 7} levels))))))
+ (testing "remaining entries are the most recent messages"
+ (let [remaining-logs (rest logs)]
+ (is (= threshold (count remaining-logs)))
+ ;; The last 5 entries should be the final messages
+ (is (=? [{:level "warn" :msg "warning 7" :timestamp "1970-01-01T00:00:01.007Z" :fqns string?}
+ {:level "info" :msg "message 8" :timestamp "1970-01-01T00:00:01.008Z" :fqns string?}
+ {:level "warn" :msg "warning 8" :timestamp "1970-01-01T00:00:01.008Z" :fqns string?}
+ {:level "info" :msg "message 9" :timestamp "1970-01-01T00:00:01.009Z" :fqns string?}
+ {:level "warn" :msg "warning 9" :timestamp "1970-01-01T00:00:01.009Z" :fqns string?}]
+ remaining-logs)))))))))
diff --git a/test/metabase/task_history/models/task_run_test.clj b/test/metabase/task_history/models/task_run_test.clj
new file mode 100644
index 00000000000..7f1758ac030
--- /dev/null
+++ b/test/metabase/task_history/models/task_run_test.clj
@@ -0,0 +1,359 @@
+(ns metabase.task-history.models.task-run-test
+ "Tests for TaskRun model and with-task-run macro."
+ (:require
+ [clojure.test :refer :all]
+ [metabase.models.interface :as mi]
+ [metabase.notification.send :as notification.send]
+ [metabase.notification.test-util :as notification.tu]
+ [metabase.pulse.send :as pulse.send]
+ [metabase.pulse.test-util :as pulse.tu]
+ [metabase.sync.util :as sync-util]
+ [metabase.task-history.core :as task-history]
+ [metabase.task-history.models.task-run :as task-run]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | with-task-run basic tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest with-task-run-creates-run-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "with-task-run creates a task run record"
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 1}
+ (is (some? (task-history/current-run-id)) "run-id is bound")
+ (let [run (t2/select-one :model/TaskRun :id (task-history/current-run-id))]
+ (is (= :started (:status run)) "status is :started during execution")
+ (is (= :sync (:run_type run)))
+ (is (= :database (:entity_type run)))
+ (is (= 1 (:entity_id run))))))))
+
+(deftest with-task-run-auto-complete-success-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "with-task-run auto-completes with :success when children all succeed"
+ (let [run-id (atom nil)]
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 1}
+ (reset! run-id (task-history/current-run-id))
+ (task-history/with-task-history {:task "test-task-1"}
+ :done)
+ (task-history/with-task-history {:task "test-task-2"}
+ :done))
+ (let [run (t2/select-one :model/TaskRun :id @run-id)]
+ (is (= :success (:status run)))
+ (is (some? (:ended_at run))))))))
+
+(deftest with-task-run-auto-complete-failure-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "with-task-run auto-completes with :failed when any child fails"
+ (let [run-id (atom nil)]
+ (try
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 1}
+ (reset! run-id (task-history/current-run-id))
+ (task-history/with-task-history {:task "test-task-1"}
+ :done)
+ (task-history/with-task-history {:task "test-task-2"}
+ (throw (Exception. "fail"))))
+ (catch Exception _))
+ (let [run (t2/select-one :model/TaskRun :id @run-id)]
+ (is (= :failed (:status run)))
+ (is (some? (:ended_at run))))))))
+
+(deftest with-task-run-auto-complete-false-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "with-task-run does NOT auto-complete when :auto-complete false"
+ (let [run-id (atom nil)]
+ (task-history/with-task-run {:run_type :subscription
+ :entity_type :dashboard
+ :entity_id 1
+ :auto-complete false}
+ (reset! run-id (task-history/current-run-id))
+ (task-history/with-task-history {:task "test-task"}
+ :done))
+ (let [run (t2/select-one :model/TaskRun :id @run-id)]
+ (is (= :started (:status run)) "status remains :started")
+ (is (nil? (:ended_at run)) "ended_at is nil"))
+ ;; Manual completion
+ (task-history/complete-task-run! @run-id)
+ (let [run (t2/select-one :model/TaskRun :id @run-id)]
+ (is (= :success (:status run)))
+ (is (some? (:ended_at run))))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Nesting prevention tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest with-task-run-prevents-nesting-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "nested with-task-run does not create a new run"
+ (let [outer-run-id (atom nil)
+ inner-run-id (atom nil)]
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 1}
+ (reset! outer-run-id (task-history/current-run-id))
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 2}
+ (reset! inner-run-id (task-history/current-run-id))))
+ (is (= @outer-run-id @inner-run-id) "inner run uses same run-id")
+ (is (= 1 (t2/count :model/TaskRun)) "only one run created")))))
+
+(deftest with-task-run-nil-info-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "with-task-run with nil run-info just executes body"
+ (task-history/with-task-run nil
+ (is (nil? (task-history/current-run-id)) "run-id remains nil"))
+ (is (zero? (t2/count :model/TaskRun)) "no run created"))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | complete-task-run! tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest complete-task-run-derives-status-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "complete-task-run! derives :success when all children succeed"
+ (let [run-id (task-run/create-task-run! {:run_type :sync
+ :entity_type :database
+ :entity_id 1})]
+ (with-redefs [task-run/current-run-id (constantly run-id)]
+ (task-history/with-task-history {:task "t1"} :ok)
+ (task-history/with-task-history {:task "t2"} :ok))
+ (task-history/complete-task-run! run-id)
+ (is (= :success (:status (t2/select-one :model/TaskRun :id run-id))))))
+
+ (testing "complete-task-run! derives :failed when any child failed"
+ (let [run-id (task-run/create-task-run! {:run_type :sync
+ :entity_type :database
+ :entity_id 1})]
+ (with-redefs [task-run/current-run-id (constantly run-id)]
+ (task-history/with-task-history {:task "t1"} :ok)
+ (try
+ (task-history/with-task-history {:task "t2"}
+ (throw (Exception. "fail")))
+ (catch Exception _)))
+ (task-history/complete-task-run! run-id)
+ (is (= :failed (:status (t2/select-one :model/TaskRun :id run-id))))))))
+
+(deftest complete-task-run-idempotent-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "complete-task-run! is idempotent - only updates if status is :started"
+ (let [run-id (task-run/create-task-run! {:run_type :sync
+ :entity_type :database
+ :entity_id 1})]
+ (with-redefs [task-run/current-run-id (constantly run-id)]
+ (task-history/with-task-history {:task "t1"} :ok))
+ ;; First completion
+ (task-history/complete-task-run! run-id)
+ (let [first-ended-at (:ended_at (t2/select-one :model/TaskRun :id run-id))]
+ (is (= :success (:status (t2/select-one :model/TaskRun :id run-id))))
+ ;; Add a failing task and try to complete again
+ (with-redefs [task-run/current-run-id (constantly run-id)]
+ (try
+ (task-history/with-task-history {:task "t2"}
+ (throw (Exception. "fail")))
+ (catch Exception _)))
+ ;; Second completion should be no-op
+ (task-history/complete-task-run! run-id)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ (is (= :success (:status run)) "status unchanged")
+ (is (= first-ended-at (:ended_at run)) "ended_at unchanged")))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | run-id propagation (async) tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest run-id-meta-propagation-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "with-run-id-meta and with-restored-run-id propagate run-id via metadata"
+ (let [captured-run-id (atom nil)]
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 1}
+ (let [data (task-history/with-run-id-meta {:some "data"})
+ original-run-id (task-history/current-run-id)]
+ ;; Simulate async - outside the with-task-run
+ (is (some? original-run-id) "run-id is set")
+ ;; Restore from metadata in a new context
+ (task-history/with-restored-run-id data
+ (reset! captured-run-id (task-history/current-run-id)))
+ (is (= original-run-id @captured-run-id) "run-id restored from metadata")))))))
+
+(deftest run-id-meta-nil-propagation-test
+ (testing "with-restored-run-id handles nil metadata gracefully"
+ (let [data {}]
+ (task-history/with-restored-run-id data
+ (is (nil? (task-history/current-run-id)) "run-id is nil when not in metadata")))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | task_history.run_id integration |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest task-history-gets-run-id-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "task history created within with-task-run gets run_id populated"
+ (let [task-name (mt/random-name)]
+ (task-history/with-task-run {:run_type :sync
+ :entity_type :database
+ :entity_id 1}
+ (task-history/with-task-history {:task task-name}
+ :done))
+ (let [th (t2/select-one :model/TaskHistory :task task-name)]
+ (is (some? (:run_id th)) "run_id is set"))))
+
+ (testing "task history created outside with-task-run has nil run_id"
+ (let [task-name (mt/random-name)]
+ (task-history/with-task-history {:task task-name}
+ :done)
+ (let [th (t2/select-one :model/TaskHistory :task task-name)]
+ (is (nil? (:run_id th)) "run_id is nil"))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Sync integration tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest sync-creates-task-run-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "sync-operation creates a task run with type :sync"
+ (let [mock-db (mi/instance :model/Database {:name "test" :id 999 :engine :h2})
+ step-name (mt/random-name)
+ sync-steps [(sync-util/create-sync-step step-name (fn [_] {:done true}))]]
+ (sync-util/sync-operation :sync-metadata mock-db "Test sync"
+ (sync-util/run-sync-operation "test-sync" mock-db sync-steps))
+ (let [run (t2/select-one :model/TaskRun :entity_type :database :entity_id 999)]
+ (is (some? run) "task run was created")
+ (is (= :sync (:run_type run)))
+ (is (= :database (:entity_type run)))
+ (is (= 999 (:entity_id run)))
+ (is (= :success (:status run))))))))
+
+(deftest analyze-creates-fingerprint-run-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "analyze operation creates a task run with type :fingerprint"
+ (let [mock-db (mi/instance :model/Database {:name "test" :id 998 :engine :h2})
+ step-name (mt/random-name)
+ sync-steps [(sync-util/create-sync-step step-name (fn [_] {:done true}))]]
+ (sync-util/sync-operation :analyze mock-db "Test analyze"
+ (sync-util/run-sync-operation "test-analyze" mock-db sync-steps))
+ (let [run (t2/select-one :model/TaskRun :entity_type :database :entity_id 998)]
+ (is (some? run) "task run was created")
+ (is (= :fingerprint (:run_type run)))
+ (is (= :success (:status run))))))))
+
+(deftest nested-sync-operations-share-run-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "nested sync operations don't create multiple task runs"
+ (let [mock-db (mi/instance :model/Database {:name "test" :id 997 :engine :h2})
+ outer-step (mt/random-name)
+ inner-step (mt/random-name)
+ outer-steps [(sync-util/create-sync-step outer-step
+ (fn [_]
+ ;; Nested sync operation
+ (sync-util/sync-operation :sync mock-db "Inner sync"
+ (sync-util/run-sync-operation "inner"
+ mock-db
+ [(sync-util/create-sync-step inner-step (fn [_] {:inner true}))]))
+ {:outer true}))]]
+ (sync-util/sync-operation :sync mock-db "Outer sync"
+ (sync-util/run-sync-operation "outer" mock-db outer-steps))
+ (is (= 1 (t2/count :model/TaskRun :entity_id 997))
+ "only one task run created for nested operations")))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Pulse integration tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest pulse->task-run-info-test
+ (testing "pulse->task-run-info extracts correct info"
+ (testing "dashboard subscription"
+ (is (= {:run_type :subscription :entity_type :dashboard :entity_id 123}
+ (pulse.send/pulse->task-run-info {:dashboard_id 123}))))
+ (testing "legacy pulse with cards"
+ (is (= {:run_type :subscription :entity_type :card :entity_id 456}
+ (pulse.send/pulse->task-run-info {:cards [{:id 456} {:id 789}]}))))
+ (testing "pulse with neither returns nil"
+ (is (nil? (pulse.send/pulse->task-run-info {}))))))
+
+(deftest pulse-send-creates-task-run-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "sending a dashboard pulse creates a task run with type :subscription"
+ (notification.tu/with-notification-testing-setup!
+ (notification.tu/with-channel-fixtures [:channel/email]
+ (mt/with-temp [:model/Dashboard {dash-id :id} {:name "Test Dashboard"}
+ :model/Card {card-id :id} {:name "Test Card"}
+ :model/Pulse {pulse-id :id} {:name "Test Pulse"
+ :dashboard_id dash-id}
+ :model/PulseCard _ {:pulse_id pulse-id
+ :card_id card-id
+ :position 0}
+ :model/PulseChannel {pc-id :id} {:pulse_id pulse-id
+ :channel_type "email"}
+ :model/PulseChannelRecipient _ {:user_id (pulse.tu/rasta-id)
+ :pulse_channel_id pc-id}]
+ (notification.tu/with-javascript-visualization-stub
+ (pulse.tu/with-captured-channel-send-messages!
+ (pulse.send/send-pulse! (t2/select-one :model/Pulse pulse-id))))
+ (let [run (t2/select-one :model/TaskRun :entity_type :dashboard :entity_id dash-id)]
+ (is (some? run) "task run was created")
+ (is (= :subscription (:run_type run)))
+ (is (= :dashboard (:entity_type run)))
+ (is (#{:success :failed} (:status run)) "task run completed"))))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Notification integration tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest notification-send-creates-task-run-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "sending a card notification creates a task run with type :alert"
+ (notification.tu/with-notification-testing-setup!
+ (notification.tu/with-channel-fixtures [:channel/email]
+ (notification.tu/with-card-notification
+ [notification {:handlers [{:channel_type :channel/email
+ :recipients [{:type :notification-recipient/user
+ :user_id (mt/user->id :crowberto)}]}]}]
+ (let [card-id (-> notification :payload :card_id)]
+ (notification.tu/with-javascript-visualization-stub
+ (notification.tu/with-mock-inbox-email!
+ (notification.send/send-notification! notification :notification/sync? true)))
+ (let [run (t2/select-one :model/TaskRun :entity_type :card :entity_id card-id)]
+ (is (some? run) "task run was created")
+ (is (= :alert (:run_type run)))
+ (is (= :card (:entity_type run)))
+ (is (#{:success :failed} (:status run)) "task run completed")))))))))
+
+(deftest notification->task-run-info-test
+ (testing "notification->task-run-info extracts correct info"
+ (testing "card notification (alert)"
+ (is (= {:run_type :alert :entity_type :card :entity_id 123}
+ (notification.send/notification->task-run-info
+ {:payload_type :notification/card
+ :payload {:card_id 123}}))))
+ (testing "card notification with nil card_id returns nil"
+ (is (nil? (notification.send/notification->task-run-info
+ {:payload_type :notification/card
+ :payload {:card_id nil}}))))
+ (testing "dashboard notification (subscription)"
+ (is (= {:run_type :subscription :entity_type :dashboard :entity_id 456}
+ (notification.send/notification->task-run-info
+ {:payload_type :notification/dashboard
+ :payload {:dashboard_id 456}}))))
+ (testing "dashboard notification with nil dashboard_id returns nil"
+ (is (nil? (notification.send/notification->task-run-info
+ {:payload_type :notification/dashboard
+ :payload {:dashboard_id nil}}))))
+ (testing "system-event notification returns nil"
+ (is (nil? (notification.send/notification->task-run-info
+ {:payload_type :notification/system-event
+ :payload {}}))))
+ (testing "testing notification returns nil"
+ (is (nil? (notification.send/notification->task-run-info
+ {:payload_type :notification/testing
+ :payload {}}))))))
diff --git a/test/metabase/task_history/task/task_run_heartbeat_test.clj b/test/metabase/task_history/task/task_run_heartbeat_test.clj
new file mode 100644
index 00000000000..c0c1ae1e625
--- /dev/null
+++ b/test/metabase/task_history/task/task_run_heartbeat_test.clj
@@ -0,0 +1,255 @@
+(ns metabase.task-history.task.task-run-heartbeat-test
+ "Tests for task run heartbeat and orphan cleanup."
+ (:require
+ [clojure.test :refer :all]
+ [java-time.api :as t]
+ [metabase.config.core :as config]
+ [metabase.task-history.task.task-run-heartbeat :as heartbeat]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(set! *warn-on-reflection* true)
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | send-heartbeat! tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest send-heartbeat-updates-own-runs-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "send-heartbeat! updates updated_at for runs belonging to current process"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at old-time
+ :updated_at old-time
+ :process_uuid config/local-process-uuid}]
+ (heartbeat/send-heartbeat!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ (is (t/after? (:updated_at run) old-time) "updated_at was bumped")))))))
+
+(deftest send-heartbeat-ignores-other-processes-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "send-heartbeat! does NOT update runs belonging to other processes"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))
+ other-uuid "other-process-uuid"]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at old-time
+ :updated_at old-time
+ :process_uuid other-uuid}]
+ (heartbeat/send-heartbeat!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ ;; Compare truncated to millis due to DB precision differences
+ (is (= (t/truncate-to old-time :millis)
+ (t/truncate-to (:updated_at run) :millis))
+ "updated_at unchanged for other process")))))))
+
+(deftest send-heartbeat-ignores-completed-runs-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "send-heartbeat! does NOT update runs that are already completed"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :success
+ :started_at old-time
+ :updated_at old-time
+ :ended_at old-time
+ :process_uuid config/local-process-uuid}]
+ (heartbeat/send-heartbeat!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ ;; Compare truncated to millis due to DB precision differences
+ (is (= (t/truncate-to old-time :millis)
+ (t/truncate-to (:updated_at run) :millis))
+ "updated_at unchanged for completed run")))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | mark-orphaned-runs! tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest mark-orphaned-runs-marks-old-runs-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "mark-orphaned-runs! marks runs with old updated_at as :abandoned"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at old-time
+ :updated_at old-time
+ :process_uuid "dead-process"}]
+ (heartbeat/mark-orphaned-runs!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ (is (= :abandoned (:status run)) "status changed to :abandoned")
+ (is (some? (:ended_at run)) "ended_at was set")))))))
+
+(deftest mark-orphaned-runs-ignores-recent-runs-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "mark-orphaned-runs! does NOT mark runs with recent updated_at"
+ (let [recent-time (t/minus (t/offset-date-time) (t/minutes 30))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at recent-time
+ :updated_at recent-time
+ :process_uuid "some-process"}]
+ (heartbeat/mark-orphaned-runs!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ (is (= :started (:status run)) "status unchanged")
+ (is (nil? (:ended_at run)) "ended_at still nil")))))))
+
+(deftest mark-orphaned-runs-ignores-completed-runs-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "mark-orphaned-runs! does NOT mark already-completed runs"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :success
+ :started_at old-time
+ :updated_at old-time
+ :ended_at old-time
+ :process_uuid "dead-process"}]
+ (heartbeat/mark-orphaned-runs!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ (is (= :success (:status run)) "status unchanged")))))))
+
+(deftest mark-orphaned-runs-marks-long-running-runs-test
+ (mt/with-model-cleanup [:model/TaskRun]
+ (testing "mark-orphaned-runs! marks runs older than 24 hours even with recent heartbeat"
+ (let [very-old-time (t/minus (t/offset-date-time) (t/hours 30))
+ recent-time (t/offset-date-time)]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at very-old-time ; started 30 hours ago
+ :updated_at recent-time ; but has recent heartbeat
+ :process_uuid config/local-process-uuid}]
+ (heartbeat/mark-orphaned-runs!)
+ (let [run (t2/select-one :model/TaskRun :id run-id)]
+ (is (= :abandoned (:status run)) "status changed to :abandoned for long-running run")
+ (is (some? (:ended_at run)) "ended_at was set")))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | mark-orphaned-tasks! tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest mark-orphaned-tasks-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "mark-orphaned-tasks! marks started tasks belonging to orphaned runs"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :abandoned ; already orphaned
+ :started_at old-time
+ :updated_at old-time
+ :ended_at old-time
+ :process_uuid "dead-process"}
+ :model/TaskHistory {task-id :id} {:task "stuck-task"
+ :run_id run-id
+ :status :started
+ :started_at old-time}]
+ (heartbeat/mark-orphaned-tasks! #{run-id})
+ (let [task (t2/select-one :model/TaskHistory :id task-id)]
+ (is (= :unknown (:status task)) "task status changed to :unknown")
+ (is (some? (:ended_at task)) "task ended_at was set")))))))
+
+(deftest mark-orphaned-tasks-ignores-completed-tasks-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "mark-orphaned-tasks! does NOT mark already-completed tasks"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :abandoned ; orphaned
+ :started_at old-time
+ :updated_at old-time
+ :ended_at old-time
+ :process_uuid "dead-process"}
+ :model/TaskHistory {task-id :id} {:task "completed-task"
+ :run_id run-id
+ :status :success
+ :started_at old-time
+ :ended_at old-time
+ :duration 100}]
+ (heartbeat/mark-orphaned-tasks! #{run-id})
+ (let [task (t2/select-one :model/TaskHistory :id task-id)]
+ (is (= :success (:status task)) "task status unchanged")))))))
+
+(deftest mark-orphaned-tasks-ignores-other-runs-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "mark-orphaned-tasks! does NOT mark tasks belonging to runs not in the provided set"
+ (let [recent-time (t/offset-date-time)]
+ (mt/with-temp [:model/TaskRun {run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at recent-time
+ :updated_at recent-time
+ :process_uuid config/local-process-uuid}
+ :model/TaskHistory {task-id :id} {:task "running-task"
+ :run_id run-id
+ :status :started
+ :started_at recent-time}]
+ ;; Pass empty set - no runs should be affected
+ (heartbeat/mark-orphaned-tasks! #{})
+ (let [task (t2/select-one :model/TaskHistory :id task-id)]
+ (is (= :started (:status task)) "task status unchanged")))))))
+
+;;; +----------------------------------------------------------------------------------------------------------------+
+;;; | Integration tests |
+;;; +----------------------------------------------------------------------------------------------------------------+
+
+(deftest full-heartbeat-cycle-test
+ (mt/with-model-cleanup [:model/TaskRun :model/TaskHistory]
+ (testing "full heartbeat cycle: heartbeat, orphan detection, task cleanup"
+ (let [old-time (t/minus (t/offset-date-time) (t/hours 3))]
+ ;; Create runs: one from current process (should get heartbeat), one from dead process (should be orphaned)
+ (mt/with-temp [:model/TaskRun {live-run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 1
+ :status :started
+ :started_at old-time
+ :updated_at old-time
+ :process_uuid config/local-process-uuid}
+ :model/TaskRun {dead-run-id :id} {:run_type :sync
+ :entity_type :database
+ :entity_id 2
+ :status :started
+ :started_at old-time
+ :updated_at old-time
+ :process_uuid "dead-process"}
+ :model/TaskHistory {live-task-id :id} {:task "live-task"
+ :run_id live-run-id
+ :status :started
+ :started_at old-time}
+ :model/TaskHistory {dead-task-id :id} {:task "dead-task"
+ :run_id dead-run-id
+ :status :started
+ :started_at old-time}]
+ ;; Run heartbeat cycle
+ (heartbeat/send-heartbeat!)
+ (let [orphaned-run-ids (heartbeat/mark-orphaned-runs!)]
+ (heartbeat/mark-orphaned-tasks! orphaned-run-ids))
+
+ ;; Live run should have updated heartbeat, dead run should be orphaned
+ (let [live-run (t2/select-one :model/TaskRun :id live-run-id)
+ dead-run (t2/select-one :model/TaskRun :id dead-run-id)]
+ (is (= :started (:status live-run)) "live run still started")
+ (is (t/after? (:updated_at live-run) old-time) "live run got heartbeat")
+ (is (= :abandoned (:status dead-run)) "dead run marked abandoned")
+ (is (some? (:ended_at dead-run)) "dead run has ended_at"))
+
+ ;; Live task should be unchanged, dead task should be marked unknown
+ (let [live-task (t2/select-one :model/TaskHistory :id live-task-id)
+ dead-task (t2/select-one :model/TaskHistory :id dead-task-id)]
+ (is (= :started (:status live-task)) "live task still started")
+ (is (= :unknown (:status dead-task)) "dead task marked unknown")
+ (is (some? (:ended_at dead-task)) "dead task has ended_at")))))))