TakawaAkirayo commented on code in PR #41232:
URL: https://github.com/apache/airflow/pull/41232#discussion_r1716531837


##########
tests/jobs/test_triggerer_job.py:
##########
@@ -787,3 +801,323 @@ def non_pytest_handlers(val):
     assert qh.__class__ == LocalQueueHandler
     assert qh.queue == listener.queue
     listener.stop()
+
+
+class RemoteJob:
+    RUNNING_STATUS = "running"
+    FAILED_STATUS = "failed"
+    SUCCESS_STATUS = "success"
+
+    def __init__(self, status):
+        self.status = status
+
+    def get_status(self):
+        return self.status
+
+    def set_status(self, status):
+        self.status = status
+
+    def kill(self):
+        self.set_status(RemoteJob.FAILED_STATUS)
+
+
+class RemoteService:
+    _instance = None
+
+    event_queue: list[tuple[RemoteJobTrigger, int]]
+    remote_jobs: dict[int, RemoteJob]
+
+    def __new__(cls):
+        if cls._instance is None:
+            cls._instance = super().__new__(cls)
+            cls._instance.event_queue = []
+            cls._instance.remote_jobs = {
+                1: RemoteJob(RemoteJob.RUNNING_STATUS),
+            }
+        return cls._instance
+
+    def complete_job(self, job_id: int):
+        self.remote_jobs[job_id].set_status(RemoteJob.SUCCESS_STATUS)
+
+    def kill_job(self, caller: RemoteJobTrigger, job_id: int):
+        self.event_queue.append((caller, job_id))
+        self.remote_jobs[job_id].kill()
+
+    def get_job(self, job_id: int):
+        return self.remote_jobs[job_id]
+
+    def reset(self):
+        self.event_queue = []
+        self.remote_jobs = {
+            1: RemoteJob(RemoteJob.RUNNING_STATUS),
+        }
+
+
+class TriggerInstances:
+    _instance = None
+
+    def __new__(cls):
+        if cls._instance is None:
+            cls._instance = super().__new__(cls)
+            cls._instance.trigger_instances = []
+        return cls._instance
+
+    def reset(self):
+        self.trigger_instances.clear()
+
+    def add_instances(self, instance):
+        self.trigger_instances.append(instance)
+
+
+class RemoteJobTrigger(BaseTrigger):
+    def __init__(self, remote_job_id, cleanup_on_reassignment: bool):
+        super().__init__()
+        self.remote_job_id = remote_job_id
+        self.finished = False
+        self.cleanup_done = False
+        self.last_status = None
+        self.cleanup_on_reassignment = cleanup_on_reassignment
+        TriggerInstances().add_instances(self)
+
+    def get_status(self):
+        return RemoteService().get_job(self.remote_job_id).get_status()
+
+    async def run(self):
+        print(f"Trigger object {id(self)}: start to run")
+
+        while self.get_status() == RemoteJob.RUNNING_STATUS:
+            await asyncio.sleep(0.1)
+        self.finished = True
+        print(f"Trigger object {id(self)}: finished with status: 
{self.get_status()}")
+        yield TriggerEvent(self.remote_job_id)
+
+    async def cleanup(self) -> None:
+        RemoteService().kill_job(self, self.remote_job_id)
+        self.cleanup_done = True
+        print(f"Trigger object {id(self)}: cleanup done")
+
+    def should_cleanup(self, termination_reason: TriggerTerminationReason | 
None) -> bool:
+        return not (
+            termination_reason == TriggerTerminationReason.REASSIGNED and not 
self.cleanup_on_reassignment
+        )

Review Comment:
   Logically, I wanted to emphasize that cleanup should not occur during 
reassignment, but this makes it appear cumbersome and unnecessary. So let's go 
with your suggestion—it makes the logic look clearer



-- 
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