Skip to content

Commit

Permalink
- update
Browse files Browse the repository at this point in the history
  • Loading branch information
dtapps committed Feb 3, 2024
1 parent 2954adc commit b3d05c6
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#### 安装

```shell
go get -v -u go.dtapp.net/gojobs@v1.0.123
go get -v -u go.dtapp.net/gojobs@v1.0.124
```
6 changes: 1 addition & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"github.com/redis/go-redis/v9"
"go.dtapp.net/golog"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
)
Expand All @@ -26,8 +25,6 @@ type Client struct {
systemOutsideIP string // 外网IP
goVersion string // go版本
sdkVersion string // sdk版本
logVersion string // log版本
redisSdkVersion string // redisSdk版本
}
redisConfig struct {
client *redis.Client // 数据库
Expand All @@ -49,8 +46,7 @@ type Client struct {
taskLogCollectionName string // 任务日志集合名
}
slog struct {
status bool // 状态
client *golog.SLog // 日志服务
status bool // 状态
}
}

Expand Down
12 changes: 0 additions & 12 deletions cofing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/redis/go-redis/v9"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"go.dtapp.net/golog"
"go.dtapp.net/gorequest"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -77,8 +76,6 @@ func (c *Client) setConfig(ctx context.Context, systemOutsideIP string) {
c.config.goVersion = runtime.Version() // go版本
c.config.sdkVersion = Version // sdk版本
c.config.systemVersion = info.SystemVersion // 系统版本
c.config.logVersion = golog.Version // log版本
c.config.redisSdkVersion = redis.Version() // redisSdk版本

}

Expand Down Expand Up @@ -172,12 +169,3 @@ func (c *Client) ConfigRedisClientFun(ctx context.Context, client *redis.Client,

return nil
}

// ConfigSLogClientFun 日志配置
func (c *Client) ConfigSLogClientFun(sLogFun golog.SLogFun) {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
c.slog.status = true
}
}
33 changes: 33 additions & 0 deletions cron_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gojobs

import (
"context"
"errors"
"fmt"
"go.dtapp.net/gotime"
"log"
"time"
)

func (c *Client) StartHandle(ctx context.Context, key any, overdue int64) error {
status, err := c.redisConfig.client.Get(ctx, fmt.Sprintf("%v", key)).Result()
if c.slog.status {
log.Println(fmt.Sprintf("【%v】查询 %v ; %s", fmt.Sprintf("%v", key), status, err))
}
if status != "" {
return errors.New("上次还在运行")
}

err = c.redisConfig.client.Set(ctx, fmt.Sprintf("%v", key), gotime.Current().Format(), time.Duration(overdue)*time.Second).Err()
if c.slog.status {
log.Println(fmt.Sprintf("【%v】设置 %s", fmt.Sprintf("%v", key), err))
}

return nil
}
func (c *Client) EndHandle(ctx context.Context, key any) {
err := c.redisConfig.client.Del(ctx, fmt.Sprintf("%v", key)).Err()
if c.slog.status {
log.Println(fmt.Sprintf("【%v】删除 %s", fmt.Sprintf("%v", key), err))
}
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil v3.21.11+incompatible
go.dtapp.net/goip v1.0.46
go.dtapp.net/golog v1.0.121
go.dtapp.net/gorequest v1.0.47
go.dtapp.net/gostring v1.0.13
go.dtapp.net/gotime v1.0.6
Expand All @@ -33,6 +32,7 @@ require (
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -44,7 +44,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/oschwald/geoip2-golang v1.9.0 // indirect
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
Expand All @@ -60,7 +59,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.dtapp.net/gojson v1.0.4 // indirect
go.dtapp.net/gorandom v1.0.2 // indirect
go.dtapp.net/gourl v1.0.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403 h1:EtZwYyLbkEcIt+B//6sujwRCnHuTEK3qiSypAX5aJeM=
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403/go.mod h1:mM6WvakkX2m+NgMiPCfFFjwfH4KzENC07zeGEqq9U7s=
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
Expand Down Expand Up @@ -88,8 +86,6 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
Expand Down Expand Up @@ -141,8 +137,6 @@ go.dtapp.net/goip v1.0.46 h1:QwCha6ZQ/NgFGS5fH9h7eQU/WOnAGcltR6eQnSpZQxo=
go.dtapp.net/goip v1.0.46/go.mod h1:n4ZgTUgfQVhXfxHTLAY/0mtLivI2VFcU+1ij1vz8VIs=
go.dtapp.net/gojson v1.0.4 h1:9en9iyOOLWoEIo2eKhqt3/Djh/3HhwsTpXxgI9efPRo=
go.dtapp.net/gojson v1.0.4/go.mod h1:G9CMVzNSRkbNzIic/vJqHCOyKtw6BW2YM8Vyn64zfM0=
go.dtapp.net/golog v1.0.121 h1:HMjLw64g3YnVULkYqzhycQZE5EaF83FhUoN5EJ9awoE=
go.dtapp.net/golog v1.0.121/go.mod h1:X0Q5X8A/UAAFT6c41q5ddAoHzEw5Hi6LttuPs8V/iuY=
go.dtapp.net/gorandom v1.0.2 h1:08BdcBP/fQiRw2Ii0OXATSTtObwNydlDlqc/j/u5O8Q=
go.dtapp.net/gorandom v1.0.2/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
go.dtapp.net/gorequest v1.0.47 h1:UnBMipHUg/oWrjtjo5JJ0Xb1V9VVfxXWtBd/EXlTl3M=
Expand All @@ -153,8 +147,6 @@ go.dtapp.net/gotime v1.0.6 h1:f8YowUxpZtJbYawe5s5PmvGxRj61ydlzxAPYr5Fcetg=
go.dtapp.net/gotime v1.0.6/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
go.dtapp.net/gotrace_id v1.0.8 h1:cNsPYzOz55B8SKit1smz5b03clr3CVaSCrFns9oOruU=
go.dtapp.net/gotrace_id v1.0.8/go.mod h1:tJzdWgm+8baWZa8K84GdiD99iPguXrn1CehFSh2W0OM=
go.dtapp.net/gourl v1.0.0 h1:Zbe0GiMFyyRy2+bjbVsYulakH5d58w3CDZkUPonlMoQ=
go.dtapp.net/gourl v1.0.0/go.mod h1:x9A/pJ3iKDTb6Gu2RtJy2iHg56IowXIcIGQdogqpGjs=
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down Expand Up @@ -213,10 +205,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
27 changes: 14 additions & 13 deletions gorm_model_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"go.dtapp.net/gotime"
"gorm.io/gorm"
"log"
"time"
)

Expand Down Expand Up @@ -46,7 +47,7 @@ func (c *Client) TaskTakeId(ctx context.Context, tx *gorm.DB, id uint) (result G
Take(&result).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("编号查询任务:%v", err))
log.Println(fmt.Sprintf("编号查询任务:%v", err))
}
}
return result
Expand All @@ -59,7 +60,7 @@ func (c *Client) TaskTake(ctx context.Context, tx *gorm.DB, customId string) (re
Take(&result).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("自定义编号查询任务:%v", err))
log.Println(fmt.Sprintf("自定义编号查询任务:%v", err))
}
}
return result
Expand All @@ -73,7 +74,7 @@ func (c *Client) taskTake(ctx context.Context, tx *gorm.DB, customId, status str
Take(&result).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("自定义编号加状态查询任务:%v", err))
log.Println(fmt.Sprintf("自定义编号加状态查询任务:%v", err))
}
}
return result
Expand Down Expand Up @@ -112,7 +113,7 @@ func (c *Client) TaskTypeTake(ctx context.Context, tx *gorm.DB, customId, Type s
Take(&result).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询单任务:%v", err))
log.Println(fmt.Sprintf("查询单任务:%v", err))
}
}
return result
Expand All @@ -126,7 +127,7 @@ func (c *Client) taskTypeTake(ctx context.Context, tx *gorm.DB, customId, Type,
Take(&result).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询单任务:%v", err))
log.Println(fmt.Sprintf("查询单任务:%v", err))
}
}
return result
Expand Down Expand Up @@ -165,7 +166,7 @@ func (c *Client) TaskFindAll(ctx context.Context, tx *gorm.DB, frequency int64)
Find(&results).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询多任务:%v", err))
log.Println(fmt.Sprintf("查询多任务:%v", err))
}
}
return results
Expand All @@ -180,7 +181,7 @@ func (c *Client) TaskFindAllType(ctx context.Context, tx *gorm.DB, Type string,
Find(&results).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询多任务:%v", err))
log.Println(fmt.Sprintf("查询多任务:%v", err))
}
}
return results
Expand All @@ -195,7 +196,7 @@ func (c *Client) taskFindAll(ctx context.Context, tx *gorm.DB, frequency int64,
Find(&results).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询多任务:%v", err))
log.Println(fmt.Sprintf("查询多任务:%v", err))
}
}
return results
Expand All @@ -211,7 +212,7 @@ func (c *Client) taskFindAllType(ctx context.Context, tx *gorm.DB, Type string,
Find(&results).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询多任务:%v", err))
log.Println(fmt.Sprintf("查询多任务:%v", err))
}
}
return results
Expand All @@ -224,7 +225,7 @@ func (c *Client) taskFindAllType(ctx context.Context, tx *gorm.DB, Type string,
Find(&results).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("查询多任务:%v", err))
log.Println(fmt.Sprintf("查询多任务:%v", err))
}
}
return results
Expand Down Expand Up @@ -290,7 +291,7 @@ func (c *Client) StartTask(ctx context.Context, tx *gorm.DB, id uint) error {
}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("任务启动失败:%v", err))
log.Println(fmt.Sprintf("任务启动失败:%v", err))
}
}
return err
Expand All @@ -309,7 +310,7 @@ func (c *Client) StartTaskCustom(ctx context.Context, tx *gorm.DB, customId stri
}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("任务启动自定义失败:%v", err))
log.Println(fmt.Sprintf("任务启动自定义失败:%v", err))
}
}
return err
Expand All @@ -331,7 +332,7 @@ func (c *Client) UpdateFrequency(ctx context.Context, tx *gorm.DB, id uint, freq
}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("更新任务频率失败:%v", err))
log.Println(fmt.Sprintf("更新任务频率失败:%v", err))
}
}
return err
Expand Down
17 changes: 9 additions & 8 deletions gorm_model_task_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"go.dtapp.net/gotime"
"log"
"time"
)

Expand Down Expand Up @@ -50,7 +51,7 @@ func (c *Client) GormTaskLogDelete(ctx context.Context, hour int64) error {
Delete(&GormModelTaskLog{}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("删除失败:%s", err))
log.Println(fmt.Sprintf("删除失败:%s", err))
}
}
return err
Expand All @@ -66,7 +67,7 @@ func (c *Client) GormTaskLogInDelete(ctx context.Context, hour int64) error {
Delete(&GormModelTaskLog{}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("删除失败:%s", err))
log.Println(fmt.Sprintf("删除失败:%s", err))
}
}
return err
Expand All @@ -82,7 +83,7 @@ func (c *Client) GormTaskLogSuccessDelete(ctx context.Context, hour int64) error
Delete(&GormModelTaskLog{}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("删除失败:%s", err))
log.Println(fmt.Sprintf("删除失败:%s", err))
}
}
return err
Expand All @@ -98,7 +99,7 @@ func (c *Client) GormTaskLogErrorDelete(ctx context.Context, hour int64) error {
Delete(&GormModelTaskLog{}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("删除失败:%s", err))
log.Println(fmt.Sprintf("删除失败:%s", err))
}
}
return err
Expand All @@ -114,7 +115,7 @@ func (c *Client) GormTaskLogTimeoutDelete(ctx context.Context, hour int64) error
Delete(&GormModelTaskLog{}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("删除失败:%s", err))
log.Println(fmt.Sprintf("删除失败:%s", err))
}
}
return err
Expand All @@ -131,7 +132,7 @@ func (c *Client) GormTaskLogWaitDelete(ctx context.Context, hour int64) error {
Delete(&GormModelTaskLog{}).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("删除失败:%s", err))
log.Println(fmt.Sprintf("删除失败:%s", err))
}
}
return err
Expand Down Expand Up @@ -162,10 +163,10 @@ func (c *Client) GormTaskLogRecord(ctx context.Context, task GormModelTask, runI
Create(&taskLog).Error
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("记录失败:%s", err))
log.Println(fmt.Sprintf("记录失败:%s", err))
}
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("记录数据:%+v", taskLog))
log.Println(fmt.Sprintf("记录数据:%+v", taskLog))
}
}
}
5 changes: 3 additions & 2 deletions mongo_model_task_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)

// MongoModelTaskLog 任务日志
Expand Down Expand Up @@ -91,10 +92,10 @@ func (c *Client) MongoTaskLogRecord(ctx context.Context, task GormModelTask, run
InsertOne(ctx, taskLog)
if err != nil {
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("记录失败:%s", err))
log.Println(fmt.Sprintf("记录失败:%s", err))
}
if c.slog.status {
c.slog.client.WithTraceId(ctx).Error(fmt.Sprintf("记录数据:%+v", taskLog))
log.Println(fmt.Sprintf("记录数据:%+v", taskLog))
}
}
}
Loading

0 comments on commit b3d05c6

Please sign in to comment.