ifixkart-backend/run_import.py

81 lines
2.6 KiB
Python

import os
import sys
import uuid
import time
import shutil
import pymysql
# Load env variables before importing app modules
env_path = "/var/www/fastapi/ifixkart/.env"
if os.path.exists(env_path):
with open(env_path, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
k, v = line.split('=', 1)
os.environ[k.strip()] = v.strip().strip("'").strip('"')
os.environ.setdefault("SECRET_KEY", "b39f1c7d2e8a4059a1e048f39572b810d7a64115e62c1490b6338fa82110c79e")
os.environ.setdefault("PUBLIC_API_KEY", "ifixkart-public-api-key-2026-secret-v1")
sys.path.insert(0, "/var/www/fastapi/ifixkart")
from app.core.database.db_session import SessionLocal
from app.models.MigrationModel import MigrationBatch, MigrationJob, BatchTypeEnum, ImportModeEnum, JobStatusEnum, PhaseEnum, BatchStatusEnum
from app.services.migration_engine.migration_worker import MigrationWorker
from app.services.migration_engine.storage_manager import StorageManager
excel_path = "/var/www/html/DATASET/stress_L5_EXTREME_20260901_210610.xlsx"
if not os.path.exists(excel_path):
excel_path = "/mnt/fam/SERVER/IfixKartEcommerce/DATASET/stress_L5_EXTREME_20260901_210610.xlsx"
print(f"=== Initializing Direct Import for Dataset: {excel_path} ===")
db = SessionLocal()
batch_id = str(uuid.uuid4())
job_id = str(uuid.uuid4())
job_dir = StorageManager.get_job_dir(job_id)
batch = MigrationBatch(
id=batch_id,
batch_type=BatchTypeEnum.PRODUCTS,
user_id="admin-user-01",
import_mode=ImportModeEnum.UPSERT,
status=BatchStatusEnum.PENDING
)
db.add(batch)
dataset_dest = os.path.join(job_dir, "dataset", "stress_L5_EXTREME_20260901_210610.xlsx")
os.makedirs(os.path.dirname(dataset_dest), exist_ok=True)
shutil.copy2(excel_path, dataset_dest)
print(f"Copied dataset to job directory: {dataset_dest}")
job = MigrationJob(
id=job_id,
batch_id=batch_id,
job_type="PRODUCTS",
is_dry_run=False,
file_name="stress_L5_EXTREME_20260901_210610.xlsx",
file_format="XLSX",
status=JobStatusEnum.QUEUED,
current_phase=PhaseEnum.UPLOAD
)
db.add(job)
db.commit()
db.close()
print(f"Migration Job {job_id} created and QUEUED. Running worker thread...")
worker = MigrationWorker(worker_id=f"direct-worker-{job_id[:8]}")
db_worker = SessionLocal()
claimed = worker.claim_next_job(db_worker)
db_worker.close()
if claimed:
print(f"Claimed job {claimed[0]} with lease version {claimed[1]}. Executing process_job...")
worker.process_job(claimed[0], claimed[1])
print("=== Migration Pipeline Completed Successfully ===")
else:
print("Failed to claim job.")