RWKV-Runner/backend-golang/app.go

161 lines
3.2 KiB
Go
Raw Normal View History

2023-05-05 23:23:34 +08:00
package backend_golang
2023-05-03 23:38:54 +08:00
import (
2023-08-27 22:53:18 +08:00
"bufio"
2023-05-03 23:38:54 +08:00
"context"
2023-06-20 22:22:14 +08:00
"errors"
2023-05-17 23:27:52 +08:00
"net/http"
2023-05-18 19:25:13 +08:00
"os"
"os/exec"
2023-06-01 16:54:21 +08:00
"path/filepath"
2023-05-20 23:34:33 +08:00
"runtime"
2023-08-27 22:53:18 +08:00
"syscall"
2023-05-17 23:27:52 +08:00
2023-07-03 17:41:47 +08:00
"github.com/fsnotify/fsnotify"
2023-05-17 23:27:52 +08:00
"github.com/minio/selfupdate"
2023-05-20 23:34:33 +08:00
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
2023-05-03 23:38:54 +08:00
)
// App struct
type App struct {
2023-06-20 22:22:14 +08:00
ctx context.Context
HasConfigData bool
ConfigData map[string]any
exDir string
cmdPrefix string
2023-05-03 23:38:54 +08:00
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
2023-05-05 23:23:34 +08:00
func (a *App) OnStartup(ctx context.Context) {
2023-05-03 23:38:54 +08:00
a.ctx = ctx
2023-06-02 22:20:57 +08:00
a.exDir = ""
2023-06-01 16:54:21 +08:00
a.cmdPrefix = ""
if runtime.GOOS == "darwin" {
2023-06-02 22:20:57 +08:00
ex, _ := os.Executable()
a.exDir = filepath.Dir(ex) + "/../../../"
2023-06-01 16:54:21 +08:00
a.cmdPrefix = "cd " + a.exDir + " && "
}
2023-05-20 13:00:08 +08:00
2023-08-27 22:53:18 +08:00
os.Chmod("./backend-rust/webgpu_server", 0777)
2023-07-09 12:10:14 +08:00
os.Mkdir(a.exDir+"models", os.ModePerm)
os.Mkdir(a.exDir+"lora-models", os.ModePerm)
os.Mkdir(a.exDir+"finetune/json2binidx_tool/data", os.ModePerm)
f, err := os.Create(a.exDir + "lora-models/train_log.txt")
if err == nil {
f.Close()
}
2023-05-20 13:00:08 +08:00
a.downloadLoop()
2023-08-27 22:53:18 +08:00
a.watchFs()
a.monitorHardware()
}
func (a *App) OnBeforeClose(ctx context.Context) bool {
if monitor != nil {
monitor.Process.Kill()
}
return false
}
2023-07-03 17:41:47 +08:00
2023-08-27 22:53:18 +08:00
func (a *App) watchFs() {
2023-07-03 17:41:47 +08:00
watcher, err := fsnotify.NewWatcher()
if err == nil {
watcher.Add("./lora-models")
watcher.Add("./models")
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
2023-08-27 22:53:18 +08:00
wruntime.EventsEmit(a.ctx, "fsnotify", event.Name)
2023-07-03 17:41:47 +08:00
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
}
2023-05-03 23:38:54 +08:00
}
2023-05-17 23:27:52 +08:00
2023-08-27 22:53:18 +08:00
var monitor *exec.Cmd
func (a *App) monitorHardware() {
if runtime.GOOS != "windows" {
return
}
monitor = exec.Command("./components/LibreHardwareMonitor.Console/LibreHardwareMonitor.Console.exe")
stdout, err := monitor.StdoutPipe()
if err != nil {
monitor = nil
return
}
go func() {
reader := bufio.NewReader(stdout)
for {
line, _, err := reader.ReadLine()
if err != nil {
wruntime.EventsEmit(a.ctx, "monitorerr", err.Error())
break
}
wruntime.EventsEmit(a.ctx, "monitor", string(line))
}
}()
2023-08-27 23:56:30 +08:00
monitor.SysProcAttr = &syscall.SysProcAttr{}
//go:custom_build windows monitor.SysProcAttr.HideWindow = true
2023-08-27 22:53:18 +08:00
monitor.Start()
}
2023-05-17 23:27:52 +08:00
func (a *App) UpdateApp(url string) (broken bool, err error) {
resp, err := http.Get(url)
if err != nil {
return false, err
}
defer resp.Body.Close()
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
if err != nil {
if rerr := selfupdate.RollbackError(err); rerr != nil {
return true, rerr
}
return false, err
}
if runtime.GOOS == "windows" {
name, err := os.Executable()
if err != nil {
return false, err
}
exec.Command(name, os.Args[1:]...).Start()
wruntime.Quit(a.ctx)
2023-05-18 19:25:13 +08:00
}
2023-05-17 23:27:52 +08:00
return false, nil
}
2023-05-20 23:34:33 +08:00
2023-06-20 22:22:14 +08:00
func (a *App) RestartApp() error {
if runtime.GOOS == "windows" {
name, err := os.Executable()
if err != nil {
return err
}
exec.Command(name, os.Args[1:]...).Start()
wruntime.Quit(a.ctx)
return nil
}
return errors.New("unsupported OS")
}
2023-05-20 23:34:33 +08:00
func (a *App) GetPlatform() string {
return runtime.GOOS
}