From a22421b549d4080ac5666626672b2316d5b52fff Mon Sep 17 00:00:00 2001
From: Ian Grant <ian.conway.grant@gmail.com>
Date: Wed, 14 May 2025 20:43:34 -0400
Subject: [PATCH] refactor: JobLedger.get_pending_jobs() sorts by recency

---
 maap_utils/JobLedger.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/maap_utils/JobLedger.py b/maap_utils/JobLedger.py
index 0543a35..1b5cdd0 100644
--- a/maap_utils/JobLedger.py
+++ b/maap_utils/JobLedger.py
@@ -37,12 +37,19 @@ class JobLedger:
 
     def get_pending_jobs(self) -> List[Job]:
         """Get jobs not in final states"""
-        return [
+
+        # Get all unfinished jobs
+        pending = [
             job
             for job_id, job in self.jobs.items()
             if self.status[job_id] not in self.FINAL_STATES
         ]
 
+        # Sort by last checked time, most recently checked last
+        pending.sort(key=lambda x: self.last_checked[x.job_id])
+
+        return pending
+
     def get_finished_jobs(self) -> List[Job]:
         """Get only finished jobs"""
         return [
-- 
GitLab