This commit is contained in:
che
2026-07-11 11:44:55 +05:00
commit 111cadd184
26 changed files with 4564 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package control_case
import (
"github.com/che4web/go4rest"
"gorm.io/gorm"
)
type ControlCaseController struct {
*go4rest.ViewSet[ControlCase]
db *gorm.DB
}
func NewControlCaseController(db *gorm.DB) *ControlCaseController {
return &ControlCaseController{
ViewSet: go4rest.NewViewSet[ControlCase](db),
db: db,
}
}
type ParamsController struct {
*go4rest.ViewSet[Params]
db *gorm.DB
}
func NewParamsController(db *gorm.DB) *ParamsController {
return &ParamsController{
ViewSet: go4rest.NewViewSet[Params](db),
db: db,
}
}
+92
View File
@@ -0,0 +1,92 @@
package control_case
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/otiai10/copy"
"gorm.io/gorm"
)
type ControlCase struct {
gorm.Model
Name string `json:"name"`
ParamsID uint `json:"params_id"`
Params Params `json:"params"`
Status string `json:"status"`
}
type Params struct {
gorm.Model
ID uint `toml:"-" csv:"id"`
Rel float64 `toml:"rel" csv:"rel"`
RelC float64 `toml:"rel_c" csv:"rel_c"`
Le float64 `toml:"le" csv:"le"`
Pr float64 `toml:"pr" csv:"pr"`
Pe float64 `toml:"pe" csv:"pe"`
InitialCondition string `toml:"-" csv:"initial_condition"`
Time float64 `toml:"time" csv:"time"`
FolderPath string `toml:"-" csv:"-"`
}
func (p *Params) ToTOML(filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
encoder := toml.NewEncoder(file)
return encoder.Encode(p)
}
func (u *Params) AfterCreate(tx *gorm.DB) (err error) {
control_case := ControlCase{ParamsID: u.ID, Status: "N"}
tx.Create(&control_case)
return
}
func (p *Params) Exist() (bool, error) {
folderPath := filepath.Join("./", fmt.Sprintf("%d", p.ID))
return exists(folderPath)
}
func (p *Params) CreateFolder() {
BIN_PATH := "./bio-convect"
folderPath := filepath.Join("./", fmt.Sprintf("%d", p.ID))
os.MkdirAll(folderPath, os.ModePerm)
fmt.Printf("folder: %s\n", folderPath)
execPath := filepath.Join("./", folderPath, BIN_PATH)
err := copy.Copy(BIN_PATH, execPath)
if err != nil {
log.Fatal(err)
}
if p.InitialCondition != "E" {
startPath := filepath.Join("./", folderPath, "storag.h5")
err = copy.Copy(p.InitialCondition, startPath)
if err != nil {
log.Fatal(err)
}
}
p.ToTOML(filepath.Join(folderPath, "config.toml"))
p.FolderPath = folderPath
}
func (p *Params) Run(ctx context.Context) {
BIN_PATH := "./bio-convect"
cmd := exec.CommandContext(ctx, BIN_PATH)
cmd.Dir = p.FolderPath
//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
fmt.Printf("before run")
err := cmd.Run()
fmt.Printf("afrer run")
if err != nil {
log.Fatal(err)
}
}
+22
View File
@@ -0,0 +1,22 @@
package control_case
import (
"github.com/che4web/go4rest"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func RegisterApp(r *gin.Engine, db *gorm.DB) {
db.AutoMigrate(&ControlCase{})
db.AutoMigrate(&Params{})
controller := NewControlCaseController(db)
params := NewParamsController(db)
go4rest.RegisterCRUDRoutes(r, "control_case", controller)
go4rest.RegisterCRUDRoutes(r, "params", params)
numJobs := 1
//jobs := make(chan Params, numJobs)
results := make(chan int, numJobs)
go RunControllWorker(db, results)
}
+115
View File
@@ -0,0 +1,115 @@
package control_case
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"sync"
"time"
"github.com/gocarina/gocsv"
"gorm.io/gorm"
)
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
func worker(id int, jobs <-chan Params, ctx context.Context) {
for j := range jobs {
fmt.Printf("Worker %d started job %d \n", id, j.ID)
j.CreateFolder()
j.Run(ctx)
time.Sleep(time.Second) // Имитация длительной задачи
fmt.Printf("Worker %d finished job %d \n", id, j.ID)
// results <- j * 2
}
}
func readCSVWithGocsv() []Params {
file, err := os.Open("db.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
var params []Params
if err := gocsv.UnmarshalFile(file, &params); err != nil {
log.Fatal(err)
}
for _, person := range params {
fmt.Printf("%+v\n", person)
}
return params
}
func readFromDb(db *gorm.DB) []ControlCase {
var cases []ControlCase
statuses := []string{"N"}
_ = db.
Where("status IN ?", statuses).
Preload("Params").
Find(&cases).Error
return cases
}
func RunControllWorker(db *gorm.DB, results chan int) {
const numJobs = 1
const numWorkers = 10
ctx, cancel := context.WithCancel(context.Background())
defer func() {
fmt.Printf("cencel")
cancel()
}() // Cancel if main exits early
jobs := make(chan Params, numJobs)
//results := make(chan int, numJobs)
// Запуск воркеров
var wg sync.WaitGroup
for w := 1; w <= numWorkers; w++ {
wg.Add(1)
go func(w int) {
defer wg.Done()
worker(w, jobs, ctx)
}(w)
}
//params := readCSVWithGocsv()
for {
cases := readFromDb(db)
// Отправка задач
for _, c := range cases {
p := c.Params
e, _ := p.Exist()
if e {
c.Status = "R"
db.Save((&c))
} else {
p.CreateFolder()
jobs <- p
}
}
time.Sleep(10 * time.Second)
}
// Ожидание завершения всех воркеров
//fmt.Printf("wait")
// wg.Wait()
// close(jobs)
//
// // Получение результатов
// for r := range results {
// fmt.Println("Result:", r)
// }
}