mik-laj commented on a change in pull request #6596: [AIRFLOW-6004] Untangle Executors class to avoid cyclic imports. Depends on [AIRFLOW-6010] URL: https://github.com/apache/airflow/pull/6596#discussion_r350253533
########## File path: airflow/executors/all_executors.py ########## @@ -0,0 +1,89 @@ +# 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. +"""All executors.""" +from typing import Optional + +from airflow import AirflowException, LoggingMixin, conf +from airflow.executors.base_executor import BaseExecutor + + +class AllExecutors: + """ + Keeps constants for all the currently available executors. + """ + + LOCAL_EXECUTOR = "LocalExecutor" + SEQUENTIAL_EXECUTOR = "SequentialExecutor" + CELERY_EXECUTOR = "CeleryExecutor" + DASK_EXECUTOR = "DaskExecutor" + KUBERNETES_EXECUTOR = "KubernetesExecutor" + + _default_executor: Optional[BaseExecutor] = None + + @classmethod + def get_default_executor(cls) -> BaseExecutor: + """Creates a new instance of the configured executor if none exists and returns it""" + if cls._default_executor is not None: + return cls._default_executor + + executor_name = conf.get('core', 'EXECUTOR') + + cls._default_executor = AllExecutors._get_executor(executor_name) + + log = LoggingMixin().log + log.info("Using executor %s", executor_name) + + return cls._default_executor + + @staticmethod + def _get_executor(executor_name: str) -> BaseExecutor: + """ + Creates a new instance of the named executor. + In case the executor name is not know in airflow, + look for it in the plugins + """ + if executor_name == AllExecutors.LOCAL_EXECUTOR: + from airflow.executors.local_executor import LocalExecutor + return LocalExecutor() + elif executor_name == AllExecutors.SEQUENTIAL_EXECUTOR: + from airflow.executors.sequential_executor import SequentialExecutor + return SequentialExecutor() + elif executor_name == AllExecutors.CELERY_EXECUTOR: + from airflow.executors.celery_executor import CeleryExecutor + return CeleryExecutor() + elif executor_name == AllExecutors.DASK_EXECUTOR: + from airflow.executors.dask_executor import DaskExecutor + return DaskExecutor() + elif executor_name == AllExecutors.KUBERNETES_EXECUTOR: + from airflow.executors.kubernetes_executor import KubernetesExecutor + return KubernetesExecutor() + else: + + # Loading plugins + from airflow import plugins_manager + # noinspection PyProtectedMember + plugins_manager._integrate_plugins() # pylint: disable=protected-access + executor_path = executor_name.split('.') + if len(executor_path) != 2: Review comment: Maybe we should require a full path to the Executor? This is not configured by the end user, but by a person who is already familiar with Airflow, so we may require some more skill. Finally, we can enter the dictionary with the old name and full path for backward compatibility. In Airflow 2.0 we would like to delete plugins for operators and hooks, so this is a good time to introduce other improvements to the plugin mechanism as well. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
