File size: 3,093 Bytes
e3456a9
 
 
3f52ffc
e3456a9
 
c152fec
0831fe7
96c1494
e3456a9
c152fec
88099db
e3456a9
 
 
3f52ffc
 
 
 
 
 
 
 
 
 
 
 
 
e3456a9
 
0831fe7
e3456a9
c152fec
e3456a9
 
94f65ce
3f52ffc
 
 
 
 
0831fe7
fa36132
 
 
 
 
 
 
e3456a9
 
fa36132
e3456a9
 
fa36132
e3456a9
 
bde160e
 
 
e3456a9
0831fe7
 
bde160e
0831fe7
 
 
 
 
 
e3456a9
c152fec
df604d9
 
 
 
 
e3456a9
 
df604d9
0831fe7
 
 
e3456a9
 
 
 
 
c152fec
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
"""
Flare – Spark startup notifier
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Projeler açılırken Spark'a /startup çağrısı yapar.
"""

from __future__ import annotations
import threading, requests
from typing import Dict, Any, Optional
from utils import log
from config_provider import ConfigProvider, ProjectConfig, VersionConfig
import os

cfg = ConfigProvider.get()

def _get_spark_token() -> Optional[str]:
    """Get Spark token based on work_mode"""
    if cfg.global_config.is_cloud_mode():
        # Cloud mode - use HuggingFace Secrets
        token = os.getenv("SPARK_TOKEN")
        if not token:
            log("❌ SPARK_TOKEN not found in HuggingFace Secrets!")
        return token
    else:
        # On-premise mode - use .env file
        from dotenv import load_dotenv
        load_dotenv()
        return os.getenv("SPARK_TOKEN")

def _select_live_version(p: ProjectConfig) -> VersionConfig | None:
    """Yayınlanmış en güncel versiyonu getir."""
    published = [v for v in p.versions if v.published]
    return max(published, key=lambda v: v.id) if published else None

def notify_startup():
    spark_base = str(cfg.global_config.spark_endpoint).rstrip("/")
    spark_token = _get_spark_token()
    
    if not spark_token:
        log("⚠️ SPARK_TOKEN not configured. Skipping Spark startup notification.")
        return

    enabled_projects = [p for p in cfg.projects if p.enabled]
    
    if not enabled_projects:
        log("ℹ️ No enabled projects found for startup notification.")
        return

    for p in enabled_projects:
        if not p.enabled:
            continue
            
        v = _select_live_version(p)
        if not v:
            log(f"⚠️ No published version found for project '{p.name}', skipping startup.")
            continue

        plain = cfg.global_config.get_plain_token()
        log(f"🔑 Cloud-token starts with {plain[:6]}… (len={len(plain)})")
        
        body: Dict[str, Any] = {
            # ----- zorunlu alanlar -----
            "work_mode": cfg.global_config.work_mode,
            "cloud_token": plain,
            "project_name": p.name,
            "project_version": v.id,
            "repo_id": v.llm.repo_id,
            "generation_config": v.llm.generation_config,
            "use_fine_tune": v.llm.use_fine_tune,
            "fine_tune_zip": v.llm.fine_tune_zip,
        }

        headers = {
            "Authorization": f"Bearer {spark_token}",
            "Content-Type": "application/json"
        }
        
        try:
            log(f"🚀 Notifying Spark /startup for project '{p.name}' …")
            r = requests.post(f"{spark_base}/startup", json=body, headers=headers, timeout=10)
            if r.status_code >= 400:
                log(f"❌ Spark responded {r.status_code}: {r.text}")
                r.raise_for_status()
            log(f"✅ Spark acknowledged startup ({r.status_code})")
        except Exception as e:
            log(f"⚠️ Spark startup failed: {e}")

def run_in_thread():
    threading.Thread(target=notify_startup, daemon=True).start()