Skip to content

Commit

Permalink
refactor: Move RunPreflightChecks function to preflight package
Browse files Browse the repository at this point in the history
Co-authored-by: logan <[email protected]>
  • Loading branch information
bgins and noryev committed Feb 3, 2025
1 parent b6342c6 commit 4582da5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/resourceprovider/preflight/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type dockerInfo struct {
Runtimes map[string]interface{} `json:"Runtimes"`
}

func (p *PreflightChecker) CheckDockerRuntime(ctx context.Context) CheckResult {
func (p *preflightChecker) CheckDockerRuntime(ctx context.Context) CheckResult {
cmd := exec.CommandContext(ctx, "docker", "info", "--format", "{{json .}}")
output, err := cmd.Output()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/resourceprovider/preflight/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func parseGPURecord(record string) (*GPUInfo, error) {
return gpu, nil
}

func (p *PreflightChecker) GetGPUInfo(ctx context.Context) ([]GPUInfo, error) {
func (p *preflightChecker) GetGPUInfo(ctx context.Context) ([]GPUInfo, error) {
if err := checkNvidiaSMI(); err != nil {
return nil, fmt.Errorf("nvidia-smi not available: %w", err)
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (p *PreflightChecker) GetGPUInfo(ctx context.Context) ([]GPUInfo, error) {
return gpus, nil
}

func (p *PreflightChecker) CheckGPU(ctx context.Context, config *GPUCheckConfig) CheckResult {
func (p *preflightChecker) CheckGPU(ctx context.Context, config *GPUCheckConfig) CheckResult {
if !config.Required {
// Attempt to retrieve GPU info
gpus, err := p.GetGPUInfo(ctx)
Expand Down
29 changes: 27 additions & 2 deletions pkg/resourceprovider/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package preflight
import (
"context"
"fmt"

"github.com/rs/zerolog/log"
)

const RequiredGPUMemoryGB = 1 // 1GB of VRAM is required to startup if GPU is enabled
Expand All @@ -29,11 +31,34 @@ type PreflightConfig struct {
}
}

type PreflightChecker struct {
type preflightChecker struct {
gpuInfo []GPUInfo
}

func (p *PreflightChecker) RunAllChecks(ctx context.Context, config PreflightConfig) error {
func RunPreflightChecks(ctx context.Context, config PreflightConfig) error {
log.Info().Msg("Starting preflight checks...")
checker := &preflightChecker{}

// Logging GPU requirements
gpuInfo, err := checker.GetGPUInfo(ctx)
if err != nil {
log.Warn().Err(err).Msg("⚠️ No GPU detected - will operate in CPU-only mode")
} else {
log.Info().
Int("gpu_count", len(gpuInfo)).
Int64("min_memory_gb", config.GPU.MinMemoryGB).
Msg("🎮 GPU requirements")
}

err = checker.RunAllChecks(ctx, config)
if err != nil {
log.Error().Err(err).Msg("❌ Preflight checks failed")
return err
}
return nil
}

func (p *preflightChecker) RunAllChecks(ctx context.Context, config PreflightConfig) error {

gpuResult := p.CheckGPU(ctx, &GPUCheckConfig{
MinMemory: config.GPU.MinMemoryGB * 1024 * 1024 * 1024,
Expand Down
25 changes: 1 addition & 24 deletions pkg/resourceprovider/resourceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewResourceProvider(
executor executor.Executor,
tracer trace.Tracer,
) (*ResourceProvider, error) {
if err := runPreflightChecks(context.Background(), options.Preflight); err != nil {
if err := preflight.RunPreflightChecks(context.Background(), options.Preflight); err != nil {
return nil, fmt.Errorf("preflight checks failed: %w", err)
}

Expand All @@ -108,29 +108,6 @@ func NewResourceProvider(
return solver, nil
}

func runPreflightChecks(ctx context.Context, config preflight.PreflightConfig) error {
log.Info().Msg("Starting preflight checks...")
checker := &preflight.PreflightChecker{}

// Logging GPU requirements
gpuInfo, err := checker.GetGPUInfo(ctx)
if err != nil {
log.Warn().Err(err).Msg("⚠️ No GPU detected - will operate in CPU-only mode")
} else {
log.Info().
Int("gpu_count", len(gpuInfo)).
Int64("min_memory_gb", config.GPU.MinMemoryGB).
Msg("🎮 GPU requirements")
}

err = checker.RunAllChecks(ctx, config)
if err != nil {
log.Error().Err(err).Msg("❌ Preflight checks failed")
return err
}
return nil
}

func (resourceProvider *ResourceProvider) Start(ctx context.Context, cm *system.CleanupManager) chan error {
if !resourceProvider.options.Pow.DisablePow {
if errCh := resourceProvider.StartMineLoop(ctx); errCh != nil {
Expand Down

0 comments on commit 4582da5

Please sign in to comment.