-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjob_manager.py
More file actions
274 lines (223 loc) · 7.4 KB
/
job_manager.py
File metadata and controls
274 lines (223 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Job Manager for handling asynchronous background processing.
This module allows long-running PDF processing tasks to run in the background
without blocking the HTTP request, preventing Azure Web App timeouts.
"""
import io
import json
import os
import threading
import time
import traceback
import uuid
from datetime import datetime
from pathlib import Path
class JobStatus:
"""Job status constants"""
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
class JobManager:
"""
Manages background jobs for PDF processing.
Uses file-based storage for job state persistence.
"""
def __init__(self, jobs_dir=".jobs"):
"""
Initialize the job manager.
Args:
jobs_dir: Directory to store job state files
"""
self.jobs_dir = Path(jobs_dir)
self.jobs_dir.mkdir(exist_ok=True)
self._lock = threading.Lock()
def create_job(self, job_id=None, user_email=None):
"""
Create a new job entry.
Args:
job_id: Optional job ID. If not provided, generates a UUID.
user_email: Optional user email for job tracking.
Returns:
str: The job ID
"""
if job_id is None:
job_id = str(uuid.uuid4())
job_data = {
"job_id": job_id,
"status": JobStatus.PENDING,
"created_at": datetime.utcnow().isoformat(),
"updated_at": datetime.utcnow().isoformat(),
"user_email": user_email,
"progress": {
"current_pdf": 0,
"total_pdfs": 0,
"current_variable": 0,
"total_variables": 0,
"message": "Job created"
},
"result": None,
"error": None
}
self._save_job(job_id, job_data)
return job_id
def get_job(self, job_id):
"""
Get job data by ID.
Args:
job_id: The job ID
Returns:
dict: Job data or None if not found
"""
job_file = self.jobs_dir / f"{job_id}.json"
if not job_file.exists():
return None
with open(job_file, 'r') as f:
return json.load(f)
def update_job(self, job_id, updates):
"""
Update job data.
Args:
job_id: The job ID
updates: Dictionary of fields to update
"""
with self._lock:
job_data = self.get_job(job_id)
if job_data is None:
return
job_data.update(updates)
job_data["updated_at"] = datetime.utcnow().isoformat()
self._save_job(job_id, job_data)
def update_progress(self, job_id, message=None, current_pdf=None, total_pdfs=None,
current_variable=None, total_variables=None):
"""
Update job progress.
Args:
job_id: The job ID
message: Progress message
current_pdf: Current PDF being processed
total_pdfs: Total number of PDFs
current_variable: Current variable being processed
total_variables: Total number of variables
"""
job_data = self.get_job(job_id)
if job_data is None:
return
progress = job_data.get("progress", {})
if message is not None:
progress["message"] = message
if current_pdf is not None:
progress["current_pdf"] = current_pdf
if total_pdfs is not None:
progress["total_pdfs"] = total_pdfs
if current_variable is not None:
progress["current_variable"] = current_variable
if total_variables is not None:
progress["total_variables"] = total_variables
self.update_job(job_id, {"progress": progress})
def mark_running(self, job_id):
"""Mark a job as running."""
self.update_job(job_id, {"status": JobStatus.RUNNING})
def mark_completed(self, job_id, result=None):
"""
Mark a job as completed.
Args:
job_id: The job ID
result: Optional result data
"""
self.update_job(job_id, {
"status": JobStatus.COMPLETED,
"result": result
})
def mark_failed(self, job_id, error):
"""
Mark a job as failed.
Args:
job_id: The job ID
error: Error message or traceback
"""
self.update_job(job_id, {
"status": JobStatus.FAILED,
"error": str(error)
})
def _save_job(self, job_id, job_data):
"""Save job data to file."""
job_file = self.jobs_dir / f"{job_id}.json"
with open(job_file, 'w') as f:
json.dump(job_data, f, indent=2)
def find_jobs_by_email(self, email):
"""
Find all jobs for a given email address.
Args:
email: User's email address
Returns:
list: List of job data dictionaries, sorted by created_at (newest first)
"""
matching_jobs = []
for job_file in self.jobs_dir.glob("*.json"):
try:
with open(job_file, 'r') as f:
job_data = json.load(f)
if job_data.get("user_email") == email:
matching_jobs.append(job_data)
except Exception:
pass # Skip corrupted files
# Sort by created_at, newest first
matching_jobs.sort(key=lambda x: x.get("created_at", ""), reverse=True)
return matching_jobs
def cleanup_old_jobs(self, max_age_hours=24):
"""
Clean up old job files.
Args:
max_age_hours: Maximum age of job files to keep in hours
"""
cutoff_time = time.time() - (max_age_hours * 3600)
for job_file in self.jobs_dir.glob("*.json"):
if job_file.stat().st_mtime < cutoff_time:
try:
job_file.unlink()
except Exception:
pass # Ignore cleanup errors
# Global job manager instance
_job_manager = None
def get_job_manager():
"""Get or create the global job manager instance."""
global _job_manager
if _job_manager is None:
_job_manager = JobManager()
return _job_manager
def run_job_async(job_id, target_func, args=(), kwargs=None):
"""
Run a function asynchronously in a background thread.
Args:
job_id: The job ID
target_func: Function to run
args: Positional arguments for the function
kwargs: Keyword arguments for the function
"""
if kwargs is None:
kwargs = {}
def wrapper():
job_manager = get_job_manager()
try:
job_manager.mark_running(job_id)
result = target_func(*args, **kwargs)
job_manager.mark_completed(job_id, result)
except Exception as e:
error_trace = traceback.format_exc()
job_manager.mark_failed(job_id, error_trace)
print(f"Job {job_id} failed: {e}")
print(error_trace)
thread = threading.Thread(target=wrapper, daemon=True)
thread.start()
return thread
def get_job_status(job_id):
"""
Get the current status of a job.
Args:
job_id: The job ID
Returns:
dict: Job status information
"""
job_manager = get_job_manager()
return job_manager.get_job(job_id)