fix
This commit is contained in:
+41
-17
@@ -5,11 +5,12 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"control/analize"
|
||||
"github.com/gocarina/gocsv"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -25,11 +26,40 @@ func exists(path string) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
func worker(id int, jobs <-chan Params, ctx context.Context) {
|
||||
func worker(id int, jobs <-chan ControlCase, ctx context.Context, db *gorm.DB) {
|
||||
for j := range jobs {
|
||||
fmt.Printf("Worker %d started job %d \n", id, j.ID)
|
||||
j.CreateFolder()
|
||||
j.Run(ctx)
|
||||
p := j.Params
|
||||
p.FolderPath = filepath.Join(".", fmt.Sprintf("%d", p.ID))
|
||||
|
||||
exists, err := p.Exist()
|
||||
if err != nil {
|
||||
fmt.Printf("Worker %d failed to check folder for job %d: %v\n", id, j.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !exists {
|
||||
if err := p.CreateFolder(); err != nil {
|
||||
fmt.Printf("Worker %d failed to prepare job %d: %v\n", id, j.ID, err)
|
||||
continue
|
||||
}
|
||||
if err := p.Run(ctx); err != nil {
|
||||
fmt.Printf("Worker %d failed to run job %d: %v\n", id, j.ID, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
csvPath := filepath.Join(p.FolderPath, "foo.csv")
|
||||
if _, err := analize.AnalyzeCSV(db, j.ID, j.Name, csvPath); err != nil {
|
||||
fmt.Printf("Worker %d failed to analyze job %d: %v\n", id, j.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
j.Status = "R"
|
||||
if err := db.Model(&ControlCase{}).Where("id = ?", j.ID).Update("status", j.Status).Error; err != nil {
|
||||
fmt.Printf("Worker %d failed to update status for job %d: %v\n", id, j.ID, err)
|
||||
continue
|
||||
}
|
||||
time.Sleep(time.Second) // Имитация длительной задачи
|
||||
fmt.Printf("Worker %d finished job %d \n", id, j.ID)
|
||||
// results <- j * 2
|
||||
@@ -39,13 +69,15 @@ func worker(id int, jobs <-chan Params, ctx context.Context) {
|
||||
func readCSVWithGocsv() []Params {
|
||||
file, err := os.Open("db.csv")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Printf("failed to open csv: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var params []Params
|
||||
if err := gocsv.UnmarshalFile(file, ¶ms); err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Printf("failed to parse csv: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, person := range params {
|
||||
@@ -73,7 +105,7 @@ func RunControllWorker(db *gorm.DB, results chan int) {
|
||||
cancel()
|
||||
}() // Cancel if main exits early
|
||||
|
||||
jobs := make(chan Params, numJobs)
|
||||
jobs := make(chan ControlCase, numJobs)
|
||||
//results := make(chan int, numJobs)
|
||||
|
||||
// Запуск воркеров
|
||||
@@ -82,7 +114,7 @@ func RunControllWorker(db *gorm.DB, results chan int) {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
worker(w, jobs, ctx)
|
||||
worker(w, jobs, ctx, db)
|
||||
}(w)
|
||||
}
|
||||
|
||||
@@ -91,15 +123,7 @@ func RunControllWorker(db *gorm.DB, results chan int) {
|
||||
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
|
||||
}
|
||||
jobs <- c
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user