From de043890c82465f533638830deefade5288a8651 Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Sat, 21 Nov 2026 20:53:16 -0600 Subject: [PATCH 1/2] fix: list the tasks on a server schedule Extract refreshes were the only task type queryable per schedule. Subscriptions and flow tasks are now filtered onto that schedule as well. Cloud subscriptions have no server schedule id, so they stay out of the list. --- tableauserverclient/models/task_item.py | 3 + .../server/endpoint/schedules_endpoint.py | 81 ++++++++++++++++++- test/test_schedule.py | 75 +++++++++++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) diff --git a/tableauserverclient/models/task_item.py b/tableauserverclient/models/task_item.py index b343f3c22..bdd7cb390 100644 --- a/tableauserverclient/models/task_item.py +++ b/tableauserverclient/models/task_item.py @@ -92,6 +92,7 @@ def _parse_element(cls, element, ns): last_run_at = None workbook_element = element.find(".//t:workbook", namespaces=ns) datasource_element = element.find(".//t:datasource", namespaces=ns) + flow_element = element.find(".//t:flow", namespaces=ns) last_run_at_element = element.find(".//t:lastRunAt", namespaces=ns) schedule_item_list = ScheduleItem.from_element(element, ns) @@ -105,6 +106,8 @@ def _parse_element(cls, element, ns): if datasource_element is not None: datasource_id = datasource_element.get("id", None) target = Target(datasource_id, "datasource") + if flow_element is not None: + target = Target(flow_element.get("id", None), "flow") if last_run_at_element is not None: last_run_at = parse_datetime(last_run_at_element.text) diff --git a/tableauserverclient/server/endpoint/schedules_endpoint.py b/tableauserverclient/server/endpoint/schedules_endpoint.py index 321d0120b..c2fe34604 100644 --- a/tableauserverclient/server/endpoint/schedules_endpoint.py +++ b/tableauserverclient/server/endpoint/schedules_endpoint.py @@ -6,14 +6,17 @@ from typing import TYPE_CHECKING, Any, Callable, Literal, overload from .endpoint import Endpoint, api, parameter_added_in -from .exceptions import MissingRequiredFieldError +from .exceptions import MissingRequiredFieldError, ServerResponseError from tableauserverclient.server import RequestFactory -from tableauserverclient.models import PaginationItem, ScheduleItem, TaskItem, ExtractItem +from tableauserverclient.server.pager import Pager +from tableauserverclient.server.request_options import RequestOptions +from tableauserverclient.models import ExtractItem, PaginationItem, ScheduleItem, SubscriptionItem, TaskItem from tableauserverclient.models.schedule_item import parse_batch_schedule_state from tableauserverclient.helpers.logging import logger AddResponse = namedtuple("AddResponse", ("result", "error", "warnings", "task_created")) +ScheduleTasks = namedtuple("ScheduleTasks", ("extracts", "subscriptions", "flow_tasks")) OK = AddResponse(result=True, error=None, warnings=None, task_created=None) if TYPE_CHECKING: @@ -282,6 +285,80 @@ def get_extract_refresh_tasks( return extract_items, pagination_item + def _collect_pages(self, fetch: Callable[..., tuple[list, PaginationItem]]) -> list: + options = RequestOptions(pagesize=1000) + collected: list = [] + while True: + batch, pagination = fetch(options) + collected.extend(batch) + if ( + not batch + or pagination.total_available is None + or pagination.page_number is None + or pagination.page_size is None + or pagination.page_size < 1 + or pagination.page_number < 1 + or pagination.page_size * pagination.page_number >= pagination.total_available + ): + return collected + options.pagenumber = pagination.page_number + 1 + options.pagesize = pagination.page_size + + @api(version="3.2") + def get_subscriptions_by_schedule(self, schedule_id: str) -> list[SubscriptionItem]: + """ + Return the Tableau Server subscriptions on this schedule. + + The REST API has no ``/schedules/{id}/subscriptions`` method. Subscriptions + are listed for the site, and each Tableau Server subscription carries the + schedule id. Tableau Cloud subscriptions use a frequency instead of a + server schedule id, so they are not included. + """ + if not schedule_id: + raise ValueError("Schedule ID undefined") + matches = [] + for subscription in Pager(self.parent_srv.subscriptions): + if subscription.schedule_id == schedule_id: + matches.append(subscription) + return matches + + @api(version="3.3") + def get_flow_tasks_by_schedule(self, schedule_id: str) -> list[TaskItem]: + """ + Return flow run tasks on this server schedule. + + Flow tasks are listed for the site and kept when their schedule id + matches. A server that does not have the flow-task method yields an + empty list. + """ + if not schedule_id: + raise ValueError("Schedule ID undefined") + try: + tasks = list(Pager(self.parent_srv.tasks.get, task_type=TaskItem.Type.RunFlow)) + except ServerResponseError as error: + if str(error.code).startswith("404"): + logger.info("Flow tasks are not available on this server: %s", error.code) + return [] + raise + return [task for task in tasks if task.schedule_id == schedule_id] + + @api(version="2.3") + def get_tasks(self, schedule_id: str): + """ + List the extract refreshes, subscriptions, and flow tasks on a schedule. + + Extract refreshes come from ``GET .../schedules/{id}/extracts``. + Subscriptions and flow tasks are filtered from the site lists, because + those task types have no schedule-scoped query. Tableau Cloud + subscriptions, which have no server schedule id, are left out. + """ + if not schedule_id: + raise ValueError("Schedule ID undefined") + extracts = self._collect_pages(lambda options: self.get_extract_refresh_tasks(schedule_id, options)) + subscriptions = self.get_subscriptions_by_schedule(schedule_id) + flow_tasks = self.get_flow_tasks_by_schedule(schedule_id) + return ScheduleTasks(extracts, subscriptions, flow_tasks) + @overload def batch_update_state( self, diff --git a/test/test_schedule.py b/test/test_schedule.py index 823a87607..5f1b09452 100644 --- a/test/test_schedule.py +++ b/test/test_schedule.py @@ -492,3 +492,78 @@ def test_batch_update_state_all(server: TSC.Server) -> None: history = m.request_history[0] assert history.text == "" + + +SUBSCRIPTIONS_XML = """ + + + + + + + + + + + + + + + +""" + +FLOW_TASKS_XML = """ + + + + + + + + + + + + + + + + + +""" + +FLOW_MISSING_XML = """ + + + Resource Not Found + Method not found + + +""" + + +def test_get_tasks_on_schedule(server: TSC.Server) -> None: + server.version = "3.10" + schedule_id = "sched-1" + with requests_mock.mock() as m: + m.get(f"{server.schedules.siteurl}/{schedule_id}/extracts", text=GET_EXTRACT_TASKS_XML.read_text()) + m.get(f"{server.baseurl}/sites/{server.site_id}/subscriptions", text=SUBSCRIPTIONS_XML) + m.get(f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", text=FLOW_TASKS_XML) + tasks = server.schedules.get_tasks(schedule_id) + + assert [item.id for item in tasks.extracts] == ["task1", "task2"] + assert [item.id for item in tasks.subscriptions] == ["sub-on-schedule"] + assert [item.id for item in tasks.flow_tasks] == ["flow-task-1"] + assert tasks.flow_tasks[0].target.id == "flow-1" + assert tasks.flow_tasks[0].target.type == "flow" + + +def test_flow_tasks_missing_on_older_server(server: TSC.Server) -> None: + server.version = "3.10" + with requests_mock.mock() as m: + m.get( + f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", + status_code=404, + text=FLOW_MISSING_XML, + ) + assert server.schedules.get_flow_tasks_by_schedule("sched-1") == [] From 6c64b895f19c5c5e3962237146a90d531151251b Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Tue, 24 Nov 2026 18:35:55 -0600 Subject: [PATCH 2/2] fix: ask only for the schedule tasks the server version has get_tasks was gated at 2.3 and then called subscription and flow methods that reject an older server. Those parts are now skipped, and the flow list returns empty for a missing method (404001) or a server below the tasks endpoint without hiding any other 404. Extract pages go through Pager, and the sample responses live under test/assets. --- .../server/endpoint/schedules_endpoint.py | 97 +++++++++------ test/assets/schedule_flow_tasks.xml | 18 +++ test/assets/schedule_flow_tasks_missing.xml | 7 ++ .../schedule_flow_tasks_site_missing.xml | 7 ++ test/assets/schedule_subscriptions.xml | 16 +++ test/test_schedule.py | 110 ++++++++++-------- 6 files changed, 169 insertions(+), 86 deletions(-) create mode 100644 test/assets/schedule_flow_tasks.xml create mode 100644 test/assets/schedule_flow_tasks_missing.xml create mode 100644 test/assets/schedule_flow_tasks_site_missing.xml create mode 100644 test/assets/schedule_subscriptions.xml diff --git a/tableauserverclient/server/endpoint/schedules_endpoint.py b/tableauserverclient/server/endpoint/schedules_endpoint.py index c2fe34604..ee44d0c76 100644 --- a/tableauserverclient/server/endpoint/schedules_endpoint.py +++ b/tableauserverclient/server/endpoint/schedules_endpoint.py @@ -8,8 +8,8 @@ from .endpoint import Endpoint, api, parameter_added_in from .exceptions import MissingRequiredFieldError, ServerResponseError from tableauserverclient.server import RequestFactory +from tableauserverclient.server.exceptions import EndpointUnavailableError from tableauserverclient.server.pager import Pager -from tableauserverclient.server.request_options import RequestOptions from tableauserverclient.models import ExtractItem, PaginationItem, ScheduleItem, SubscriptionItem, TaskItem from tableauserverclient.models.schedule_item import parse_batch_schedule_state @@ -285,34 +285,26 @@ def get_extract_refresh_tasks( return extract_items, pagination_item - def _collect_pages(self, fetch: Callable[..., tuple[list, PaginationItem]]) -> list: - options = RequestOptions(pagesize=1000) - collected: list = [] - while True: - batch, pagination = fetch(options) - collected.extend(batch) - if ( - not batch - or pagination.total_available is None - or pagination.page_number is None - or pagination.page_size is None - or pagination.page_size < 1 - or pagination.page_number < 1 - or pagination.page_size * pagination.page_number >= pagination.total_available - ): - return collected - options.pagenumber = pagination.page_number + 1 - options.pagesize = pagination.page_size - @api(version="3.2") def get_subscriptions_by_schedule(self, schedule_id: str) -> list[SubscriptionItem]: """ Return the Tableau Server subscriptions on this schedule. - The REST API has no ``/schedules/{id}/subscriptions`` method. Subscriptions - are listed for the site, and each Tableau Server subscription carries the - schedule id. Tableau Cloud subscriptions use a frequency instead of a + The REST API has no ``/schedules/{id}/subscriptions`` method. This + lists the site's subscriptions and keeps those whose schedule id + matches. Tableau Cloud subscriptions use a frequency instead of a server schedule id, so they are not included. + + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_subscriptions.htm#list_subscriptions + + Parameters + ---------- + schedule_id : str + The schedule to match. + + Returns + ------- + list[SubscriptionItem] """ if not schedule_id: raise ValueError("Schedule ID undefined") @@ -322,41 +314,76 @@ def get_subscriptions_by_schedule(self, schedule_id: str) -> list[SubscriptionIt matches.append(subscription) return matches - @api(version="3.3") def get_flow_tasks_by_schedule(self, schedule_id: str) -> list[TaskItem]: """ Return flow run tasks on this server schedule. Flow tasks are listed for the site and kept when their schedule id - matches. A server that does not have the flow-task method yields an - empty list. + matches. There is no version gate: ``tasks.get`` raises + ``EndpointUnavailableError`` below REST API 2.6, and a server that + does not implement the method answers 404001. Both return an empty + list. Any other error is raised, including a 404 for a missing site. + + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_flow.htm#get_flow_run_tasks + + Parameters + ---------- + schedule_id : str + The schedule to match. + + Returns + ------- + list[TaskItem] """ if not schedule_id: raise ValueError("Schedule ID undefined") try: tasks = list(Pager(self.parent_srv.tasks.get, task_type=TaskItem.Type.RunFlow)) + except EndpointUnavailableError: + logger.info("Flow tasks are not available before REST API 2.6") + return [] except ServerResponseError as error: - if str(error.code).startswith("404"): + if error.code == "404001": logger.info("Flow tasks are not available on this server: %s", error.code) return [] raise return [task for task in tasks if task.schedule_id == schedule_id] @api(version="2.3") - def get_tasks(self, schedule_id: str): + def get_tasks(self, schedule_id: str) -> ScheduleTasks: """ List the extract refreshes, subscriptions, and flow tasks on a schedule. - Extract refreshes come from ``GET .../schedules/{id}/extracts``. - Subscriptions and flow tasks are filtered from the site lists, because - those task types have no schedule-scoped query. Tableau Cloud - subscriptions, which have no server schedule id, are left out. + Extract refreshes come from the schedule. Subscriptions are included + from REST API 3.2 and flow tasks from 3.3; on an older server those + parts are empty and are not requested. Tableau Cloud subscriptions, + which have no server schedule id, are left out. + + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_jobs_tasks_and_schedules.htm#list_extract_refresh_tasks + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_subscriptions.htm#list_subscriptions + REST API: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref_flow.htm#get_flow_run_tasks + + Parameters + ---------- + schedule_id : str + The schedule to list. + + Returns + ------- + ScheduleTasks + ``extracts``, ``subscriptions``, and ``flow_tasks``. """ if not schedule_id: raise ValueError("Schedule ID undefined") - extracts = self._collect_pages(lambda options: self.get_extract_refresh_tasks(schedule_id, options)) - subscriptions = self.get_subscriptions_by_schedule(schedule_id) - flow_tasks = self.get_flow_tasks_by_schedule(schedule_id) + extracts: list[ExtractItem] = list( + Pager(lambda options: self.get_extract_refresh_tasks(schedule_id, options)) # type: ignore[arg-type] + ) + subscriptions = ( + self.get_subscriptions_by_schedule(schedule_id) if self.parent_srv.check_at_least_version("3.2") else [] + ) + flow_tasks = ( + self.get_flow_tasks_by_schedule(schedule_id) if self.parent_srv.check_at_least_version("3.3") else [] + ) return ScheduleTasks(extracts, subscriptions, flow_tasks) @overload diff --git a/test/assets/schedule_flow_tasks.xml b/test/assets/schedule_flow_tasks.xml new file mode 100644 index 000000000..9c00ac454 --- /dev/null +++ b/test/assets/schedule_flow_tasks.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/test/assets/schedule_flow_tasks_missing.xml b/test/assets/schedule_flow_tasks_missing.xml new file mode 100644 index 000000000..f0bf9558f --- /dev/null +++ b/test/assets/schedule_flow_tasks_missing.xml @@ -0,0 +1,7 @@ + + + + Resource Not Found + Method not found + + diff --git a/test/assets/schedule_flow_tasks_site_missing.xml b/test/assets/schedule_flow_tasks_site_missing.xml new file mode 100644 index 000000000..04e4a9b38 --- /dev/null +++ b/test/assets/schedule_flow_tasks_site_missing.xml @@ -0,0 +1,7 @@ + + + + Site not found + The site ID in the URI doesn't correspond to an existing site. + + diff --git a/test/assets/schedule_subscriptions.xml b/test/assets/schedule_subscriptions.xml new file mode 100644 index 000000000..a5ed9b70f --- /dev/null +++ b/test/assets/schedule_subscriptions.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/test/test_schedule.py b/test/test_schedule.py index 5f1b09452..3403b599a 100644 --- a/test/test_schedule.py +++ b/test/test_schedule.py @@ -27,6 +27,10 @@ ADD_DATASOURCE_TO_SCHEDULE = TEST_ASSET_DIR / "schedule_add_datasource.xml" ADD_FLOW_TO_SCHEDULE = TEST_ASSET_DIR / "schedule_add_flow.xml" GET_EXTRACT_TASKS_XML = TEST_ASSET_DIR / "schedule_get_extract_refresh_tasks.xml" +SUBSCRIPTIONS_XML = TEST_ASSET_DIR / "schedule_subscriptions.xml" +FLOW_TASKS_XML = TEST_ASSET_DIR / "schedule_flow_tasks.xml" +FLOW_MISSING_XML = TEST_ASSET_DIR / "schedule_flow_tasks_missing.xml" +FLOW_OTHER_404_XML = TEST_ASSET_DIR / "schedule_flow_tasks_site_missing.xml" BATCH_UPDATE_STATE = TEST_ASSET_DIR / "schedule_batch_update_state.xml" WORKBOOK_GET_BY_ID_XML = TEST_ASSET_DIR / "workbook_get_by_id.xml" @@ -494,61 +498,13 @@ def test_batch_update_state_all(server: TSC.Server) -> None: assert history.text == "" -SUBSCRIPTIONS_XML = """ - - - - - - - - - - - - - - - -""" - -FLOW_TASKS_XML = """ - - - - - - - - - - - - - - - - - -""" - -FLOW_MISSING_XML = """ - - - Resource Not Found - Method not found - - -""" - - def test_get_tasks_on_schedule(server: TSC.Server) -> None: server.version = "3.10" schedule_id = "sched-1" with requests_mock.mock() as m: m.get(f"{server.schedules.siteurl}/{schedule_id}/extracts", text=GET_EXTRACT_TASKS_XML.read_text()) - m.get(f"{server.baseurl}/sites/{server.site_id}/subscriptions", text=SUBSCRIPTIONS_XML) - m.get(f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", text=FLOW_TASKS_XML) + m.get(f"{server.baseurl}/sites/{server.site_id}/subscriptions", text=SUBSCRIPTIONS_XML.read_text()) + m.get(f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", text=FLOW_TASKS_XML.read_text()) tasks = server.schedules.get_tasks(schedule_id) assert [item.id for item in tasks.extracts] == ["task1", "task2"] @@ -558,12 +514,64 @@ def test_get_tasks_on_schedule(server: TSC.Server) -> None: assert tasks.flow_tasks[0].target.type == "flow" +def test_get_tasks_skips_methods_the_server_version_does_not_have(server: TSC.Server) -> None: + schedule_id = "sched-1" + extracts = f"{server.schedules.siteurl}/{schedule_id}/extracts" + subscriptions = f"{server.baseurl}/sites/{server.site_id}/subscriptions" + flows = f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow" + + server.version = "2.4" + with requests_mock.mock() as m: + m.get(extracts, text=GET_EXTRACT_TASKS_XML.read_text()) + m.get(subscriptions, text=SUBSCRIPTIONS_XML.read_text()) + m.get(flows, text=FLOW_TASKS_XML.read_text()) + tasks = server.schedules.get_tasks(schedule_id) + assert [item.id for item in tasks.extracts] == ["task1", "task2"] + assert tasks.subscriptions == [] + assert tasks.flow_tasks == [] + assert [call.path for call in m.request_history] == [ + f"/api/2.4/sites/{server.site_id}/schedules/{schedule_id}/extracts" + ] + + server.version = "3.2" + extracts = f"{server.schedules.siteurl}/{schedule_id}/extracts" + subscriptions = f"{server.baseurl}/sites/{server.site_id}/subscriptions" + flows = f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow" + with requests_mock.mock() as m: + m.get(extracts, text=GET_EXTRACT_TASKS_XML.read_text()) + m.get(subscriptions, text=SUBSCRIPTIONS_XML.read_text()) + m.get(flows, text=FLOW_TASKS_XML.read_text()) + tasks = server.schedules.get_tasks(schedule_id) + assert [item.id for item in tasks.subscriptions] == ["sub-on-schedule"] + assert tasks.flow_tasks == [] + assert flows not in [call.url for call in m.request_history] + + def test_flow_tasks_missing_on_older_server(server: TSC.Server) -> None: + server.version = "2.4" + with requests_mock.mock() as m: + m.get(f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", text=FLOW_TASKS_XML.read_text()) + assert server.schedules.get_flow_tasks_by_schedule("sched-1") == [] + assert m.request_history == [] + server.version = "3.10" with requests_mock.mock() as m: m.get( f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", status_code=404, - text=FLOW_MISSING_XML, + text=FLOW_MISSING_XML.read_text(), ) assert server.schedules.get_flow_tasks_by_schedule("sched-1") == [] + + +def test_flow_tasks_other_404_is_raised(server: TSC.Server) -> None: + server.version = "3.10" + with requests_mock.mock() as m: + m.get( + f"{server.baseurl}/sites/{server.site_id}/tasks/runFlow", + status_code=404, + text=FLOW_OTHER_404_XML.read_text(), + ) + with pytest.raises(TSC.ServerResponseError) as raised: + server.schedules.get_flow_tasks_by_schedule("sched-1") + assert raised.value.code == "404000"