Skip to content

Commit

Permalink
Override pool for TaskInstance when pool is passed from cli. (apache#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tirkarthi authored Apr 30, 2022
1 parent 511d0ee commit 3970ea3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 3 additions & 2 deletions airflow/cli/commands/task_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def _get_ti(
exec_date_or_run_id: str,
map_index: int,
*,
pool: Optional[str] = None,
create_if_necessary: CreateIfNecessary = False,
session: Session = NEW_SESSION,
) -> Tuple[TaskInstance, bool]:
Expand Down Expand Up @@ -165,7 +166,7 @@ def _get_ti(
ti.dag_run = dag_run
else:
ti = ti_or_none
ti.refresh_from_task(task)
ti.refresh_from_task(task, pool_override=pool)
return ti, dr_created


Expand Down Expand Up @@ -361,7 +362,7 @@ def task_run(args, dag=None):
# Use DAG from parameter
pass
task = dag.get_task(task_id=args.task_id)
ti, _ = _get_ti(task, args.execution_date_or_run_id, args.map_index)
ti, _ = _get_ti(task, args.execution_date_or_run_id, args.map_index, pool=args.pool)
ti.init_run_context(raw=args.raw)

hostname = get_hostname()
Expand Down
21 changes: 19 additions & 2 deletions tests/cli/commands/test_task_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
from airflow.cli.commands import task_command
from airflow.configuration import conf
from airflow.exceptions import AirflowException, DagRunNotFound
from airflow.models import DagBag, DagRun, TaskInstance
from airflow.models import DagBag, DagRun, Pool, TaskInstance
from airflow.utils import timezone
from airflow.utils.dates import days_ago
from airflow.utils.session import create_session
from airflow.utils.state import State
from airflow.utils.types import DagRunType
from tests.test_utils.config import conf_vars
from tests.test_utils.db import clear_db_runs
from tests.test_utils.db import clear_db_pools, clear_db_runs

DEFAULT_DATE = days_ago(1)
ROOT_FOLDER = os.path.realpath(
Expand Down Expand Up @@ -512,6 +512,23 @@ def test_logging_with_run_task(self):
f"task_id={self.task_id}, execution_date=20170101T000000" in logs
)

@unittest.skipIf(not hasattr(os, 'fork'), "Forking not available")
def test_run_task_with_pool(self):
pool_name = 'test_pool_run'

clear_db_pools()
with create_session() as session:
pool = Pool(pool=pool_name, slots=1)
session.add(pool)
session.commit()

assert session.query(TaskInstance).filter_by(pool=pool_name).first() is None
task_command.task_run(self.parser.parse_args(self.task_args + ['--pool', pool_name]))
assert session.query(TaskInstance).filter_by(pool=pool_name).first() is not None

session.delete(pool)
session.commit()

# For this test memory spins out of control on Python 3.6. TODO(potiuk): FIXME")
@pytest.mark.quarantined
@mock.patch("airflow.task.task_runner.standard_task_runner.CAN_FORK", False)
Expand Down

0 comments on commit 3970ea3

Please sign in to comment.