44from typing import Optional
55
66from syft_job .config import SyftJobConfig
7+ from syft_job .job_storage import JobRef , JobStorage
78from syft_job .models import JobState , JobStatus , JobSubmissionMetadata
89
910from syft_bg .common .monitor import Monitor
@@ -29,28 +30,20 @@ def __init__(
2930 self .job_config = SyftJobConfig .from_syftbox_folder (
3031 str (self .syftbox_root ), do_email
3132 )
33+ self .job_manager = JobStorage (config = self .job_config )
3234
3335 def _check_all_entities (self ):
3436 self .process_local_status_changes ()
3537
3638 def process_local_status_changes (self ):
37- inbox_dir = self .job_config .get_all_submissions_dir (self .do_email )
38- if not inbox_dir .exists ():
39- return
40-
41- for ds_dir in inbox_dir .iterdir ():
42- if not ds_dir .is_dir ():
43- continue
44- for job_path in ds_dir .iterdir ():
45- if not job_path .is_dir ():
46- continue
47- try :
48- self ._maybe_process_job (job_path )
49- except Exception as e :
50- print (f"[JobMonitor] Error checking job { job_path .name } : { e } " )
51-
52- def _maybe_process_job (self , job_path : Path ):
53- metadata = self ._load_job_metadata (job_path )
39+ for ref in self .job_manager .iter_submission_refs (self .do_email ):
40+ try :
41+ self ._maybe_process_job (ref )
42+ except Exception as e :
43+ print (f"[JobMonitor] Error checking job { ref .job_name } : { e } " )
44+
45+ def _maybe_process_job (self , ref : JobRef ):
46+ metadata = self ._load_job_metadata (ref )
5447 if not metadata :
5548 return
5649
@@ -62,7 +55,7 @@ def _maybe_process_job(self, job_path: Path):
6255 if success :
6356 print (f"[JobMonitor] Sent new job notification: { job_name } " )
6457
65- review_state = self ._load_review_state (ds_email , job_name )
58+ review_state = self ._load_review_state (ref )
6659
6760 if review_state and review_state .status in (
6861 JobStatus .APPROVED ,
@@ -85,61 +78,40 @@ def _maybe_process_job(self, job_path: Path):
8578
8679 def seed_existing_jobs (self ):
8780 """On fresh state, mark all existing jobs so we don't re-notify old jobs."""
88- inbox_dir = self .job_config .get_all_submissions_dir (self .do_email )
89- if not inbox_dir .exists ():
90- return
91-
9281 count = 0
93- for ds_dir in inbox_dir .iterdir ():
94- if not ds_dir .is_dir ():
82+ for ref in self .job_manager .iter_submission_refs (self .do_email ):
83+ metadata = self ._load_job_metadata (ref )
84+ if not metadata :
9585 continue
96- for job_path in ds_dir .iterdir ():
97- if not job_path .is_dir ():
98- continue
99- metadata = self ._load_job_metadata (job_path )
100- if not metadata :
101- continue
102- self .state .mark_notified (metadata .name , "new" )
103- review_state = self ._load_review_state (
104- metadata .submitted_by , metadata .name
105- )
106- if review_state :
107- if review_state .status in (
108- JobStatus .APPROVED ,
109- JobStatus .RUNNING ,
110- JobStatus .DONE ,
111- JobStatus .FAILED ,
112- ):
113- self .state .mark_notified (metadata .name , "approved" )
114- if review_state .status == JobStatus .DONE :
115- self .state .mark_notified (metadata .name , "executed" )
116- if review_state .status == JobStatus .FAILED :
117- self .state .mark_notified (metadata .name , "failed" )
118- count += 1
86+ self .state .mark_notified (ref .job_name , "new" )
87+ review_state = self ._load_review_state (ref )
88+ if review_state :
89+ if review_state .status in (
90+ JobStatus .APPROVED ,
91+ JobStatus .RUNNING ,
92+ JobStatus .DONE ,
93+ JobStatus .FAILED ,
94+ ):
95+ self .state .mark_notified (metadata .name , "approved" )
96+ if review_state .status == JobStatus .DONE :
97+ self .state .mark_notified (metadata .name , "executed" )
98+ if review_state .status == JobStatus .FAILED :
99+ self .state .mark_notified (metadata .name , "failed" )
100+ count += 1
119101
120102 if count :
121103 print (f"[JobMonitor] Seeded { count } existing jobs on fresh state" )
122104
123- def _load_review_state (self , ds_email : str , job_name : str ) -> Optional [JobState ]:
105+ def _load_review_state (self , ref : JobRef ) -> Optional [JobState ]:
124106 """Load state.yaml from the job's review directory."""
125- review_dir = self .job_config .get_review_job_dir (
126- self .do_email , ds_email , job_name
127- )
128- state_file = review_dir / "state.yaml"
129- if not state_file .exists ():
130- return None
131107 try :
132- return JobState . load ( state_file )
108+ return self . job_manager . read_state ( ref )
133109 except Exception :
134110 return None
135111
136- def _load_job_metadata (self , job_path : Path ) -> Optional [JobSubmissionMetadata ]:
137- config_file = job_path / "config.yaml"
138- if not config_file .exists ():
139- return None
140-
112+ def _load_job_metadata (self , ref : JobRef ) -> Optional [JobSubmissionMetadata ]:
141113 try :
142- return JobSubmissionMetadata . load ( config_file )
114+ return self . job_manager . read_submission ( ref )
143115 except Exception as e :
144- print (f"[JobMonitor] Error reading job config { config_file } : { e } " )
116+ print (f"[JobMonitor] Error reading job config for { ref . job_name } : { e } " )
145117 return None
0 commit comments