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 
 
 
 
 
 
3754bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Celery task workers for async inference and batch processing.
Handles long-running DICOM processing jobs with progress tracking via Redis.

Run worker with: celery -A tasks worker --loglevel=info
"""

import logging
import os
import shutil
import datetime
import ssl
import sys
import traceback
from pathlib import Path
from typing import Any
from zoneinfo import ZoneInfo

# Ensure the app directory is in the Python path so imports work in worker processes
APP_DIR = Path(__file__).parent.absolute()
if str(APP_DIR) not in sys.path:
    sys.path.insert(0, str(APP_DIR))

try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass

from celery import Celery, current_task

logger = logging.getLogger(__name__)
IST = ZoneInfo("Asia/Kolkata")

def _now_ist() -> datetime.datetime:
    return datetime.datetime.now(IST).replace(tzinfo=None)

def _env_int(name: str, default: int | None = None, *, minimum: int | None = None) -> int | None:
    raw = os.environ.get(name)
    if raw is None:
        return default
    try:
        value = int(raw)
        if minimum is not None and value < minimum:
            return default
        return value
    except ValueError:
        return default

# Extract Redis URL from environment
REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379/0")

# Initialize Celery app
celery_app = Celery(
    "ich_tasks",
    broker=REDIS_URL,
    backend=REDIS_URL,
)

# Configure Celery with SSL support for Upstash Redis
ssl_config = None
redis_backend_ssl = None
if REDIS_URL.startswith("rediss://"):
    ssl_config = {"ssl_cert_reqs": ssl.CERT_NONE}
    redis_backend_ssl = {"ssl_cert_reqs": ssl.CERT_NONE}

celery_app.conf.update(
    broker_use_ssl=ssl_config,
    redis_backend_use_ssl=redis_backend_ssl,
    task_serializer="json",
    accept_content=["json"],
    result_serializer="json",
    timezone="UTC",
    enable_utc=True,
    task_track_started=True,
    task_time_limit=3600,  # 1 hour hard limit
    task_soft_time_limit=3300,  # 55 min soft limit
    result_expires=86400,  # 24 hours
    worker_prefetch_multiplier=1,  # Prevent long-running tasks from getting stuck behind each other
    task_acks_late=True,  # Only acknowledge task after it completely finishes
)

extra_conf: dict[str, Any] = {}
worker_concurrency = _env_int("ICH_CELERY_CONCURRENCY", None, minimum=1)
worker_prefetch = _env_int("ICH_CELERY_PREFETCH_MULTIPLIER", None, minimum=1)
if worker_concurrency is not None:
    extra_conf["worker_concurrency"] = worker_concurrency
if worker_prefetch is not None:
    extra_conf["worker_prefetch_multiplier"] = worker_prefetch
if extra_conf:
    celery_app.conf.update(**extra_conf)

def _iter_batches(items: list[str], batch_size: int) -> list[list[str]]:
    return [items[i:i + batch_size] for i in range(0, len(items), batch_size)]


@celery_app.task(bind=True, name="tasks.process_dicom_batch")
def process_dicom_batch(
    self,
    batch_id: str,
    dcm_paths: list[str],
    user_id: int,
    temp_dir: str | None = None,
) -> dict[str, Any]:
    """
    Process a batch of DICOM files asynchronously with progress tracking.
    
    Args:
        batch_id: Unique identifier for this batch job
        dcm_paths: List of DICOM file paths to process
        user_id: User ID for audit and data isolation
        temp_dir: Optional temporary directory to clean up after
    
    Returns:
        Dictionary with final batch status and results matching frontend expectations
    """
    # Import here to avoid circular imports. Add diagnostics to help debug
    # ModuleNotFoundError issues when Celery workers can't find `app_new`.
    try:
        # Ensure APP_DIR is present in sys.path for worker subprocesses
        if str(APP_DIR) not in sys.path:
            sys.path.insert(0, str(APP_DIR))
            logger.info(f"Inserted APP_DIR into sys.path: {APP_DIR}")
        else:
            logger.info(f"APP_DIR already in sys.path: {APP_DIR}")

        logger.info(f"tasks.py APP_DIR={APP_DIR}")
        logger.info(f"sys.path (first 10): {sys.path[:10]}")
        # List files in the app dir for visibility
        try:
            files = [p.name for p in Path(APP_DIR).iterdir() if p.exists()]
            logger.info(f"APP_DIR contents: {files[:50]}")
        except Exception as _e:
            logger.warning(f"Could not list APP_DIR contents: {_e}")

        from app_new import app, _run_inference_on_dcm
        from auth_utils import log_audit
        from models import ScreeningUpload, db
    except Exception as e:
        logger.error("Failed importing application modules inside Celery worker:\n" + traceback.format_exc())
        raise

    total = len(dcm_paths)
    succeeded_ids = []
    failed_ids = []
    started_at = _now_ist().isoformat()

    logger.info(f"Batch {batch_id} starting: {total} files for user {user_id}")

    try:
        with app.app_context():
            use_gpu_batch = False
            batch_size = 1
            _infer_images_batch = None
            _persist_inference_result = None
            try:
                from app_new import (
                    GPU_BATCH_SIZE,
                    _gpu_batch_ready,
                    _infer_images_batch,
                    _persist_inference_result,
                )
                use_gpu_batch = _gpu_batch_ready() and total > 1
                batch_size = max(1, GPU_BATCH_SIZE)
            except Exception:
                use_gpu_batch = False

            if use_gpu_batch and _infer_images_batch and _persist_inference_result:
                logger.info(
                    "GPU batch inference enabled (size=%s); per-image traces are skipped.",
                    batch_size,
                )
                processed = 0
                revoked = False
                for chunk in _iter_batches(dcm_paths, batch_size):
                    if revoked:
                        break

                    paths = [Path(p) for p in chunk]
                    upload_records: list[ScreeningUpload] = []
                    for path in paths:
                        request_ctx = current_task.request
                        is_revoked = bool(getattr(request_ctx, "is_revoked", False)) or bool(
                            getattr(request_ctx, "revoked", False)
                        )
                        if is_revoked:
                            logger.info(f"Batch {batch_id} revoked, stopping")
                            revoked = True
                            break

                        upload_record = ScreeningUpload(
                            user_id=user_id,
                            file_name=path.name,
                            original_filename=path.name,
                            file_size=path.stat().st_size if path.exists() else None,
                            file_path=str(path),
                            processing_status="processing",
                        )
                        db.session.add(upload_record)
                        db.session.commit()
                        upload_records.append(upload_record)

                    if revoked:
                        break

                    try:
                        batch_results = _infer_images_batch(paths)
                    except Exception as exc:
                        logger.error(
                            f"Batch {batch_id}: GPU batch inference failed — {exc}",
                            exc_info=True,
                        )
                        for path, upload_record in zip(paths, upload_records, strict=False):
                            image_id = path.stem
                            db.session.rollback()
                            upload_record.processing_status = "failed"
                            try:
                                db.session.commit()
                            except Exception:
                                db.session.rollback()
                            failed_ids.append(image_id)
                            processed += 1
                            self.update_state(
                                state="PROGRESS",
                                meta={
                                    "batch_id": batch_id,
                                    "user_id": user_id,
                                    "status": "running",
                                    "total": total,
                                    "processed": processed,
                                    "succeeded": len(succeeded_ids),
                                    "failed_ids": list(failed_ids),
                                    "image_ids": list(succeeded_ids),
                                    "current_file": "",
                                    "started_at": started_at,
                                    "finished_at": None,
                                    "error": None,
                                    "temp_dir": temp_dir,
                                },
                            )
                        continue

                    for (path, upload_record), (img_rgb, inference) in zip(
                        zip(paths, upload_records, strict=False),
                        batch_results,
                        strict=False,
                    ):
                        image_id = path.stem
                        self.update_state(
                            state="PROGRESS",
                            meta={
                                "batch_id": batch_id,
                                "user_id": user_id,
                                "status": "running",
                                "total": total,
                                "processed": processed,
                                "succeeded": len(succeeded_ids),
                                "failed_ids": list(failed_ids),
                                "image_ids": list(succeeded_ids),
                                "current_file": image_id,
                                "started_at": started_at,
                                "finished_at": None,
                                "error": None,
                                "temp_dir": temp_dir,
                            },
                        )

                        try:
                            report = _persist_inference_result(
                                image_id,
                                user_id,
                                upload_record.id,
                                img_rgb,
                                inference,
                            )
                            if report:
                                upload_record.processing_status = "completed"
                                db.session.commit()
                                succeeded_ids.append(image_id)
                            else:
                                upload_record.processing_status = "failed"
                                db.session.commit()
                                failed_ids.append(image_id)
                        except Exception as exc:
                            logger.error(f"Batch {batch_id}: failed {image_id}{exc}")
                            db.session.rollback()
                            upload_record.processing_status = "failed"
                            try:
                                db.session.commit()
                            except Exception:
                                db.session.rollback()
                            failed_ids.append(image_id)

                        processed += 1
                        self.update_state(
                            state="PROGRESS",
                            meta={
                                "batch_id": batch_id,
                                "user_id": user_id,
                                "status": "running",
                                "total": total,
                                "processed": processed,
                                "succeeded": len(succeeded_ids),
                                "failed_ids": list(failed_ids),
                                "image_ids": list(succeeded_ids),
                                "current_file": "",
                                "started_at": started_at,
                                "finished_at": None,
                                "error": None,
                                "temp_dir": temp_dir,
                            },
                        )
            else:
                for i, path_str in enumerate(dcm_paths, 1):
                    # Check if task was revoked (compat across Celery versions)
                    request_ctx = current_task.request
                    is_revoked = bool(getattr(request_ctx, "is_revoked", False)) or bool(
                        getattr(request_ctx, "revoked", False)
                    )
                    if is_revoked:
                        logger.info(f"Batch {batch_id} revoked, stopping")
                        break

                    path = Path(path_str)
                    image_id = path.stem

                    upload_record = ScreeningUpload(
                        user_id=user_id,
                        file_name=path.name,
                        original_filename=path.name,
                        file_size=path.stat().st_size if path.exists() else None,
                        file_path=str(path),
                        processing_status="processing",
                    )
                    db.session.add(upload_record)
                    db.session.commit()

                    # Update Celery task state with progress (matches _BATCHES format for frontend)
                    self.update_state(
                        state="PROGRESS",
                        meta={
                            "batch_id": batch_id,
                            "user_id": user_id,
                            "status": "running",
                            "total": total,
                            "processed": i - 1,
                            "succeeded": len(succeeded_ids),
                            "failed_ids": list(failed_ids),
                            "image_ids": list(succeeded_ids),
                            "current_file": image_id,
                            "started_at": started_at,
                            "finished_at": None,
                            "error": None,
                            "temp_dir": temp_dir,
                        },
                    )

                    try:
                        report, _ = _run_inference_on_dcm(path, user_id, upload_record.id)
                        if report:
                            upload_record.processing_status = "completed"
                            db.session.commit()
                            succeeded_ids.append(image_id)
                        else:
                            upload_record.processing_status = "failed"
                            db.session.commit()
                            failed_ids.append(image_id)
                    except Exception as e:
                        logger.error(f"Batch {batch_id}: failed {image_id}{e}")
                        db.session.rollback()
                        upload_record.processing_status = "failed"
                        try:
                            db.session.commit()
                        except Exception:
                            db.session.rollback()
                        failed_ids.append(image_id)

                    # Update after processing each file
                    self.update_state(
                        state="PROGRESS",
                        meta={
                            "batch_id": batch_id,
                            "user_id": user_id,
                            "status": "running",
                            "total": total,
                            "processed": i,
                            "succeeded": len(succeeded_ids),
                            "failed_ids": list(failed_ids),
                            "image_ids": list(succeeded_ids),
                            "current_file": "",
                            "started_at": started_at,
                            "finished_at": None,
                            "error": None,
                            "temp_dir": temp_dir,
                        },
                    )

        # Cleanup temporary directory if provided
        if temp_dir and Path(temp_dir).exists():
            try:
                shutil.rmtree(temp_dir, ignore_errors=True)
                logger.info(f"Cleaned up temp_dir: {temp_dir}")
            except Exception as e:
                logger.warning(f"Failed to clean temp_dir {temp_dir}: {e}")

        # Log final audit result
        with app.app_context():
            audit_status = "success" if len(failed_ids) == 0 else "partial"
            log_audit(
                "batch_processing_completed",
                user_id=user_id,
                details=f"batch_id={batch_id}, processed={total}, succeeded={len(succeeded_ids)}, failed={len(failed_ids)}",
                status=audit_status,
            )

        # Return final result matching _BATCHES format for frontend compatibility
        result = {
            "batch_id": batch_id,
            "user_id": user_id,
            "status": "completed",
            "total": total,
            "processed": total,
            "succeeded": len(succeeded_ids),
            "failed_ids": list(failed_ids),
            "image_ids": list(succeeded_ids),
            "current_file": "",
            "started_at": started_at,
            "finished_at": _now_ist().isoformat(),
            "error": None,
            "temp_dir": temp_dir,
        }

        logger.info(
            f"Batch {batch_id} complete: {len(succeeded_ids)}/{total} succeeded, "
            f"{len(failed_ids)} failed"
        )
        return result

    except Exception as e:
        logger.error(f"Batch {batch_id} error: {e}", exc_info=True)
        with app.app_context():
            log_audit(
                "batch_processing_failed",
                user_id=user_id,
                details=f"batch_id={batch_id}, error={str(e)}",
                status="failure",
            )
        raise


@celery_app.task(name="tasks.health_check")
def health_check() -> str:
    """Simple health check task for monitoring."""
    return "Celery worker is healthy"

@celery_app.task
def cleanup_expired_otps():
    """Periodic task to delete expired OTPs from the database."""
    from app_new import app
    from models import db, PendingOtp, now_ist
    
    with app.app_context():
        try:
            deleted = PendingOtp.query.filter(PendingOtp.expires_at < now_ist()).delete()
            db.session.commit()
            if deleted > 0:
                logger.info("Cleaned up %d expired OTP rows.", deleted)
        except Exception as exc:
            db.session.rollback()
            logger.error("Error cleaning up OTPs: %s", exc)

celery_app.conf.beat_schedule = {
    'cleanup-expired-otps-every-15-mins': {
        'task': 'tasks.cleanup_expired_otps',
        'schedule': 900.0,  # 15 minutes in seconds
    },
}