scheduler_command.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Scheduler command."""
  18. from __future__ import annotations
  19. import logging
  20. from argparse import Namespace
  21. from contextlib import contextmanager
  22. from multiprocessing import Process
  23. from airflow import settings
  24. from airflow.cli.commands.daemon_utils import run_command_with_daemon_option
  25. from airflow.configuration import conf
  26. from airflow.executors.executor_loader import ExecutorLoader
  27. from airflow.jobs.job import Job, run_job
  28. from airflow.jobs.scheduler_job_runner import SchedulerJobRunner
  29. from airflow.utils import cli as cli_utils
  30. from airflow.utils.cli import process_subdir
  31. from airflow.utils.providers_configuration_loader import providers_configuration_loaded
  32. from airflow.utils.scheduler_health import serve_health_check
  33. log = logging.getLogger(__name__)
  34. def _run_scheduler_job(args) -> None:
  35. job_runner = SchedulerJobRunner(
  36. job=Job(), subdir=process_subdir(args.subdir), num_runs=args.num_runs, do_pickle=args.do_pickle
  37. )
  38. ExecutorLoader.validate_database_executor_compatibility(job_runner.job.executor.__class__)
  39. enable_health_check = conf.getboolean("scheduler", "ENABLE_HEALTH_CHECK")
  40. with _serve_logs(args.skip_serve_logs), _serve_health_check(enable_health_check):
  41. run_job(job=job_runner.job, execute_callable=job_runner._execute)
  42. @cli_utils.action_cli
  43. @providers_configuration_loaded
  44. def scheduler(args: Namespace):
  45. """Start Airflow Scheduler."""
  46. print(settings.HEADER)
  47. run_command_with_daemon_option(
  48. args=args,
  49. process_name="scheduler",
  50. callback=lambda: _run_scheduler_job(args),
  51. should_setup_logging=True,
  52. )
  53. @contextmanager
  54. def _serve_logs(skip_serve_logs: bool = False):
  55. """Start serve_logs sub-process."""
  56. from airflow.utils.serve_logs import serve_logs
  57. sub_proc = None
  58. executor_class, _ = ExecutorLoader.import_default_executor_cls()
  59. if executor_class.serve_logs:
  60. if skip_serve_logs is False:
  61. sub_proc = Process(target=serve_logs)
  62. sub_proc.start()
  63. try:
  64. yield
  65. finally:
  66. if sub_proc:
  67. sub_proc.terminate()
  68. @contextmanager
  69. def _serve_health_check(enable_health_check: bool = False):
  70. """Start serve_health_check sub-process."""
  71. sub_proc = None
  72. if enable_health_check:
  73. sub_proc = Process(target=serve_health_check)
  74. sub_proc.start()
  75. try:
  76. yield
  77. finally:
  78. if sub_proc:
  79. sub_proc.terminate()