Skip to content

Commit 8226866

Browse files
authored
🐛 fix: update task handling and improve error responses (#268)
1 parent 4c2a392 commit 8226866

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

apis/task.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package apis
22

33
import (
4+
"errors"
45
"log/slog"
56
"net/http"
67

7-
"github.com/mss-boot-io/mss-boot-admin/center"
8-
98
"github.com/gin-gonic/gin"
9+
"github.com/mss-boot-io/mss-boot-admin/center"
1010
"github.com/mss-boot-io/mss-boot-admin/config"
1111
"github.com/mss-boot-io/mss-boot-admin/dto"
1212
"github.com/mss-boot-io/mss-boot-admin/models"
13+
"github.com/mss-boot-io/mss-boot/core/server/task"
1314
"github.com/mss-boot-io/mss-boot/pkg/enum"
1415
"github.com/mss-boot-io/mss-boot/pkg/response"
1516
"github.com/mss-boot-io/mss-boot/pkg/response/actions"
1617
"github.com/mss-boot-io/mss-boot/pkg/response/controller"
18+
"gorm.io/gorm"
1719
)
1820

1921
/*
@@ -82,27 +84,37 @@ func (e *Task) Operate(c *gin.Context) {
8284
api.Err(http.StatusUnprocessableEntity)
8385
return
8486
}
85-
var count int64
86-
err := center.Default.GetDB(c, &models.Task{}).Model(&models.Task{}).Where("id = ?", req.ID).Count(&count).Error
87+
t := &models.Task{}
88+
err := center.Default.GetDB(c, &models.Task{}).
89+
Model(&models.Task{}).
90+
Where("id = ?", req.ID).
91+
First(t).Error
8792
if err != nil {
88-
api.AddError(err).Log.Error("count task error")
93+
if errors.Is(err, gorm.ErrRecordNotFound) {
94+
api.Err(http.StatusNotFound)
95+
return
96+
}
97+
api.AddError(err).Log.Error("get task error")
8998
api.Err(http.StatusInternalServerError)
9099
return
91100
}
92-
if count == 0 {
93-
api.Err(http.StatusNotFound)
94-
return
95-
}
96101
var status enum.Status
97102
switch req.Operate {
98103
case "start":
104+
err = task.UpdateJob(t.ID, t.Spec, t)
99105
status = enum.Enabled
100106
case "stop":
107+
err = task.RemoveJob(t.ID)
101108
status = enum.Disabled
102109
default:
103110
api.Err(http.StatusBadRequest, "operate not support")
104111
return
105112
}
113+
if err != nil {
114+
api.AddError(err).Log.Error("task operate error")
115+
api.Err(http.StatusInternalServerError)
116+
return
117+
}
106118

107119
err = center.Default.GetDB(c, &models.Task{}).Model(&models.Task{}).Where("id = ?", req.ID).Update("status", status).Error
108120
if err != nil {

cmd/server/server.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ func setup() error {
190190
// setup 06 task init
191191
if config.Cfg.Task.Enable {
192192
runnable = append(runnable,
193-
task.New(task.WithStorage(&models.TaskStorage{DB: gormdb.DB}), task.WithSchedule("task", config.Cfg.Task.Spec, &taskE{})))
193+
task.New(
194+
task.WithStorage(&models.TaskStorage{DB: gormdb.DB}),
195+
task.WithSchedule("task", config.Cfg.Task.Spec, &taskE{}),
196+
),
197+
)
194198
}
195199

196200
// setup 07 init virtual models
@@ -250,7 +254,7 @@ func (t *taskE) Run() {
250254
}
251255
}
252256
//check
253-
err = gormdb.DB.Where("provider = ?", models.TaskProviderDefault).Where("status = ?", enum.Enabled).Find(&tasks).Error
257+
err = gormdb.DB.Not("provider = ?", models.TaskProviderK8S).Where("status = ?", enum.Enabled).Find(&tasks).Error
254258
if err != nil {
255259
slog.Error("task run get tasks error", slog.Any("err", err))
256260
return

config/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ cache:
6666
- '*'
6767
memory: ''
6868
# redis:
69-
# addr: '127.0.0.1:6379'
69+
# addr: '127.0.0.1:6379'
70+
queue:
71+
memory:
72+
poolSize: 10

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674
1919
github.com/grafana/pyroscope-go v1.2.2
2020
github.com/larksuite/oapi-sdk-go/v3 v3.4.16
21-
github.com/mss-boot-io/mss-boot v0.5.1-0.20250427230712-554bc1183420
21+
github.com/mss-boot-io/mss-boot v0.5.1-0.20250503002006-869d064bbb88
2222
github.com/nsqio/go-nsq v1.1.0
2323
github.com/redis/go-redis/v9 v9.8.0
2424
github.com/robfig/cron/v3 v3.0.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8
498498
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
499499
github.com/mss-boot-io/mss-boot v0.5.1-0.20250427230712-554bc1183420 h1:rf7+hhmLuTkuJcg24yzVQavThuZKYl4pfzLNOq4Cgfg=
500500
github.com/mss-boot-io/mss-boot v0.5.1-0.20250427230712-554bc1183420/go.mod h1:/CeSAd5sHVMzPjT5yEl8yMUWwsw/xnVBi7mQy+qoJ04=
501+
github.com/mss-boot-io/mss-boot v0.5.1-0.20250503002006-869d064bbb88 h1:pwXYu72+eD6A11j0vWxy6XZpgt0NWH8upM0jm3sjCkw=
502+
github.com/mss-boot-io/mss-boot v0.5.1-0.20250503002006-869d064bbb88/go.mod h1:13tAJryxeyFpAMFvIe39C8jLvQBLLAckWee0yfh3X70=
501503
github.com/mss-boot-io/redisqueue/v2 v2.0.0-20240222064111-d36e396df7f9 h1:/YgpHiqgrxz/0+mKoETXu21c6/fyDt7/j9bdD3UkbdU=
502504
github.com/mss-boot-io/redisqueue/v2 v2.0.0-20240222064111-d36e396df7f9/go.mod h1:f/sISkwvecPq37zygQzWNj5ntJoP/jcoK0kPIjFTWFI=
503505
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=

models/task.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package models
22

33
import (
44
"bytes"
5+
"context"
56
"database/sql"
67
"encoding/json"
78
"errors"
@@ -39,6 +40,13 @@ const (
3940
TaskProviderFunc TaskProvider = "func"
4041
)
4142

43+
func init() {
44+
TaskFuncMap["test"] = func(ctx context.Context, args ...string) error {
45+
fmt.Println("task", time.Now(), args)
46+
return nil
47+
}
48+
}
49+
4250
// Task support http/grpc/script
4351
type Task struct {
4452
ModelGormTenant
@@ -210,7 +218,7 @@ func (t *Task) AfterUpdate(tx *gorm.DB) error {
210218

211219
func (t *Task) AfterDelete(tx *gorm.DB) error {
212220
switch t.Provider {
213-
case TaskProviderDefault, "":
221+
case TaskProviderDefault, "", TaskProviderFunc:
214222
return nil
215223
}
216224
clientSet := config.Cfg.Clusters.GetClientSet(t.Cluster)
@@ -383,15 +391,18 @@ func (t *TaskStorage) Remove(key string) error {
383391
}
384392
tk.EntryID = 0
385393
tk.CheckedAt = sql.NullTime{}
386-
return t.DB.Updates(tk).Error
394+
return t.DB.Model(tk).UpdateColumns(map[string]interface{}{
395+
"entry_id": 0,
396+
"checked_at": nil,
397+
}).Error
387398
}
388399

389400
func (t *TaskStorage) ListKeys() ([]string, error) {
390401
if t.DB == nil {
391402
return nil, fmt.Errorf("db is nil")
392403
}
393404
var tasks []*Task
394-
err := t.DB.Where("status = ?", enum.Enabled).Where("provider = ?", TaskProviderDefault).Find(&tasks).Error
405+
err := t.DB.Where("status = ?", enum.Enabled).Not("provider = ?", TaskProviderK8S).Find(&tasks).Error
395406
if err != nil {
396407
return nil, err
397408
}

0 commit comments

Comments
 (0)