triggerer_command.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """Triggerer command."""
  18. from __future__ import annotations
  19. from contextlib import contextmanager
  20. from functools import partial
  21. from multiprocessing import Process
  22. from typing import Generator
  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.jobs.job import Job, run_job
  27. from airflow.jobs.triggerer_job_runner import TriggererJobRunner
  28. from airflow.utils import cli as cli_utils
  29. from airflow.utils.providers_configuration_loader import providers_configuration_loaded
  30. from airflow.utils.serve_logs import serve_logs
  31. @contextmanager
  32. def _serve_logs(skip_serve_logs: bool = False) -> Generator[None, None, None]:
  33. """Start serve_logs sub-process."""
  34. sub_proc = None
  35. if skip_serve_logs is False:
  36. port = conf.getint("logging", "trigger_log_server_port", fallback=8794)
  37. sub_proc = Process(target=partial(serve_logs, port=port))
  38. sub_proc.start()
  39. try:
  40. yield
  41. finally:
  42. if sub_proc:
  43. sub_proc.terminate()
  44. def triggerer_run(skip_serve_logs: bool, capacity: int, triggerer_heartrate: float):
  45. with _serve_logs(skip_serve_logs):
  46. triggerer_job_runner = TriggererJobRunner(job=Job(heartrate=triggerer_heartrate), capacity=capacity)
  47. run_job(job=triggerer_job_runner.job, execute_callable=triggerer_job_runner._execute)
  48. @cli_utils.action_cli
  49. @providers_configuration_loaded
  50. def triggerer(args):
  51. """Start Airflow Triggerer."""
  52. settings.MASK_SECRETS_IN_LOGS = True
  53. print(settings.HEADER)
  54. triggerer_heartrate = conf.getfloat("triggerer", "JOB_HEARTBEAT_SEC")
  55. run_command_with_daemon_option(
  56. args=args,
  57. process_name="triggerer",
  58. callback=lambda: triggerer_run(args.skip_serve_logs, args.capacity, triggerer_heartrate),
  59. should_setup_logging=True,
  60. )