- update only

master
李光春 2 years ago
parent 651ee16aef
commit ecec124fdb

@ -39,8 +39,6 @@ github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQ
github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.42.27/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc=
github.com/aws/aws-sdk-go v1.44.41 h1:FNW3Tb8vKvXLZ7lzGlg/dCAXhK4RC5fyFewD11oJhUM=
github.com/aws/aws-sdk-go v1.44.41/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.44.42 h1:sPkafCTLh2diZtDojetwbhU7QWQljYvc3PRjnrgKFlE=
github.com/aws/aws-sdk-go v1.44.42/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
@ -722,8 +720,6 @@ golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220622184535-263ec571b305 h1:dAgbJ2SP4jD6XYfMNLVj0BF21jo2PjChrtGaAvF5M3I=
golang.org/x/net v0.0.0-20220622184535-263ec571b305/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@ -782,8 +778,6 @@ golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664 h1:wEZYwx+kK+KlZ0hpvP2Ls1Xr4+RWnlzGFwPP0aiDjIU=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
@ -845,8 +839,6 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
google.golang.org/genproto v0.0.0-20220623142657-077d458a5694 h1:itnFmgk4Ls5nT+mYO2ZK6F0DpKsGZLhB5BB9y5ZL2HA=
google.golang.org/genproto v0.0.0-20220623142657-077d458a5694/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA=
google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f h1:hJ/Y5SqPXbarffmAsApliUlcvMU+wScNGfyop4bZm8o=
google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=

@ -3,7 +3,7 @@ package gojobs
import (
"context"
"go.dtapp.net/library/utils/gojobs/pb"
"go.dtapp.net/library/utils/gouuid"
"go.dtapp.net/library/utils/only"
"google.golang.org/grpc"
)
@ -52,7 +52,7 @@ func NewWorker(config *WorkerConfig) *Worker {
// SubscribeCron 订阅服务
func (w *Worker) SubscribeCron() pb.PubSub_SubscribeClient {
stream, err := w.Pub.Subscribe(context.Background(), &pb.SubscribeRequest{
Id: gouuid.GetUuId(),
Id: only.GetUuId(),
Value: prefix,
Ip: w.ClientIp,
})
@ -65,7 +65,7 @@ func (w *Worker) SubscribeCron() pb.PubSub_SubscribeClient {
// StartCron 启动任务
func (w *Worker) StartCron() pb.PubSub_SubscribeClient {
stream, err := w.Pub.Subscribe(context.Background(), &pb.SubscribeRequest{
Id: gouuid.GetUuId(),
Id: only.GetUuId(),
Value: prefixSprintf(w.ClientIp),
Ip: w.ClientIp,
})

@ -8,7 +8,7 @@ import (
"go.dtapp.net/library/utils/goarray"
"go.dtapp.net/library/utils/goip"
"go.dtapp.net/library/utils/gojobs/jobs_gorm_model"
"go.dtapp.net/library/utils/gouuid"
"go.dtapp.net/library/utils/only"
"gorm.io/gorm"
"log"
"runtime"
@ -105,7 +105,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
statusEdit := j.EditTask(j.db, info.Id).
Select("run_id").
Updates(jobs_gorm_model.Task{
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
})
if statusEdit.RowsAffected == 0 {
log.Println("statusEdit", statusEdit.Error)
@ -120,7 +120,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
Updates(jobs_gorm_model.Task{
StatusDesc: "执行成功",
Number: info.Number + 1,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
UpdatedIp: j.config.OutsideIp,
Result: desc,
})
@ -150,7 +150,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
Updates(jobs_gorm_model.Task{
StatusDesc: "执行失败",
Number: info.Number + 1,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
UpdatedIp: j.config.OutsideIp,
Result: desc,
})
@ -206,7 +206,7 @@ func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
Params: config.Params,
StatusDesc: "首次添加任务",
Frequency: config.Frequency,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
CustomId: config.CustomId,
CustomSequence: config.CustomSequence,
Type: config.Type,
@ -242,7 +242,7 @@ func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) erro
Params: config.Params,
StatusDesc: "首次添加任务",
Frequency: config.Frequency,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
CustomId: config.CustomId,
CustomSequence: config.CustomSequence,
Type: config.Type,
@ -276,7 +276,7 @@ func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNu
StatusDesc: "首次添加任务",
Frequency: config.Frequency,
MaxNumber: config.MaxNumber,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
CustomId: config.CustomId,
CustomSequence: config.CustomSequence,
Type: config.Type,
@ -314,7 +314,7 @@ func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdM
StatusDesc: "首次添加任务",
Frequency: config.Frequency,
MaxNumber: config.MaxNumber,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
CustomId: config.CustomId,
CustomSequence: config.CustomSequence,
Type: config.Type,

@ -0,0 +1,55 @@
package gosnowflake
import (
"errors"
"sync"
"time"
)
const (
workerBits uint8 = 10
numberBits uint8 = 12
workerMax int64 = -1 ^ (-1 << workerBits)
numberMax int64 = -1 ^ (-1 << numberBits)
timeShift uint8 = workerBits + numberBits
workerShift uint8 = numberBits
startTime int64 = 1525705533000 // 如果在程序跑了一段时间修改了epoch这个值 可能会导致生成相同的ID
)
type Worker struct {
mu sync.Mutex
timestamp int64
workerId int64
number int64
}
func NewWorker(workerId int64) (*Worker, error) {
if workerId < 0 || workerId > workerMax {
return nil, errors.New("worker ID excess of quantity")
}
// 生成一个新节点
return &Worker{
timestamp: 0,
workerId: workerId,
number: 0,
}, nil
}
func (w *Worker) GetId() int64 {
w.mu.Lock()
defer w.mu.Unlock()
now := time.Now().UnixNano() / 1e6
if w.timestamp == now {
w.number++
if w.number > numberMax {
for now <= w.timestamp {
now = time.Now().UnixNano() / 1e6
}
}
} else {
w.number = 0
w.timestamp = now
}
ID := (now-startTime)<<timeShift | (w.workerId << workerShift) | (w.number)
return ID
}

@ -4,7 +4,7 @@ import (
"go.dtapp.net/library/utils/dorm"
"go.dtapp.net/library/utils/gojson"
"go.dtapp.net/library/utils/gotime"
"go.dtapp.net/library/utils/gouuid"
"go.dtapp.net/library/utils/only"
"gorm.io/gorm"
"log"
)
@ -30,7 +30,7 @@ func (app *App) Add(tx *gorm.DB, Type string, params interface{}, frequency int6
Params: gojson.JsonEncodeNoError(params),
StatusDesc: "首次添加任务",
Frequency: frequency,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
Type: Type,
CreatedIp: app.OutsideIp,
UpdatedIp: app.OutsideIp,
@ -50,7 +50,7 @@ func (app *App) AddCustomId(tx *gorm.DB, Type string, params interface{}, freque
Params: gojson.JsonEncodeNoError(params),
StatusDesc: "首次添加任务",
Frequency: frequency,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
CustomId: customId,
Type: Type,
CreatedIp: app.OutsideIp,
@ -72,7 +72,7 @@ func (app *App) AddCustomIdMaxNumber(tx *gorm.DB, Type string, params interface{
StatusDesc: "首次添加任务",
Frequency: frequency,
MaxNumber: maxNumber,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
CustomId: customId,
Type: Type,
CreatedIp: app.OutsideIp,
@ -184,7 +184,7 @@ func (app *App) AddInOrderCustomIdObservationClone(tx *gorm.DB, Type string, cus
Updates(Task{
Status: TASK_SUCCESS,
StatusDesc: "已完成,停止观察",
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
UpdatedIp: app.OutsideIp,
UpdatedAt: gotime.Current().Format(),
})
@ -198,7 +198,7 @@ func (app *App) AddInOrderCustomIdObservationClone(tx *gorm.DB, Type string, cus
func (app *App) AddIn(tx *gorm.DB, param TaskParams, params interface{}) *gorm.DB {
param.Status = TASK_IN
param.StatusDesc = "首次添加任务"
param.RunId = gouuid.GetUuId()
param.RunId = only.GetUuId()
param.Params = gojson.JsonEncodeNoError(params)
param.CreatedIp = app.OutsideIp
param.UpdatedIp = app.OutsideIp
@ -226,7 +226,7 @@ func (app *App) AddWaitNewServiceNext(tx *gorm.DB, param TaskParams, params inte
func (app *App) AddWait(tx *gorm.DB, param TaskParams, params interface{}) *gorm.DB {
param.Status = TASK_WAIT
param.StatusDesc = "首次添加任务"
param.RunId = gouuid.GetUuId()
param.RunId = only.GetUuId()
param.Params = gojson.JsonEncodeNoError(params)
param.CreatedIp = app.OutsideIp
param.UpdatedIp = app.OutsideIp
@ -294,7 +294,7 @@ func (app *App) Run(tx *gorm.DB, info Task, status int, desc string) {
}
if status == 0 {
statusEdit := app.Edit(tx, info.Id).Select("run_id").Updates(Task{
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
})
if statusEdit.RowsAffected == 0 {
log.Println("statusEdit", statusEdit.Error)
@ -307,7 +307,7 @@ func (app *App) Run(tx *gorm.DB, info Task, status int, desc string) {
statusEdit := app.Edit(tx, info.Id).Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result").Updates(Task{
StatusDesc: "执行成功",
Number: info.Number + 1,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
UpdatedIp: app.OutsideIp,
UpdatedAt: gotime.Current().Format(),
Result: desc,
@ -335,7 +335,7 @@ func (app *App) Run(tx *gorm.DB, info Task, status int, desc string) {
statusEdit := app.Edit(tx, info.Id).Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result").Updates(Task{
StatusDesc: "执行失败",
Number: info.Number + 1,
RunId: gouuid.GetUuId(),
RunId: only.GetUuId(),
UpdatedIp: app.OutsideIp,
UpdatedAt: gotime.Current().Format(),
Result: desc,

@ -0,0 +1,130 @@
package only
import (
"errors"
"fmt"
"go.dtapp.net/library/utils/gorandom"
"go.dtapp.net/library/utils/gotime"
)
// GenerateId 生成18一编号
func GenerateId(customId string) string {
one, err := generateIdOne(customId)
if err == nil {
return one
}
two, err := generateIdTwo(customId)
if err == nil {
return two
}
three, err := generateIdThree(customId)
if err == nil {
return three
}
four, err := generateIdFour(customId)
return four
}
// 生成18位时间[年月日时分]唯一编号
func generateIdOne(customId string) (string, error) {
var (
randomLength = 4 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
dataLength = 18 // 默认数据长度
dateFormat = "200601021504" // 时间格式
dateFormatLength = len(dateFormat) // 时间格式长度
)
// 重新计算随机数据长度
randomLength = dataLength - (dateFormatLength + customIdLength)
if dateFormatLength+customIdLength > dataLength {
return "", errors.New("超出长度")
} else if dateFormatLength+customIdLength == dataLength {
return "", errors.New("无法使用时间方法")
} else if randomLength < 3 {
return "", errors.New("自定义的数据过长")
}
return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil
}
// 生成18位时间[年月日时]唯一编号
func generateIdTwo(customId string) (string, error) {
var (
randomLength = 4 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
dataLength = 18 // 默认数据长度
dateFormat = "2006010215" // 时间格式
dateFormatLength = len(dateFormat) // 时间格式长度
)
// 重新计算随机数据长度
randomLength = dataLength - (dateFormatLength + customIdLength)
if dateFormatLength+customIdLength > dataLength {
return "", errors.New("超出长度")
} else if dateFormatLength+customIdLength == dataLength {
return "", errors.New("无法使用时间方法")
} else if randomLength < 3 {
return "", errors.New("自定义的数据过长")
}
return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil
}
// 生成18位时间[年月日]唯一编号
func generateIdThree(customId string) (string, error) {
var (
randomLength = 4 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
dataLength = 18 // 默认数据长度
dateFormat = "20060102" // 时间格式
dateFormatLength = len(dateFormat) // 时间格式长度
)
// 重新计算随机数据长度
randomLength = dataLength - (dateFormatLength + customIdLength)
if dateFormatLength+customIdLength > dataLength {
return "", errors.New("超出长度")
} else if dateFormatLength+customIdLength == dataLength {
return "", errors.New("无法使用时间方法")
} else if randomLength < 3 {
return "", errors.New("自定义的数据过长")
}
return fmt.Sprintf("%v%s%s", customId, gotime.Current().SetFormat(dateFormat), gorandom.Numeric(randomLength)), nil
}
// 生成18位随机唯一编号
func generateIdFour(customId string) (string, error) {
var (
randomLength = 4 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
dataLength = 18 // 默认数据长度
)
// 重新计算随机数据长度
randomLength = dataLength - customIdLength
if customIdLength >= dataLength {
return "", errors.New("超出长度")
} else if randomLength < 3 {
return "", errors.New("自定义的数据过长")
}
return fmt.Sprintf("%v%s", customId, gorandom.Numeric(randomLength)), nil
}

@ -0,0 +1,8 @@
package only
type Only struct {
}
func NewOnly() *Only {
return &Only{}
}

@ -1,4 +1,4 @@
package gouuid
package only
import (
"crypto/rand"
@ -6,7 +6,7 @@ import (
"time"
)
// GetUuId 获取唯一ID
// GetUuId 由 32 个十六进制数字组成,以 6 个组显示,由连字符 - 分隔
func GetUuId() string {
unix32bits := uint32(time.Now().UTC().Unix())
buff := make([]byte, 12)
Loading…
Cancel
Save