Skip to content

Commit

Permalink
orm:新增NewSvcImplSilent方法,schedule:新增手动调用语法糖
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Jun 18, 2024
1 parent b52fc1a commit 6234e33
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
12 changes: 12 additions & 0 deletions orm/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"database/sql"

"github.com/keepchen/go-sail/v3/lib/db"
"github.com/keepchen/go-sail/v3/lib/logger"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -97,6 +98,17 @@ var NewSvcImpl = func(dbr *gorm.DB, dbw *gorm.DB, logger *zap.Logger) Svc {
}
}

// NewSvcImplSilent 初始化
//
// 使用默认的读写实例和日志对象
var NewSvcImplSilent = func() Svc {
return &SvcImpl{
dbr: db.GetInstance().R,
dbw: db.GetInstance().W,
logger: logger.GetLogger(),
}
}

func (a *SvcImpl) Model(value interface{}) Svc {
if a.tx == nil {
a.tx = a.dbw
Expand Down
4 changes: 2 additions & 2 deletions schedule/delay.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (j *taskJob) RunAfter(delay time.Duration) (cancel CancelFunc) {
timer := time.After(delay)
cancel = j.cancelFunc

wrappedTaskFunc := func() {
j.wrappedTaskFunc = func() {
j.running = true

defer func() {
Expand All @@ -41,7 +41,7 @@ func (j *taskJob) RunAfter(delay time.Duration) (cancel CancelFunc) {
for {
select {
case <-timer:
go wrappedTaskFunc()
go j.wrappedTaskFunc()
break LOOP
case <-j.cancelTaskChan:
break LOOP
Expand Down
4 changes: 2 additions & 2 deletions schedule/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (j *taskJob) run() {
go func() {
ticker := time.NewTicker(j.interval)
defer ticker.Stop()
wrappedTaskFunc := func() {
j.wrappedTaskFunc = func() {
j.running = true

defer func() {
Expand All @@ -47,7 +47,7 @@ func (j *taskJob) run() {
for {
select {
case <-ticker.C:
go wrappedTaskFunc()
go j.wrappedTaskFunc()
//收到退出信号,终止任务
case <-j.cancelTaskChan:
if j.withoutOverlapping && j.lockedByMe {
Expand Down
62 changes: 62 additions & 0 deletions schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type Scheduler interface {
type taskJob struct {
name string
task func()
wrappedTaskFunc func()
interval time.Duration
lockerKey string
lockedByMe bool
Expand Down Expand Up @@ -238,3 +239,64 @@ func JobIsRunning(jobName string) bool {

return running
}

// Call 手动启动任务
//
// jobName 任务名称
//
// mandatory 如果为true,将不检测堆叠状态而直接执行
//
// # Note
//
// 内部函数将被同步式的调用
func Call(jobName string, mandatory bool) {
var (
job *taskJob
lockerKey = generateJobNameKey(jobName)
)
taskSchedules.mux.RLock()
if jb, ok := taskSchedules.pool[lockerKey]; ok {
job = jb
}
taskSchedules.mux.RUnlock()
if job == nil {
fmt.Printf("[GO-SAIL] <Schedule> call job {%s} failed,cause job not registered.\n", job.name)
return
}
if mandatory {
job.task()
} else {
job.wrappedTaskFunc()
}
}

// MustCall 手动启动任务
//
// jobName 任务名称
//
// mandatory 如果为true,将不检测堆叠状态而直接执行
//
// # Note
//
// 1.若jobName在任务列表中不存在(如未注册或被取消),将panic
//
// 2.内部函数将被同步式的调用
func MustCall(jobName string, mandatory bool) {
var (
job *taskJob
lockerKey = generateJobNameKey(jobName)
)
taskSchedules.mux.RLock()
if jb, ok := taskSchedules.pool[lockerKey]; ok {
job = jb
}
taskSchedules.mux.RUnlock()
if job == nil {
panic(fmt.Errorf("job name: %s not registered", jobName))
}
if mandatory {
job.task()
} else {
job.wrappedTaskFunc()
}
}
4 changes: 2 additions & 2 deletions schedule/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (j *taskJob) RunAt(crontabExpr string) (cancel CancelFunc) {
})

//因为AddFunc内部是协程启动,因此这里的方法使用同步方式调用
wrappedTaskFunc := func() {
j.wrappedTaskFunc = func() {
j.running = true

defer func() {
Expand All @@ -56,7 +56,7 @@ func (j *taskJob) RunAt(crontabExpr string) (cancel CancelFunc) {
}
}

jobID, jobErr := cronJob.AddFunc(crontabExpr, wrappedTaskFunc)
jobID, jobErr := cronJob.AddFunc(crontabExpr, j.wrappedTaskFunc)
if jobErr != nil {
fmt.Printf("[GO-SAIL] <Schedule> add job {%s} failed: %v\n", j.name, jobErr.Error())
}
Expand Down

0 comments on commit 6234e33

Please sign in to comment.