aboutsummaryrefslogtreecommitdiff
path: root/cmd/orodyagzou/main.go
blob: bc4d19feadb24e17cbdb1071a485bb23c9bda961 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"log/slog"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"sync"
	"time"

	"github.com/joho/godotenv"
	"within.website/x/internal"
	"within.website/x/web/vastai/vastaicli"
)

var (
	bind          = flag.String("bind", ":3238", "HTTP port to bind to")
	diskSizeGB    = flag.Int("vastai-disk-size-gb", 32, "amount of disk we need from vast.ai")
	dockerImage   = flag.String("docker-image", "reg.xeiaso.net/xeserv/waifuwave:latest", "docker image to start")
	onstartCmd    = flag.String("onstart-cmd", "/opt/comfyui/startup.sh", "onstart command to run in vast.ai")
	vastaiPort    = flag.Int("vastai-port", 8080, "port that the guest will use in vast.ai")
	vastaiFilters = flag.String("vastai-filters", "verified=True cuda_max_good>=12.1 gpu_ram>=24 num_gpus=1 inet_down>=2000", "vast.ai search filters")

	idleTimeout = flag.Duration("idle-timeout", 5*time.Minute, "how long the instance should be considered \"idle\" before it is slain")
)

func main() {
	internal.HandleStartup()
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	if flag.NArg() != 1 {
		fmt.Println("usage: orodyagzou [flags] <whatever.env>")
		os.Exit(2)
	}

	fname := flag.Arg(0)
	slog.Debug("using env file", "fname", fname)

	env, err := godotenv.Read(fname)
	if err != nil {
		slog.Error("can't read env file", "fname", fname, "err", err)
		os.Exit(1)
	}

	var cfg vastaicli.InstanceConfig

	cfg.DiskSizeGB = *diskSizeGB
	cfg.Environment = env
	cfg.DockerImage = *dockerImage
	cfg.OnStartCMD = *onstartCmd
	cfg.Ports = append(cfg.Ports, *vastaiPort)

	images := &ScaleToZeroProxy{
		cfg: cfg,
	}

	go images.slayLoop(ctx)

	mux := http.NewServeMux()
	mux.Handle("/", images)

	fmt.Printf("http://localhost%s\n", *bind)
	log.Fatal(http.ListenAndServe(*bind, mux))
}

type ScaleToZeroProxy struct {
	cfg vastaicli.InstanceConfig

	// locked fields
	lock        sync.RWMutex
	endpointURL string
	instanceID  int
	ready       bool
	lastUsed    time.Time
}

func (s *ScaleToZeroProxy) slayLoop(ctx context.Context) {
	t := time.NewTicker(time.Second)
	defer t.Stop()

	for {
		select {
		case <-ctx.Done():
			slog.Error("context canceled", "err", ctx.Err())
			return
		case <-t.C:
			s.lock.RLock()
			ready := s.ready
			lastUsed := s.lastUsed
			s.lock.RUnlock()

			if !ready {
				continue
			}

			if lastUsed.Add(*idleTimeout).Before(time.Now()) {
				if err := s.slay(ctx); err != nil {
					slog.Error("can't slay instance", "err", err)
				}
			}
		}
	}
}

func (s *ScaleToZeroProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	s.lock.RLock()
	ready := s.ready
	s.lock.RUnlock()

	if !ready {
		if err := s.mint(r.Context()); err != nil {
			slog.Error("can't mint", "err", err)
			http.Error(w, "can't mint", http.StatusInternalServerError)
			return
		}
	}

	s.lock.RLock()
	endpointURL := s.endpointURL
	s.lock.RUnlock()

	u, err := url.Parse(endpointURL)
	if err != nil {
		slog.Error("can't url parse", "err", err, "url", s.endpointURL)
		http.Error(w, "can't url parse", http.StatusInternalServerError)
		return
	}

	next := httputil.NewSingleHostReverseProxy(u)
	next.ServeHTTP(w, r)

	s.lock.Lock()
	s.lastUsed = time.Now()
	s.lock.Unlock()
}

func (s *ScaleToZeroProxy) mint(ctx context.Context) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	candidates, err := vastaicli.Search(ctx, *vastaiFilters, "dph+")
	if err != nil {
		return err
	}

	candidate := candidates[0]
	slog.Info("found instance", "costDPH", candidate.DphTotal, "gpuName", candidate.GpuName)

	instanceData, err := vastaicli.Mint(ctx, candidate.AskContractID, s.cfg)
	if err != nil {
		return err
	}

	slog.Info("created instance, waiting for things to settle", "id", instanceData.NewContract)

	instance, err := s.delayUntilRunning(ctx, instanceData.NewContract)
	if err != nil {
		return err
	}

	addr, ok := instance.AddrFor(s.cfg.Ports[0])
	if !ok {
		return fmt.Errorf("somehow can't get port %d for instance %d, is god dead?", s.cfg.Ports[0], instance.ID)
	}

	s.endpointURL = "http://" + addr + "/"
	s.ready = true
	s.instanceID = instance.ID
	s.lastUsed = time.Now().Add(5 * time.Minute)

	if err := s.delayUntilReady(ctx, s.endpointURL); err != nil {
		return fmt.Errorf("can't do healthcheck: %w", err)
	}

	slog.Info("ready", "endpointURL", s.endpointURL, "instanceID", s.instanceID)

	return nil
}

func (s *ScaleToZeroProxy) slay(ctx context.Context) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	if err := vastaicli.Slay(ctx, s.instanceID); err != nil {
		return err
	}

	s.endpointURL = ""
	s.ready = false
	s.lastUsed = time.Now()
	s.instanceID = 0

	slog.Info("instance slayed", "docker_image", s.cfg.DockerImage)

	return nil
}

func (s *ScaleToZeroProxy) delayUntilReady(ctx context.Context, endpointURL string) error {
	type cogHealthCheck struct {
		Status string `json:"status"`
	}

	u, err := url.Parse(endpointURL)
	if err != nil {
		return fmt.Errorf("[unexpected] can't parse endpoint url %q: %w", endpointURL, err)
	}

	u.Path = "/health-check"

	t := time.NewTicker(time.Second)
	defer t.Stop()

	failCount := 0

	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-t.C:
			if failCount >= 100 {
				return fmt.Errorf("healthcheck failed %d times", failCount+1)
			}

			resp, err := http.Get(u.String())
			if err != nil {
				slog.Error("health check failed", "err", err)
				continue
			}

			var status cogHealthCheck
			if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
				return fmt.Errorf("can't parse health check response: %w", err)
			}

			if status.Status == "READY" {
				slog.Info("health check passed")
				return nil
			}
		}
	}
}

func (s *ScaleToZeroProxy) delayUntilRunning(ctx context.Context, instanceID int) (*vastaicli.Instance, error) {
	t := time.NewTicker(10 * time.Second)
	defer t.Stop()

	var instance *vastaicli.Instance
	var err error

	for {
		select {
		case <-