Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/jobs/base_job_runner.py: 58%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

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 

18from __future__ import annotations 

19 

20from typing import TYPE_CHECKING, Generic, TypeVar 

21 

22from airflow.utils.session import NEW_SESSION, provide_session 

23 

24if TYPE_CHECKING: 

25 from sqlalchemy.orm import Session 

26 

27 from airflow.jobs.job import Job 

28 from airflow.serialization.pydantic.job import JobPydantic 

29 

30J = TypeVar("J", "Job", "JobPydantic", "Job | JobPydantic") 

31 

32 

33class BaseJobRunner(Generic[J]): 

34 """Abstract class for job runners to derive from.""" 

35 

36 job_type = "undefined" 

37 

38 def __init__(self, job: J) -> None: 

39 if job.job_type and job.job_type != self.job_type: 

40 raise Exception( 

41 f"The job is already assigned a different job_type: {job.job_type}." 

42 f"This is a bug and should be reported." 

43 ) 

44 job.job_type = self.job_type 

45 self.job: J = job 

46 

47 def _execute(self) -> int | None: 

48 """ 

49 Executes the logic connected to the runner. This method should be 

50 overridden by subclasses. 

51 

52 :meta private: 

53 :return: return code if available, otherwise None 

54 """ 

55 raise NotImplementedError() 

56 

57 @provide_session 

58 def heartbeat_callback(self, session: Session = NEW_SESSION) -> None: 

59 """Callback that is called during heartbeat. This method can be overwritten by the runners.""" 

60 

61 @classmethod 

62 @provide_session 

63 def most_recent_job(cls, session: Session = NEW_SESSION) -> Job | None: 

64 """Returns the most recent job of this type, if any, based on last heartbeat received.""" 

65 from airflow.jobs.job import most_recent_job 

66 

67 return most_recent_job(cls.job_type, session=session)