villebro commented on code in PR #36368: URL: https://github.com/apache/superset/pull/36368#discussion_r2673030128
########## docs/developer_portal/async-tasks.md: ########## @@ -0,0 +1,460 @@ +--- +title: Async Task Framework +sidebar_label: Async Tasks +sidebar_position: 5 +--- + + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# Global Async Task Framework (GATF) + +The Global Async Task Framework provides a unified way to manage asynchronous tasks in Apache Superset. It handles task registration, execution, status tracking, cancellation, and deduplication. + +## Overview + +GATF uses the **ambient context pattern** where tasks access their execution context via `get_context()` instead of receiving it as a parameter. This results in clean, business-focused function signatures without framework boilerplate. + +### Key Features + +- **Clean Signatures**: Task functions contain only business args +- **Ambient Context**: Access context via `get_context()` - no parameter passing +- **Dual Execution**: Synchronous (for testing) and asynchronous (via Celery) +- **Optional Deduplication**: Use idempotency keys to prevent duplicate execution +- **Progressive Updates**: Update payload and check cancellation during execution +- **Type Safety**: Full type hints with ParamSpec support + +## Quick Start + +### Define a Task + +```python +import requests +from superset_core.api.types import async_task, get_context + +@async_task() +def fetch_data(api_url: str) -> None: + """ + Example task that fetches data from an external API. + + Features: + - Automatic cancellation check before execution + - Simple cleanup handler + - Cancellation checking during execution + """ + ctx = get_context() + + # Cleanup runs automatically on success, failure, or cancellation + @ctx.on_cleanup + def cleanup(): + logger.info("Data fetch completed") + + # No initial check needed - framework checks before execution! + # Fetch data with timeout (prevents hanging) + response = requests.get(api_url, timeout=60) + data = response.json() + + # Check before next operation + if ctx.is_cancelled(): + return + + # Process and cache the data + process_and_cache(data) +``` + +### Execute Tasks Asynchronously or Synchronously + +The `@async_task` decorator enables flexible execution modes: + +```python +# Asynchronous execution via Celery (for production workloads) +task = long_running_task.schedule() +# Task runs in background worker, returns immediately +print(task.status) # "pending" + +# Synchronous execution (for testing or when blocking is acceptable) +task = long_running_task() +# Task executes inline, blocks until complete +print(task.status) # "success" +``` + +**When to use each mode:** +- **Async (`.schedule()`)**: Production workloads, long-running operations, non-blocking execution +- **Sync (direct call)**: Unit testing, development, or lightweight operations + +## Core Concepts + +### Ambient Context + +Tasks access execution context via `get_context()`: + +```python +@async_task() +def my_task(business_arg: int) -> None: Review Comment: Task prioritization is heavily dependent on the capabilities of the executor, and the implementation details of those tend to vary a lot. The plan is to add support for prioritization, but it's still unclear how to do this in the cleanest possible manner. Task dependencies on the other hand will likely not be in scope for this framework, as that's a slightly separate topic. Note that you should be able to trigger tasks within tasks if needed (in this case it makes sense to call then synchronously to encapsulate them inside the same operation). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
