Compare commits
92 Commits
Author | SHA1 | Date |
---|---|---|
李光春 | 9ffa6c2c05 | 2 years ago |
李光春 | cd06d7d7e9 | 2 years ago |
李光春 | 7f12ec6a01 | 2 years ago |
李光春 | 9469dcc3d6 | 2 years ago |
李光春 | 78ed7bdfd8 | 2 years ago |
李光春 | 3e404e4454 | 2 years ago |
李光春 | bb6f68d444 | 2 years ago |
李光春 | 60b50bf6f8 | 2 years ago |
李光春 | 11884afa4f | 2 years ago |
李光春 | 625deb44ca | 2 years ago |
李光春 | d84a9656a7 | 2 years ago |
李光春 | 3d5d7c3a7d | 2 years ago |
李光春 | 3a9b47cc7f | 2 years ago |
李光春 | 5d910fb616 | 2 years ago |
李光春 | a2f2689d10 | 2 years ago |
李光春 | 883fcec2cb | 2 years ago |
李光春 | 2c6cf23f1b | 2 years ago |
李光春 | e015e8f77d | 2 years ago |
李光春 | d7a75aaac3 | 2 years ago |
李光春 | e4858bb504 | 2 years ago |
李光春 | 44fdf7efe0 | 2 years ago |
李光春 | dcbcbbe079 | 2 years ago |
李光春 | 21c597f0f5 | 2 years ago |
李光春 | 19adea0395 | 2 years ago |
李光春 | 882cb83ec8 | 2 years ago |
李光春 | 1d5d63b0de | 2 years ago |
李光春 | 4e1dc1e1b1 | 2 years ago |
李光春 | b53a7490ce | 2 years ago |
李光春 | 917a716c90 | 2 years ago |
李光春 | fbb4ccb197 | 2 years ago |
李光春 | 1bb38af4d7 | 2 years ago |
李光春 | 6b3b29d0c2 | 2 years ago |
李光春 | 1d1be8330b | 2 years ago |
李光春 | 79b18ef741 | 2 years ago |
李光春 | 049585a1fa | 2 years ago |
李光春 | bab21c66bc | 2 years ago |
李光春 | c66aa424b3 | 2 years ago |
李光春 | 649ace79cf | 2 years ago |
李光春 | f7b6e6c8bc | 2 years ago |
李光春 | d57ee8daaf | 2 years ago |
李光春 | 7cf8704ec8 | 2 years ago |
李光春 | 48494e8a9e | 2 years ago |
李光春 | a89dea2d47 | 2 years ago |
李光春 | 082767de39 | 2 years ago |
李光春 | a20d2b6d80 | 2 years ago |
李光春 | 5014f8a92a | 2 years ago |
李光春 | 44366e6c9a | 2 years ago |
李光春 | 8a56a93570 | 2 years ago |
李光春 | 7a14377892 | 2 years ago |
李光春 | d5f6ecadda | 2 years ago |
李光春 | 8e88606e91 | 2 years ago |
李光春 | 08ee16063c | 2 years ago |
李光春 | a03babb8b6 | 2 years ago |
李光春 | 5e80f3edf0 | 2 years ago |
李光春 | 3705004c3d | 2 years ago |
李光春 | 6a2a5447b9 | 2 years ago |
李光春 | 7bfa68d812 | 2 years ago |
李光春 | 7b6ca6f957 | 2 years ago |
李光春 | 663eab78a8 | 2 years ago |
李光春 | 84d516deff | 2 years ago |
李光春 | b76dd4fb57 | 2 years ago |
李光春 | 590b7bf7b2 | 2 years ago |
李光春 | 68cafc549f | 2 years ago |
李光春 | 1ebc8cf708 | 2 years ago |
李光春 | 32f2f777b1 | 2 years ago |
李光春 | 02ced2c278 | 2 years ago |
李光春 | b077c70c8c | 2 years ago |
李光春 | 25ec0c0518 | 2 years ago |
李光春 | 60639e325f | 2 years ago |
李光春 | 619889b539 | 2 years ago |
李光春 | 4f4e4ac2da | 2 years ago |
李光春 | 622f6be827 | 2 years ago |
李光春 | 2db9b0f087 | 2 years ago |
李光春 | 1b7ede2dc1 | 2 years ago |
李光春 | f56537c5f3 | 2 years ago |
李光春 | b964e771af | 2 years ago |
李光春 | 31a9c723f7 | 2 years ago |
李光春 | 7ad0f885be | 2 years ago |
李光春 | 51aa5af8f0 | 2 years ago |
李光春 | d3e8488d23 | 2 years ago |
李光春 | 5e211cff29 | 2 years ago |
李光春 | a4c29d277c | 2 years ago |
李光春 | 22777cfcab | 2 years ago |
李光春 | 7a9c8ff826 | 2 years ago |
李光春 | b202078cd9 | 2 years ago |
李光春 | e666c3aee4 | 2 years ago |
李光春 | 924e02a997 | 2 years ago |
李光春 | 8f5f027392 | 2 years ago |
李光春 | 1af2e005ef | 2 years ago |
李光春 | 4f9a879e3d | 2 years ago |
李光春 | cf7c6c895a | 2 years ago |
李光春 | 2f0855e660 | 2 years ago |
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 李光春
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,130 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/golog"
|
||||
)
|
||||
|
||||
// 前缀
|
||||
// lockKeyPrefix 锁Key前缀 xxx_lock
|
||||
// lockKeySeparator 锁Key分隔符 :
|
||||
// cornKeyPrefix 任务Key前缀 xxx_cron
|
||||
// cornKeyCustom 任务Key自定义 xxx_cron_自定义 xxx_cron_自定义_*
|
||||
type redisPrefixFun func() (lockKeyPrefix, lockKeySeparator, cornKeyPrefix, cornKeyCustom string)
|
||||
|
||||
// ClientConfig 实例配置
|
||||
type ClientConfig struct {
|
||||
GormClientFun dorm.GormClientFun // 数据库驱动
|
||||
MongoClientFun dorm.MongoClientFun // 数据库驱动
|
||||
RedisClientFun dorm.RedisClientFun // 数据库驱动
|
||||
RedisPrefixFun redisPrefixFun // 前缀
|
||||
ZapLog *golog.ZapLog // 日志服务
|
||||
CurrentIp string // 当前ip
|
||||
}
|
||||
|
||||
// Client 实例
|
||||
type Client struct {
|
||||
gormClient *dorm.GormClient // 数据库
|
||||
mongoClient *dorm.MongoClient // 数据库
|
||||
zapLog *golog.ZapLog // 日志服务
|
||||
config struct {
|
||||
systemHostname string // 主机名
|
||||
systemOs string // 系统类型
|
||||
systemVersion string // 系统版本
|
||||
systemKernel string // 系统内核
|
||||
systemKernelVersion string // 系统内核版本
|
||||
systemBootTime uint64 // 系统开机时间
|
||||
cpuCores int // CPU核数
|
||||
cpuModelName string // CPU型号名称
|
||||
cpuMhz float64 // CPU兆赫
|
||||
systemInsideIp string // 内网ip
|
||||
systemOutsideIp string // 外网ip
|
||||
goVersion string // go版本
|
||||
sdkVersion string // sdk版本
|
||||
mongoVersion string // mongo版本
|
||||
mongoSdkVersion string // mongo sdk版本
|
||||
redisVersion string // redis版本
|
||||
redisSdkVersion string // redis sdk版本
|
||||
logVersion string // log版本
|
||||
}
|
||||
cache struct {
|
||||
redisClient *dorm.RedisClient // 数据库
|
||||
redisLockClient *dorm.RedisClientLock // 锁服务
|
||||
lockKeyPrefix string // 锁Key前缀 xxx_lock
|
||||
lockKeySeparator string // 锁Key分隔符 :
|
||||
cornKeyPrefix string // 任务Key前缀 xxx_cron
|
||||
cornKeyCustom string // 任务Key自定义
|
||||
}
|
||||
mongoConfig struct {
|
||||
stats bool // 状态
|
||||
databaseName string // 库名
|
||||
}
|
||||
}
|
||||
|
||||
// NewClient 创建实例
|
||||
func NewClient(config *ClientConfig) (*Client, error) {
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
c := &Client{}
|
||||
|
||||
c.zapLog = config.ZapLog
|
||||
|
||||
if config.CurrentIp != "" && config.CurrentIp != "0.0.0.0" {
|
||||
c.config.systemOutsideIp = config.CurrentIp
|
||||
}
|
||||
c.config.systemOutsideIp = goip.IsIp(c.config.systemOutsideIp)
|
||||
if c.config.systemOutsideIp == "" {
|
||||
return nil, currentIpNoConfig
|
||||
}
|
||||
|
||||
// 配置缓存
|
||||
redisClient := config.RedisClientFun()
|
||||
if redisClient != nil && redisClient.GetDb() != nil {
|
||||
c.cache.redisClient = redisClient
|
||||
c.cache.redisLockClient = c.cache.redisClient.NewLock()
|
||||
} else {
|
||||
return nil, redisPrefixFunNoConfig
|
||||
}
|
||||
|
||||
// 配置缓存前缀
|
||||
c.cache.lockKeyPrefix, c.cache.lockKeySeparator, c.cache.cornKeyPrefix, c.cache.cornKeyCustom = config.RedisPrefixFun()
|
||||
if c.cache.lockKeyPrefix == "" || c.cache.lockKeySeparator == "" || c.cache.cornKeyPrefix == "" || c.cache.cornKeyCustom == "" {
|
||||
return nil, redisPrefixFunNoConfig
|
||||
}
|
||||
|
||||
// 配置信息
|
||||
c.setConfig(ctx)
|
||||
|
||||
// 配置关系数据库
|
||||
gormClient := config.GormClientFun()
|
||||
if gormClient != nil && gormClient.GetDb() != nil {
|
||||
c.gormClient = gormClient
|
||||
|
||||
c.autoMigrateTask(ctx)
|
||||
c.autoMigrateTaskLog(ctx)
|
||||
} else {
|
||||
return nil, gormClientFunNoConfig
|
||||
}
|
||||
|
||||
// 配置非关系数据库
|
||||
mongoClient, databaseName := config.MongoClientFun()
|
||||
if mongoClient != nil && mongoClient.GetDb() != nil {
|
||||
c.mongoClient = mongoClient
|
||||
|
||||
if databaseName == "" {
|
||||
return nil, mongoClientFunNoConfig
|
||||
} else {
|
||||
c.mongoConfig.databaseName = databaseName
|
||||
}
|
||||
|
||||
TaskLog{}.createCollection(ctx, c.zapLog, c.mongoClient, c.mongoConfig.databaseName)
|
||||
TaskLog{}.createIndexes(ctx, c.zapLog, c.mongoClient, c.mongoConfig.databaseName)
|
||||
|
||||
c.mongoConfig.stats = true
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package gojobs
|
||||
|
||||
const (
|
||||
Version = "1.0.113"
|
||||
SpecifyIpNull = "0.0.0.0"
|
||||
)
|
@ -0,0 +1,179 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/gostring"
|
||||
"go.dtapp.net/gotime"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ConfigCreateInCustomId 创建正在运行任务
|
||||
type ConfigCreateInCustomId struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
TypeName string // 类型名称
|
||||
SpecifyIp string // 指定外网IP
|
||||
CurrentIp string // 当前外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomId 创建正在运行任务
|
||||
func (c *Client) CreateInCustomId(ctx context.Context, config *ConfigCreateInCustomId) error {
|
||||
if config.CurrentIp == "" {
|
||||
config.CurrentIp = c.config.systemOutsideIp
|
||||
}
|
||||
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
RunId: gostring.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
TypeName: config.TypeName,
|
||||
CreatedIp: config.CurrentIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: config.CurrentIp,
|
||||
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomIdOnly 创建正在运行唯一任务
|
||||
type ConfigCreateInCustomIdOnly struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
TypeName string // 类型名称
|
||||
SpecifyIp string // 指定外网IP
|
||||
CurrentIp string // 当前外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomIdOnly 创建正在运行唯一任务
|
||||
func (c *Client) CreateInCustomIdOnly(ctx context.Context, config *ConfigCreateInCustomIdOnly) error {
|
||||
query := c.TaskTypeTakeIn(ctx, config.Tx, config.CustomId, config.Type)
|
||||
if query.Id != 0 {
|
||||
return TaskIsExist
|
||||
}
|
||||
if config.CurrentIp == "" {
|
||||
config.CurrentIp = c.config.systemOutsideIp
|
||||
}
|
||||
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
RunId: gostring.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
TypeName: config.TypeName,
|
||||
CreatedIp: config.CurrentIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: config.CurrentIp,
|
||||
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||||
type ConfigCreateInCustomIdMaxNumber struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
MaxNumber int64 // 最大次数
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
TypeName string // 类型名称
|
||||
SpecifyIp string // 指定外网IP
|
||||
CurrentIp string // 当前外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||||
func (c *Client) CreateInCustomIdMaxNumber(ctx context.Context, config *ConfigCreateInCustomIdMaxNumber) error {
|
||||
if config.CurrentIp == "" {
|
||||
config.CurrentIp = c.config.systemOutsideIp
|
||||
}
|
||||
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
MaxNumber: config.MaxNumber,
|
||||
RunId: gostring.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
TypeName: config.TypeName,
|
||||
CreatedIp: config.CurrentIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: config.CurrentIp,
|
||||
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||||
type ConfigCreateInCustomIdMaxNumberOnly struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
MaxNumber int64 // 最大次数
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
TypeName string // 类型名称
|
||||
SpecifyIp string // 指定外网IP
|
||||
CurrentIp string // 当前外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||||
func (c *Client) CreateInCustomIdMaxNumberOnly(ctx context.Context, config *ConfigCreateInCustomIdMaxNumberOnly) error {
|
||||
query := c.TaskTypeTakeIn(ctx, config.Tx, config.CustomId, config.Type)
|
||||
if query.Id != 0 {
|
||||
return TaskIsExist
|
||||
}
|
||||
if config.CurrentIp == "" {
|
||||
config.CurrentIp = c.config.systemOutsideIp
|
||||
}
|
||||
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
MaxNumber: config.MaxNumber,
|
||||
RunId: gostring.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
TypeName: config.TypeName,
|
||||
CreatedIp: config.CurrentIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: config.CurrentIp,
|
||||
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/gostring"
|
||||
"go.dtapp.net/gotime"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ConfigCreateWaitCustomId 创建正在运行任务
|
||||
type ConfigCreateWaitCustomId struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
TypeName string // 类型名称
|
||||
SpecifyIp string // 指定外网IP
|
||||
CurrentIp string // 当前外网IP
|
||||
}
|
||||
|
||||
// CreateWaitCustomId 创建正在运行任务
|
||||
func (c *Client) CreateWaitCustomId(ctx context.Context, config *ConfigCreateWaitCustomId) error {
|
||||
if config.CurrentIp == "" {
|
||||
config.CurrentIp = c.config.systemOutsideIp
|
||||
}
|
||||
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_WAIT,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加等待任务",
|
||||
Frequency: config.Frequency,
|
||||
RunId: gostring.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
TypeName: config.TypeName,
|
||||
CreatedIp: config.CurrentIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: config.CurrentIp,
|
||||
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jasonlvhit/gocron"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCron1(t *testing.T) {
|
||||
|
||||
// 创建一个cron实例 精确到秒
|
||||
crontab := NewCron()
|
||||
|
||||
log.Println(crontab)
|
||||
|
||||
err := crontab.AddJobByFunc("1", "*/1 * * * * *", func() {
|
||||
log.Println("哈哈哈哈")
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("添加任务时出错:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = crontab.AddJobByFunc("2", "*/2 * * * * *", func() {
|
||||
log.Println("啊啊啊啊")
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("添加任务时出错:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
crontab.Start()
|
||||
select {}
|
||||
}
|
||||
|
||||
func TestCron2(t *testing.T) {
|
||||
i := 0
|
||||
s := gocron.NewScheduler()
|
||||
s.Every(5).Seconds().Do(func() {
|
||||
i++
|
||||
log.Println("execute per 5 seconds", i)
|
||||
})
|
||||
<-s.Start()
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package gojobs
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
currentIpNoConfig = errors.New("请配置 CurrentIp")
|
||||
redisPrefixFunNoConfig = errors.New("请配置 RedisPrefixFun")
|
||||
gormClientFunNoConfig = errors.New("请配置 GormClientFun")
|
||||
mongoClientFunNoConfig = errors.New("请配置 MongoClientFun")
|
||||
TaskIsExist = errors.New("任务已存在")
|
||||
)
|
@ -1,37 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.etcd.io/etcd/client/v3"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Watch 监听
|
||||
func (e Etcd) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
|
||||
log.Println("监听:", key)
|
||||
return e.Client.Watch(ctx, key, opts...)
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
func (e Etcd) Create(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
|
||||
log.Println("创建:", key, val)
|
||||
return e.Client.Put(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
// Get 获取
|
||||
func (e Etcd) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
|
||||
log.Println("获取:", key)
|
||||
return e.Client.Get(ctx, key, opts...)
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
func (e Etcd) Update(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
|
||||
log.Println("更新:", key, val)
|
||||
return e.Client.Put(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (e Etcd) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
|
||||
log.Println("删除:", key)
|
||||
return e.Client.Delete(ctx, key, opts...)
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"github.com/go-redis/redis/v9"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetDb 获取数据库驱动
|
||||
func (c *Client) GetDb() *gorm.DB {
|
||||
return c.gormClient.GetDb()
|
||||
}
|
||||
|
||||
// GetMongoDb 获取数据库驱动
|
||||
func (c *Client) GetMongoDb() *mongo.Client {
|
||||
return c.mongoClient.GetDb()
|
||||
}
|
||||
|
||||
// GetRedis 获取缓存数据库驱动
|
||||
func (c *Client) GetRedis() *redis.Client {
|
||||
return c.cache.redisClient.GetDb()
|
||||
}
|
||||
|
||||
// GetCurrentIp 获取当前ip
|
||||
func (c *Client) GetCurrentIp() string {
|
||||
return c.config.systemOutsideIp
|
||||
}
|
||||
|
||||
// GetSubscribeAddress 获取订阅地址
|
||||
func (c *Client) GetSubscribeAddress() string {
|
||||
return c.cache.cornKeyPrefix + "_" + c.cache.cornKeyCustom
|
||||
}
|
@ -1,56 +1,99 @@
|
||||
module go.dtapp.net/gojobs
|
||||
|
||||
go 1.18
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
gitee.com/chunanyong/zorm v1.5.5
|
||||
github.com/beego/beego/v2 v2.0.4
|
||||
github.com/go-redis/redis/v9 v9.0.0-rc.2
|
||||
github.com/jasonlvhit/gocron v0.0.1
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
go.dtapp.net/goarray v1.0.0
|
||||
go.dtapp.net/goip v1.0.17
|
||||
go.dtapp.net/goredis v1.0.1
|
||||
go.dtapp.net/gotime v1.0.2
|
||||
go.dtapp.net/gouuid v1.0.0
|
||||
go.etcd.io/etcd/api/v3 v3.5.4
|
||||
go.etcd.io/etcd/client/v3 v3.5.4
|
||||
google.golang.org/grpc v1.47.0
|
||||
google.golang.org/protobuf v1.28.0
|
||||
gorm.io/gorm v1.23.6
|
||||
xorm.io/xorm v1.3.1
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||
go.dtapp.net/dorm v1.0.54
|
||||
go.dtapp.net/goip v1.0.39
|
||||
go.dtapp.net/golog v1.0.102
|
||||
go.dtapp.net/gostring v1.0.10
|
||||
go.dtapp.net/gotime v1.0.5
|
||||
go.dtapp.net/gotrace_id v1.0.6
|
||||
go.mongodb.org/mongo-driver v1.11.0
|
||||
gorm.io/gorm v1.24.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/basgys/goxml2json v1.1.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||
github.com/goccy/go-json v0.9.7 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/gin-gonic/gin v1.8.1 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.11.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||
github.com/goccy/go-json v0.9.11 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
|
||||
github.com/jackc/pgconn v1.13.0 // indirect
|
||||
github.com/jackc/pgio v1.0.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||
github.com/jackc/pgtype v1.12.0 // indirect
|
||||
github.com/jackc/pgx/v4 v4.17.2 // 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
|
||||
github.com/klauspost/compress v1.15.12 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/lib/pq v1.10.7 // indirect
|
||||
github.com/mattn/go-isatty v0.0.16 // indirect
|
||||
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.6.6 // indirect
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||
github.com/oschwald/geoip2-golang v1.8.0 // indirect
|
||||
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda // indirect
|
||||
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect
|
||||
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
|
||||
github.com/segmentio/fasthash v1.0.3 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.11 // indirect
|
||||
github.com/tklauser/numcpus v0.6.0 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
github.com/ulikunitz/xz v0.5.10 // indirect
|
||||
go.dtapp.net/gorequest v1.0.20 // indirect
|
||||
go.dtapp.net/gostring v1.0.3 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
github.com/upper/db/v4 v4.6.0 // indirect
|
||||
github.com/uptrace/bun v1.1.9 // indirect
|
||||
github.com/uptrace/bun/dialect/mysqldialect v1.1.9 // indirect
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.9 // indirect
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.9 // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.1 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.3 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
go.dtapp.net/gorandom v1.0.1 // indirect
|
||||
go.dtapp.net/gorequest v1.0.36 // indirect
|
||||
go.dtapp.net/gourl v1.0.0 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.21.0 // indirect
|
||||
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
|
||||
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20220608133413-ed9918b62aac // indirect
|
||||
xorm.io/builder v0.3.11 // indirect
|
||||
go.uber.org/zap v1.23.0 // indirect
|
||||
golang.org/x/crypto v0.3.0 // indirect
|
||||
golang.org/x/mod v0.7.0 // indirect
|
||||
golang.org/x/net v0.2.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.2.0 // indirect
|
||||
golang.org/x/text v0.4.0 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gorm.io/driver/mysql v1.4.4 // indirect
|
||||
gorm.io/driver/postgres v1.4.5 // indirect
|
||||
mellium.im/sasl v0.3.0 // indirect
|
||||
modernc.org/ccgo/v3 v3.16.8 // indirect
|
||||
xorm.io/builder v0.3.12 // indirect
|
||||
xorm.io/xorm v1.3.2 // indirect
|
||||
)
|
||||
|
@ -0,0 +1,196 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/gotime"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TaskTakeId 编号查询任务
|
||||
func (c *Client) TaskTakeId(ctx context.Context, tx *gorm.DB, id uint) (result jobs_gorm_model.Task) {
|
||||
err := tx.Where("id = ?", id).Take(&result).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("编号查询任务:%v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskTake 自定义编号查询任务
|
||||
func (c *Client) TaskTake(ctx context.Context, tx *gorm.DB, customId string) (result jobs_gorm_model.Task) {
|
||||
err := tx.Where("custom_id = ?", customId).Take(&result).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("自定义编号查询任务:%v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 自定义编号加状态查询任务
|
||||
func (c *Client) taskTake(ctx context.Context, tx *gorm.DB, customId, status string) (result jobs_gorm_model.Task) {
|
||||
err := tx.Where("custom_id = ?", customId).Where("status = ?", status).Take(&result).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("自定义编号加状态查询任务:%v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskTakeIn 查询单任务 - 任务运行
|
||||
func (c *Client) TaskTakeIn(ctx context.Context, tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return c.taskTake(ctx, tx, customId, TASK_IN)
|
||||
}
|
||||
|
||||
// TaskTakeSuccess 查询单任务 - 任务完成
|
||||
func (c *Client) TaskTakeSuccess(ctx context.Context, tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return c.taskTake(ctx, tx, customId, TASK_SUCCESS)
|
||||
}
|
||||
|
||||
// TaskTakeError 查询单任务 - 任务异常
|
||||
func (c *Client) TaskTakeError(ctx context.Context, tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return c.taskTake(ctx, tx, customId, TASK_ERROR)
|
||||
}
|
||||
|
||||
// TaskTakeTimeout 查询单任务 - 任务超时
|
||||
func (c *Client) TaskTakeTimeout(ctx context.Context, tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return c.taskTake(ctx, tx, customId, TASK_TIMEOUT)
|
||||
}
|
||||
|
||||
// TaskTakeWait 查询单任务 - 任务等待
|
||||
func (c *Client) TaskTakeWait(ctx context.Context, tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return c.taskTake(ctx, tx, customId, TASK_WAIT)
|
||||
}
|
||||
|
||||
// TaskTypeTake 查询单任务
|
||||
func (c *Client) TaskTypeTake(ctx context.Context, tx *gorm.DB, customId, Type string) (result jobs_gorm_model.Task) {
|
||||
err := tx.Where("custom_id = ?", customId).Where("type = ?", Type).Take(&result).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("查询单任务:%v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 查询单任务
|
||||
func (c *Client) taskTypeTake(ctx context.Context, tx *gorm.DB, customId, Type, status string) (result jobs_gorm_model.Task) {
|
||||
err := tx.Where("custom_id = ?", customId).Where("type = ?", Type).Where("status = ?", status).Take(&result).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("查询单任务:%v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskTypeTakeIn 查询单任务 - 任务运行
|
||||
func (c *Client) TaskTypeTakeIn(ctx context.Context, tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return c.taskTypeTake(ctx, tx, customId, Type, TASK_IN)
|
||||
}
|
||||
|
||||
// TaskTypeTakeSuccess 查询单任务 - 任务完成
|
||||
func (c *Client) TaskTypeTakeSuccess(ctx context.Context, tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return c.taskTypeTake(ctx, tx, customId, Type, TASK_SUCCESS)
|
||||
}
|
||||
|
||||
// TaskTypeTakeError 查询单任务 - 任务异常
|
||||
func (c *Client) TaskTypeTakeError(ctx context.Context, tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return c.taskTypeTake(ctx, tx, customId, Type, TASK_ERROR)
|
||||
}
|
||||
|
||||
// TaskTypeTakeTimeout 查询单任务 - 任务超时
|
||||
func (c *Client) TaskTypeTakeTimeout(ctx context.Context, tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return c.taskTypeTake(ctx, tx, customId, Type, TASK_TIMEOUT)
|
||||
}
|
||||
|
||||
// TaskTypeTakeWait 查询单任务 - 任务等待
|
||||
func (c *Client) TaskTypeTakeWait(ctx context.Context, tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return c.taskTypeTake(ctx, tx, customId, Type, TASK_WAIT)
|
||||
}
|
||||
|
||||
// TaskFindAll 查询多任务
|
||||
func (c *Client) TaskFindAll(ctx context.Context, tx *gorm.DB, frequency int64) (results []jobs_gorm_model.Task) {
|
||||
err := tx.Where("frequency = ?", frequency).Order("id asc").Find(&results).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("查询多任务:%v", err)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// 查询多任务
|
||||
func (c *Client) taskFindAll(ctx context.Context, tx *gorm.DB, frequency int64, status string) (results []jobs_gorm_model.Task) {
|
||||
err := tx.Where("frequency = ?", frequency).Where("status = ?", status).Order("id asc").Find(&results).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("查询多任务:%v", err)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// TaskFindAllIn 查询多任务 - 任务运行
|
||||
func (c *Client) TaskFindAllIn(ctx context.Context, tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return c.taskFindAll(ctx, tx, frequency, TASK_IN)
|
||||
}
|
||||
|
||||
// TaskFindAllSuccess 查询多任务 - 任务完成
|
||||
func (c *Client) TaskFindAllSuccess(ctx context.Context, tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return c.taskFindAll(ctx, tx, frequency, TASK_SUCCESS)
|
||||
}
|
||||
|
||||
// TaskFindAllError 查询多任务 - 任务异常
|
||||
func (c *Client) TaskFindAllError(ctx context.Context, tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return c.taskFindAll(ctx, tx, frequency, TASK_ERROR)
|
||||
}
|
||||
|
||||
// TaskFindAllTimeout 查询多任务 - 任务超时
|
||||
func (c *Client) TaskFindAllTimeout(ctx context.Context, tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return c.taskFindAll(ctx, tx, frequency, TASK_TIMEOUT)
|
||||
}
|
||||
|
||||
// TaskFindAllWait 查询多任务 - 任务等待
|
||||
func (c *Client) TaskFindAllWait(ctx context.Context, tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return c.taskFindAll(ctx, tx, frequency, TASK_WAIT)
|
||||
}
|
||||
|
||||
// StartTask 任务启动
|
||||
func (c *Client) StartTask(ctx context.Context, tx *gorm.DB, id uint) error {
|
||||
err := c.EditTask(tx, id).
|
||||
Select("status", "status_desc").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
StatusDesc: "启动任务",
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("任务启动失败:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// StartTaskCustom 任务启动自定义
|
||||
func (c *Client) StartTaskCustom(ctx context.Context, tx *gorm.DB, customId string, customSequence int64) error {
|
||||
err := tx.Model(&jobs_gorm_model.Task{}).
|
||||
Where("custom_id = ?", customId).
|
||||
Where("custom_sequence = ?", customSequence).
|
||||
Where("status = ?", TASK_WAIT).
|
||||
Select("status", "status_desc").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
StatusDesc: "启动任务",
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("任务启动自定义失败:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// EditTask 任务修改
|
||||
func (c *Client) EditTask(tx *gorm.DB, id uint) *gorm.DB {
|
||||
return tx.Model(&jobs_gorm_model.Task{}).Where("id = ?", id)
|
||||
}
|
||||
|
||||
// UpdateFrequency 更新任务频率
|
||||
func (c *Client) UpdateFrequency(ctx context.Context, tx *gorm.DB, id uint, frequency int64) error {
|
||||
err := c.EditTask(tx, id).
|
||||
Select("frequency", "next_run_time").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Frequency: frequency,
|
||||
NextRunTime: gotime.Current().AfterSeconds(frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("更新任务频率失败:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.dtapp.net/goip"
|
||||
)
|
||||
|
||||
var ip string
|
||||
|
||||
func configIp() {
|
||||
ip = goip.GetOutsideIp()
|
||||
}
|
||||
|
||||
const prefix = "cron:"
|
||||
|
||||
const prefixIp = "cron_%s:"
|
||||
|
||||
func prefixSprintf(str string) string {
|
||||
return fmt.Sprintf(prefixIp, str)
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// ClientConfig 客户端配置
|
||||
type ClientConfig struct {
|
||||
Address string // 服务端口 127.0.0.1:8888
|
||||
}
|
||||
|
||||
// Client 定时任务
|
||||
type Client struct {
|
||||
ClientConfig // 配置
|
||||
Conn *grpc.ClientConn // 链接信息
|
||||
}
|
||||
|
||||
// NewClient 创建客户端
|
||||
func NewClient(config *ClientConfig) *Client {
|
||||
|
||||
if config.Address == "" {
|
||||
panic("[客户端]请填写服务端口")
|
||||
}
|
||||
|
||||
c := &Client{}
|
||||
|
||||
c.Address = config.Address
|
||||
|
||||
var err error
|
||||
|
||||
// 建立连接 获取client
|
||||
c.Conn, err = grpc.Dial(c.Address, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
panic("[客户端]{连接失败}" + err.Error())
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/gojobs/pb"
|
||||
"go.dtapp.net/gouuid"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// WorkerConfig 工作配置
|
||||
type WorkerConfig struct {
|
||||
Address string // 服务端口 127.0.0.1:8888
|
||||
ClientIp string // 自己的ip地址
|
||||
}
|
||||
|
||||
// Worker 工作
|
||||
type Worker struct {
|
||||
WorkerConfig // 配置
|
||||
Pub pb.PubSubClient // 订阅
|
||||
Conn *grpc.ClientConn // 链接信息
|
||||
}
|
||||
|
||||
// NewWorker 创建工作
|
||||
func NewWorker(config *WorkerConfig) *Worker {
|
||||
|
||||
if config.Address == "" {
|
||||
panic("[工作线]请填写服务端口")
|
||||
}
|
||||
if config.ClientIp == "" {
|
||||
panic("[定时任务]请填写ip地址")
|
||||
}
|
||||
|
||||
w := &Worker{}
|
||||
|
||||
w.Address = config.Address
|
||||
w.ClientIp = config.ClientIp
|
||||
|
||||
var err error
|
||||
|
||||
// 建立连接 获取client
|
||||
w.Conn, err = grpc.Dial(w.Address, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
panic("[工作线]{连接失败}" + err.Error())
|
||||
}
|
||||
|
||||
// 新建一个客户端
|
||||
w.Pub = pb.NewPubSubClient(w.Conn)
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
// SubscribeCron 订阅服务
|
||||
func (w *Worker) SubscribeCron() pb.PubSub_SubscribeClient {
|
||||
stream, err := w.Pub.Subscribe(context.Background(), &pb.SubscribeRequest{
|
||||
Id: gouuid.GetUuId(),
|
||||
Value: prefix,
|
||||
Ip: w.ClientIp,
|
||||
})
|
||||
if err != nil {
|
||||
panic("[工作线]{订阅服务失败}" + err.Error())
|
||||
}
|
||||
return stream
|
||||
}
|
||||
|
||||
// StartCron 启动任务
|
||||
func (w *Worker) StartCron() pb.PubSub_SubscribeClient {
|
||||
stream, err := w.Pub.Subscribe(context.Background(), &pb.SubscribeRequest{
|
||||
Id: gouuid.GetUuId(),
|
||||
Value: prefixSprintf(w.ClientIp),
|
||||
Ip: w.ClientIp,
|
||||
})
|
||||
if err != nil {
|
||||
panic("[工作线]{启动任务失败}" + err.Error())
|
||||
}
|
||||
return stream
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import "github.com/beego/beego/v2/client/orm"
|
||||
|
||||
type JobsBeegoOrm struct {
|
||||
Db *orm.Ormer
|
||||
}
|
||||
|
||||
func NewJobsBeegoOrm(db *orm.Ormer) *JobsBeegoOrm {
|
||||
var (
|
||||
jobsBeegoOrm = &JobsBeegoOrm{}
|
||||
)
|
||||
jobsBeegoOrm.Db = db
|
||||
return jobsBeegoOrm
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package jobs_beego_orm_model
|
||||
|
||||
// Task 任务
|
||||
type Task struct {
|
||||
Id uint `orm:"auto" json:"id"` // 记录编号
|
||||
Status string `json:"status"` // 状态码
|
||||
Params string `json:"params"` // 参数
|
||||
ParamsType string `json:"params_type"` // 参数类型
|
||||
StatusDesc string `json:"status_desc"` // 状态描述
|
||||
Frequency int64 `json:"frequency"` // 频率(秒单位)
|
||||
Number int64 `json:"number"` // 当前次数
|
||||
MaxNumber int64 `json:"max_number"` // 最大次数
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
CustomId string `json:"custom_id"` // 自定义编号
|
||||
CustomSequence int64 `json:"custom_sequence"` // 自定义顺序
|
||||
Type string `json:"type"` // 类型
|
||||
CreatedIp string `json:"created_ip"` // 创建外网IP
|
||||
SpecifyIp string `json:"specify_ip"` // 指定外网IP
|
||||
UpdatedIp string `json:"updated_ip"` // 更新外网IP
|
||||
Result string `json:"result"` // 结果
|
||||
CreatedAt string `orm:"auto_now_add;type(text)" json:"created_at"` // 创建时间
|
||||
UpdatedAt string `orm:"auto_now;type(text)" json:"updated_at"` // 更新时间
|
||||
DeletedAt string `orm:"on_delete(set_null)" json:"deleted_at"` // 删除时间
|
||||
}
|
||||
|
||||
func (m *Task) TableName() string {
|
||||
return "task"
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package jobs_beego_orm_model
|
||||
|
||||
// TaskIp 任务Ip
|
||||
type TaskIp struct {
|
||||
Id int64 `orm:"auto" json:"id"`
|
||||
TaskType string `json:"task_type"` // 任务编号
|
||||
Ips string `json:"ips"` // 任务IP
|
||||
}
|
||||
|
||||
func (m *TaskIp) TableName() string {
|
||||
return "task_ip"
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package jobs_beego_orm_model
|
||||
|
||||
// TaskLog 任务日志模型
|
||||
type TaskLog struct {
|
||||
Id uint `orm:"auto" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
StatusCode int `json:"status_code"` // 状态码
|
||||
Desc string `json:"desc"` // 结果
|
||||
Version int `json:"version"` // 版本
|
||||
CreatedAt string `orm:"auto_now_add;type(text)" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLog) TableName() string {
|
||||
return "task_log"
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package jobs_beego_orm_model
|
||||
|
||||
// TaskLogRun 任务执行日志模型
|
||||
type TaskLogRun struct {
|
||||
Id uint `orm:"auto" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
OutsideIp string `json:"outside_ip"` // 外网ip
|
||||
InsideIp string `json:"inside_ip"` // 内网ip
|
||||
Os string `json:"os"` // 系统类型
|
||||
Arch string `json:"arch"` // 系统架构
|
||||
Gomaxprocs int `json:"gomaxprocs"` // CPU核数
|
||||
GoVersion string `json:"go_version"` // GO版本
|
||||
MacAddrs string `json:"mac_addrs"` // Mac地址
|
||||
CreatedAt string `orm:"auto_now_add;type(text)" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLogRun) TableName() string {
|
||||
return "task_log_run"
|
||||
}
|
@ -1,331 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.dtapp.net/goarray"
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/goredis"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gouuid"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// ConfigJobsGorm 配置
|
||||
type ConfigJobsGorm struct {
|
||||
MainService int // 主要服务
|
||||
Db *gorm.DB // 数据库
|
||||
Redis *goredis.Client // 缓存数据库服务
|
||||
}
|
||||
|
||||
// JobsGorm Gorm数据库驱动
|
||||
type JobsGorm struct {
|
||||
runVersion string // 运行版本
|
||||
os string // 系统类型
|
||||
arch string // 系统架构
|
||||
maxProCs int // CPU核数
|
||||
version string // GO版本
|
||||
macAddrS string // Mac地址
|
||||
insideIp string // 内网ip
|
||||
outsideIp string // 外网ip
|
||||
mainService int // 主要服务
|
||||
db *gorm.DB // 数据库
|
||||
redis *goredis.Client // 缓存数据库服务
|
||||
}
|
||||
|
||||
// NewJobsGorm 初始化
|
||||
func NewJobsGorm(config *ConfigJobsGorm) *JobsGorm {
|
||||
|
||||
var (
|
||||
j = &JobsGorm{}
|
||||
)
|
||||
|
||||
j.runVersion = Version
|
||||
j.os = runtime.GOOS
|
||||
j.arch = runtime.GOARCH
|
||||
j.maxProCs = runtime.GOMAXPROCS(0)
|
||||
j.version = runtime.Version()
|
||||
j.macAddrS = goarray.TurnString(goip.GetMacAddr())
|
||||
j.insideIp = goip.GetInsideIp()
|
||||
j.outsideIp = goip.GetOutsideIp()
|
||||
j.mainService = config.MainService
|
||||
j.db = config.Db
|
||||
j.redis = config.Redis
|
||||
|
||||
err := j.db.AutoMigrate(
|
||||
&jobs_gorm_model.Task{},
|
||||
&jobs_gorm_model.TaskLog{},
|
||||
&jobs_gorm_model.TaskLogRun{},
|
||||
&jobs_gorm_model.TaskIp{},
|
||||
)
|
||||
if err != nil {
|
||||
panic(errors.New(fmt.Sprintf("创建任务模型失败:%v\n", err)))
|
||||
}
|
||||
|
||||
return j
|
||||
}
|
||||
|
||||
func (j *JobsGorm) GetDb() *gorm.DB {
|
||||
return j.db
|
||||
}
|
||||
|
||||
func (j *JobsGorm) GetRedis() *goredis.Client {
|
||||
return j.redis
|
||||
}
|
||||
|
||||
// Run 运行
|
||||
func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
|
||||
// 请求函数记录
|
||||
statusCreate := j.db.Create(&jobs_gorm_model.TaskLog{
|
||||
TaskId: info.Id,
|
||||
StatusCode: status,
|
||||
Desc: desc,
|
||||
Version: j.runVersion,
|
||||
CreatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if statusCreate.RowsAffected == 0 {
|
||||
log.Println("statusCreate", statusCreate.Error)
|
||||
}
|
||||
if status == 0 {
|
||||
statusEdit := j.EditTask(j.db, info.Id).
|
||||
Select("run_id", "updated_at").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
RunId: gouuid.GetUuId(),
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if statusEdit.RowsAffected == 0 {
|
||||
log.Println("statusEdit", statusEdit.Error)
|
||||
}
|
||||
return
|
||||
}
|
||||
// 任务
|
||||
if status == CodeSuccess {
|
||||
// 执行成功
|
||||
statusEdit := j.EditTask(j.db, info.Id).
|
||||
Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
StatusDesc: "执行成功",
|
||||
Number: info.Number + 1,
|
||||
RunId: gouuid.GetUuId(),
|
||||
UpdatedIp: j.outsideIp,
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
Result: desc,
|
||||
})
|
||||
if statusEdit.RowsAffected == 0 {
|
||||
log.Println("statusEdit", statusEdit.Error)
|
||||
}
|
||||
}
|
||||
if status == CodeEnd {
|
||||
// 执行成功、提前结束
|
||||
statusEdit := j.EditTask(j.db, info.Id).
|
||||
Select("status", "status_desc", "number", "updated_ip", "updated_at", "result").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Status: TASK_SUCCESS,
|
||||
StatusDesc: "结束执行",
|
||||
Number: info.Number + 1,
|
||||
UpdatedIp: j.outsideIp,
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
Result: desc,
|
||||
})
|
||||
if statusEdit.RowsAffected == 0 {
|
||||
log.Println("statusEdit", statusEdit.Error)
|
||||
}
|
||||
}
|
||||
if status == CodeError {
|
||||
// 执行失败
|
||||
statusEdit := j.EditTask(j.db, info.Id).
|
||||
Select("status_desc", "number", "run_id", "updated_ip", "updated_at", "result").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
StatusDesc: "执行失败",
|
||||
Number: info.Number + 1,
|
||||
RunId: gouuid.GetUuId(),
|
||||
UpdatedIp: j.outsideIp,
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
Result: desc,
|
||||
})
|
||||
if statusEdit.RowsAffected == 0 {
|
||||
log.Println("statusEdit", statusEdit.Error)
|
||||
}
|
||||
}
|
||||
if info.MaxNumber != 0 {
|
||||
if info.Number+1 >= info.MaxNumber {
|
||||
// 关闭执行
|
||||
statusEdit := j.EditTask(j.db, info.Id).
|
||||
Select("status", "updated_at").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Status: TASK_TIMEOUT,
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if statusEdit.RowsAffected == 0 {
|
||||
log.Println("statusEdit", statusEdit.Error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunAddLog 任务执行日志
|
||||
func (j *JobsGorm) RunAddLog(id uint, runId string) *gorm.DB {
|
||||
return j.db.Create(&jobs_gorm_model.TaskLogRun{
|
||||
TaskId: id,
|
||||
RunId: runId,
|
||||
InsideIp: j.insideIp,
|
||||
OutsideIp: j.outsideIp,
|
||||
Os: j.os,
|
||||
Arch: j.arch,
|
||||
Gomaxprocs: j.maxProCs,
|
||||
GoVersion: j.version,
|
||||
MacAddrs: j.macAddrS,
|
||||
CreatedAt: gotime.Current().Format(),
|
||||
})
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomId 创建正在运行任务
|
||||
type ConfigCreateInCustomId struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
SpecifyIp string // 指定外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomId 创建正在运行任务
|
||||
func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
|
||||
createStatus := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
RunId: gouuid.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
CreatedIp: j.outsideIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: j.outsideIp,
|
||||
CreatedAt: gotime.Current().Format(),
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if createStatus.RowsAffected == 0 {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomIdOnly 创建正在运行唯一任务
|
||||
type ConfigCreateInCustomIdOnly struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
SpecifyIp string // 指定外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomIdOnly 创建正在运行唯一任务
|
||||
func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) error {
|
||||
query := j.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
|
||||
if query.Id != 0 {
|
||||
return errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
|
||||
}
|
||||
createStatus := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
RunId: gouuid.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
CreatedIp: j.outsideIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: j.outsideIp,
|
||||
CreatedAt: gotime.Current().Format(),
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if createStatus.RowsAffected == 0 {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||||
type ConfigCreateInCustomIdMaxNumber struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
MaxNumber int64 // 最大次数
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
SpecifyIp string // 指定外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||||
func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNumber) error {
|
||||
createStatus := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
MaxNumber: config.MaxNumber,
|
||||
RunId: gouuid.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
CreatedIp: j.outsideIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: j.outsideIp,
|
||||
CreatedAt: gotime.Current().Format(),
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if createStatus.RowsAffected == 0 {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigCreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||||
type ConfigCreateInCustomIdMaxNumberOnly struct {
|
||||
Tx *gorm.DB // 驱动
|
||||
Params string // 参数
|
||||
Frequency int64 // 频率(秒单位)
|
||||
MaxNumber int64 // 最大次数
|
||||
CustomId string // 自定义编号
|
||||
CustomSequence int64 // 自定义顺序
|
||||
Type string // 类型
|
||||
SpecifyIp string // 指定外网IP
|
||||
}
|
||||
|
||||
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||||
func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdMaxNumberOnly) error {
|
||||
query := j.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
|
||||
if query.Id != 0 {
|
||||
return errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
|
||||
}
|
||||
createStatus := config.Tx.Create(&jobs_gorm_model.Task{
|
||||
Status: TASK_IN,
|
||||
Params: config.Params,
|
||||
StatusDesc: "首次添加任务",
|
||||
Frequency: config.Frequency,
|
||||
MaxNumber: config.MaxNumber,
|
||||
RunId: gouuid.GetUuId(),
|
||||
CustomId: config.CustomId,
|
||||
CustomSequence: config.CustomSequence,
|
||||
Type: config.Type,
|
||||
CreatedIp: j.outsideIp,
|
||||
SpecifyIp: config.SpecifyIp,
|
||||
UpdatedIp: j.outsideIp,
|
||||
CreatedAt: gotime.Current().Format(),
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
if createStatus.RowsAffected == 0 {
|
||||
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RefreshIp 刷新Ip
|
||||
func (j *JobsGorm) RefreshIp(tx *gorm.DB) {
|
||||
xip := goip.GetOutsideIp()
|
||||
if j.outsideIp == "" || j.outsideIp == "0.0.0.0" {
|
||||
return
|
||||
}
|
||||
if j.outsideIp == xip {
|
||||
return
|
||||
}
|
||||
tx.Where("ips = ?", j.outsideIp).Delete(&jobs_gorm_model.TaskIp{}) // 删除
|
||||
j.outsideIp = xip
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/goredis"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Lock 上锁
|
||||
func (j *JobsGorm) Lock(info jobs_gorm_model.Task, id any) string {
|
||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||
judgeCache := j.redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
||||
if judgeCache != "" {
|
||||
return judgeCache
|
||||
}
|
||||
j.redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器上锁成功", j.outsideIp), goredis.WithExpire(time.Millisecond*time.Duration(info.Frequency)*3))
|
||||
return ""
|
||||
}
|
||||
|
||||
// Unlock Lock 解锁
|
||||
func (j *JobsGorm) Unlock(info jobs_gorm_model.Task, id any) {
|
||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||
j.redis.NewStringOperation().Del(cacheName)
|
||||
}
|
||||
|
||||
// LockForever 永远上锁
|
||||
func (j *JobsGorm) LockForever(info jobs_gorm_model.Task, id any) string {
|
||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||
judgeCache := j.redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
||||
if judgeCache != "" {
|
||||
return judgeCache
|
||||
}
|
||||
j.redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器永远上锁成功", j.outsideIp))
|
||||
return ""
|
||||
}
|
@ -1,198 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/gotime"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TaskTakeId 查询单任务
|
||||
func (j *JobsGorm) TaskTakeId(tx *gorm.DB, id uint) (result jobs_gorm_model.Task) {
|
||||
tx.Where("id = ?", id).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskTake 查询单任务
|
||||
func (j *JobsGorm) TaskTake(tx *gorm.DB, customId string) (result jobs_gorm_model.Task) {
|
||||
tx.Where("custom_id = ?", customId).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// 查询单任务
|
||||
func (j *JobsGorm) taskTake(tx *gorm.DB, customId, status string) (result jobs_gorm_model.Task) {
|
||||
tx.Where("custom_id = ?", customId).Where("status = ?", status).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskTakeIn 查询单任务 - 任务运行
|
||||
func (j *JobsGorm) TaskTakeIn(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return j.taskTake(tx, customId, TASK_IN)
|
||||
}
|
||||
|
||||
// TaskTakeSuccess 查询单任务 - 任务完成
|
||||
func (j *JobsGorm) TaskTakeSuccess(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return j.taskTake(tx, customId, TASK_SUCCESS)
|
||||
}
|
||||
|
||||
// TaskTakeError 查询单任务 - 任务异常
|
||||
func (j *JobsGorm) TaskTakeError(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return j.taskTake(tx, customId, TASK_ERROR)
|
||||
}
|
||||
|
||||
// TaskTakeTimeout 查询单任务 - 任务超时
|
||||
func (j *JobsGorm) TaskTakeTimeout(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return j.taskTake(tx, customId, TASK_TIMEOUT)
|
||||
}
|
||||
|
||||
// TaskTakeWait 查询单任务 - 任务等待
|
||||
func (j *JobsGorm) TaskTakeWait(tx *gorm.DB, customId string) jobs_gorm_model.Task {
|
||||
return j.taskTake(tx, customId, TASK_WAIT)
|
||||
}
|
||||
|
||||
// TaskTypeTake 查询单任务
|
||||
func (j *JobsGorm) TaskTypeTake(tx *gorm.DB, customId, Type string) (result jobs_gorm_model.Task) {
|
||||
tx.Where("custom_id = ?", customId).Where("type = ?", Type).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// 查询单任务
|
||||
func (j *JobsGorm) taskTypeTake(tx *gorm.DB, customId, Type, status string) (result jobs_gorm_model.Task) {
|
||||
tx.Where("custom_id = ?", customId).Where("type = ?", Type).Where("status = ?", status).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskTypeTakeIn 查询单任务 - 任务运行
|
||||
func (j *JobsGorm) TaskTypeTakeIn(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return j.taskTypeTake(tx, customId, Type, TASK_IN)
|
||||
}
|
||||
|
||||
// TaskTypeTakeSuccess 查询单任务 - 任务完成
|
||||
func (j *JobsGorm) TaskTypeTakeSuccess(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return j.taskTypeTake(tx, customId, Type, TASK_SUCCESS)
|
||||
}
|
||||
|
||||
// TaskTypeTakeError 查询单任务 - 任务异常
|
||||
func (j *JobsGorm) TaskTypeTakeError(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return j.taskTypeTake(tx, customId, Type, TASK_ERROR)
|
||||
}
|
||||
|
||||
// TaskTypeTakeTimeout 查询单任务 - 任务超时
|
||||
func (j *JobsGorm) TaskTypeTakeTimeout(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return j.taskTypeTake(tx, customId, Type, TASK_TIMEOUT)
|
||||
}
|
||||
|
||||
// TaskTypeTakeWait 查询单任务 - 任务等待
|
||||
func (j *JobsGorm) TaskTypeTakeWait(tx *gorm.DB, customId, Type string) jobs_gorm_model.Task {
|
||||
return j.taskTypeTake(tx, customId, Type, TASK_WAIT)
|
||||
}
|
||||
|
||||
// TaskFindAll 查询多任务
|
||||
func (j *JobsGorm) TaskFindAll(tx *gorm.DB, frequency int64) (results []jobs_gorm_model.Task) {
|
||||
tx.Where("frequency = ?", frequency).Order("id asc").Find(&results)
|
||||
return results
|
||||
}
|
||||
|
||||
// 查询多任务
|
||||
func (j *JobsGorm) taskFindAll(tx *gorm.DB, frequency int64, status string) (results []jobs_gorm_model.Task) {
|
||||
tx.Where("frequency = ?", frequency).Where("status = ?", status).Order("id asc").Find(&results)
|
||||
return results
|
||||
}
|
||||
|
||||
// TaskFindAllIn 查询多任务 - 任务运行
|
||||
func (j *JobsGorm) TaskFindAllIn(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return j.taskFindAll(tx, frequency, TASK_IN)
|
||||
}
|
||||
|
||||
// TaskFindAllSuccess 查询多任务 - 任务完成
|
||||
func (j *JobsGorm) TaskFindAllSuccess(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return j.taskFindAll(tx, frequency, TASK_SUCCESS)
|
||||
}
|
||||
|
||||
// TaskFindAllError 查询多任务 - 任务异常
|
||||
func (j *JobsGorm) TaskFindAllError(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return j.taskFindAll(tx, frequency, TASK_ERROR)
|
||||
}
|
||||
|
||||
// TaskFindAllTimeout 查询多任务 - 任务超时
|
||||
func (j *JobsGorm) TaskFindAllTimeout(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return j.taskFindAll(tx, frequency, TASK_TIMEOUT)
|
||||
}
|
||||
|
||||
// TaskFindAllWait 查询多任务 - 任务等待
|
||||
func (j *JobsGorm) TaskFindAllWait(tx *gorm.DB, frequency int64) []jobs_gorm_model.Task {
|
||||
return j.taskFindAll(tx, frequency, TASK_WAIT)
|
||||
}
|
||||
|
||||
// EditTask 任务修改
|
||||
func (j *JobsGorm) EditTask(tx *gorm.DB, id uint) *gorm.DB {
|
||||
return tx.Model(&jobs_gorm_model.Task{}).Where("id = ?", id)
|
||||
}
|
||||
|
||||
// UpdateFrequency 更新任务频率
|
||||
func (j *JobsGorm) UpdateFrequency(tx *gorm.DB, id uint, frequency int64) *gorm.DB {
|
||||
return j.EditTask(tx, id).
|
||||
Select("frequency", "updated_at").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Frequency: frequency,
|
||||
UpdatedAt: gotime.Current().Format(),
|
||||
})
|
||||
}
|
||||
|
||||
func (j *JobsGorm) taskIpTake(tx *gorm.DB, taskType, ips string) (result jobs_gorm_model.TaskIp) {
|
||||
tx.Where("task_type = ?", taskType).Where("ips = ?", ips).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskIpUpdate 更新ip
|
||||
func (j *JobsGorm) TaskIpUpdate(tx *gorm.DB, taskType, ips string) *gorm.DB {
|
||||
query := j.taskIpTake(tx, taskType, ips)
|
||||
if query.Id != 0 {
|
||||
return tx
|
||||
}
|
||||
updateStatus := tx.Create(&jobs_gorm_model.TaskIp{
|
||||
TaskType: taskType,
|
||||
Ips: ips,
|
||||
})
|
||||
if updateStatus.RowsAffected == 0 {
|
||||
log.Println("任务更新失败:", updateStatus.Error)
|
||||
}
|
||||
return updateStatus
|
||||
}
|
||||
|
||||
// TaskIpInit 实例任务ip
|
||||
func (j *JobsGorm) TaskIpInit(tx *gorm.DB, ips map[string]string) bool {
|
||||
if j.outsideIp == "" || j.outsideIp == "0.0.0.0" {
|
||||
return false
|
||||
}
|
||||
tx.Where("ips = ?", j.outsideIp).Delete(&jobs_gorm_model.TaskIp{}) // 删除
|
||||
for k, v := range ips {
|
||||
if v == "" {
|
||||
j.TaskIpUpdate(tx, k, j.outsideIp)
|
||||
} else {
|
||||
find := strings.Contains(v, ",")
|
||||
if find == true {
|
||||
// 包含
|
||||
parts := strings.Split(v, ",")
|
||||
for _, vv := range parts {
|
||||
if vv == j.outsideIp {
|
||||
j.TaskIpUpdate(tx, k, j.outsideIp)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 不包含
|
||||
if v == j.outsideIp {
|
||||
j.TaskIpUpdate(tx, k, j.outsideIp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TaskLogRunTake 查询任务执行日志
|
||||
func (j *JobsGorm) TaskLogRunTake(tx *gorm.DB, taskId uint, runId string) (result jobs_gorm_model.TaskLogRun) {
|
||||
tx.Select("id", "os", "arch", "outside_ip", "created_at").Where("task_id = ?", taskId).Where("run_id = ?", runId).Take(&result)
|
||||
return result
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package jobs_gorm_model
|
||||
|
||||
// TaskIp 任务Ip
|
||||
type TaskIp struct {
|
||||
Id int64 `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
||||
TaskType string `gorm:"comment:任务编号" json:"task_type"` // 任务编号
|
||||
Ips string `gorm:"comment:任务IP" json:"ips"` // 任务IP
|
||||
}
|
||||
|
||||
func (m *TaskIp) TableName() string {
|
||||
return "task_ip"
|
||||
}
|
@ -1,15 +1,24 @@
|
||||
package jobs_gorm_model
|
||||
|
||||
import "time"
|
||||
|
||||
// TaskLog 任务日志模型
|
||||
type TaskLog struct {
|
||||
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
||||
TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号
|
||||
StatusCode int `gorm:"comment:状态码" json:"status_code"` // 状态码
|
||||
Desc string `gorm:"comment:结果" json:"desc"` // 结果
|
||||
Version string `gorm:"comment:版本" json:"version"` // 版本
|
||||
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间
|
||||
LogId uint `gorm:"primaryKey;comment:日志编号" json:"log_id"` // 日志编号
|
||||
TaskId uint `gorm:"index;comment:任务编号" json:"task_id"` // 任务编号
|
||||
TaskRunId string `gorm:"comment:执行编号" json:"task_run_id"` // 执行编号
|
||||
TaskResultCode int `gorm:"index;comment:执行状态码" json:"task_result_code"` // 执行状态码
|
||||
TaskResultDesc string `gorm:"comment:执行结果" json:"task_result_desc"` // 执行结果
|
||||
SystemHostName string `gorm:"comment:主机名" json:"system_host_name"` // 主机名
|
||||
SystemInsideIp string `gorm:"default:0.0.0.0;comment:内网ip" json:"system_inside_ip"` // 内网ip
|
||||
SystemOs string `gorm:"comment:系统类型" json:"system_os"` // 系统类型
|
||||
SystemArch string `gorm:"comment:系统架构" json:"system_arch"` // 系统架构
|
||||
GoVersion string `gorm:"comment:go版本" json:"go_version"` // go版本
|
||||
SdkVersion string `gorm:"comment:sdk版本" json:"sdk_version"` // sdk版本
|
||||
SystemOutsideIp string `gorm:"default:0.0.0.0;comment:外网ip" json:"system_outside_ip"` // 外网ip
|
||||
LogTime time.Time `gorm:"autoCreateTime;comment:日志时间" json:"log_time"` // 日志时间
|
||||
}
|
||||
|
||||
func (m *TaskLog) TableName() string {
|
||||
func (TaskLog) TableName() string {
|
||||
return "task_log"
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package jobs_gorm_model
|
||||
|
||||
// TaskLogRun 任务执行日志模型
|
||||
type TaskLogRun struct {
|
||||
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
|
||||
TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号
|
||||
RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号
|
||||
OutsideIp string `gorm:"comment:外网ip" json:"outside_ip"` // 外网ip
|
||||
InsideIp string `gorm:"comment:内网ip" json:"inside_ip"` // 内网ip
|
||||
Os string `gorm:"comment:系统类型" json:"os"` // 系统类型
|
||||
Arch string `gorm:"comment:系统架构" json:"arch"` // 系统架构
|
||||
Gomaxprocs int `gorm:"comment:CPU核数" json:"gomaxprocs"` // CPU核数
|
||||
GoVersion string `gorm:"comment:GO版本" json:"go_version"` // GO版本
|
||||
MacAddrs string `gorm:"comment:Mac地址" json:"mac_addrs"` // Mac地址
|
||||
CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLogRun) TableName() string {
|
||||
return "task_log_run"
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
//
|
||||
//type JobsOption func(*JobsCron)
|
||||
//
|
||||
//// WithRedis 缓存服务驱动
|
||||
//func WithRedis(db *goredis.Client) JobsOption {
|
||||
// return func(opts *JobsCron) {
|
||||
// opts.redis = db
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//// WithGorm 数据库服务驱动
|
||||
//func WithGorm(db *gorm.DB) JobsOption {
|
||||
// return func(opts *JobsCron) {
|
||||
// opts.db = db
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//// WithMainService 是否主要服务(主要服务可删除过期服务)
|
||||
//func WithMainService(status int) JobsOption {
|
||||
// return func(opts *JobsCron) {
|
||||
// opts.mainService = status
|
||||
// }
|
||||
//}
|
@ -1,13 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestJobs1(t *testing.T) {
|
||||
//t.Log(NewJobsGorm(&ConfigJobsGorm{
|
||||
// MainService: 0,
|
||||
// Db: nil,
|
||||
// Redis: nil,
|
||||
//}).Run())
|
||||
//t.Log(NewJobsXorm().Run())
|
||||
//t.Log(NewJobsZorm().Run())
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import "xorm.io/xorm"
|
||||
|
||||
// Xorm数据库驱动
|
||||
type jobsXorm struct {
|
||||
db *xorm.Engine
|
||||
}
|
||||
|
||||
// NewJobsXorm 初始化
|
||||
func NewJobsXorm(db *xorm.Engine) *jobsXorm {
|
||||
var (
|
||||
j = &jobsXorm{}
|
||||
)
|
||||
j.db = db
|
||||
return j
|
||||
}
|
||||
|
||||
func (j *jobsXorm) Run() {
|
||||
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package jobs_xorm_model
|
||||
|
||||
// Task 任务
|
||||
type Task struct {
|
||||
Id uint `xorm:"pk autoincr" json:"id"` // 记录编号
|
||||
Status string `json:"status"` // 状态码
|
||||
Params string `json:"params"` // 参数
|
||||
ParamsType string `json:"params_type"` // 参数类型
|
||||
StatusDesc string `json:"status_desc"` // 状态描述
|
||||
Frequency int64 `json:"frequency"` // 频率(秒单位)
|
||||
Number int64 `json:"number"` // 当前次数
|
||||
MaxNumber int64 `json:"max_number"` // 最大次数
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
CustomId string `json:"custom_id"` // 自定义编号
|
||||
CustomSequence int64 `json:"custom_sequence"` // 自定义顺序
|
||||
Type string `json:"type"` // 类型
|
||||
CreatedIp string `json:"created_ip"` // 创建外网IP
|
||||
SpecifyIp string `json:"specify_ip"` // 指定外网IP
|
||||
UpdatedIp string `json:"updated_ip"` // 更新外网IP
|
||||
Result string `json:"result"` // 结果
|
||||
CreatedAt string `xorm:"created" json:"created_at"` // 创建时间
|
||||
UpdatedAt string `xorm:"created" json:"updated_at"` // 更新时间
|
||||
DeletedAt string `xorm:"deleted" json:"deleted_at"` // 删除时间
|
||||
}
|
||||
|
||||
func (m *Task) TableName() string {
|
||||
return "task"
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package jobs_xorm_model
|
||||
|
||||
// TaskIp 任务Ip
|
||||
type TaskIp struct {
|
||||
Id int64 `xorm:"pk autoincr" json:"id"`
|
||||
TaskType string `json:"task_type"` // 任务编号
|
||||
Ips string `json:"ips"` // 任务IP
|
||||
}
|
||||
|
||||
func (m *TaskIp) TableName() string {
|
||||
return "task_ip"
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package jobs_xorm_model
|
||||
|
||||
// TaskLog 任务日志模型
|
||||
type TaskLog struct {
|
||||
Id uint `xorm:"pk autoincr" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
StatusCode int `json:"status_code"` // 状态码
|
||||
Desc string `json:"desc"` // 结果
|
||||
Version int `json:"version"` // 版本
|
||||
CreatedAt string `xorm:"created" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLog) TableName() string {
|
||||
return "task_log"
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package jobs_xorm_model
|
||||
|
||||
// TaskLogRun 任务执行日志模型
|
||||
type TaskLogRun struct {
|
||||
Id uint `xorm:"pk autoincr" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
OutsideIp string `json:"outside_ip"` // 外网ip
|
||||
InsideIp string `json:"inside_ip"` // 内网ip
|
||||
Os string `json:"os"` // 系统类型
|
||||
Arch string `json:"arch"` // 系统架构
|
||||
Gomaxprocs int `json:"gomaxprocs"` // CPU核数
|
||||
GoVersion string `json:"go_version"` // GO版本
|
||||
MacAddrs string `json:"mac_addrs"` // Mac地址
|
||||
CreatedAt string `xorm:"created" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLogRun) TableName() string {
|
||||
return "task_log_run"
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import "gitee.com/chunanyong/zorm"
|
||||
|
||||
// Zorm数据库驱动
|
||||
type jobsZorm struct {
|
||||
db *zorm.DBDao
|
||||
}
|
||||
|
||||
// NewJobsZorm 初始化
|
||||
func NewJobsZorm(db *zorm.DBDao) *jobsZorm {
|
||||
var (
|
||||
j = &jobsZorm{}
|
||||
)
|
||||
j.db = db
|
||||
return j
|
||||
}
|
||||
|
||||
func (j *jobsZorm) Run() {
|
||||
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package jobs_zorm_model
|
||||
|
||||
// Task 任务
|
||||
type Task struct {
|
||||
Id uint `zorm:"primaryKey" json:"id"` // 记录编号
|
||||
Status string `json:"status"` // 状态码
|
||||
Params string `json:"params"` // 参数
|
||||
ParamsType string `json:"params_type"` // 参数类型
|
||||
StatusDesc string `json:"status_desc"` // 状态描述
|
||||
Frequency int64 `json:"frequency"` // 频率(秒单位)
|
||||
Number int64 `json:"number"` // 当前次数
|
||||
MaxNumber int64 `json:"max_number"` // 最大次数
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
CustomId string `json:"custom_id"` // 自定义编号
|
||||
CustomSequence int64 `json:"custom_sequence"` // 自定义顺序
|
||||
Type string `json:"type"` // 类型
|
||||
CreatedIp string `json:"created_ip"` // 创建外网IP
|
||||
SpecifyIp string `json:"specify_ip"` // 指定外网IP
|
||||
UpdatedIp string `json:"updated_ip"` // 更新外网IP
|
||||
Result string `json:"result"` // 结果
|
||||
CreatedAt string `zorm:"type:text" json:"created_at"` // 创建时间
|
||||
UpdatedAt string `zorm:"type:text" json:"updated_at"` // 更新时间
|
||||
DeletedAt string `zorm:"index;deleted" json:"deleted_at"` // 删除时间
|
||||
}
|
||||
|
||||
func (m *Task) TableName() string {
|
||||
return "task"
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package jobs_zorm_model
|
||||
|
||||
// TaskIp 任务Ip
|
||||
type TaskIp struct {
|
||||
Id int64 `zorm:"primaryKey" json:"id"`
|
||||
TaskType string `json:"task_type"` // 任务编号
|
||||
Ips string `json:"ips"` // 任务IP
|
||||
}
|
||||
|
||||
func (m *TaskIp) TableName() string {
|
||||
return "task_ip"
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package jobs_zorm_model
|
||||
|
||||
// TaskLog 任务日志模型
|
||||
type TaskLog struct {
|
||||
Id uint `zorm:"primaryKey" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
StatusCode int `json:"status_code"` // 状态码
|
||||
Desc string `json:"desc"` // 结果
|
||||
Version int `json:"version"` // 版本
|
||||
CreatedAt string `zorm:"type:text" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLog) TableName() string {
|
||||
return "task_log"
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package jobs_zorm_model
|
||||
|
||||
// TaskLogRun 任务执行日志模型
|
||||
type TaskLogRun struct {
|
||||
Id uint `zorm:"primaryKey" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
OutsideIp string `json:"outside_ip"` // 外网ip
|
||||
InsideIp string `json:"inside_ip"` // 内网ip
|
||||
Os string `json:"os"` // 系统类型
|
||||
Arch string `json:"arch"` // 系统架构
|
||||
Gomaxprocs int `json:"gomaxprocs"` // CPU核数
|
||||
GoVersion string `json:"go_version"` // GO版本
|
||||
MacAddrs string `json:"mac_addrs"` // Mac地址
|
||||
CreatedAt string `zorm:"type:text" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLogRun) TableName() string {
|
||||
return "task_log_run"
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
)
|
||||
|
||||
// 创建模型
|
||||
func (c *Client) autoMigrateTask(ctx context.Context) {
|
||||
err := c.gormClient.GetDb().AutoMigrate(&jobs_gorm_model.Task{})
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建模型:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建模型
|
||||
func (c *Client) autoMigrateTaskLog(ctx context.Context) {
|
||||
err := c.gormClient.GetDb().AutoMigrate(&jobs_gorm_model.TaskLog{})
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建模型:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GormTaskLogDelete 删除
|
||||
func (c *Client) GormTaskLogDelete(ctx context.Context, hour int64) error {
|
||||
err := c.gormClient.GetDb().Where("log_time < ?", gotime.Current().BeforeHour(hour).Format()).Delete(&jobs_gorm_model.TaskLog{}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("删除失败:%s", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// TaskLogRecord 记录
|
||||
func (c *Client) TaskLogRecord(ctx context.Context, task jobs_gorm_model.Task, taskResultCode int, taskResultDesc string) {
|
||||
runId := gotrace_id.GetTraceIdContext(ctx)
|
||||
c.GormTaskLogRecord(ctx, task, runId, taskResultCode, taskResultDesc)
|
||||
if c.mongoConfig.stats {
|
||||
c.MongoTaskLogRecord(ctx, task, runId, taskResultCode, taskResultDesc)
|
||||
}
|
||||
}
|
||||
|
||||
// GormTaskLogRecord 记录
|
||||
func (c *Client) GormTaskLogRecord(ctx context.Context, task jobs_gorm_model.Task, runId string, taskResultCode int, taskResultDesc string) {
|
||||
|
||||
taskLog := jobs_gorm_model.TaskLog{
|
||||
TaskId: task.Id,
|
||||
TaskRunId: runId,
|
||||
TaskResultCode: taskResultCode,
|
||||
TaskResultDesc: taskResultDesc,
|
||||
SystemHostName: c.config.systemHostname,
|
||||
SystemInsideIp: c.config.systemInsideIp,
|
||||
SystemOs: c.config.systemOs,
|
||||
SystemArch: c.config.systemKernel,
|
||||
GoVersion: c.config.goVersion,
|
||||
SdkVersion: c.config.sdkVersion,
|
||||
SystemOutsideIp: c.config.systemOutsideIp,
|
||||
}
|
||||
err := c.gormClient.GetDb().Create(&taskLog).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录失败:%s", err)
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录数据:%+v", taskLog)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/golog"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// TaskLog 任务日志模型
|
||||
type TaskLog struct {
|
||||
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
|
||||
LogTime primitive.DateTime `json:"log_time,omitempty" bson:"log_time"` //【记录】时间
|
||||
Task struct {
|
||||
Id uint `json:"id" bson:"id"` //【任务】编号
|
||||
RunId string `json:"run_id" bson:"run_id"` //【任务】执行编号
|
||||
ResultCode int `json:"result_code" bson:"result_code"` //【任务】执行状态码
|
||||
ResultDesc string `json:"result_desc" bson:"result_desc"` //【任务】执行结果
|
||||
ResultTime string `json:"result_time" bson:"result_time"` //【任务】执行时间
|
||||
} `json:"task,omitempty" bson:"task,omitempty"` //【任务】信息
|
||||
System struct {
|
||||
Hostname string `json:"hostname" bson:"hostname"` //【系统】主机名
|
||||
Os string `json:"os" bson:"os"` //【系统】系统类型
|
||||
Version string `json:"version" bson:"version"` //【系统】系统版本
|
||||
Kernel string `json:"kernel" bson:"kernel"` //【系统】系统内核
|
||||
KernelVersion string `json:"kernel_version" bson:"kernel_version"` //【系统】系统内核版本
|
||||
BootTime string `json:"boot_time" bson:"boot_time"` //【系统】系统开机时间
|
||||
CpuCores int `json:"cpu_cores,omitempty" bson:"cpu_cores,omitempty"` //【系统】CPU核数
|
||||
CpuModelName string `json:"cpu_model_name,omitempty" bson:"cpu_model_name,omitempty"` //【系统】CPU型号名称
|
||||
CpuMhz float64 `json:"cpu_mhz,omitempty" bson:"cpu_mhz,omitempty"` //【系统】CPU兆赫
|
||||
InsideIp string `json:"inside_ip" bson:"inside_ip"` //【系统】内网ip
|
||||
OutsideIp string `json:"outside_ip" bson:"outside_ip"` //【系统】外网ip
|
||||
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【系统】go版本
|
||||
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【系统】sdk版本
|
||||
MongoVersion string `json:"mongo_version,omitempty" bson:"mongo_version,omitempty"` //【系统】mongo版本
|
||||
MongoSdkVersion string `json:"mongo_sdk_version,omitempty" bson:"mongo_sdk_version,omitempty"` //【系统】mongo sdk版本
|
||||
RedisVersion string `json:"redis_version,omitempty" bson:"redis_version,omitempty"` //【系统】redis版本
|
||||
RedisSdkVersion string `json:"redis_sdk_version,omitempty" bson:"redis_sdk_version,omitempty"` //【系统】redis sdk版本
|
||||
LogVersion string `json:"log_version,omitempty" bson:"log_version,omitempty"` //【系统】log版本
|
||||
} `json:"system,omitempty" bson:"system,omitempty"` //【系统】信息
|
||||
}
|
||||
|
||||
func (TaskLog) CollectionName() string {
|
||||
return "task_log"
|
||||
}
|
||||
|
||||
// 创建时间序列集合
|
||||
func (TaskLog) createCollection(ctx context.Context, zapLog *golog.ZapLog, db *dorm.MongoClient, databaseName string) {
|
||||
err := db.Database(databaseName).CreateCollection(ctx, TaskLog{}.CollectionName(), options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("log_time")))
|
||||
if err != nil {
|
||||
zapLog.WithTraceId(ctx).Sugar().Errorf("创建时间序列集合:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建索引
|
||||
func (TaskLog) createIndexes(ctx context.Context, zapLog *golog.ZapLog, db *dorm.MongoClient, databaseName string) {
|
||||
_, err := db.Database(databaseName).Collection(TaskLog{}.CollectionName()).CreateManyIndexes(ctx, []mongo.IndexModel{{
|
||||
Keys: bson.D{{
|
||||
Key: "log_time",
|
||||
Value: -1,
|
||||
}},
|
||||
}})
|
||||
if err != nil {
|
||||
zapLog.WithTraceId(ctx).Sugar().Errorf("创建索引:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// MongoTaskLogRecord 记录
|
||||
func (c *Client) MongoTaskLogRecord(ctx context.Context, task jobs_gorm_model.Task, runId string, taskResultCode int, taskResultDesc string) {
|
||||
|
||||
taskLog := TaskLog{
|
||||
LogId: primitive.NewObjectID(),
|
||||
LogTime: primitive.NewDateTimeFromTime(gotime.Current().Time),
|
||||
}
|
||||
|
||||
taskLog.Task.Id = task.Id
|
||||
taskLog.Task.RunId = runId
|
||||
taskLog.Task.ResultCode = taskResultCode
|
||||
taskLog.Task.ResultDesc = taskResultDesc
|
||||
taskLog.Task.ResultTime = gotime.Current().Format()
|
||||
|
||||
taskLog.System.Hostname = c.config.systemHostname //【系统】主机名
|
||||
taskLog.System.Os = c.config.systemOs //【系统】系统类型
|
||||
taskLog.System.Version = c.config.systemVersion //【系统】系统版本
|
||||
taskLog.System.Kernel = c.config.systemKernel //【系统】系统内核
|
||||
taskLog.System.KernelVersion = c.config.systemKernelVersion //【系统】系统内核版本
|
||||
taskLog.System.BootTime = gotime.SetCurrent(gotime.SetCurrentUnix(int64(c.config.systemBootTime)).Time).Format() //【系统】系统开机时间
|
||||
taskLog.System.CpuCores = c.config.cpuCores //【系统】CPU核数
|
||||
taskLog.System.CpuModelName = c.config.cpuModelName //【程序】CPU型号名称
|
||||
taskLog.System.CpuMhz = c.config.cpuMhz //【系统】CPU兆赫
|
||||
taskLog.System.InsideIp = c.config.systemInsideIp //【系统】内网ip
|
||||
taskLog.System.OutsideIp = c.config.systemOutsideIp //【系统】外网ip
|
||||
taskLog.System.GoVersion = c.config.goVersion //【系统】Go版本
|
||||
taskLog.System.SdkVersion = c.config.sdkVersion //【系统】Sdk版本
|
||||
taskLog.System.MongoVersion = c.config.mongoVersion //【系统】mongo版本
|
||||
taskLog.System.MongoSdkVersion = c.config.mongoSdkVersion //【系统】mongo sdk版本
|
||||
taskLog.System.RedisVersion = c.config.redisVersion //【系统】redis版本
|
||||
taskLog.System.RedisSdkVersion = c.config.redisSdkVersion //【系统】redis sdk版本
|
||||
taskLog.System.LogVersion = c.config.logVersion //【系统】log版本
|
||||
|
||||
_, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(TaskLog{}.CollectionName()).InsertOne(ctx, taskLog)
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录失败:%s", err)
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("记录数据:%+v", taskLog)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MongoTaskLogDelete 删除
|
||||
func (c *Client) MongoTaskLogDelete(ctx context.Context, hour int64) (*mongo.DeleteResult, error) {
|
||||
filter := bson.D{{"log_time", bson.D{{"$lt", primitive.NewDateTimeFromTime(gotime.Current().BeforeHour(hour).Time)}}}}
|
||||
return c.mongoClient.Database(c.mongoConfig.databaseName).Collection(TaskLog{}.CollectionName()).DeleteMany(ctx, filter)
|
||||
}
|
@ -1,221 +0,0 @@
|
||||
// 版本
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.0
|
||||
// protoc v3.19.4
|
||||
// source: pb/basics.proto
|
||||
|
||||
// 包名
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// 请求消息
|
||||
type BasicsRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (x *BasicsRequest) Reset() {
|
||||
*x = BasicsRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_basics_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *BasicsRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*BasicsRequest) ProtoMessage() {}
|
||||
|
||||
func (x *BasicsRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_basics_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use BasicsRequest.ProtoReflect.Descriptor instead.
|
||||
func (*BasicsRequest) Descriptor() ([]byte, []int) {
|
||||
return file_pb_basics_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *BasicsRequest) GetMessage() string {
|
||||
if x != nil {
|
||||
return x.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
type BasicsResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (x *BasicsResponse) Reset() {
|
||||
*x = BasicsResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_basics_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *BasicsResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*BasicsResponse) ProtoMessage() {}
|
||||
|
||||
func (x *BasicsResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_basics_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use BasicsResponse.ProtoReflect.Descriptor instead.
|
||||
func (*BasicsResponse) Descriptor() ([]byte, []int) {
|
||||
return file_pb_basics_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *BasicsResponse) GetMessage() string {
|
||||
if x != nil {
|
||||
return x.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_pb_basics_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_pb_basics_proto_rawDesc = []byte{
|
||||
0x0a, 0x0f, 0x70, 0x62, 0x2f, 0x62, 0x61, 0x73, 0x69, 0x63, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x12, 0x02, 0x70, 0x62, 0x22, 0x29, 0x0a, 0x0d, 0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x52,
|
||||
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67,
|
||||
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
|
||||
0x22, 0x2a, 0x0a, 0x0e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||
0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x6a, 0x0a, 0x06,
|
||||
0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x12, 0x2f, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x11,
|
||||
0x2e, 0x70, 0x62, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
|
||||
0x74, 0x1a, 0x12, 0x2e, 0x70, 0x62, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x52, 0x65, 0x73,
|
||||
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2f, 0x0a, 0x04, 0x50, 0x6f, 0x6e, 0x67, 0x12,
|
||||
0x11, 0x2e, 0x70, 0x62, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65,
|
||||
0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x62, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x73, 0x52, 0x65,
|
||||
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2e, 0x2f, 0x70,
|
||||
0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_pb_basics_proto_rawDescOnce sync.Once
|
||||
file_pb_basics_proto_rawDescData = file_pb_basics_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_pb_basics_proto_rawDescGZIP() []byte {
|
||||
file_pb_basics_proto_rawDescOnce.Do(func() {
|
||||
file_pb_basics_proto_rawDescData = protoimpl.X.CompressGZIP(file_pb_basics_proto_rawDescData)
|
||||
})
|
||||
return file_pb_basics_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_pb_basics_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_pb_basics_proto_goTypes = []interface{}{
|
||||
(*BasicsRequest)(nil), // 0: pb.BasicsRequest
|
||||
(*BasicsResponse)(nil), // 1: pb.BasicsResponse
|
||||
}
|
||||
var file_pb_basics_proto_depIdxs = []int32{
|
||||
0, // 0: pb.Basics.Ping:input_type -> pb.BasicsRequest
|
||||
0, // 1: pb.Basics.Pong:input_type -> pb.BasicsRequest
|
||||
1, // 2: pb.Basics.Ping:output_type -> pb.BasicsResponse
|
||||
1, // 3: pb.Basics.Pong:output_type -> pb.BasicsResponse
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_pb_basics_proto_init() }
|
||||
func file_pb_basics_proto_init() {
|
||||
if File_pb_basics_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_pb_basics_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*BasicsRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_pb_basics_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*BasicsResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_pb_basics_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_pb_basics_proto_goTypes,
|
||||
DependencyIndexes: file_pb_basics_proto_depIdxs,
|
||||
MessageInfos: file_pb_basics_proto_msgTypes,
|
||||
}.Build()
|
||||
File_pb_basics_proto = out.File
|
||||
file_pb_basics_proto_rawDesc = nil
|
||||
file_pb_basics_proto_goTypes = nil
|
||||
file_pb_basics_proto_depIdxs = nil
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
// 版本
|
||||
syntax = "proto3";
|
||||
|
||||
// 包名
|
||||
package pb;
|
||||
|
||||
// 别名
|
||||
option go_package = "../pb";
|
||||
|
||||
// 定义服务
|
||||
service Basics{
|
||||
// 心跳
|
||||
rpc Ping(BasicsRequest) returns (BasicsResponse){};
|
||||
// 心跳
|
||||
rpc Pong(BasicsRequest) returns (BasicsResponse){};
|
||||
}
|
||||
// 请求消息
|
||||
message BasicsRequest {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
message BasicsResponse {
|
||||
string message = 1;
|
||||
}
|
@ -1,145 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.19.4
|
||||
// source: pb/basics.proto
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// BasicsClient is the client API for Basics service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type BasicsClient interface {
|
||||
// 心跳
|
||||
Ping(ctx context.Context, in *BasicsRequest, opts ...grpc.CallOption) (*BasicsResponse, error)
|
||||
// 心跳
|
||||
Pong(ctx context.Context, in *BasicsRequest, opts ...grpc.CallOption) (*BasicsResponse, error)
|
||||
}
|
||||
|
||||
type basicsClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewBasicsClient(cc grpc.ClientConnInterface) BasicsClient {
|
||||
return &basicsClient{cc}
|
||||
}
|
||||
|
||||
func (c *basicsClient) Ping(ctx context.Context, in *BasicsRequest, opts ...grpc.CallOption) (*BasicsResponse, error) {
|
||||
out := new(BasicsResponse)
|
||||
err := c.cc.Invoke(ctx, "/pb.Basics/Ping", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *basicsClient) Pong(ctx context.Context, in *BasicsRequest, opts ...grpc.CallOption) (*BasicsResponse, error) {
|
||||
out := new(BasicsResponse)
|
||||
err := c.cc.Invoke(ctx, "/pb.Basics/Pong", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// BasicsServer is the server API for Basics service.
|
||||
// All implementations must embed UnimplementedBasicsServer
|
||||
// for forward compatibility
|
||||
type BasicsServer interface {
|
||||
// 心跳
|
||||
Ping(context.Context, *BasicsRequest) (*BasicsResponse, error)
|
||||
// 心跳
|
||||
Pong(context.Context, *BasicsRequest) (*BasicsResponse, error)
|
||||
mustEmbedUnimplementedBasicsServer()
|
||||
}
|
||||
|
||||
// UnimplementedBasicsServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedBasicsServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedBasicsServer) Ping(context.Context, *BasicsRequest) (*BasicsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented")
|
||||
}
|
||||
func (UnimplementedBasicsServer) Pong(context.Context, *BasicsRequest) (*BasicsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Pong not implemented")
|
||||
}
|
||||
func (UnimplementedBasicsServer) mustEmbedUnimplementedBasicsServer() {}
|
||||
|
||||
// UnsafeBasicsServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to BasicsServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeBasicsServer interface {
|
||||
mustEmbedUnimplementedBasicsServer()
|
||||
}
|
||||
|
||||
func RegisterBasicsServer(s grpc.ServiceRegistrar, srv BasicsServer) {
|
||||
s.RegisterService(&Basics_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Basics_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(BasicsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(BasicsServer).Ping(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/pb.Basics/Ping",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(BasicsServer).Ping(ctx, req.(*BasicsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Basics_Pong_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(BasicsRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(BasicsServer).Pong(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/pb.Basics/Pong",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(BasicsServer).Pong(ctx, req.(*BasicsRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Basics_ServiceDesc is the grpc.ServiceDesc for Basics service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Basics_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "pb.Basics",
|
||||
HandlerType: (*BasicsServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Ping",
|
||||
Handler: _Basics_Ping_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Pong",
|
||||
Handler: _Basics_Pong_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "pb/basics.proto",
|
||||
}
|
@ -1,440 +0,0 @@
|
||||
// 版本
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.0
|
||||
// protoc v3.19.4
|
||||
// source: pb/pubsub.proto
|
||||
|
||||
// 包名
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// 请求消息
|
||||
type PublishRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
|
||||
Method string `protobuf:"bytes,3,opt,name=method,proto3" json:"method,omitempty"`
|
||||
Ip string `protobuf:"bytes,4,opt,name=ip,proto3" json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PublishRequest) Reset() {
|
||||
*x = PublishRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PublishRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PublishRequest) ProtoMessage() {}
|
||||
|
||||
func (x *PublishRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PublishRequest.ProtoReflect.Descriptor instead.
|
||||
func (*PublishRequest) Descriptor() ([]byte, []int) {
|
||||
return file_pb_pubsub_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *PublishRequest) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PublishRequest) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PublishRequest) GetMethod() string {
|
||||
if x != nil {
|
||||
return x.Method
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PublishRequest) GetIp() string {
|
||||
if x != nil {
|
||||
return x.Ip
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
type PublishResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
|
||||
Ip string `protobuf:"bytes,3,opt,name=ip,proto3" json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PublishResponse) Reset() {
|
||||
*x = PublishResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PublishResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PublishResponse) ProtoMessage() {}
|
||||
|
||||
func (x *PublishResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PublishResponse.ProtoReflect.Descriptor instead.
|
||||
func (*PublishResponse) Descriptor() ([]byte, []int) {
|
||||
return file_pb_pubsub_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *PublishResponse) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PublishResponse) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PublishResponse) GetIp() string {
|
||||
if x != nil {
|
||||
return x.Ip
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 请求消息
|
||||
type SubscribeRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
|
||||
Method string `protobuf:"bytes,3,opt,name=method,proto3" json:"method,omitempty"`
|
||||
Ip string `protobuf:"bytes,4,opt,name=ip,proto3" json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SubscribeRequest) Reset() {
|
||||
*x = SubscribeRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SubscribeRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SubscribeRequest) ProtoMessage() {}
|
||||
|
||||
func (x *SubscribeRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SubscribeRequest.ProtoReflect.Descriptor instead.
|
||||
func (*SubscribeRequest) Descriptor() ([]byte, []int) {
|
||||
return file_pb_pubsub_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *SubscribeRequest) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SubscribeRequest) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SubscribeRequest) GetMethod() string {
|
||||
if x != nil {
|
||||
return x.Method
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SubscribeRequest) GetIp() string {
|
||||
if x != nil {
|
||||
return x.Ip
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
type SubscribeResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
|
||||
Method string `protobuf:"bytes,3,opt,name=method,proto3" json:"method,omitempty"`
|
||||
}
|
||||
|
||||
func (x *SubscribeResponse) Reset() {
|
||||
*x = SubscribeResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *SubscribeResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*SubscribeResponse) ProtoMessage() {}
|
||||
|
||||
func (x *SubscribeResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_pubsub_proto_msgTypes[3]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use SubscribeResponse.ProtoReflect.Descriptor instead.
|
||||
func (*SubscribeResponse) Descriptor() ([]byte, []int) {
|
||||
return file_pb_pubsub_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *SubscribeResponse) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SubscribeResponse) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SubscribeResponse) GetMethod() string {
|
||||
if x != nil {
|
||||
return x.Method
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_pb_pubsub_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_pb_pubsub_proto_rawDesc = []byte{
|
||||
0x0a, 0x0f, 0x70, 0x62, 0x2f, 0x70, 0x75, 0x62, 0x73, 0x75, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x12, 0x02, 0x70, 0x62, 0x22, 0x5e, 0x0a, 0x0e, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68,
|
||||
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
|
||||
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a,
|
||||
0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d,
|
||||
0x65, 0x74, 0x68, 0x6f, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x04, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x02, 0x69, 0x70, 0x22, 0x47, 0x0a, 0x0f, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68,
|
||||
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75,
|
||||
0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x0e,
|
||||
0x0a, 0x02, 0x69, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x70, 0x22, 0x60,
|
||||
0x0a, 0x10, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65,
|
||||
0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02,
|
||||
0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x74, 0x68,
|
||||
0x6f, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
|
||||
0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x70,
|
||||
0x22, 0x51, 0x0a, 0x11, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x52, 0x65, 0x73,
|
||||
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6d,
|
||||
0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x74,
|
||||
0x68, 0x6f, 0x64, 0x32, 0x78, 0x0a, 0x06, 0x50, 0x75, 0x62, 0x53, 0x75, 0x62, 0x12, 0x32, 0x0a,
|
||||
0x07, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x12, 0x12, 0x2e, 0x70, 0x62, 0x2e, 0x50, 0x75,
|
||||
0x62, 0x6c, 0x69, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x70,
|
||||
0x62, 0x2e, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
|
||||
0x65, 0x12, 0x3a, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x12, 0x14,
|
||||
0x2e, 0x70, 0x62, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x52, 0x65, 0x71,
|
||||
0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72,
|
||||
0x69, 0x62, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x30, 0x01, 0x42, 0x07, 0x5a,
|
||||
0x05, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_pb_pubsub_proto_rawDescOnce sync.Once
|
||||
file_pb_pubsub_proto_rawDescData = file_pb_pubsub_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_pb_pubsub_proto_rawDescGZIP() []byte {
|
||||
file_pb_pubsub_proto_rawDescOnce.Do(func() {
|
||||
file_pb_pubsub_proto_rawDescData = protoimpl.X.CompressGZIP(file_pb_pubsub_proto_rawDescData)
|
||||
})
|
||||
return file_pb_pubsub_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_pb_pubsub_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
|
||||
var file_pb_pubsub_proto_goTypes = []interface{}{
|
||||
(*PublishRequest)(nil), // 0: pb.PublishRequest
|
||||
(*PublishResponse)(nil), // 1: pb.PublishResponse
|
||||
(*SubscribeRequest)(nil), // 2: pb.SubscribeRequest
|
||||
(*SubscribeResponse)(nil), // 3: pb.SubscribeResponse
|
||||
}
|
||||
var file_pb_pubsub_proto_depIdxs = []int32{
|
||||
0, // 0: pb.PubSub.Publish:input_type -> pb.PublishRequest
|
||||
2, // 1: pb.PubSub.Subscribe:input_type -> pb.SubscribeRequest
|
||||
1, // 2: pb.PubSub.Publish:output_type -> pb.PublishResponse
|
||||
3, // 3: pb.PubSub.Subscribe:output_type -> pb.SubscribeResponse
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_pb_pubsub_proto_init() }
|
||||
func file_pb_pubsub_proto_init() {
|
||||
if File_pb_pubsub_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_pb_pubsub_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PublishRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_pb_pubsub_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PublishResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_pb_pubsub_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SubscribeRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_pb_pubsub_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*SubscribeResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_pb_pubsub_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 4,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_pb_pubsub_proto_goTypes,
|
||||
DependencyIndexes: file_pb_pubsub_proto_depIdxs,
|
||||
MessageInfos: file_pb_pubsub_proto_msgTypes,
|
||||
}.Build()
|
||||
File_pb_pubsub_proto = out.File
|
||||
file_pb_pubsub_proto_rawDesc = nil
|
||||
file_pb_pubsub_proto_goTypes = nil
|
||||
file_pb_pubsub_proto_depIdxs = nil
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
// 版本
|
||||
syntax = "proto3";
|
||||
|
||||
// 包名
|
||||
package pb;
|
||||
|
||||
// 别名
|
||||
option go_package = "../pb";
|
||||
|
||||
// 定义服务
|
||||
service PubSub {
|
||||
// [发布] 消息
|
||||
rpc Publish (PublishRequest) returns (PublishResponse);
|
||||
// [订阅] 消息
|
||||
rpc Subscribe (SubscribeRequest) returns (stream SubscribeResponse);
|
||||
}
|
||||
|
||||
// 请求消息
|
||||
message PublishRequest {
|
||||
string id = 1;
|
||||
string value = 2;
|
||||
string method = 3;
|
||||
string ip = 4;
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
message PublishResponse {
|
||||
string id = 1;
|
||||
string value = 2;
|
||||
string ip = 3;
|
||||
}
|
||||
|
||||
// 请求消息
|
||||
message SubscribeRequest {
|
||||
string id = 1;
|
||||
string value = 2;
|
||||
string method = 3;
|
||||
string ip = 4;
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
message SubscribeResponse {
|
||||
string id = 1;
|
||||
string value = 2;
|
||||
string method = 3;
|
||||
}
|
@ -1,173 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.19.4
|
||||
// source: pb/pubsub.proto
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// PubSubClient is the client API for PubSub service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type PubSubClient interface {
|
||||
// [发布] 消息
|
||||
Publish(ctx context.Context, in *PublishRequest, opts ...grpc.CallOption) (*PublishResponse, error)
|
||||
// [订阅] 消息
|
||||
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (PubSub_SubscribeClient, error)
|
||||
}
|
||||
|
||||
type pubSubClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewPubSubClient(cc grpc.ClientConnInterface) PubSubClient {
|
||||
return &pubSubClient{cc}
|
||||
}
|
||||
|
||||
func (c *pubSubClient) Publish(ctx context.Context, in *PublishRequest, opts ...grpc.CallOption) (*PublishResponse, error) {
|
||||
out := new(PublishResponse)
|
||||
err := c.cc.Invoke(ctx, "/pb.PubSub/Publish", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *pubSubClient) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (PubSub_SubscribeClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &PubSub_ServiceDesc.Streams[0], "/pb.PubSub/Subscribe", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &pubSubSubscribeClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type PubSub_SubscribeClient interface {
|
||||
Recv() (*SubscribeResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type pubSubSubscribeClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *pubSubSubscribeClient) Recv() (*SubscribeResponse, error) {
|
||||
m := new(SubscribeResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// PubSubServer is the server API for PubSub service.
|
||||
// All implementations must embed UnimplementedPubSubServer
|
||||
// for forward compatibility
|
||||
type PubSubServer interface {
|
||||
// [发布] 消息
|
||||
Publish(context.Context, *PublishRequest) (*PublishResponse, error)
|
||||
// [订阅] 消息
|
||||
Subscribe(*SubscribeRequest, PubSub_SubscribeServer) error
|
||||
mustEmbedUnimplementedPubSubServer()
|
||||
}
|
||||
|
||||
// UnimplementedPubSubServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedPubSubServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedPubSubServer) Publish(context.Context, *PublishRequest) (*PublishResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Publish not implemented")
|
||||
}
|
||||
func (UnimplementedPubSubServer) Subscribe(*SubscribeRequest, PubSub_SubscribeServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method Subscribe not implemented")
|
||||
}
|
||||
func (UnimplementedPubSubServer) mustEmbedUnimplementedPubSubServer() {}
|
||||
|
||||
// UnsafePubSubServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to PubSubServer will
|
||||
// result in compilation errors.
|
||||
type UnsafePubSubServer interface {
|
||||
mustEmbedUnimplementedPubSubServer()
|
||||
}
|
||||
|
||||
func RegisterPubSubServer(s grpc.ServiceRegistrar, srv PubSubServer) {
|
||||
s.RegisterService(&PubSub_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _PubSub_Publish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(PublishRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(PubSubServer).Publish(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/pb.PubSub/Publish",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(PubSubServer).Publish(ctx, req.(*PublishRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _PubSub_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
m := new(SubscribeRequest)
|
||||
if err := stream.RecvMsg(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return srv.(PubSubServer).Subscribe(m, &pubSubSubscribeServer{stream})
|
||||
}
|
||||
|
||||
type PubSub_SubscribeServer interface {
|
||||
Send(*SubscribeResponse) error
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type pubSubSubscribeServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *pubSubSubscribeServer) Send(m *SubscribeResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
// PubSub_ServiceDesc is the grpc.ServiceDesc for PubSub service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var PubSub_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "pb.PubSub",
|
||||
HandlerType: (*PubSubServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Publish",
|
||||
Handler: _PubSub_Publish_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "Subscribe",
|
||||
Handler: _PubSub_Subscribe_Handler,
|
||||
ServerStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "pb/pubsub.proto",
|
||||
}
|
@ -1,234 +0,0 @@
|
||||
// 版本
|
||||
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.0
|
||||
// protoc v3.19.4
|
||||
// source: pb/task.proto
|
||||
|
||||
// 包名
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// 请求消息
|
||||
type TaskRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (x *TaskRequest) Reset() {
|
||||
*x = TaskRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_task_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *TaskRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*TaskRequest) ProtoMessage() {}
|
||||
|
||||
func (x *TaskRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_task_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use TaskRequest.ProtoReflect.Descriptor instead.
|
||||
func (*TaskRequest) Descriptor() ([]byte, []int) {
|
||||
return file_pb_task_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *TaskRequest) GetMessage() string {
|
||||
if x != nil {
|
||||
return x.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
type TaskResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (x *TaskResponse) Reset() {
|
||||
*x = TaskResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_pb_task_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *TaskResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*TaskResponse) ProtoMessage() {}
|
||||
|
||||
func (x *TaskResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_pb_task_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use TaskResponse.ProtoReflect.Descriptor instead.
|
||||
func (*TaskResponse) Descriptor() ([]byte, []int) {
|
||||
return file_pb_task_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *TaskResponse) GetMessage() string {
|
||||
if x != nil {
|
||||
return x.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_pb_task_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_pb_task_proto_rawDesc = []byte{
|
||||
0x0a, 0x0d, 0x70, 0x62, 0x2f, 0x74, 0x61, 0x73, 0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
|
||||
0x02, 0x70, 0x62, 0x22, 0x27, 0x0a, 0x0b, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65,
|
||||
0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x28, 0x0a, 0x0c,
|
||||
0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07,
|
||||
0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d,
|
||||
0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0xfb, 0x01, 0x0a, 0x04, 0x54, 0x61, 0x73, 0x6b, 0x12,
|
||||
0x30, 0x0a, 0x09, 0x55, 0x6e, 0x61, 0x72, 0x79, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x0f, 0x2e, 0x70,
|
||||
0x62, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e,
|
||||
0x70, 0x62, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22,
|
||||
0x00, 0x12, 0x3c, 0x0a, 0x13, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x74, 0x72, 0x65, 0x61,
|
||||
0x6d, 0x69, 0x6e, 0x67, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x54, 0x61,
|
||||
0x73, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x54,
|
||||
0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x12,
|
||||
0x3c, 0x0a, 0x13, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69,
|
||||
0x6e, 0x67, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x54, 0x61, 0x73, 0x6b,
|
||||
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x54, 0x61, 0x73,
|
||||
0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x12, 0x45, 0x0a,
|
||||
0x1a, 0x42, 0x69, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x53, 0x74,
|
||||
0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x54, 0x61, 0x73, 0x6b, 0x12, 0x0f, 0x2e, 0x70, 0x62,
|
||||
0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70,
|
||||
0x62, 0x2e, 0x54, 0x61, 0x73, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00,
|
||||
0x28, 0x01, 0x30, 0x01, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_pb_task_proto_rawDescOnce sync.Once
|
||||
file_pb_task_proto_rawDescData = file_pb_task_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_pb_task_proto_rawDescGZIP() []byte {
|
||||
file_pb_task_proto_rawDescOnce.Do(func() {
|
||||
file_pb_task_proto_rawDescData = protoimpl.X.CompressGZIP(file_pb_task_proto_rawDescData)
|
||||
})
|
||||
return file_pb_task_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_pb_task_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_pb_task_proto_goTypes = []interface{}{
|
||||
(*TaskRequest)(nil), // 0: pb.TaskRequest
|
||||
(*TaskResponse)(nil), // 1: pb.TaskResponse
|
||||
}
|
||||
var file_pb_task_proto_depIdxs = []int32{
|
||||
0, // 0: pb.Task.UnaryTask:input_type -> pb.TaskRequest
|
||||
0, // 1: pb.Task.ServerStreamingTask:input_type -> pb.TaskRequest
|
||||
0, // 2: pb.Task.ClientStreamingTask:input_type -> pb.TaskRequest
|
||||
0, // 3: pb.Task.BidirectionalStreamingTask:input_type -> pb.TaskRequest
|
||||
1, // 4: pb.Task.UnaryTask:output_type -> pb.TaskResponse
|
||||
1, // 5: pb.Task.ServerStreamingTask:output_type -> pb.TaskResponse
|
||||
1, // 6: pb.Task.ClientStreamingTask:output_type -> pb.TaskResponse
|
||||
1, // 7: pb.Task.BidirectionalStreamingTask:output_type -> pb.TaskResponse
|
||||
4, // [4:8] is the sub-list for method output_type
|
||||
0, // [0:4] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_pb_task_proto_init() }
|
||||
func file_pb_task_proto_init() {
|
||||
if File_pb_task_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_pb_task_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*TaskRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_pb_task_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*TaskResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_pb_task_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_pb_task_proto_goTypes,
|
||||
DependencyIndexes: file_pb_task_proto_depIdxs,
|
||||
MessageInfos: file_pb_task_proto_msgTypes,
|
||||
}.Build()
|
||||
File_pb_task_proto = out.File
|
||||
file_pb_task_proto_rawDesc = nil
|
||||
file_pb_task_proto_goTypes = nil
|
||||
file_pb_task_proto_depIdxs = nil
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
// 版本
|
||||
syntax = "proto3";
|
||||
|
||||
// 包名
|
||||
package pb;
|
||||
|
||||
// 别名
|
||||
option go_package = "../pb";
|
||||
|
||||
// 定义服务
|
||||
service Task{
|
||||
// 普通一元方法
|
||||
rpc UnaryTask(TaskRequest) returns (TaskResponse){};
|
||||
// 服务端推送流
|
||||
rpc ServerStreamingTask(TaskRequest) returns (stream TaskResponse){};
|
||||
// 客户端推送流
|
||||
rpc ClientStreamingTask(stream TaskRequest) returns (TaskResponse){};
|
||||
// 双向推送流
|
||||
rpc BidirectionalStreamingTask(stream TaskRequest) returns (stream TaskResponse){};
|
||||
}
|
||||
|
||||
// 请求消息
|
||||
message TaskRequest {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
// 响应消息
|
||||
message TaskResponse {
|
||||
string message = 1;
|
||||
}
|
@ -1,315 +0,0 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.19.4
|
||||
// source: pb/task.proto
|
||||
|
||||
package pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// TaskClient is the client API for Task service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type TaskClient interface {
|
||||
// 普通一元方法
|
||||
UnaryTask(ctx context.Context, in *TaskRequest, opts ...grpc.CallOption) (*TaskResponse, error)
|
||||
// 服务端推送流
|
||||
ServerStreamingTask(ctx context.Context, in *TaskRequest, opts ...grpc.CallOption) (Task_ServerStreamingTaskClient, error)
|
||||
// 客户端推送流
|
||||
ClientStreamingTask(ctx context.Context, opts ...grpc.CallOption) (Task_ClientStreamingTaskClient, error)
|
||||
// 双向推送流
|
||||
BidirectionalStreamingTask(ctx context.Context, opts ...grpc.CallOption) (Task_BidirectionalStreamingTaskClient, error)
|
||||
}
|
||||
|
||||
type taskClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewTaskClient(cc grpc.ClientConnInterface) TaskClient {
|
||||
return &taskClient{cc}
|
||||
}
|
||||
|
||||
func (c *taskClient) UnaryTask(ctx context.Context, in *TaskRequest, opts ...grpc.CallOption) (*TaskResponse, error) {
|
||||
out := new(TaskResponse)
|
||||
err := c.cc.Invoke(ctx, "/pb.Task/UnaryTask", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *taskClient) ServerStreamingTask(ctx context.Context, in *TaskRequest, opts ...grpc.CallOption) (Task_ServerStreamingTaskClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &Task_ServiceDesc.Streams[0], "/pb.Task/ServerStreamingTask", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &taskServerStreamingTaskClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type Task_ServerStreamingTaskClient interface {
|
||||
Recv() (*TaskResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type taskServerStreamingTaskClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *taskServerStreamingTaskClient) Recv() (*TaskResponse, error) {
|
||||
m := new(TaskResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *taskClient) ClientStreamingTask(ctx context.Context, opts ...grpc.CallOption) (Task_ClientStreamingTaskClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &Task_ServiceDesc.Streams[1], "/pb.Task/ClientStreamingTask", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &taskClientStreamingTaskClient{stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type Task_ClientStreamingTaskClient interface {
|
||||
Send(*TaskRequest) error
|
||||
CloseAndRecv() (*TaskResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type taskClientStreamingTaskClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *taskClientStreamingTaskClient) Send(m *TaskRequest) error {
|
||||
return x.ClientStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *taskClientStreamingTaskClient) CloseAndRecv() (*TaskResponse, error) {
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := new(TaskResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *taskClient) BidirectionalStreamingTask(ctx context.Context, opts ...grpc.CallOption) (Task_BidirectionalStreamingTaskClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &Task_ServiceDesc.Streams[2], "/pb.Task/BidirectionalStreamingTask", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &taskBidirectionalStreamingTaskClient{stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type Task_BidirectionalStreamingTaskClient interface {
|
||||
Send(*TaskRequest) error
|
||||
Recv() (*TaskResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type taskBidirectionalStreamingTaskClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *taskBidirectionalStreamingTaskClient) Send(m *TaskRequest) error {
|
||||
return x.ClientStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *taskBidirectionalStreamingTaskClient) Recv() (*TaskResponse, error) {
|
||||
m := new(TaskResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// TaskServer is the server API for Task service.
|
||||
// All implementations must embed UnimplementedTaskServer
|
||||
// for forward compatibility
|
||||
type TaskServer interface {
|
||||
// 普通一元方法
|
||||
UnaryTask(context.Context, *TaskRequest) (*TaskResponse, error)
|
||||
// 服务端推送流
|
||||
ServerStreamingTask(*TaskRequest, Task_ServerStreamingTaskServer) error
|
||||
// 客户端推送流
|
||||
ClientStreamingTask(Task_ClientStreamingTaskServer) error
|
||||
// 双向推送流
|
||||
BidirectionalStreamingTask(Task_BidirectionalStreamingTaskServer) error
|
||||
mustEmbedUnimplementedTaskServer()
|
||||
}
|
||||
|
||||
// UnimplementedTaskServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedTaskServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedTaskServer) UnaryTask(context.Context, *TaskRequest) (*TaskResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method UnaryTask not implemented")
|
||||
}
|
||||
func (UnimplementedTaskServer) ServerStreamingTask(*TaskRequest, Task_ServerStreamingTaskServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method ServerStreamingTask not implemented")
|
||||
}
|
||||
func (UnimplementedTaskServer) ClientStreamingTask(Task_ClientStreamingTaskServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method ClientStreamingTask not implemented")
|
||||
}
|
||||
func (UnimplementedTaskServer) BidirectionalStreamingTask(Task_BidirectionalStreamingTaskServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method BidirectionalStreamingTask not implemented")
|
||||
}
|
||||
func (UnimplementedTaskServer) mustEmbedUnimplementedTaskServer() {}
|
||||
|
||||
// UnsafeTaskServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to TaskServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeTaskServer interface {
|
||||
mustEmbedUnimplementedTaskServer()
|
||||
}
|
||||
|
||||
func RegisterTaskServer(s grpc.ServiceRegistrar, srv TaskServer) {
|
||||
s.RegisterService(&Task_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Task_UnaryTask_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(TaskRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TaskServer).UnaryTask(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/pb.Task/UnaryTask",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TaskServer).UnaryTask(ctx, req.(*TaskRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Task_ServerStreamingTask_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
m := new(TaskRequest)
|
||||
if err := stream.RecvMsg(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return srv.(TaskServer).ServerStreamingTask(m, &taskServerStreamingTaskServer{stream})
|
||||
}
|
||||
|
||||
type Task_ServerStreamingTaskServer interface {
|
||||
Send(*TaskResponse) error
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type taskServerStreamingTaskServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *taskServerStreamingTaskServer) Send(m *TaskResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func _Task_ClientStreamingTask_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
return srv.(TaskServer).ClientStreamingTask(&taskClientStreamingTaskServer{stream})
|
||||
}
|
||||
|
||||
type Task_ClientStreamingTaskServer interface {
|
||||
SendAndClose(*TaskResponse) error
|
||||
Recv() (*TaskRequest, error)
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type taskClientStreamingTaskServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *taskClientStreamingTaskServer) SendAndClose(m *TaskResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *taskClientStreamingTaskServer) Recv() (*TaskRequest, error) {
|
||||
m := new(TaskRequest)
|
||||
if err := x.ServerStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func _Task_BidirectionalStreamingTask_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
return srv.(TaskServer).BidirectionalStreamingTask(&taskBidirectionalStreamingTaskServer{stream})
|
||||
}
|
||||
|
||||
type Task_BidirectionalStreamingTaskServer interface {
|
||||
Send(*TaskResponse) error
|
||||
Recv() (*TaskRequest, error)
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type taskBidirectionalStreamingTaskServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *taskBidirectionalStreamingTaskServer) Send(m *TaskResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *taskBidirectionalStreamingTaskServer) Recv() (*TaskRequest, error) {
|
||||
m := new(TaskRequest)
|
||||
if err := x.ServerStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Task_ServiceDesc is the grpc.ServiceDesc for Task service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Task_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "pb.Task",
|
||||
HandlerType: (*TaskServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "UnaryTask",
|
||||
Handler: _Task_UnaryTask_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "ServerStreamingTask",
|
||||
Handler: _Task_ServerStreamingTask_Handler,
|
||||
ServerStreams: true,
|
||||
},
|
||||
{
|
||||
StreamName: "ClientStreamingTask",
|
||||
Handler: _Task_ClientStreamingTask_Handler,
|
||||
ClientStreams: true,
|
||||
},
|
||||
{
|
||||
StreamName: "BidirectionalStreamingTask",
|
||||
Handler: _Task_BidirectionalStreamingTask_Handler,
|
||||
ServerStreams: true,
|
||||
ClientStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "pb/task.proto",
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v9"
|
||||
)
|
||||
|
||||
// Publish 发布
|
||||
// ctx 上下文
|
||||
// channel 频道
|
||||
// message 消息
|
||||
func (c *Client) Publish(ctx context.Context, channel string, message interface{}) error {
|
||||
publish, err := c.cache.redisClient.Publish(ctx, channel, message).Result()
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("发布失败:%s %s %v %s\n", channel, message, publish, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type SubscribeResult struct {
|
||||
err error
|
||||
Message *redis.PubSub
|
||||
}
|
||||
|
||||
// Subscribe 订阅
|
||||
func (c *Client) Subscribe(ctx context.Context) SubscribeResult {
|
||||
return SubscribeResult{
|
||||
Message: c.cache.redisClient.Subscribe(ctx, c.cache.cornKeyPrefix+"_"+c.cache.cornKeyCustom),
|
||||
}
|
||||
}
|
||||
|
||||
// PSubscribe 订阅,支持通配符匹配(ch_user_*)
|
||||
func (c *Client) PSubscribe(ctx context.Context) SubscribeResult {
|
||||
return SubscribeResult{
|
||||
Message: c.cache.redisClient.PSubscribe(ctx, c.cache.cornKeyPrefix+"_"+c.cache.cornKeyCustom+"_*"),
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
)
|
||||
|
||||
// Run 运行
|
||||
func (c *Client) Run(ctx context.Context, task jobs_gorm_model.Task, taskResultCode int, taskResultDesc string) {
|
||||
|
||||
runId := gotrace_id.GetTraceIdContext(ctx)
|
||||
if runId == "" {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Error("上下文没有跟踪编号")
|
||||
return
|
||||
}
|
||||
|
||||
c.GormTaskLogRecord(ctx, task, runId, taskResultCode, taskResultDesc)
|
||||
if c.mongoConfig.stats {
|
||||
c.MongoTaskLogRecord(ctx, task, runId, taskResultCode, taskResultDesc)
|
||||
}
|
||||
|
||||
switch taskResultCode {
|
||||
case 0:
|
||||
err := c.EditTask(c.gormClient.GetDb(), task.Id).
|
||||
Select("run_id", "result", "next_run_time").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
RunId: runId,
|
||||
Result: taskResultDesc,
|
||||
NextRunTime: gotime.Current().AfterSeconds(task.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
|
||||
}
|
||||
return
|
||||
case CodeSuccess:
|
||||
// 执行成功
|
||||
err := c.EditTask(c.gormClient.GetDb(), task.Id).
|
||||
Select("status_desc", "number", "run_id", "updated_ip", "result", "next_run_time").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
StatusDesc: "执行成功",
|
||||
Number: task.Number + 1,
|
||||
RunId: runId,
|
||||
UpdatedIp: c.config.systemOutsideIp,
|
||||
Result: taskResultDesc,
|
||||
NextRunTime: gotime.Current().AfterSeconds(task.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
|
||||
}
|
||||
case CodeEnd:
|
||||
// 执行成功、提前结束
|
||||
err := c.EditTask(c.gormClient.GetDb(), task.Id).
|
||||
Select("status", "status_desc", "number", "updated_ip", "result", "next_run_time").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Status: TASK_SUCCESS,
|
||||
StatusDesc: "结束执行",
|
||||
Number: task.Number + 1,
|
||||
UpdatedIp: c.config.systemOutsideIp,
|
||||
Result: taskResultDesc,
|
||||
NextRunTime: gotime.Current().Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
|
||||
}
|
||||
case CodeError:
|
||||
// 执行失败
|
||||
err := c.EditTask(c.gormClient.GetDb(), task.Id).
|
||||
Select("status_desc", "number", "run_id", "updated_ip", "result", "next_run_time").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
StatusDesc: "执行失败",
|
||||
Number: task.Number + 1,
|
||||
RunId: runId,
|
||||
UpdatedIp: c.config.systemOutsideIp,
|
||||
Result: taskResultDesc,
|
||||
NextRunTime: gotime.Current().AfterSeconds(task.Frequency).Time,
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if task.MaxNumber != 0 {
|
||||
if task.Number+1 >= task.MaxNumber {
|
||||
// 关闭执行
|
||||
err := c.EditTask(c.gormClient.GetDb(), task.Id).
|
||||
Select("status").
|
||||
Updates(jobs_gorm_model.Task{
|
||||
Status: TASK_TIMEOUT,
|
||||
}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package gojobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||||
)
|
||||
|
||||
type TaskLockOperation struct {
|
||||
client *Client // 实例
|
||||
task jobs_gorm_model.Task // 任务
|
||||
}
|
||||
|
||||
func (c *Client) NewLock(task jobs_gorm_model.Task) (*TaskLockOperation, error) {
|
||||
if task.Id == 0 {
|
||||
return nil, errors.New("任务数据不正常")
|
||||
}
|
||||
return &TaskLockOperation{
|
||||
client: c,
|
||||
task: task,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Lock 上锁
|
||||
func (tlo *TaskLockOperation) Lock(ctx context.Context, id any) error {
|
||||
_, err := tlo.client.Lock(ctx, tlo.task, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// Unlock 解锁
|
||||
func (tlo *TaskLockOperation) Unlock(ctx context.Context, id any) error {
|
||||
return tlo.client.Unlock(ctx, tlo.task, id)
|
||||
}
|
||||
|
||||
// LockForever 永远上锁
|
||||
func (tlo *TaskLockOperation) LockForever(ctx context.Context, id any) error {
|
||||
_, err := tlo.client.LockForever(ctx, tlo.task, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// LockId 上锁
|
||||
func (tlo *TaskLockOperation) LockId(ctx context.Context) error {
|
||||
_, err := tlo.client.LockId(ctx, tlo.task)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnlockId 解锁
|
||||
func (tlo *TaskLockOperation) UnlockId(ctx context.Context) error {
|
||||
return tlo.client.UnlockId(ctx, tlo.task)
|
||||
}
|
||||
|
||||
// LockForeverId 永远上锁
|
||||
func (tlo *TaskLockOperation) LockForeverId(ctx context.Context) error {
|
||||
_, err := tlo.client.LockForeverId(ctx, tlo.task)
|
||||
return err
|
||||
}
|
||||
|
||||
// LockCustomId 上锁
|
||||
func (tlo *TaskLockOperation) LockCustomId(ctx context.Context) error {
|
||||
_, err := tlo.client.LockCustomId(ctx, tlo.task)
|
||||
return err
|
||||
}
|
||||
|
||||
// UnlockCustomId 解锁
|
||||
func (tlo *TaskLockOperation) UnlockCustomId(ctx context.Context) error {
|
||||
return tlo.client.UnlockCustomId(ctx, tlo.task)
|
||||
}
|
||||
|
||||
// LockForeverCustomId 永远上锁
|
||||
func (tlo *TaskLockOperation) LockForeverCustomId(ctx context.Context) error {
|
||||
_, err := tlo.client.LockForeverCustomId(ctx, tlo.task)
|
||||
return err
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
const Version = "1.0.34"
|
@ -1,7 +0,0 @@
|
||||
package gojobs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
t.Log(Version)
|
||||
}
|
Loading…
Reference in new issue