job.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. import datetime
  18. from functools import cached_property
  19. from typing import TYPE_CHECKING, Optional
  20. from airflow.executors.executor_loader import ExecutorLoader
  21. from airflow.jobs.base_job_runner import BaseJobRunner
  22. from airflow.utils.pydantic import BaseModel as BaseModelPydantic, ConfigDict
  23. def check_runner_initialized(job_runner: Optional[BaseJobRunner], job_type: str) -> BaseJobRunner:
  24. if job_runner is None:
  25. raise ValueError(f"In order to run {job_type} you need to initialize the {job_type}Runner first.")
  26. return job_runner
  27. class JobPydantic(BaseModelPydantic):
  28. """Serializable representation of the Job ORM SqlAlchemyModel used by internal API."""
  29. id: Optional[int]
  30. dag_id: Optional[str]
  31. state: Optional[str]
  32. job_type: Optional[str]
  33. start_date: Optional[datetime.datetime]
  34. end_date: Optional[datetime.datetime]
  35. latest_heartbeat: datetime.datetime
  36. executor_class: Optional[str]
  37. hostname: Optional[str]
  38. unixname: Optional[str]
  39. grace_multiplier: float = 2.1
  40. model_config = ConfigDict(from_attributes=True)
  41. @cached_property
  42. def executor(self):
  43. return ExecutorLoader.get_default_executor()
  44. @cached_property
  45. def heartrate(self) -> float:
  46. from airflow.jobs.job import Job
  47. if TYPE_CHECKING:
  48. assert self.job_type is not None
  49. return Job._heartrate(self.job_type)
  50. def is_alive(self) -> bool:
  51. """Is this job currently alive."""
  52. from airflow.jobs.job import Job, health_check_threshold
  53. return Job._is_alive(
  54. state=self.state,
  55. health_check_threshold_value=health_check_threshold(self.job_type, self.heartrate),
  56. latest_heartbeat=self.latest_heartbeat,
  57. )