Content-Length: 491344 | pFad | https://github.com/apache/airflow/commit/008b4bab14222da068b737d6332db4963b994007

6C [AIRFLOW-6730] Use total_seconds instead of seconds (#7363) · apache/airflow@008b4ba · GitHub
Skip to content

Commit 008b4ba

Browse files
authored
[AIRFLOW-6730] Use total_seconds instead of seconds (#7363)
* [AIRFLOW-6730] Use total_seconds instead of seconds * adds tests and fixes types issue * fix test
1 parent ee16d30 commit 008b4ba

File tree

7 files changed

+19
-9
lines changed

7 files changed

+19
-9
lines changed

airflow/jobs/base_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def is_alive(self, grace_multiplier=2.1):
119119
"""
120120
return (
121121
self.state == State.RUNNING and
122-
(timezone.utcnow() - self.latest_heartbeat).seconds < self.heartrate * grace_multiplier
122+
(timezone.utcnow() - self.latest_heartbeat).total_seconds() < self.heartrate * grace_multiplier
123123
)
124124

125125
@provide_session

airflow/jobs/scheduler_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ def is_alive(self, grace_multiplier=None):
989989
scheduler_health_check_threshold = conf.getint('scheduler', 'scheduler_health_check_threshold')
990990
return (
991991
self.state == State.RUNNING and
992-
(timezone.utcnow() - self.latest_heartbeat).seconds < scheduler_health_check_threshold
992+
(timezone.utcnow() - self.latest_heartbeat).total_seconds() < scheduler_health_check_threshold
993993
)
994994

995995
@provide_session

airflow/kubernetes/pod_launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def run_pod(
111111
if resp.status.start_time is None:
112112
while self.pod_not_started(pod):
113113
delta = dt.now() - curr_time
114-
if delta.seconds >= startup_timeout:
114+
if delta.total_seconds() >= startup_timeout:
115115
raise AirflowException("Pod took too long to start")
116116
time.sleep(1)
117117
self.log.debug('Pod not yet started')

airflow/providers/google/cloud/operators/dataproc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def _get_init_action_timeout(self):
244244
return self.init_action_timeout
245245
elif match.group(2) == "m":
246246
val = float(match.group(1))
247-
return "{}s".format(timedelta(minutes=val).seconds)
247+
return "{}s".format(int(timedelta(minutes=val).total_seconds()))
248248

249249
raise AirflowException(
250250
"DataprocClusterCreateOperator init_action_timeout"
@@ -621,13 +621,13 @@ def _graceful_decommission_timeout_object(self) -> Optional[Dict]:
621621
timeout = int(match.group(1))
622622
elif match.group(2) == "m":
623623
val = float(match.group(1))
624-
timeout = timedelta(minutes=val).seconds
624+
timeout = int(timedelta(minutes=val).total_seconds())
625625
elif match.group(2) == "h":
626626
val = float(match.group(1))
627-
timeout = timedelta(hours=val).seconds
627+
timeout = int(timedelta(hours=val).total_seconds())
628628
elif match.group(2) == "d":
629629
val = float(match.group(1))
630-
timeout = timedelta(days=val).seconds
630+
timeout = int(timedelta(days=val).total_seconds())
631631

632632
if not timeout:
633633
raise AirflowException(

airflow/providers/google/cloud/operators/postgres_to_gcs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ def convert_type(self, value, schema_type):
9191
return time.mktime(value.timetuple())
9292
if isinstance(value, datetime.time):
9393
formated_time = time.strptime(str(value), "%H:%M:%S")
94-
return datetime.timedelta(
94+
return int(datetime.timedelta(
9595
hours=formated_time.tm_hour,
9696
minutes=formated_time.tm_min,
97-
seconds=formated_time.tm_sec).seconds
97+
seconds=formated_time.tm_sec).total_seconds())
9898
if isinstance(value, dict):
9999
return json.dumps(value)
100100
if isinstance(value, Decimal):

tests/jobs/test_base_job.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ def test_is_alive(self):
9595
job.latest_heartbeat = timezone.utcnow() - datetime.timedelta(seconds=21)
9696
self.assertFalse(job.is_alive())
9797

98+
# test because .seconds was used before instead of total_seconds
99+
# internal repr of datetime is (days, seconds)
100+
job.latest_heartbeat = timezone.utcnow() - datetime.timedelta(days=1)
101+
self.assertFalse(job.is_alive())
102+
98103
job.state = State.SUCCESS
99104
job.latest_heartbeat = timezone.utcnow() - datetime.timedelta(seconds=10)
100105
self.assertFalse(job.is_alive(), "Completed jobs even with recent heartbeat should not be alive")

tests/jobs/test_scheduler_job.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,11 @@ def test_is_alive(self):
10661066
job.latest_heartbeat = timezone.utcnow() - datetime.timedelta(seconds=31)
10671067
self.assertFalse(job.is_alive())
10681068

1069+
# test because .seconds was used before instead of total_seconds
1070+
# internal repr of datetime is (days, seconds)
1071+
job.latest_heartbeat = timezone.utcnow() - datetime.timedelta(days=1)
1072+
self.assertFalse(job.is_alive())
1073+
10691074
job.state = State.SUCCESS
10701075
job.latest_heartbeat = timezone.utcnow() - datetime.timedelta(seconds=10)
10711076
self.assertFalse(job.is_alive(), "Completed jobs even with recent heartbeat should not be alive")

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/apache/airflow/commit/008b4bab14222da068b737d6332db4963b994007

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy