-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathjobs.py
More file actions
593 lines (498 loc) · 19.3 KB
/
jobs.py
File metadata and controls
593 lines (498 loc) · 19.3 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
"""
Job queue management for AutoPopulate 2.0.
Each auto-populated table (Computed/Imported) has an associated jobs table
with the naming pattern ``~~table_name``. The jobs table tracks job status,
priority, scheduling, and error information.
"""
from __future__ import annotations
import logging
import os
import platform
import subprocess
from .condition import AndList, Not, make_condition
from .errors import DataJointError, DuplicateError
from .heading import Heading
from .table import Table
ERROR_MESSAGE_LENGTH = 2047
TRUNCATION_APPENDIX = "...truncated"
logger = logging.getLogger(__name__.split(".")[0])
def _get_job_version() -> str:
"""
Get version string based on config settings.
Returns
-------
str
Version string, or empty string if version tracking disabled.
"""
from .settings import config
method = config.jobs.version_method
if method is None or method == "none":
return ""
elif method == "git":
try:
result = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
capture_output=True,
text=True,
timeout=5,
)
return result.stdout.strip() if result.returncode == 0 else ""
except Exception:
return ""
return ""
class Job(Table):
"""
Per-table job queue for AutoPopulate 2.0.
Each auto-populated table (Computed/Imported) has an associated job table
with the naming pattern ``~~table_name``. The job table tracks job status,
priority, scheduling, and error information.
Parameters
----------
target_table : Table
The Computed/Imported table instance this jobs table manages.
Attributes
----------
target : Table
The auto-populated table this jobs table manages.
pending : QueryExpression
Query for jobs with ``status='pending'``.
reserved : QueryExpression
Query for jobs with ``status='reserved'``.
errors : QueryExpression
Query for jobs with ``status='error'``.
completed : QueryExpression
Query for jobs with ``status='success'``.
ignored : QueryExpression
Query for jobs with ``status='ignore'``.
Examples
--------
>>> MyTable.jobs.refresh() # Add new jobs, clean up stale ones
>>> MyTable.jobs.pending # Query pending jobs
>>> MyTable.jobs.errors # Query failed jobs
"""
def __init__(self, target_table: Table) -> None:
"""
Initialize jobs table for an auto-populated table.
Parameters
----------
target_table : Table
The Computed/Imported table instance this jobs table manages.
"""
self._target = target_table
self._connection = target_table.connection
self.database = target_table.database
# Compute table name: ~~base_name
target_name = target_table.table_name
base_name = target_name.lstrip("_")
self._table_name = f"~~{base_name}"
# Generate definition from target's FK-derived primary key
self._definition = self._generate_definition()
# Initialize heading and support
self._heading = Heading(
table_info=dict(
conn=self._connection,
database=self.database,
table_name=self._table_name,
context=None,
)
)
self._support = [self.full_table_name]
@property
def table_name(self):
return self._table_name
@property
def definition(self):
return self._definition
@property
def target(self):
"""The auto-populated table this jobs table manages."""
return self._target
def _generate_definition(self) -> str:
"""
Generate jobs table definition from target's FK-derived primary key.
Returns
-------
str
DataJoint table definition string.
"""
pk_attrs = self._get_fk_derived_pk_attrs()
if not pk_attrs:
raise DataJointError(
f"Cannot create jobs table for {self._target.full_table_name}: no FK-derived primary key attributes found."
)
pk_lines = "\n ".join(f"{name} : {dtype}" for name, dtype in pk_attrs)
return f"""
# Job queue for {self._target.full_table_name}
{pk_lines}
---
status : enum('pending', 'reserved', 'success', 'error', 'ignore')
priority : int8
created_time=CURRENT_TIMESTAMP(3) : datetime(3)
scheduled_time=CURRENT_TIMESTAMP(3) : datetime(3)
reserved_time=null : datetime(3)
completed_time=null : datetime(3)
duration=null : float64
error_message="" : varchar({ERROR_MESSAGE_LENGTH})
error_stack=null : <blob>
user="" : varchar(255)
host="" : varchar(255)
pid=0 : int32
connection_id=0 : int64
version="" : varchar(64)
INDEX (status, priority, scheduled_time)
"""
def _get_fk_derived_pk_attrs(self) -> list[tuple[str, str]]:
"""
Extract FK-derived primary key attributes using the dependency graph.
FK-derived attributes are those that come from primary FK references.
Uses connection.dependencies to identify FK relationships.
Returns
-------
list[tuple[str, str]]
List of (attribute_name, datatype) tuples in target PK order.
"""
heading = self._target.heading
target_pk = heading.primary_key
# Load dependency graph if not already loaded
self._connection.dependencies.load()
# Get primary FK parents and collect their attribute mappings
# parents(primary=True) returns FKs that contribute to primary key
parents = self._target.parents(primary=True, foreign_key_info=True)
fk_derived_attrs = set()
for _parent_name, props in parents:
# attr_map: child_attr -> parent_attr
fk_derived_attrs.update(props.get("attr_map", {}).keys())
fk_attrs = []
for name in target_pk:
if name in fk_derived_attrs:
# FK-derived: comes from a primary FK parent
attr = heading[name]
fk_attrs.append((name, attr.type))
else:
# Native PK attribute - not from FK
logger.warning(
f"Ignoring non-FK primary key attribute '{name}' in jobs table "
f"for {self._target.full_table_name}. Job granularity will be degraded."
)
return fk_attrs
def _get_pk(self, key: dict) -> dict:
"""
Extract primary key values from a key dict.
Parameters
----------
key : dict
Dictionary containing at least the primary key attributes.
Returns
-------
dict
Dictionary with only the primary key attributes.
"""
return {k: key[k] for k in self.primary_key if k in key}
def delete(self) -> None:
"""Delete all entries, bypassing interactive prompts and dependencies."""
self.delete_quick()
def drop(self) -> None:
"""Drop the table, bypassing interactive prompts and dependencies."""
self.drop_quick()
# -------------------------------------------------------------------------
# Status filter properties
# -------------------------------------------------------------------------
@property
def pending(self) -> "Job":
"""
Query for pending jobs awaiting processing.
Returns
-------
Job
Restricted query with ``status='pending'``.
"""
return self & "status='pending'"
@property
def reserved(self) -> "Job":
"""
Query for jobs currently being processed.
Returns
-------
Job
Restricted query with ``status='reserved'``.
"""
return self & "status='reserved'"
@property
def errors(self) -> "Job":
"""
Query for jobs that failed with errors.
Returns
-------
Job
Restricted query with ``status='error'``.
"""
return self & "status='error'"
@property
def ignored(self) -> "Job":
"""
Query for jobs marked to be skipped.
Returns
-------
Job
Restricted query with ``status='ignore'``.
"""
return self & "status='ignore'"
@property
def completed(self) -> "Job":
"""
Query for successfully completed jobs.
Returns
-------
Job
Restricted query with ``status='success'``.
"""
return self & "status='success'"
# -------------------------------------------------------------------------
# Core job management methods
# -------------------------------------------------------------------------
def refresh(
self,
*restrictions,
delay: float = 0,
priority: int | None = None,
stale_timeout: float | None = None,
orphan_timeout: float | None = None,
) -> dict:
"""
Refresh the jobs queue: add new jobs and clean up stale/orphaned jobs.
Parameters
----------
*restrictions : any
Conditions to filter key_source (for adding new jobs).
delay : float, optional
Seconds from now until new jobs become available for processing.
Default 0 (immediately available). Uses database server time.
priority : int, optional
Priority for new jobs (lower = more urgent).
Default from ``config.jobs.default_priority``.
stale_timeout : float, optional
Seconds after which jobs are checked for staleness.
Jobs older than this are removed if key not in key_source.
Default from ``config.jobs.stale_timeout``. Set to 0 to skip.
orphan_timeout : float, optional
Seconds after which reserved jobs are considered orphaned.
Reserved jobs older than this are deleted and re-added as pending.
Default None (no orphan cleanup).
Returns
-------
dict
Status counts with keys: ``'added'``, ``'removed'``,
``'orphaned'``, ``'re_pended'``.
Notes
-----
Operations performed:
1. Add new jobs: ``(key_source & restrictions) - target - jobs`` → insert as pending
2. Re-pend success jobs: if ``keep_completed=True`` and key in key_source but not in target
3. Remove stale jobs: jobs older than stale_timeout whose keys not in key_source
4. Remove orphaned jobs: reserved jobs older than orphan_timeout (if specified)
"""
from .settings import config
# Ensure jobs table exists
if not self.is_declared:
self.declare()
# Get defaults from config
if priority is None:
priority = config.jobs.default_priority
if stale_timeout is None:
stale_timeout = config.jobs.stale_timeout
result = {"added": 0, "removed": 0, "orphaned": 0, "re_pended": 0}
# 1. Add new jobs
key_source = self._target.key_source
if restrictions:
key_source = key_source & AndList(restrictions)
# Keys that need jobs: in key_source, not in target, not in jobs
# Disable semantic_check for Job table (self) because its attributes may not have matching lineage
new_keys = (key_source - self._target).restrict(Not(self), semantic_check=False).proj()
new_key_list = new_keys.keys()
if new_key_list:
# Use server time for scheduling (CURRENT_TIMESTAMP(3) matches datetime(3) precision)
interval_expr = self.adapter.interval_expr(delay, "second")
scheduled_time = self.connection.query(f"SELECT CURRENT_TIMESTAMP(3) + {interval_expr}").fetchone()[0]
for key in new_key_list:
job_entry = {
**key,
"status": "pending",
"priority": priority,
"scheduled_time": scheduled_time,
}
try:
self.insert1(job_entry, ignore_extra_fields=True)
result["added"] += 1
except DuplicateError:
pass # Job already exists
# 2. Re-pend success jobs if keep_completed=True
if config.jobs.keep_completed:
# Success jobs whose keys are in key_source but not in target
# Disable semantic_check for Job table operations (job table PK has different lineage than target)
success_to_repend = self.completed.restrict(key_source, semantic_check=False).restrict(
Not(self._target), semantic_check=False
)
repend_keys = success_to_repend.keys()
for key in repend_keys:
(self & key).delete_quick()
self.insert1({**key, "status": "pending", "priority": priority})
result["re_pended"] += 1
# 3. Remove stale jobs (not ignore status) - use server CURRENT_TIMESTAMP for consistent timing
if stale_timeout > 0:
stale_interval = self.adapter.interval_expr(stale_timeout, "second")
old_jobs = self & f"created_time < CURRENT_TIMESTAMP - {stale_interval}" & "status != 'ignore'"
for key in old_jobs.keys():
# Check if key still in key_source
if not (key_source & key):
(self & key).delete_quick()
result["removed"] += 1
# 4. Handle orphaned reserved jobs - use server CURRENT_TIMESTAMP for consistent timing
if orphan_timeout is not None and orphan_timeout > 0:
orphan_interval = self.adapter.interval_expr(orphan_timeout, "second")
orphaned_jobs = self.reserved & f"reserved_time < CURRENT_TIMESTAMP - {orphan_interval}"
for key in orphaned_jobs.keys():
(self & key).delete_quick()
self.insert1({**key, "status": "pending", "priority": priority})
result["orphaned"] += 1
return result
def reserve(self, key: dict) -> bool:
"""
Attempt to reserve a pending job for processing.
Atomically updates status to ``'reserved'`` if currently ``'pending'``
and ``scheduled_time <= now``, using a single UPDATE with a WHERE clause
that includes the status check. This prevents race conditions where
multiple workers could reserve the same job simultaneously.
Parameters
----------
key : dict
Primary key dict of the job to reserve.
Returns
-------
bool
True if reservation successful, False if job not available.
"""
pk = self._get_pk(key)
where = make_condition(self, pk, set())
qi = self.adapter.quote_identifier
assignments = ", ".join(f"{qi(k)}=%s" for k in ("status", "host", "pid", "connection_id", "user", "version"))
query = (
f"UPDATE {self.full_table_name} "
f"SET {assignments}, {qi('reserved_time')}=CURRENT_TIMESTAMP(3) "
f"WHERE {where} AND {qi('status')}='pending' "
f"AND {qi('scheduled_time')} <= CURRENT_TIMESTAMP(3)"
)
args = [
"reserved",
platform.node(),
os.getpid(),
self.connection.connection_id,
self.connection.get_user(),
_get_job_version(),
]
cursor = self.connection.query(query, args=args)
return cursor.rowcount == 1
def complete(self, key: dict, duration: float | None = None) -> None:
"""
Mark a job as successfully completed.
Parameters
----------
key : dict
Primary key dict of the job.
duration : float, optional
Execution duration in seconds.
Notes
-----
Based on ``config.jobs.keep_completed``:
- If True: updates status to ``'success'`` with completion time and duration
- If False: deletes the job entry
"""
from .settings import config
if config.jobs.keep_completed:
# Use server time for completed_time
server_now = self.connection.query("SELECT CURRENT_TIMESTAMP").fetchone()[0]
pk = self._get_pk(key)
update_row = {
**pk,
"status": "success",
"completed_time": server_now,
}
if duration is not None:
update_row["duration"] = duration
self.update1(update_row)
else:
(self & key).delete_quick()
def error(self, key: dict, error_message: str, error_stack: str | None = None) -> None:
"""
Mark a job as failed with error details.
Parameters
----------
key : dict
Primary key dict of the job.
error_message : str
Error message (truncated to 2047 chars if longer).
error_stack : str, optional
Full stack trace.
"""
if len(error_message) > ERROR_MESSAGE_LENGTH:
error_message = error_message[: ERROR_MESSAGE_LENGTH - len(TRUNCATION_APPENDIX)] + TRUNCATION_APPENDIX
# Use server time for completed_time
server_now = self.connection.query("SELECT CURRENT_TIMESTAMP").fetchone()[0]
pk = self._get_pk(key)
update_row = {
**pk,
"status": "error",
"completed_time": server_now,
"error_message": error_message,
}
if error_stack is not None:
update_row["error_stack"] = error_stack
self.update1(update_row)
def ignore(self, key: dict) -> None:
"""
Mark a job to be ignored (skipped during populate).
If the key doesn't exist in the jobs table, inserts it with
``status='ignore'``. If it exists, updates the status to ``'ignore'``.
Parameters
----------
key : dict
Primary key dict of the job.
"""
from .settings import config
pk = self._get_pk(key)
if pk in self:
self.update1({**pk, "status": "ignore"})
else:
priority = config.jobs.default_priority
self.insert1({**pk, "status": "ignore", "priority": priority})
def progress(self) -> dict:
"""
Return job status breakdown.
Returns
-------
dict
Counts by status with keys: ``'pending'``, ``'reserved'``,
``'success'``, ``'error'``, ``'ignore'``, ``'total'``.
"""
if not self.is_declared:
return {
"pending": 0,
"reserved": 0,
"success": 0,
"error": 0,
"ignore": 0,
"total": 0,
}
# Query status counts
result = self.connection.query(f"SELECT status, COUNT(*) as n FROM {self.full_table_name} GROUP BY status").fetchall()
counts = {
"pending": 0,
"reserved": 0,
"success": 0,
"error": 0,
"ignore": 0,
}
for row in result:
status, n = row
counts[status] = n
counts["total"] = sum(counts.values())
return counts