-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
298 lines (248 loc) · 7.27 KB
/
Copy pathqueue.go
File metadata and controls
298 lines (248 loc) · 7.27 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
"github.com/gorilla/websocket"
)
var (
// WebSocket upgrader
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
// Connected WebSocket clients
clients = make(map[*websocket.Conn]bool)
clientsMu sync.Mutex
// Job is a struct containing job data
queue = make(chan QueueItem, 255)
// API Endpoints
api_txt2img = "/sdapi/v1/txt2img"
api_progress = "/sdapi/v1/progress"
)
type ConfigItem struct {
ID int `db:"id" json:"id"`
Data string `db:"data" json:"data"`
}
func ProcessQueue2() {
// Im guessing its done, i dont know
_, err := db.Exec("UPDATE stable_diffusion_queue SET status='done' WHERE status='processing'")
if err != nil {
log.Fatalf("Update error %v\n", err)
return
}
current_pool := []QueueItem{}
err = db.Select(¤t_pool, "SELECT * FROM stable_diffusion_queue WHERE status='pending'")
// TODO: Status should be enum
if err != nil {
log.Fatalf("Fetching error %v\n", err)
return
}
for _, item := range current_pool {
queue <- item
}
for job := range queue {
updateQueueItem(job.WithStatus("processing"))
if err := handleGen(job); err != nil {
updateQueueItem(job.WithStatus("pending"))
log.Fatalf("Generation error %v\n", err)
} else {
updateQueueItem(job.WithStatus("done"))
log.Printf("Done!\n")
}
}
}
func handleGen(item QueueItem) error {
log.Printf("Submitting image...\n")
config := make(map[string]interface{})
var jsonData []byte
err := db.Get(&jsonData, "SELECT data FROM stable_diffusion_config")
if err != nil || len(jsonData) == 0 {
return fmt.Errorf("empty config: %v", err)
}
// Unmarshal into map
if err = json.Unmarshal(jsonData, &config); err != nil || config == nil {
return fmt.Errorf("error unmarshaling JSON: %v", err)
}
config["prompt"] = item.Prompt
config["n_iter"] = item.BatchCount
// Marshal into data
if jsonData, err = json.Marshal(&config); err != nil {
return fmt.Errorf("error marshaling JSON: %v", err)
}
_, err = http.Post(sd_url+api_txt2img, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("error unmarshaling JSON: %v", err)
}
return nil
}
func handleSysConfig(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid method", http.StatusBadRequest)
log.Printf("Invalid method\n")
return
}
json.NewEncoder(w).Encode(cfg)
}
func handleConfig(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
bytedata, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Read error", http.StatusInternalServerError)
log.Printf("read error %v\n", err)
return
}
// Use a query to either insert or update the single row
query := `REPLACE INTO stable_diffusion_config (id, data) VALUES (0, ?)`
// TODO: This is a bad validation hack, do better
if len(bytedata) < 10 {
http.Error(w, "Too small", http.StatusBadRequest)
log.Printf("Too small %v\n", err)
return
}
// Execute the query
_, err = db.Exec(query, string(bytedata))
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
log.Printf("database error %v\n", err)
return
}
w.WriteHeader(http.StatusOK)
case http.MethodGet:
var item ConfigItem
err := db.Get(&item, "SELECT * FROM stable_diffusion_config")
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
log.Printf("database error %v\n", err)
return
}
var citem any
if err := json.NewDecoder(strings.NewReader(item.Data)).Decode(&citem); err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
log.Printf("Database error %v\n", err)
return
}
json.NewEncoder(w).Encode(citem)
default:
http.Error(w, "Invalid method", http.StatusMethodNotAllowed)
return
}
}
func handleStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid method", http.StatusBadRequest)
log.Printf("Invalid method\n")
return
}
resp, err := http.Get(sd_url + api_progress)
if err != nil {
http.Error(w, "Fetching status error", http.StatusInternalServerError)
log.Printf("Fetching status error %v\n", err)
return
}
if _, err := io.Copy(w, resp.Body); err != nil {
http.Error(w, "Failed to copy response body", http.StatusInternalServerError)
log.Printf("error copying response body: %v\n", err)
return
}
}
func handleQueue(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
var item QueueItem
if err := json.NewDecoder(r.Body).Decode(&item); err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
log.Printf("Database error %v\n", err)
return
}
if item.BatchCount > cfg.Batches {
log.Printf("WARNING: Batch count was bigger than allowed (%d > %d), clamping...\n", item.BatchCount, cfg.Batches)
item.BatchCount = cfg.Batches
}
err := db.Get(&item, "INSERT INTO stable_diffusion_queue (prompt, batch_count) VALUES (?, ?) RETURNING *", item.Prompt, item.BatchCount)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
log.Printf("Database error %v\n", err)
return
}
log.Printf("Add to queue... %v\n", item.ID)
queue <- item
json.NewEncoder(w).Encode(item)
case http.MethodGet:
queue := []QueueItem{}
err := db.Select(&queue, "SELECT * FROM stable_diffusion_queue")
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
log.Printf("Database error %v\n", err)
return
}
json.NewEncoder(w).Encode(queue)
case http.MethodDelete:
var item QueueItem
if err := json.NewDecoder(r.Body).Decode(&item); err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
log.Printf("Database error %v\n", err)
return
}
_, err := db.Exec("DELETE FROM stable_diffusion_queue WHERE id = ?", item.ID)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
log.Printf("Database error %v\n", err)
return
}
w.WriteHeader(http.StatusOK)
}
}
// WebSocket handler to register clients
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
return
}
// Register the client
clientsMu.Lock()
clients[conn] = true
clientsMu.Unlock()
// Handle disconnection
defer func() {
clientsMu.Lock()
delete(clients, conn)
clientsMu.Unlock()
conn.Close()
}()
// Keep the connection alive
for {
_, _, err := conn.ReadMessage()
if err != nil {
break
}
}
}
// Function to broadcast updates to all connected clients
func broadcastUpdate(update QueueItem) {
clientsMu.Lock()
defer clientsMu.Unlock()
message, _ := json.Marshal(update)
for conn := range clients {
if err := conn.WriteMessage(websocket.TextMessage, message); err != nil {
conn.Close()
delete(clients, conn)
}
}
}
func updateQueueItem(item QueueItem) error {
_, err := db.Exec("UPDATE stable_diffusion_queue SET status = ?, result_url = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?", item.Status, item.ResultURL, item.ID)
if err != nil {
return fmt.Errorf("database error %v", err)
}
// Broadcast the update
// TODO: Consider sending them all
broadcastUpdate(item)
return nil
}