|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "strings" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "codex-queue-bot/internal/codex" |
| 14 | + "codex-queue-bot/internal/commands" |
| 15 | + "codex-queue-bot/internal/config" |
| 16 | + "codex-queue-bot/internal/hub" |
| 17 | + "codex-queue-bot/internal/jobs" |
| 18 | + "codex-queue-bot/internal/proxyenv" |
| 19 | +) |
| 20 | + |
| 21 | +var version = "dev" |
| 22 | + |
| 23 | +func main() { |
| 24 | + configDefault := os.Getenv("CONFIG_FILE") |
| 25 | + if configDefault == "" { |
| 26 | + configDefault = "config.json" |
| 27 | + } |
| 28 | + configPath := flag.String("config", configDefault, "path to JSON configuration") |
| 29 | + checkOnly := flag.Bool("check", false, "validate configuration, Codex executable, and prompts, then exit") |
| 30 | + showVersion := flag.Bool("version", false, "print version and exit") |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + if *showVersion { |
| 34 | + fmt.Println(version) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + logger := newLogger(os.Getenv("LOG_LEVEL")) |
| 39 | + slog.SetDefault(logger) |
| 40 | + proxyConfig := proxyenv.Apply() |
| 41 | + if proxyConfig.Enabled() { |
| 42 | + logger.Info( |
| 43 | + "outbound proxy enabled", |
| 44 | + "http", proxyConfig.HTTPProxy != "", |
| 45 | + "https", proxyConfig.HTTPSProxy != "", |
| 46 | + "all", proxyConfig.AllProxy != "", |
| 47 | + "no_proxy", proxyConfig.NoProxy != "", |
| 48 | + ) |
| 49 | + } |
| 50 | + cfg, err := config.Load(*configPath) |
| 51 | + if err != nil { |
| 52 | + logger.Error("configuration error", "error", err) |
| 53 | + os.Exit(2) |
| 54 | + } |
| 55 | + |
| 56 | + runner := &codex.Runner{ |
| 57 | + Binary: cfg.Codex.Binary, |
| 58 | + PromptsFile: cfg.Codex.PromptsFile, |
| 59 | + Timeout: cfg.RequestTimeout(), |
| 60 | + ReasoningEffort: cfg.Codex.ReasoningEffort, |
| 61 | + Overrides: cfg.Codex.ConfigOverrides, |
| 62 | + Logger: logger, |
| 63 | + } |
| 64 | + if err := runner.Check(); err != nil { |
| 65 | + logger.Error("Codex preflight failed", "error", err) |
| 66 | + os.Exit(2) |
| 67 | + } |
| 68 | + if *checkOnly { |
| 69 | + fmt.Printf("configuration OK: %d target(s), Codex=%s, prompts=%s\n", len(cfg.Codex.Targets), cfg.Codex.Binary, cfg.Codex.PromptsFile) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 74 | + defer stop() |
| 75 | + hubClient := hub.New(cfg.OpenILink.BaseURL, cfg.OpenILink.Token, cfg.HTTPTimeout(), logger) |
| 76 | + manager := jobs.New( |
| 77 | + ctx, |
| 78 | + cfg.Codex.Targets, |
| 79 | + runner, |
| 80 | + hubClient, |
| 81 | + logger, |
| 82 | + cfg.RetryMin(), |
| 83 | + cfg.RetryMax(), |
| 84 | + cfg.Codex.MaxParallel, |
| 85 | + cfg.Codex.SuccessMessage, |
| 86 | + ) |
| 87 | + handler := commands.New(manager, hubClient, logger, cfg.OpenILink.AllowedUserIDs) |
| 88 | + |
| 89 | + logger.Info( |
| 90 | + "Codex queue bot started", |
| 91 | + "version", version, |
| 92 | + "openilink", cfg.OpenILink.BaseURL, |
| 93 | + "targets", strings.Join(manager.TargetNames(), ","), |
| 94 | + "max_parallel", cfg.Codex.MaxParallel, |
| 95 | + ) |
| 96 | + if err := hubClient.Run(ctx, handler.Handle); err != nil { |
| 97 | + logger.Error("OpenILink listener stopped", "error", err) |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | + logger.Info("Codex queue bot stopped") |
| 101 | +} |
| 102 | + |
| 103 | +func newLogger(rawLevel string) *slog.Logger { |
| 104 | + level := slog.LevelInfo |
| 105 | + switch strings.ToLower(strings.TrimSpace(rawLevel)) { |
| 106 | + case "debug": |
| 107 | + level = slog.LevelDebug |
| 108 | + case "warn", "warning": |
| 109 | + level = slog.LevelWarn |
| 110 | + case "error": |
| 111 | + level = slog.LevelError |
| 112 | + } |
| 113 | + return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level})) |
| 114 | +} |
0 commit comments