vincbeck commented on code in PR #30308:
URL: https://github.com/apache/airflow/pull/30308#discussion_r1153683681


##########
airflow/jobs/base_job.py:
##########
@@ -221,34 +221,24 @@ def heartbeat(self, only_if_necessary: bool = False):
             # We didn't manage to heartbeat, so make sure that the timestamp 
isn't updated
             self.latest_heartbeat = previous_heartbeat
 
-    def run(self) -> int | None:
-        """Starts the job."""
+    @provide_session
+    def prepare_for_execution(self, session=NEW_SESSION):
+        """Prepares the job for execution."""
         Stats.incr(self.__class__.__name__.lower() + "_start", 1, 1)
-        # Adding an entry in the DB
-        ret = None
-        with create_session() as session:
-            self.state = State.RUNNING
-            session.add(self)
-            session.commit()
-            make_transient(self)
-            try:
-                ret = self.job_runner.execute()
-                # In case of max runs or max duration
-                self.state = State.SUCCESS
-            except SystemExit:
-                # In case of ^C or SIGTERM
-                self.state = State.SUCCESS
-            except Exception:
-                self.state = State.FAILED
-                raise
-            finally:
-                get_listener_manager().hook.before_stopping(component=self)
-                self.end_date = timezone.utcnow()
-                session.merge(self)
-                session.commit()
+        self.state = State.RUNNING
+        self.start_date = timezone.utcnow()
+        self.state = State.RUNNING

Review Comment:
   Duplicate



##########
airflow/jobs/base_job.py:
##########
@@ -221,34 +221,24 @@ def heartbeat(self, only_if_necessary: bool = False):
             # We didn't manage to heartbeat, so make sure that the timestamp 
isn't updated
             self.latest_heartbeat = previous_heartbeat
 
-    def run(self) -> int | None:
-        """Starts the job."""
+    @provide_session
+    def prepare_for_execution(self, session=NEW_SESSION):
+        """Prepares the job for execution."""
         Stats.incr(self.__class__.__name__.lower() + "_start", 1, 1)
-        # Adding an entry in the DB
-        ret = None
-        with create_session() as session:
-            self.state = State.RUNNING
-            session.add(self)
-            session.commit()
-            make_transient(self)
-            try:
-                ret = self.job_runner.execute()
-                # In case of max runs or max duration
-                self.state = State.SUCCESS
-            except SystemExit:
-                # In case of ^C or SIGTERM
-                self.state = State.SUCCESS
-            except Exception:
-                self.state = State.FAILED
-                raise
-            finally:
-                get_listener_manager().hook.before_stopping(component=self)
-                self.end_date = timezone.utcnow()
-                session.merge(self)
-                session.commit()
+        self.state = State.RUNNING
+        self.start_date = timezone.utcnow()

Review Comment:
   Just curious, how come it was not done before?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to