-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.go
85 lines (75 loc) · 1.99 KB
/
start.go
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
package main
import (
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/shirou/gopsutil/process"
)
const stableDiffusionStartTimeout = 30 * time.Second
const stableDiffusionPingInterval = 500 * time.Millisecond
func isStableDiffusionRunning() (bool, error) {
processes, err := process.Processes()
if err != nil {
return false, fmt.Errorf("can't check if stable diffusion is running: %s", err.Error())
}
for _, proc := range processes {
cmdline, err := proc.Cmdline()
if err != nil {
continue
}
if strings.Contains(cmdline, "python3 launch") {
return true, nil
}
}
return false, nil
}
func startStableDiffusionIfNeeded(ctx context.Context) error {
isRunning, err := isStableDiffusionRunning()
if err != nil {
return err
}
if isRunning {
fmt.Println("stable diffusion is already running")
} else {
fmt.Println("starting stable diffusion... ")
cmd := exec.Cmd{
Path: params.StableDiffusionWebUIPath,
Args: []string{params.StableDiffusionWebUIPath, "--api"},
Dir: filepath.Dir(params.StableDiffusionWebUIPath),
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("can't start stable diffusion: %s", err.Error())
}
}
fmt.Println("checking stable diffusion api...")
startedAt := time.Now()
var lastPingAt time.Time
for {
elapsedSinceLastPing := time.Since(lastPingAt)
if elapsedSinceLastPing < stableDiffusionPingInterval {
time.Sleep(stableDiffusionPingInterval - elapsedSinceLastPing)
}
_, _, err := sdAPI.GetProgress(ctx)
if err == nil {
break
}
if !errors.Is(err, syscall.ECONNREFUSED) {
if err.Error() == "Not Found" {
return fmt.Errorf("can't start stable diffusion, api is not enabled")
}
return fmt.Errorf("can't start stable diffusion: %s", err.Error())
}
if time.Since(startedAt) > stableDiffusionStartTimeout {
return fmt.Errorf("can't start stable diffusion: ping timeout")
}
lastPingAt = time.Now()
fmt.Println(" ping...")
}
fmt.Println(" ok")
return nil
}