- update jobs

- update ip
master
李光春 1 year ago
parent fd91f6808d
commit 9ff541c4af

@ -1,53 +1,53 @@
package goip
import (
"net"
"strconv"
)
var (
ipv4 = "IPV4"
ipv6 = "IPV6"
)
type AnalyseResult struct {
IP string `json:"ip,omitempty"` // 输入的ip地址
Country string `json:"country,omitempty"` // 国家或地区
Province string `json:"province,omitempty"` // 省份
City string `json:"city,omitempty"` // 城市
Area string `json:"area,omitempty"` // 区域
Isp string `json:"isp,omitempty"` // 运营商
Ip string `json:"ip"` // ip
Continent string `json:"continent"` // 大陆
Country string `json:"country"` // 国家
Province string `json:"province"` // 省份
City string `json:"city"` // 城市
Isp string `json:"isp"` // 运营商
LocationTimeZone string `json:"location_time_zone"` // 位置时区
LocationLatitude float64 `json:"location_latitude"` // 位置纬度
LocationLongitude float64 `json:"location_longitude"` // 位置经度
}
func (c *Client) Analyse(item string) AnalyseResult {
isIp := c.isIpv4OrIpv6(item)
ipByte := net.ParseIP(item)
switch isIp {
case ipv4:
info := c.V4db.Find(item)
search, err := c.V4Region.MemorySearch(item)
if err != nil {
return AnalyseResult{
IP: info.IP,
Country: info.Country,
Area: info.Area,
}
} else {
return AnalyseResult{
IP: search.IP,
Country: search.Country,
Province: search.Province,
City: search.City,
Isp: info.Area,
}
ip2regionV2Info, _ := c.QueryIp2RegionV2(ipByte)
geoIpInfo, _ := c.QueryGeoIp(ipByte)
return AnalyseResult{
Ip: ipByte.String(),
Continent: geoIpInfo.Continent.Name,
Country: geoIpInfo.Country.Name,
Province: ip2regionV2Info.Province,
City: ip2regionV2Info.City,
Isp: ip2regionV2Info.Operator,
LocationTimeZone: geoIpInfo.Location.TimeZone,
LocationLatitude: geoIpInfo.Location.Latitude,
LocationLongitude: geoIpInfo.Location.Longitude,
}
case ipv6:
info := c.V6db.Find(item)
geoIpInfo, _ := c.QueryGeoIp(ipByte)
ipv6Info, _ := c.QueryIpv6wry(ipByte)
return AnalyseResult{
IP: info.IP,
Country: info.Country,
Province: info.Province,
City: info.City,
Area: info.Area,
Isp: info.Isp,
Ip: ipByte.String(),
Continent: geoIpInfo.Continent.Name,
Country: geoIpInfo.Country.Name,
Province: ipv6Info.Province,
City: ipv6Info.City,
Isp: ipv6Info.Isp,
LocationTimeZone: geoIpInfo.Location.TimeZone,
LocationLatitude: geoIpInfo.Location.Latitude,
LocationLongitude: geoIpInfo.Location.Longitude,
}
default:
return AnalyseResult{}

@ -0,0 +1,39 @@
package goip
import (
"github.com/dtapps/go-library/utils/goip/geoip"
"github.com/dtapps/go-library/utils/goip/ip2region"
"github.com/dtapps/go-library/utils/goip/ip2region_v2"
"github.com/dtapps/go-library/utils/goip/ipv6wry"
"github.com/dtapps/go-library/utils/goip/qqwry"
)
type Client struct {
ip2regionV2Client *ip2region_v2.Client
ip2regionClient *ip2region.Client
qqwryClient *qqwry.Client
geoIpClient *geoip.Client
ipv6wryClient *ipv6wry.Client
}
// NewIp 实例化
func NewIp() *Client {
c := &Client{}
c.ip2regionV2Client, _ = ip2region_v2.New()
c.ip2regionClient = ip2region.New()
c.qqwryClient = qqwry.New()
c.geoIpClient, _ = geoip.New()
c.ipv6wryClient = ipv6wry.New()
return c
}
func (c *Client) Close() {
c.geoIpClient.Close()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 MiB

@ -0,0 +1,52 @@
package geoip
import (
_ "embed"
"github.com/oschwald/geoip2-golang"
)
//go:embed GeoLite2-ASN.mmdb
var asnBuff []byte
//go:embed GeoLite2-City.mmdb
var cityBuff []byte
//go:embed GeoLite2-Country.mmdb
var countryBuff []byte
type Client struct {
asnDb *geoip2.Reader
cityDb *geoip2.Reader
countryDb *geoip2.Reader
}
func New() (*Client, error) {
var err error
c := &Client{}
c.asnDb, err = geoip2.FromBytes(asnBuff)
if err != nil {
return nil, err
}
c.cityDb, err = geoip2.FromBytes(cityBuff)
if err != nil {
return nil, err
}
c.countryDb, err = geoip2.FromBytes(countryBuff)
if err != nil {
return nil, err
}
return c, err
}
func (c *Client) Close() {
c.asnDb.Close()
c.cityDb.Close()
c.countryDb.Close()
}

@ -0,0 +1,23 @@
package geoip
import (
"io/ioutil"
"log"
"net/http"
)
func OnlineDownload(downloadUrl string, downloadName string) {
resp, err := http.Get(downloadUrl)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
err = ioutil.WriteFile("./"+downloadName, body, 0644)
if err != nil {
panic(err)
}
log.Printf("已下载最新 ip2region.xdb 数据库 %s ", "./"+downloadName)
}

@ -0,0 +1,31 @@
package geoip
import (
"github.com/dtapps/go-library/utils/gostring"
)
var licenseKey = "" // 许可证密钥
func GetGeoLite2AsnDownloadUrl(licenseKey string) string {
return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=YOUR_LICENSE_KEY&suffix=tar.gz", "YOUR_LICENSE_KEY", licenseKey)
}
//func GetGeoLite2AsnCsvDownloadUrl(licenseKey string) string {
// return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN-CSV&license_key=YOUR_LICENSE_KEY&suffix=zip", "YOUR_LICENSE_KEY", licenseKey)
//}
func GetGeoLite2CityDownloadUrl(licenseKey string) string {
return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=YOUR_LICENSE_KEY&suffix=tar.gz", "YOUR_LICENSE_KEY", licenseKey)
}
//func GetGeoLite2CityCsvDownloadUrl(licenseKey string) string {
// return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&license_key=YOUR_LICENSE_KEY&suffix=zip", "YOUR_LICENSE_KEY", licenseKey)
//}
func GetGeoLite2CountryDownloadUrl(licenseKey string) string {
return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_LICENSE_KEY&suffix=tar.gz", "YOUR_LICENSE_KEY", licenseKey)
}
//func GetGeoLite2CountryCsvDownloadUrl(licenseKey string) string {
// return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&license_key=YOUR_LICENSE_KEY&suffix=zip", "YOUR_LICENSE_KEY", licenseKey)
//}

@ -0,0 +1,66 @@
package geoip
import (
_ "embed"
"net"
)
// QueryCityResult 返回
type QueryCityResult struct {
Ip string `json:"ip,omitempty"` // ip
Continent struct {
Code string `json:"code,omitempty"` // 大陆代码
Name string `json:"name,omitempty"` // 大陆名称
} `json:"continent,omitempty"`
Country struct {
Code string `json:"code,omitempty"` // 国家代码
Name string `json:"name,omitempty"` // 国家名称
} `json:"country,omitempty"`
Province struct {
Code string `json:"code,omitempty"` // 省份代码
Name string `json:"name,omitempty"` // 省份名称
} `json:"province,omitempty"`
City struct {
Name string `json:"name,omitempty"` // 城市名称
} `json:"city,omitempty"`
Location struct {
TimeZone string `json:"time_zone,omitempty"` // 位置时区
Latitude float64 `json:"latitude,omitempty"` // 坐标纬度
Longitude float64 `json:"longitude,omitempty"` // 坐标经度
} `json:"location,omitempty"`
}
func (c *Client) QueryCity(ipAddress net.IP) (result QueryCityResult, err error) {
record, err := c.cityDb.City(ipAddress)
if err != nil {
return QueryCityResult{}, err
}
// ip
result.Ip = ipAddress.String()
// 大陆
result.Continent.Code = record.Continent.Code
result.Continent.Name = record.Continent.Names["zh-CN"]
// 国家
result.Country.Code = record.Country.IsoCode
result.Country.Name = record.Country.Names["zh-CN"]
// 省份
if len(record.Subdivisions) > 0 {
result.Province.Code = record.Subdivisions[0].IsoCode
result.Province.Name = record.Subdivisions[0].Names["zh-CN"]
}
// 城市
result.City.Name = record.City.Names["zh-CN"]
// 位置
result.Location.TimeZone = record.Location.TimeZone
result.Location.Latitude = record.Location.Latitude
result.Location.Longitude = record.Location.Longitude
return result, err
}

@ -1,63 +0,0 @@
package goip
import (
"github.com/dtapps/go-library/utils/goip/ip2region"
"github.com/dtapps/go-library/utils/goip/v4"
"github.com/dtapps/go-library/utils/goip/v6"
"log"
"strings"
)
type Client struct {
V4Region ip2region.Ip2Region // IPV4
V4db v4.Pointer // IPV4
V6db v6.Pointer // IPV6
}
// NewIp 实例化
func NewIp() *Client {
app := &Client{}
v4Num := app.V4db.InitIPV4Data()
log.Printf("IPV4 库加载完成 共加载:%d 条 IP 记录\n", v4Num)
v6Num := app.V6db.InitIPV4Data()
log.Printf("IPV6 库加载完成 共加载:%d 条 IP 记录\n", v6Num)
return app
}
func (c *Client) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) {
res = c.V4db.Find(ip)
resInfo, _ = c.V4Region.MemorySearch(ip)
return res, resInfo
}
func (c *Client) Ipv6(ip string) (res v6.Result) {
res = c.V6db.Find(ip)
return res
}
func (c *Client) isIpv4OrIpv6(ip string) string {
if len(ip) < 7 {
return ""
}
arrIpv4 := strings.Split(ip, ".")
if len(arrIpv4) == 4 {
//. 判断IPv4
for _, val := range arrIpv4 {
if !c.CheckIpv4(val) {
return ""
}
}
return ipv4
}
arrIpv6 := strings.Split(ip, ":")
if len(arrIpv6) == 8 {
// 判断Ipv6
for _, val := range arrIpv6 {
if !c.CheckIpv6(val) {
return "Neither"
}
}
return ipv6
}
return ""
}

@ -4,11 +4,13 @@ import (
"context"
"encoding/json"
"github.com/dtapps/go-library/utils/gorequest"
"log"
"net"
)
// GetInsideIp 内网ip
func GetInsideIp(ctx context.Context) string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
panic(err)
@ -47,30 +49,35 @@ func Ips(ctx context.Context) (map[string]string, error) {
return ips, nil
}
var respGetOutsideIp struct {
Data struct {
Ip string `json:"ip"`
} `json:"data"`
}
// GetOutsideIp 外网ip
func GetOutsideIp(ctx context.Context) (ip string) {
ip = "0.0.0.0"
get := gorequest.NewHttp()
get.SetUri("https://api.dtapp.net/ip")
response, err := get.Get(ctx)
func GetOutsideIp(ctx context.Context) string {
// 返回结果
type respGetOutsideIp struct {
Data struct {
Ip string `json:"ip,omitempty"`
} `json:"data"`
}
// 请求
getHttp := gorequest.NewHttp()
getHttp.SetUri("https://api.dtapp.net/ip")
response, err := getHttp.Get(ctx)
if err != nil {
return
log.Printf("[GetOutsideIp]getHttp.Get%s\n", err)
return "0.0.0.0"
}
err = json.Unmarshal(response.ResponseBody, &respGetOutsideIp)
// 解析
var responseJson respGetOutsideIp
err = json.Unmarshal(response.ResponseBody, &responseJson)
if err != nil {
return
log.Printf("[GetOutsideIp]json.Unmarshal%s\n", err)
return "0.0.0.0"
}
if respGetOutsideIp.Data.Ip == "" {
return
if responseJson.Data.Ip == "" {
responseJson.Data.Ip = "0.0.0.0"
}
ip = respGetOutsideIp.Data.Ip
return respGetOutsideIp.Data.Ip
return responseJson.Data.Ip
}
// GetMacAddr 获取Mac地址

@ -0,0 +1,103 @@
package ip2region
import (
_ "embed"
"errors"
"github.com/dtapps/go-library/utils/gostring"
"os"
"strconv"
"strings"
)
const (
IndexBlockLength = 12
)
//go:embed ip2region.db
var dbBuff []byte
type Client struct {
// db file handler
dbFileHandler *os.File
//header block info
headerSip []int64
headerPtr []int64
headerLen int64
// super block index info
firstIndexPtr int64
lastIndexPtr int64
totalBlocks int64
// for memory mode only
// the original db binary string
dbFile string
}
func New() *Client {
c := &Client{}
return c
}
// 获取Ip信息
func getIpInfo(ipStr string, cityId int64, line []byte) (result QueryResult) {
lineSlice := strings.Split(string(line), "|")
length := len(lineSlice)
result.CityId = cityId
if length < 5 {
for i := 0; i <= 5-length; i++ {
lineSlice = append(lineSlice, "")
}
}
if lineSlice[0] != "0" {
result.Country = gostring.SpaceAndLineBreak(lineSlice[0])
}
if lineSlice[1] != "0" {
result.Region = gostring.SpaceAndLineBreak(lineSlice[1])
}
if lineSlice[2] != "0" {
result.Province = gostring.SpaceAndLineBreak(lineSlice[2])
}
if lineSlice[3] != "0" {
result.City = gostring.SpaceAndLineBreak(lineSlice[3])
}
if lineSlice[4] != "0" {
result.Isp = gostring.SpaceAndLineBreak(lineSlice[4])
}
result.Ip = ipStr
return result
}
func getLong(b []byte, offset int64) int64 {
val := int64(b[offset]) |
int64(b[offset+1])<<8 |
int64(b[offset+2])<<16 |
int64(b[offset+3])<<24
return val
}
func ip2long(IpStr string) (int64, error) {
bits := strings.Split(IpStr, ".")
if len(bits) != 4 {
return 0, errors.New("ip format error")
}
var sum int64
for i, n := range bits {
bit, _ := strconv.ParseInt(n, 10, 64)
sum += bit << uint(24-8*i)
}
return sum, nil
}

@ -2,16 +2,22 @@ package ip2region
import (
"io/ioutil"
"log"
"net/http"
)
func getOnline() ([]byte, error) {
resp, err := http.Get("https://ghproxy.com/?q=https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.db?raw=true")
func OnlineDownload() {
resp, err := http.Get("https://ghproxy.com/?q=https://github.com/lionsoul2014/ip2region/blob/master/v1.0/data/ip2region.db?raw=true")
if err != nil {
return nil, err
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
err = ioutil.WriteFile("./ip2region.db", body, 0644)
if err != nil {
panic(err)
}
log.Printf("已下载最新 ip2region 数据库 %s ", "./ip2region.db")
}

@ -1,177 +0,0 @@
package ip2region
import (
_ "embed"
"errors"
"github.com/dtapps/go-library/utils/gostring"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
const (
IndexBlockLength = 12
)
type Ip2Region struct {
// db file handler
dbFileHandler *os.File
//header block info
headerSip []int64
headerPtr []int64
headerLen int64
// super block index info
firstIndexPtr int64
lastIndexPtr int64
totalBlocks int64
// for memory mode only
// the original db binary string
dbFile string
}
//go:embed ip2region.db
var dbBinStr []byte
type IpInfo struct {
IP string `json:"ip,omitempty"` // 输入的ip地址
CityID int64 `json:"city_id,omitempty"` // 城市ID
Country string `json:"country,omitempty"` // 国家
Region string `json:"region,omitempty"` // 区域
Province string `json:"province,omitempty"` // 省份
City string `json:"city,omitempty"` // 城市
ISP string `json:"isp,omitempty"` // 运营商
}
func (ip IpInfo) String() string {
return ip.IP + "|" + strconv.FormatInt(ip.CityID, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.ISP
}
// 获取Ip信息
func getIpInfo(ipStr string, cityId int64, line []byte) (ipInfo IpInfo) {
lineSlice := strings.Split(string(line), "|")
length := len(lineSlice)
ipInfo.CityID = cityId
if length < 5 {
for i := 0; i <= 5-length; i++ {
lineSlice = append(lineSlice, "")
}
}
if lineSlice[0] != "0" {
ipInfo.Country = gostring.SpaceAndLineBreak(lineSlice[0])
}
if lineSlice[1] != "0" {
ipInfo.Region = gostring.SpaceAndLineBreak(lineSlice[1])
}
if lineSlice[2] != "0" {
ipInfo.Province = gostring.SpaceAndLineBreak(lineSlice[2])
}
if lineSlice[3] != "0" {
ipInfo.City = gostring.SpaceAndLineBreak(lineSlice[3])
}
if lineSlice[4] != "0" {
ipInfo.ISP = gostring.SpaceAndLineBreak(lineSlice[4])
}
ipInfo.IP = ipStr
return ipInfo
}
// MemorySearch memory算法整个数据库全部载入内存单次查询都在0.1x毫秒内
func (r *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error) {
ipInfo.IP = ipStr
if r.totalBlocks == 0 {
if err != nil {
return ipInfo, err
}
r.firstIndexPtr = getLong(dbBinStr, 0)
r.lastIndexPtr = getLong(dbBinStr, 4)
r.totalBlocks = (r.lastIndexPtr-r.firstIndexPtr)/IndexBlockLength + 1
}
ip, err := ip2long(ipStr)
if err != nil {
return ipInfo, err
}
h := r.totalBlocks
var dataPtr, l int64
for l <= h {
m := (l + h) >> 1
p := r.firstIndexPtr + m*IndexBlockLength
sip := getLong(dbBinStr, p)
if ip < sip {
h = m - 1
} else {
eip := getLong(dbBinStr, p+4)
if ip > eip {
l = m + 1
} else {
dataPtr = getLong(dbBinStr, p+8)
break
}
}
}
if dataPtr == 0 {
return ipInfo, errors.New("not found")
}
dataLen := (dataPtr >> 24) & 0xFF
dataPtr = dataPtr & 0x00FFFFFF
ipInfo = getIpInfo(ipStr, getLong(dbBinStr, dataPtr), dbBinStr[(dataPtr)+4:dataPtr+dataLen])
return ipInfo, nil
}
func getLong(b []byte, offset int64) int64 {
val := int64(b[offset]) |
int64(b[offset+1])<<8 |
int64(b[offset+2])<<16 |
int64(b[offset+3])<<24
return val
}
func ip2long(IpStr string) (int64, error) {
bits := strings.Split(IpStr, ".")
if len(bits) != 4 {
return 0, errors.New("ip format error")
}
var sum int64
for i, n := range bits {
bit, _ := strconv.ParseInt(n, 10, 64)
sum += bit << uint(24-8*i)
}
return sum, nil
}
func (r *Ip2Region) OnlineDownload() (err error) {
tmpData, err := getOnline()
if err != nil {
return errors.New("下载失败 %s" + err.Error())
}
if err := ioutil.WriteFile("./ip2region.db", tmpData, 0644); err == nil {
log.Printf("已下载最新 ip2region 数据库 %s ", "./ip2region.db")
} else {
return errors.New("保存失败")
}
return nil
}

@ -0,0 +1,73 @@
package ip2region
import (
"errors"
"net"
"strconv"
)
type QueryResult struct {
Ip string `json:"ip,omitempty"` // ip
CityId int64 `json:"city_id,omitempty"` // 城市代码
Country string `json:"country,omitempty"` // 国家
Region string `json:"region,omitempty"` // 区域
Province string `json:"province,omitempty"` // 省份
City string `json:"city,omitempty"` // 城市
Isp string `json:"isp,omitempty"` // 运营商
}
func (ip QueryResult) String() string {
return ip.Ip + "|" + strconv.FormatInt(ip.CityId, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.Isp
}
// Query memory算法整个数据库全部载入内存单次查询都在0.1x毫秒内
func (c *Client) Query(ipAddress net.IP) (result QueryResult, err error) {
result.Ip = ipAddress.String()
if c.totalBlocks == 0 {
if err != nil {
return QueryResult{}, err
}
c.firstIndexPtr = getLong(dbBuff, 0)
c.lastIndexPtr = getLong(dbBuff, 4)
c.totalBlocks = (c.lastIndexPtr-c.firstIndexPtr)/IndexBlockLength + 1
}
ip, err := ip2long(result.Ip)
if err != nil {
return QueryResult{}, err
}
h := c.totalBlocks
var dataPtr, l int64
for l <= h {
m := (l + h) >> 1
p := c.firstIndexPtr + m*IndexBlockLength
sip := getLong(dbBuff, p)
if ip < sip {
h = m - 1
} else {
eip := getLong(dbBuff, p+4)
if ip > eip {
l = m + 1
} else {
dataPtr = getLong(dbBuff, p+8)
break
}
}
}
if dataPtr == 0 {
return QueryResult{}, errors.New("not found")
}
dataLen := (dataPtr >> 24) & 0xFF
dataPtr = dataPtr & 0x00FFFFFF
result = getIpInfo(result.Ip, getLong(dbBuff, dataPtr), dbBuff[(dataPtr)+4:dataPtr+dataLen])
return result, nil
}

@ -0,0 +1,26 @@
package ip2region_v2
import _ "embed"
//go:embed ip2region.xdb
var cBuff []byte
type Client struct {
db *Searcher
}
func New() (*Client, error) {
var err error
c := &Client{}
// 1、从 dbPath 加载整个 xdb 到内存
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
c.db, err = NewWithBuffer(cBuff)
if err != nil {
return nil, err
}
return c, err
}

@ -0,0 +1,23 @@
package ip2region_v2
import (
"io/ioutil"
"log"
"net/http"
)
func OnlineDownload() {
resp, err := http.Get("https://ghproxy.com/?q=https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb?raw=true")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
err = ioutil.WriteFile("./ip2region.xdb", body, 0644)
if err != nil {
panic(err)
}
log.Printf("已下载最新 ip2region.xdb 数据库 %s ", "./ip2region.xdb")
}

@ -0,0 +1,52 @@
package ip2region_v2
import (
_ "embed"
"github.com/dtapps/go-library/utils/gostring"
"net"
)
// QueryResult 返回
type QueryResult struct {
Ip string `json:"ip,omitempty"` // ip
Country string `json:"country,omitempty"` // 国家
Province string `json:"province,omitempty"` // 省份
City string `json:"city,omitempty"` // 城市
Operator string `json:"operator,omitempty"` // 运营商
}
func (c *Client) Query(ipAddress net.IP) (result QueryResult, err error) {
// 备注:并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
str, err := c.db.SearchByStr(ipAddress.String())
if err != nil {
return QueryResult{}, err
}
split := gostring.Split(str, "|")
if len(split) <= 0 {
return QueryResult{}, err
}
result.Ip = ipAddress.String()
result.Country = split[0]
if result.Country == "0" {
result.Country = ""
}
result.Province = split[2]
if result.Province == "0" {
result.Province = ""
}
result.City = split[3]
if result.City == "0" {
result.City = ""
}
result.Operator = split[4]
if result.Operator == "0" {
result.Operator = ""
}
return result, err
}

@ -0,0 +1,240 @@
package ip2region_v2
import (
"encoding/binary"
"fmt"
"os"
)
const (
HeaderInfoLength = 256
VectorIndexRows = 256
VectorIndexCols = 256
VectorIndexSize = 8
SegmentIndexBlockSize = 14
)
// --- Index policy define
type IndexPolicy int
const (
VectorIndexPolicy IndexPolicy = 1
BTreeIndexPolicy IndexPolicy = 2
)
func (i IndexPolicy) String() string {
switch i {
case VectorIndexPolicy:
return "VectorIndex"
case BTreeIndexPolicy:
return "BtreeIndex"
default:
return "unknown"
}
}
// --- Header define
type Header struct {
// data []byte
Version uint16
IndexPolicy IndexPolicy
CreatedAt uint32
StartIndexPtr uint32
EndIndexPtr uint32
}
func NewHeader(input []byte) (*Header, error) {
if len(input) < 16 {
return nil, fmt.Errorf("invalid input buffer")
}
return &Header{
Version: binary.LittleEndian.Uint16(input),
IndexPolicy: IndexPolicy(binary.LittleEndian.Uint16(input[2:])),
CreatedAt: binary.LittleEndian.Uint32(input[4:]),
StartIndexPtr: binary.LittleEndian.Uint32(input[8:]),
EndIndexPtr: binary.LittleEndian.Uint32(input[12:]),
}, nil
}
// --- searcher implementation
type Searcher struct {
handle *os.File
// header info
header *Header
ioCount int
// use it only when this feature enabled.
// Preload the vector index will reduce the number of IO operations
// thus speedup the search process
vectorIndex []byte
// content buffer.
// running with the whole xdb file cached
contentBuff []byte
}
func baseNew(dbFile string, vIndex []byte, cBuff []byte) (*Searcher, error) {
var err error
// content buff first
if cBuff != nil {
return &Searcher{
vectorIndex: nil,
contentBuff: cBuff,
}, nil
}
// open the xdb binary file
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
if err != nil {
return nil, err
}
return &Searcher{
handle: handle,
vectorIndex: vIndex,
}, nil
}
func NewWithFileOnly(dbFile string) (*Searcher, error) {
return baseNew(dbFile, nil, nil)
}
func NewWithVectorIndex(dbFile string, vIndex []byte) (*Searcher, error) {
return baseNew(dbFile, vIndex, nil)
}
func NewWithBuffer(cBuff []byte) (*Searcher, error) {
return baseNew("", nil, cBuff)
}
func (s *Searcher) Close() {
if s.handle != nil {
err := s.handle.Close()
if err != nil {
return
}
}
}
// GetIOCount return the global io count for the last search
func (s *Searcher) GetIOCount() int {
return s.ioCount
}
// SearchByStr find the region for the specified ip string
func (s *Searcher) SearchByStr(str string) (string, error) {
ip, err := CheckIP(str)
if err != nil {
return "", err
}
return s.Search(ip)
}
// Search find the region for the specified long ip
func (s *Searcher) Search(ip uint32) (string, error) {
// reset the global ioCount
s.ioCount = 0
// locate the segment index block based on the vector index
var il0 = (ip >> 24) & 0xFF
var il1 = (ip >> 16) & 0xFF
var idx = il0*VectorIndexCols*VectorIndexSize + il1*VectorIndexSize
var sPtr, ePtr = uint32(0), uint32(0)
if s.vectorIndex != nil {
sPtr = binary.LittleEndian.Uint32(s.vectorIndex[idx:])
ePtr = binary.LittleEndian.Uint32(s.vectorIndex[idx+4:])
} else if s.contentBuff != nil {
sPtr = binary.LittleEndian.Uint32(s.contentBuff[HeaderInfoLength+idx:])
ePtr = binary.LittleEndian.Uint32(s.contentBuff[HeaderInfoLength+idx+4:])
} else {
// read the vector index block
var buff = make([]byte, VectorIndexSize)
err := s.read(int64(HeaderInfoLength+idx), buff)
if err != nil {
return "", fmt.Errorf("read vector index block at %d: %w", HeaderInfoLength+idx, err)
}
sPtr = binary.LittleEndian.Uint32(buff)
ePtr = binary.LittleEndian.Uint32(buff[4:])
}
// fmt.Printf("sPtr=%d, ePtr=%d", sPtr, ePtr)
// binary search the segment index to get the region
var dataLen, dataPtr = 0, uint32(0)
var buff = make([]byte, SegmentIndexBlockSize)
var l, h = 0, int((ePtr - sPtr) / SegmentIndexBlockSize)
for l <= h {
m := (l + h) >> 1
p := sPtr + uint32(m*SegmentIndexBlockSize)
err := s.read(int64(p), buff)
if err != nil {
return "", fmt.Errorf("read segment index at %d: %w", p, err)
}
// decode the data step by step to reduce the unnecessary operations
sip := binary.LittleEndian.Uint32(buff)
if ip < sip {
h = m - 1
} else {
eip := binary.LittleEndian.Uint32(buff[4:])
if ip > eip {
l = m + 1
} else {
dataLen = int(binary.LittleEndian.Uint16(buff[8:]))
dataPtr = binary.LittleEndian.Uint32(buff[10:])
break
}
}
}
//fmt.Printf("dataLen: %d, dataPtr: %d", dataLen, dataPtr)
if dataLen == 0 {
return "", nil
}
// load and return the region data
var regionBuff = make([]byte, dataLen)
err := s.read(int64(dataPtr), regionBuff)
if err != nil {
return "", fmt.Errorf("read region at %d: %w", dataPtr, err)
}
return string(regionBuff), nil
}
// do the data read operation based on the setting.
// content buffer first or will read from the file.
// this operation will invoke the Seek for file based read.
func (s *Searcher) read(offset int64, buff []byte) error {
if s.contentBuff != nil {
cLen := copy(buff, s.contentBuff[offset:])
if cLen != len(buff) {
return fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
}
} else {
_, err := s.handle.Seek(offset, 0)
if err != nil {
return fmt.Errorf("seek to %d: %w", offset, err)
}
s.ioCount++
rLen, err := s.handle.Read(buff)
if err != nil {
return fmt.Errorf("handle read: %w", err)
}
if rLen != len(buff) {
return fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
}
}
return nil
}

@ -0,0 +1,165 @@
package ip2region_v2
import (
"fmt"
"os"
"strconv"
"strings"
)
var shiftIndex = []int{24, 16, 8, 0}
func CheckIP(ip string) (uint32, error) {
var ps = strings.Split(ip, ".")
if len(ps) != 4 {
return 0, fmt.Errorf("invalid ip address `%s`", ip)
}
var val = uint32(0)
for i, s := range ps {
d, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("the %dth part `%s` is not an integer", i, s)
}
if d < 0 || d > 255 {
return 0, fmt.Errorf("the %dth part `%s` should be an integer bettween 0 and 255", i, s)
}
val |= uint32(d) << shiftIndex[i]
}
// convert the ip to integer
return val, nil
}
func Long2IP(ip uint32) string {
return fmt.Sprintf("%d.%d.%d.%d", (ip>>24)&0xFF, (ip>>16)&0xFF, (ip>>8)&0xFF, ip&0xFF)
}
func MidIP(sip uint32, eip uint32) uint32 {
return uint32((uint64(sip) + uint64(eip)) >> 1)
}
// LoadHeader load the header info from the specified handle
func LoadHeader(handle *os.File) (*Header, error) {
_, err := handle.Seek(0, 0)
if err != nil {
return nil, fmt.Errorf("seek to the header: %w", err)
}
var buff = make([]byte, HeaderInfoLength)
rLen, err := handle.Read(buff)
if err != nil {
return nil, err
}
if rLen != len(buff) {
return nil, fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
}
return NewHeader(buff)
}
// LoadHeaderFromFile load header info from the specified db file path
func LoadHeaderFromFile(dbFile string) (*Header, error) {
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
if err != nil {
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
}
header, err := LoadHeader(handle)
if err != nil {
return nil, err
}
_ = handle.Close()
return header, nil
}
// LoadHeaderFromBuff wrap the header info from the content buffer
func LoadHeaderFromBuff(cBuff []byte) (*Header, error) {
return NewHeader(cBuff[0:256])
}
// LoadVectorIndex util function to load the vector index from the specified file handle
func LoadVectorIndex(handle *os.File) ([]byte, error) {
// load all the vector index block
_, err := handle.Seek(HeaderInfoLength, 0)
if err != nil {
return nil, fmt.Errorf("seek to vector index: %w", err)
}
var buff = make([]byte, VectorIndexRows*VectorIndexCols*VectorIndexSize)
rLen, err := handle.Read(buff)
if err != nil {
return nil, err
}
if rLen != len(buff) {
return nil, fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
}
return buff, nil
}
// LoadVectorIndexFromFile load vector index from a specified file path
func LoadVectorIndexFromFile(dbFile string) ([]byte, error) {
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
if err != nil {
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
}
vIndex, err := LoadVectorIndex(handle)
if err != nil {
return nil, err
}
_ = handle.Close()
return vIndex, nil
}
// LoadContent load the whole xdb content from the specified file handle
func LoadContent(handle *os.File) ([]byte, error) {
// get file size
fi, err := handle.Stat()
if err != nil {
return nil, fmt.Errorf("stat: %w", err)
}
size := fi.Size()
// seek to the head of the file
_, err = handle.Seek(0, 0)
if err != nil {
return nil, fmt.Errorf("seek to get xdb file length: %w", err)
}
var buff = make([]byte, size)
rLen, err := handle.Read(buff)
if err != nil {
return nil, err
}
if rLen != len(buff) {
return nil, fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
}
return buff, nil
}
// LoadContentFromFile load the whole xdb content from the specified db file path
func LoadContentFromFile(dbFile string) ([]byte, error) {
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
if err != nil {
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
}
cBuff, err := LoadContent(handle)
if err != nil {
return nil, err
}
_ = handle.Close()
return cBuff, nil
}

@ -0,0 +1,138 @@
package ipv6wry
import (
_ "embed"
"encoding/binary"
"log"
)
var (
header []byte
country []byte
area []byte
v6ip uint64
offset uint32
start uint32
end uint32
)
//go:embed ipv6wry.db
var datBuff []byte
type Client struct {
Offset uint32
ItemLen uint32
IndexLen uint32
}
func New() *Client {
c := &Client{}
buf := datBuff[0:8]
start := binary.LittleEndian.Uint32(buf[:4])
end := binary.LittleEndian.Uint32(buf[4:])
num := int64((end-start)/7 + 1)
log.Printf("ipv6wry.db 共加载:%d 条ip记录\n", num)
return c
}
// ReadData 从文件中读取数据
func (c *Client) readData(length uint32) (rs []byte) {
end := c.Offset + length
dataNum := uint32(len(datBuff))
if c.Offset > dataNum {
return nil
}
if end > dataNum {
end = dataNum
}
rs = datBuff[c.Offset:end]
c.Offset = end
return rs
}
func (c *Client) getAddr() ([]byte, []byte) {
mode := c.readData(1)[0]
if mode == 0x01 {
// [IP][0x01][国家和地区信息的绝对偏移地址]
c.Offset = byteToUInt32(c.readData(3))
return c.getAddr()
}
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
_offset := c.Offset - 1
c1 := c.readArea(_offset)
if mode == 0x02 {
c.Offset = 4 + _offset
} else {
c.Offset = _offset + uint32(1+len(c1))
}
c2 := c.readArea(c.Offset)
return c1, c2
}
func (c *Client) readArea(offset uint32) []byte {
c.Offset = offset
mode := c.readData(1)[0]
if mode == 0x01 || mode == 0x02 {
return c.readArea(byteToUInt32(c.readData(3)))
}
c.Offset = offset
return c.readString()
}
func (c *Client) readString() []byte {
data := make([]byte, 0)
for {
buf := c.readData(1)
if buf[0] == 0 {
break
}
data = append(data, buf[0])
}
return data
}
func (c *Client) searchIndex(ip uint64) uint32 {
c.ItemLen = 8
c.IndexLen = 11
header = datBuff[8:24]
start = binary.LittleEndian.Uint32(header[8:])
counts := binary.LittleEndian.Uint32(header[:8])
end = start + counts*c.IndexLen
buf := make([]byte, c.IndexLen)
for {
mid := start + c.IndexLen*(((end-start)/c.IndexLen)>>1)
buf = datBuff[mid : mid+c.IndexLen]
_ip := binary.LittleEndian.Uint64(buf[:c.ItemLen])
if end-start == c.IndexLen {
if ip >= binary.LittleEndian.Uint64(datBuff[end:end+c.ItemLen]) {
buf = datBuff[end : end+c.IndexLen]
}
return byteToUInt32(buf[c.ItemLen:])
}
if _ip > ip {
end = mid
} else if _ip < ip {
start = mid
} else if _ip == ip {
return byteToUInt32(buf[c.ItemLen:])
}
}
}
func byteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}

@ -1,4 +1,4 @@
package v6
package ipv6wry
import (
"github.com/saracen/go7z"
@ -9,28 +9,39 @@ import (
"os"
)
func getOnline() (data []byte, err error) {
func OnlineDownload() {
resp, err := http.Get("https://ip.zxinc.org/ip.7z")
if err != nil {
return nil, err
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
panic(err)
}
file7z, err := ioutil.TempFile("", "*")
if err != nil {
return nil, err
panic(err)
}
defer os.Remove(file7z.Name())
if err := ioutil.WriteFile(file7z.Name(), body, 0644); err == nil {
return Un7z(file7z.Name())
err = ioutil.WriteFile(file7z.Name(), body, 0644)
if err != nil {
panic(err)
}
tmpData, err := Un7z(file7z.Name())
if err != nil {
panic(err)
}
err = ioutil.WriteFile("./ipv6wry.db", tmpData, 0644)
if err != nil {
panic(err)
}
return
log.Printf("已下载最新 ZX IPv6数据库 %s ", "./ipv6wry.db")
}
func Un7z(filePath string) (data []byte, err error) {

@ -0,0 +1,82 @@
package ipv6wry
import (
"github.com/dtapps/go-library/utils/gostring"
"math/big"
"net"
"strings"
)
// QueryResult 返回
type QueryResult struct {
Ip string `json:"ip,omitempty"` // ip
Country string `json:"country,omitempty"` // 国家
Province string `json:"province,omitempty"` // 省份
City string `json:"city,omitempty"` // 城市
Area string `json:"area,omitempty"` // 区域
Isp string `json:"isp,omitempty"` // 运营商
}
// Query ip地址查询对应归属地信息
func (c *Client) Query(ipAddress net.IP) (result QueryResult) {
result.Ip = ipAddress.String()
c.Offset = 0
tp := big.NewInt(0)
op := big.NewInt(0)
tp.SetBytes(ipAddress.To16())
op.SetString("18446744073709551616", 10)
op.Div(tp, op)
tp.SetString("FFFFFFFFFFFFFFFF", 16)
op.And(op, tp)
v6ip = op.Uint64()
offset = c.searchIndex(v6ip)
c.Offset = offset
country, area = c.getAddr()
// 解析地区数据
info := strings.Split(string(country), "\t")
if len(info) > 0 {
i := 1
for {
if i > len(info) {
break
}
switch i {
case 1:
result.Country = info[i-1]
result.Country = gostring.SpaceAndLineBreak(result.Country)
case 2:
result.Province = info[i-1]
result.Province = gostring.SpaceAndLineBreak(result.Province)
case 3:
result.City = info[i-1]
result.City = gostring.SpaceAndLineBreak(result.City)
case 4:
result.Area = info[i-1]
result.Area = gostring.SpaceAndLineBreak(result.Area)
}
i++ // 自增
}
} else {
result.Country = string(country)
result.Country = gostring.SpaceAndLineBreak(result.Country)
}
// 运营商
result.Isp = string(area)
// Delete ZX (防止不相关的信息产生干扰)
if result.Isp == "ZX" || result.Isp == "" {
result.Isp = ""
} else {
result.Isp = " " + result.Isp
}
result.Isp = gostring.SpaceAndLineBreak(result.Isp)
return result
}

@ -0,0 +1,86 @@
package goip
import (
"github.com/dtapps/go-library/utils/gostring"
"net"
"strings"
)
var (
ipv4 = "IPV4"
ipv6 = "IPV6"
)
func (c *Client) isIpv4OrIpv6(ip string) string {
if len(ip) < 7 {
return ""
}
arrIpv4 := strings.Split(ip, ".")
if len(arrIpv4) == 4 {
//. 判断IPv4
for _, val := range arrIpv4 {
if !c.CheckIpv4(val) {
return ""
}
}
return ipv4
}
arrIpv6 := strings.Split(ip, ":")
if len(arrIpv6) == 8 {
// 判断Ipv6
for _, val := range arrIpv6 {
if !c.CheckIpv6(val) {
return "Neither"
}
}
return ipv6
}
return ""
}
// IsIp 是否ip
func IsIp(ipStr string) string {
if ipStr == "" {
return ""
}
// ipv4
if gostring.Contains(ipStr, "/32") {
cidr, _, _ := net.ParseCIDR(ipStr)
if cidr != nil {
return cidr.String()
}
}
// ipv6
if gostring.Contains(ipStr, "/128") {
cidr, _, _ := net.ParseCIDR(ipStr)
if cidr != nil {
return cidr.String()
}
}
// 解析
result := net.ParseIP(ipStr).String()
if result != "<nil>" {
return result
}
return ""
}
// IsIpConsistent 两个ip是否一致
func IsIpConsistent(ipStr1, ipStr2 string) bool {
ip1Result := IsIp(ipStr1)
ip2Result := IsIp(ipStr2)
if ip1Result != "" && ip2Result != "" {
if ip1Result == ip2Result {
return true
}
}
return false
}

@ -0,0 +1,139 @@
package qqwry
import (
_ "embed"
"encoding/binary"
"log"
)
var (
header []byte
country []byte
area []byte
offset uint32
start uint32
end uint32
)
//go:embed qqwry.dat
var datBuff []byte
type Client struct {
Offset uint32
ItemLen uint32
IndexLen uint32
}
func New() *Client {
c := &Client{}
buf := datBuff[0:8]
start := binary.LittleEndian.Uint32(buf[:4])
end := binary.LittleEndian.Uint32(buf[4:])
num := int64((end-start)/7 + 1)
log.Printf("qqwry.dat 共加载:%d 条ip记录\n", num)
return c
}
// ReadData 从文件中读取数据
func (c *Client) readData(length uint32) (rs []byte) {
end := c.Offset + length
dataNum := uint32(len(datBuff))
if c.Offset > dataNum {
return nil
}
if end > dataNum {
end = dataNum
}
rs = datBuff[c.Offset:end]
c.Offset = end
return rs
}
// 获取地址信息
func (c *Client) getAddr() ([]byte, []byte) {
mode := c.readData(1)[0]
if mode == 0x01 {
// [IP][0x01][国家和地区信息的绝对偏移地址]
c.Offset = byteToUInt32(c.readData(3))
return c.getAddr()
}
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
_offset := c.Offset - 1
c1 := c.readArea(_offset)
if mode == 0x02 {
c.Offset = 4 + _offset
} else {
c.Offset = _offset + uint32(1+len(c1))
}
c2 := c.readArea(c.Offset)
return c1, c2
}
// 读取区
func (c *Client) readArea(offset uint32) []byte {
c.Offset = offset
mode := c.readData(1)[0]
if mode == 0x01 || mode == 0x02 {
return c.readArea(byteToUInt32(c.readData(3)))
}
c.Offset = offset
return c.readString()
}
// 读取字符串
func (c *Client) readString() []byte {
data := make([]byte, 0)
for {
buf := c.readData(1)
if buf[0] == 0 {
break
}
data = append(data, buf[0])
}
return data
}
// 搜索索引
func (c *Client) searchIndex(ip uint32) uint32 {
c.ItemLen = 4
c.IndexLen = 7
header = datBuff[0:8]
start = binary.LittleEndian.Uint32(header[:4])
end = binary.LittleEndian.Uint32(header[4:])
buf := make([]byte, c.IndexLen)
for {
mid := start + c.IndexLen*(((end-start)/c.IndexLen)>>1)
buf = datBuff[mid : mid+c.IndexLen]
_ip := binary.LittleEndian.Uint32(buf[:c.ItemLen])
if end-start == c.IndexLen {
if ip >= binary.LittleEndian.Uint32(datBuff[end:end+c.ItemLen]) {
buf = datBuff[end : end+c.IndexLen]
}
return byteToUInt32(buf[c.ItemLen:])
}
if _ip > ip {
end = mid
} else if _ip < ip {
start = mid
} else if _ip == ip {
return byteToUInt32(buf[c.ItemLen:])
}
}
}
// 字节转UInt32
func byteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}

@ -1,10 +1,11 @@
package v4
package qqwry
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io/ioutil"
"log"
"net/http"
)
@ -25,34 +26,45 @@ func getKey() (uint32, error) {
}
}
// 在线获取内容
func getOnline() ([]byte, error) {
func OnlineDownload() {
resp, err := http.Get("https://update.cz88.net/ip/qqwry.rar")
if err != nil {
return nil, err
panic(err)
}
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
return nil, err
} else {
if key, err := getKey(); err != nil {
return nil, err
} else {
for i := 0; i < 0x200; i++ {
key = key * 0x805
key++
key = key & 0xff
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
key, err := getKey()
if err != nil {
panic(err)
}
for i := 0; i < 0x200; i++ {
key = key * 0x805
key++
key = key & 0xff
body[i] = byte(uint32(body[i]) ^ key)
}
body[i] = byte(uint32(body[i]) ^ key)
}
reader, err := zlib.NewReader(bytes.NewReader(body))
if err != nil {
panic(err)
}
reader, err := zlib.NewReader(bytes.NewReader(body))
if err != nil {
return nil, err
}
tmpData, err := ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
return ioutil.ReadAll(reader)
}
err = ioutil.WriteFile("./qqwry.dat", tmpData, 0644)
if err != nil {
panic(err)
}
log.Printf("已下载最新 纯真 IPv4数据库 %s ", "./qqwry.dat")
}

@ -0,0 +1,53 @@
package qqwry
import (
"encoding/binary"
"errors"
"github.com/dtapps/go-library/utils/gostring"
"golang.org/x/text/encoding/simplifiedchinese"
"net"
)
// QueryResult 返回
type QueryResult struct {
Ip string `json:"ip,omitempty"` // ip
Country string `json:"country,omitempty"` // 国家或地区
Area string `json:"area,omitempty"` // 区域
}
// Query ip地址查询对应归属地信息
func (c *Client) Query(ipAddress net.IP) (result QueryResult, err error) {
c.Offset = 0
// 偏移
offset = c.searchIndex(binary.BigEndian.Uint32(ipAddress.To4()))
if offset <= 0 {
return QueryResult{}, errors.New("搜索失败")
}
result.Ip = ipAddress.String()
c.Offset = offset + c.ItemLen
country, area = c.getAddr()
enc := simplifiedchinese.GBK.NewDecoder()
result.Country, _ = enc.String(string(country))
result.Country = gostring.SpaceAndLineBreak(result.Country)
result.Area, _ = enc.String(string(area))
// Delete CZ88.NET (防止不相关的信息产生干扰)
if result.Area == " CZ88.NET" || result.Area == "" {
result.Area = ""
} else {
result.Area = " " + result.Area
}
result.Area = gostring.SpaceAndLineBreak(result.Area)
return result, nil
}

@ -0,0 +1,87 @@
package goip
import (
"errors"
"github.com/dtapps/go-library/utils/goip/geoip"
"github.com/dtapps/go-library/utils/goip/ip2region"
"github.com/dtapps/go-library/utils/goip/ip2region_v2"
"github.com/dtapps/go-library/utils/goip/ipv6wry"
"github.com/dtapps/go-library/utils/goip/qqwry"
"net"
)
var (
QueryIncorrect = errors.New("ip地址不正确")
)
// QueryQqWry 纯真IP库
// https://www.cz88.net/
func (c *Client) QueryQqWry(ipAddress net.IP) (result qqwry.QueryResult, err error) {
if ipAddress.To4() == nil {
return result, QueryIncorrect
}
query, err := c.qqwryClient.Query(ipAddress)
if err != nil {
return qqwry.QueryResult{}, err
}
return query, err
}
// QueryIp2Region ip2region
// https://github.com/lionsoul2014/ip2region
func (c *Client) QueryIp2Region(ipAddress net.IP) (result ip2region.QueryResult, err error) {
if ipAddress.To4() == nil {
return result, QueryIncorrect
}
query, err := c.ip2regionClient.Query(ipAddress)
if err != nil {
return ip2region.QueryResult{}, err
}
return query, err
}
// QueryIp2RegionV2 ip2region
// https://github.com/lionsoul2014/ip2region
func (c *Client) QueryIp2RegionV2(ipAddress net.IP) (result ip2region_v2.QueryResult, err error) {
if ipAddress.To4() == nil {
return result, QueryIncorrect
}
query, err := c.ip2regionV2Client.Query(ipAddress)
if err != nil {
return ip2region_v2.QueryResult{}, err
}
return query, nil
}
// QueryGeoIp ip2region
// https://www.maxmind.com/
func (c *Client) QueryGeoIp(ipAddress net.IP) (result geoip.QueryCityResult, err error) {
if ipAddress.String() == "<nil>" {
return result, QueryIncorrect
}
query, err := c.geoIpClient.QueryCity(ipAddress)
if err != nil {
return geoip.QueryCityResult{}, err
}
return query, nil
}
// QueryIpv6wry ip2region
// https://ip.zxinc.org
func (c *Client) QueryIpv6wry(ipAddress net.IP) (result ipv6wry.QueryResult, err error) {
if ipAddress.To16() == nil {
return result, QueryIncorrect
}
query := c.ipv6wryClient.Query(ipAddress)
return query, nil
}

@ -1,201 +0,0 @@
package v4
import (
_ "embed"
"encoding/binary"
"errors"
"github.com/dtapps/go-library/utils/gostring"
"golang.org/x/text/encoding/simplifiedchinese"
"io/ioutil"
"log"
"net"
)
var (
header []byte
country []byte
area []byte
offset uint32
start uint32
end uint32
)
//go:embed qqwry.dat
var dat []byte
type Pointer struct {
Offset uint32
ItemLen uint32
IndexLen uint32
}
// Result 返回
type Result struct {
IP string `json:"ip,omitempty"` // 输入的ip地址
Country string `json:"country,omitempty"` // 国家或地区
Area string `json:"area,omitempty"` // 区域
}
// InitIPV4Data 加载
func (q *Pointer) InitIPV4Data() int64 {
buf := dat[0:8]
start := binary.LittleEndian.Uint32(buf[:4])
end := binary.LittleEndian.Uint32(buf[4:])
return int64((end-start)/7 + 1)
}
// ReadData 从文件中读取数据
func (q *Pointer) readData(length uint32) (rs []byte) {
end := q.Offset + length
dataNum := uint32(len(dat))
if q.Offset > dataNum {
return nil
}
if end > dataNum {
end = dataNum
}
rs = dat[q.Offset:end]
q.Offset = end
return rs
}
// Find ip地址查询对应归属地信息
func (q *Pointer) Find(ip string) (res Result) {
// 赋值
res.IP = ip
if net.ParseIP("61.241.55.180").To4() == nil {
// 不是ip地址
return res
}
q.Offset = 0
// 偏移
offset = q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4()))
if offset <= 0 {
return
}
q.Offset = offset + q.ItemLen
country, area = q.getAddr()
enc := simplifiedchinese.GBK.NewDecoder()
res.Country, _ = enc.String(string(country))
res.Country = gostring.SpaceAndLineBreak(res.Country)
res.Area, _ = enc.String(string(area))
// Delete CZ88.NET (防止不相关的信息产生干扰)
if res.Area == " CZ88.NET" || res.Area == "" {
res.Area = ""
} else {
res.Area = " " + res.Area
}
res.Area = gostring.SpaceAndLineBreak(res.Area)
return
}
// 获取地址信息
func (q *Pointer) getAddr() ([]byte, []byte) {
mode := q.readData(1)[0]
if mode == 0x01 {
// [IP][0x01][国家和地区信息的绝对偏移地址]
q.Offset = byteToUInt32(q.readData(3))
return q.getAddr()
}
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
_offset := q.Offset - 1
c1 := q.readArea(_offset)
if mode == 0x02 {
q.Offset = 4 + _offset
} else {
q.Offset = _offset + uint32(1+len(c1))
}
c2 := q.readArea(q.Offset)
return c1, c2
}
// 读取区
func (q *Pointer) readArea(offset uint32) []byte {
q.Offset = offset
mode := q.readData(1)[0]
if mode == 0x01 || mode == 0x02 {
return q.readArea(byteToUInt32(q.readData(3)))
}
q.Offset = offset
return q.readString()
}
// 读取字符串
func (q *Pointer) readString() []byte {
data := make([]byte, 0)
for {
buf := q.readData(1)
if buf[0] == 0 {
break
}
data = append(data, buf[0])
}
return data
}
// 搜索索引
func (q *Pointer) searchIndex(ip uint32) uint32 {
q.ItemLen = 4
q.IndexLen = 7
header = dat[0:8]
start = binary.LittleEndian.Uint32(header[:4])
end = binary.LittleEndian.Uint32(header[4:])
buf := make([]byte, q.IndexLen)
for {
mid := start + q.IndexLen*(((end-start)/q.IndexLen)>>1)
buf = dat[mid : mid+q.IndexLen]
_ip := binary.LittleEndian.Uint32(buf[:q.ItemLen])
if end-start == q.IndexLen {
if ip >= binary.LittleEndian.Uint32(dat[end:end+q.ItemLen]) {
buf = dat[end : end+q.IndexLen]
}
return byteToUInt32(buf[q.ItemLen:])
}
if _ip > ip {
end = mid
} else if _ip < ip {
start = mid
} else if _ip == ip {
return byteToUInt32(buf[q.ItemLen:])
}
}
}
// 字节转UInt32
func byteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}
// OnlineDownload 在线下载
func (q *Pointer) OnlineDownload() (err error) {
tmpData, err := getOnline()
if err != nil {
return errors.New("下载失败")
}
if err := ioutil.WriteFile("./qqwry.dat", tmpData, 0644); err == nil {
log.Printf("已下载最新 纯真 IPv4数据库 %s ", "./qqwry.dat")
} else {
return errors.New("保存失败")
}
return nil
}

@ -1,226 +0,0 @@
package v6
import (
_ "embed"
"encoding/binary"
"errors"
"github.com/dtapps/go-library/utils/gostring"
"io/ioutil"
"log"
"math/big"
"net"
"strings"
)
var (
header []byte
country []byte
area []byte
v6ip uint64
offset uint32
start uint32
end uint32
)
type Result struct {
IP string `json:"ip,omitempty"` // 输入的ip地址
Country string `json:"country,omitempty"` // 国家
Province string `json:"province,omitempty"` // 省份
City string `json:"city,omitempty"` // 城市
Area string `json:"area,omitempty"` // 区域
Isp string `json:"isp,omitempty"` // 运营商
}
//go:embed ipv6wry.db
var dat []byte
type Pointer struct {
Offset uint32
ItemLen uint32
IndexLen uint32
}
// InitIPV4Data 加载
func (q *Pointer) InitIPV4Data() int64 {
buf := dat[0:8]
start := binary.LittleEndian.Uint32(buf[:4])
end := binary.LittleEndian.Uint32(buf[4:])
return int64((end-start)/7 + 1)
}
// ReadData 从文件中读取数据
func (q *Pointer) readData(length uint32) (rs []byte) {
end := q.Offset + length
dataNum := uint32(len(dat))
if q.Offset > dataNum {
return nil
}
if end > dataNum {
end = dataNum
}
rs = dat[q.Offset:end]
q.Offset = end
return rs
}
// Find ip地址查询对应归属地信息
func (q *Pointer) Find(ip string) (res Result) {
res = Result{}
res.IP = ip
q.Offset = 0
tp := big.NewInt(0)
op := big.NewInt(0)
tp.SetBytes(net.ParseIP(ip).To16())
op.SetString("18446744073709551616", 10)
op.Div(tp, op)
tp.SetString("FFFFFFFFFFFFFFFF", 16)
op.And(op, tp)
v6ip = op.Uint64()
offset = q.searchIndex(v6ip)
q.Offset = offset
country, area = q.getAddr()
// 解析地区数据
info := strings.Split(string(country), "\t")
if len(info) > 0 {
i := 1
for {
if i > len(info) {
break
}
switch i {
case 1:
res.Country = info[i-1]
res.Country = gostring.SpaceAndLineBreak(res.Country)
case 2:
res.Province = info[i-1]
res.Province = gostring.SpaceAndLineBreak(res.Province)
case 3:
res.City = info[i-1]
res.City = gostring.SpaceAndLineBreak(res.City)
case 4:
res.Area = info[i-1]
res.Area = gostring.SpaceAndLineBreak(res.Area)
}
i++ // 自增
}
} else {
res.Country = string(country)
res.Country = gostring.SpaceAndLineBreak(res.Country)
}
// 运营商
res.Isp = string(area)
// Delete ZX (防止不相关的信息产生干扰)
if res.Isp == "ZX" || res.Isp == "" {
res.Isp = ""
} else {
res.Isp = " " + res.Isp
}
res.Isp = gostring.SpaceAndLineBreak(res.Isp)
return
}
func (q *Pointer) getAddr() ([]byte, []byte) {
mode := q.readData(1)[0]
if mode == 0x01 {
// [IP][0x01][国家和地区信息的绝对偏移地址]
q.Offset = byteToUInt32(q.readData(3))
return q.getAddr()
}
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
_offset := q.Offset - 1
c1 := q.readArea(_offset)
if mode == 0x02 {
q.Offset = 4 + _offset
} else {
q.Offset = _offset + uint32(1+len(c1))
}
c2 := q.readArea(q.Offset)
return c1, c2
}
func (q *Pointer) readArea(offset uint32) []byte {
q.Offset = offset
mode := q.readData(1)[0]
if mode == 0x01 || mode == 0x02 {
return q.readArea(byteToUInt32(q.readData(3)))
}
q.Offset = offset
return q.readString()
}
func (q *Pointer) readString() []byte {
data := make([]byte, 0)
for {
buf := q.readData(1)
if buf[0] == 0 {
break
}
data = append(data, buf[0])
}
return data
}
func (q *Pointer) searchIndex(ip uint64) uint32 {
q.ItemLen = 8
q.IndexLen = 11
header = dat[8:24]
start = binary.LittleEndian.Uint32(header[8:])
counts := binary.LittleEndian.Uint32(header[:8])
end = start + counts*q.IndexLen
buf := make([]byte, q.IndexLen)
for {
mid := start + q.IndexLen*(((end-start)/q.IndexLen)>>1)
buf = dat[mid : mid+q.IndexLen]
_ip := binary.LittleEndian.Uint64(buf[:q.ItemLen])
if end-start == q.IndexLen {
if ip >= binary.LittleEndian.Uint64(dat[end:end+q.ItemLen]) {
buf = dat[end : end+q.IndexLen]
}
return byteToUInt32(buf[q.ItemLen:])
}
if _ip > ip {
end = mid
} else if _ip < ip {
start = mid
} else if _ip == ip {
return byteToUInt32(buf[q.ItemLen:])
}
}
}
func byteToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}
// OnlineDownload 在线下载
func (q *Pointer) OnlineDownload() (err error) {
tmpData, err := getOnline()
if err != nil {
return errors.New("下载失败")
}
if err := ioutil.WriteFile("./ipv6wry.db", tmpData, 0644); err == nil {
log.Printf("已下载最新 ZX IPv6数据库 %s ", "./ipv6wry.db")
} else {
return errors.New("保存失败")
}
return nil
}

@ -0,0 +1,130 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/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,88 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library"
"github.com/dtapps/go-library/utils/goip"
"github.com/go-redis/redis/v9"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"go.mongodb.org/mongo-driver/version"
"log"
"runtime"
)
type systemResult struct {
SystemHostname string // 主机名
SystemOs string // 系统类型
SystemVersion string // 系统版本
SystemKernel string // 系统内核
SystemKernelVersion string // 系统内核版本
SystemUpTime uint64 // 系统运行时间
SystemBootTime uint64 // 系统开机时间
CpuCores int // CPU核数
CpuModelName string // CPU型号名称
CpuMhz float64 // CPU兆赫
}
func getSystem() (result systemResult) {
hInfo, err := host.Info()
if err != nil {
log.Printf("getSystem.host.Info%s\n", err)
}
result.SystemHostname = hInfo.Hostname
result.SystemOs = hInfo.OS
result.SystemVersion = hInfo.PlatformVersion
result.SystemKernel = hInfo.KernelArch
result.SystemKernelVersion = hInfo.KernelVersion
result.SystemUpTime = hInfo.Uptime
if hInfo.BootTime != 0 {
result.SystemBootTime = hInfo.BootTime
}
hCpu, err := cpu.Times(true)
if err != nil {
log.Printf("getSystem.cpu.Times%s\n", err)
}
result.CpuCores = len(hCpu)
cInfo, err := cpu.Info()
if err != nil {
log.Printf("getSystem.cpu.Info%s\n", err)
}
if len(cInfo) > 0 {
result.CpuModelName = cInfo[0].ModelName
result.CpuMhz = cInfo[0].Mhz
}
return result
}
func (c *Client) setConfig(ctx context.Context) {
info := getSystem()
c.config.systemHostname = info.SystemHostname
c.config.systemOs = info.SystemOs
c.config.systemVersion = info.SystemVersion
c.config.systemKernel = info.SystemKernel
c.config.systemKernelVersion = info.SystemKernelVersion
c.config.systemBootTime = info.SystemBootTime
c.config.cpuCores = info.CpuCores
c.config.cpuModelName = info.CpuModelName
c.config.cpuMhz = info.CpuMhz
c.config.systemInsideIp = goip.GetInsideIp(ctx)
c.config.sdkVersion = go_library.Version()
c.config.goVersion = runtime.Version()
c.config.mongoSdkVersion = version.Driver
c.config.redisSdkVersion = redis.Version()
c.config.logVersion = go_library.Version()
}

@ -0,0 +1,5 @@
package gojobs
const (
SpecifyIpNull = "0.0.0.0"
)

@ -1,113 +1,15 @@
package gojobs
import (
"context"
"errors"
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gostring"
"github.com/dtapps/go-library/utils/gotime"
"gorm.io/gorm"
"log"
)
// Run 运行
func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
// 请求函数记录
err := j.gormClient.Db.Create(&jobs_gorm_model.TaskLog{
TaskId: info.Id,
StatusCode: status,
Desc: desc,
Version: j.config.runVersion,
}).Error
if err != nil {
log.Println("statusCreate", err.Error())
}
if status == 0 {
err = j.EditTask(j.gormClient.Db, info.Id).
Select("run_id").
Updates(jobs_gorm_model.Task{
RunId: gostring.GetUuId(),
}).Error
if err != nil {
log.Println("statusEdit", err.Error())
}
return
}
// 任务
if status == CodeSuccess {
// 执行成功
err = j.EditTask(j.gormClient.Db, info.Id).
Select("status_desc", "number", "run_id", "updated_ip", "result").
Updates(jobs_gorm_model.Task{
StatusDesc: "执行成功",
Number: info.Number + 1,
RunId: gostring.GetUuId(),
UpdatedIp: j.config.outsideIp,
Result: desc,
}).Error
if err != nil {
log.Println("statusEdit", err.Error())
}
}
if status == CodeEnd {
// 执行成功、提前结束
err = j.EditTask(j.gormClient.Db, info.Id).
Select("status", "status_desc", "number", "updated_ip", "result").
Updates(jobs_gorm_model.Task{
Status: TASK_SUCCESS,
StatusDesc: "结束执行",
Number: info.Number + 1,
UpdatedIp: j.config.outsideIp,
Result: desc,
}).Error
if err != nil {
log.Println("statusEdit", err.Error())
}
}
if status == CodeError {
// 执行失败
err = j.EditTask(j.gormClient.Db, info.Id).
Select("status_desc", "number", "run_id", "updated_ip", "result").
Updates(jobs_gorm_model.Task{
StatusDesc: "执行失败",
Number: info.Number + 1,
RunId: gostring.GetUuId(),
UpdatedIp: j.config.outsideIp,
Result: desc,
}).Error
if err != nil {
log.Println("statusEdit", err.Error())
}
}
if info.MaxNumber != 0 {
if info.Number+1 >= info.MaxNumber {
// 关闭执行
err = j.EditTask(j.gormClient.Db, info.Id).
Select("status").
Updates(jobs_gorm_model.Task{
Status: TASK_TIMEOUT,
}).Error
if err != nil {
log.Println("statusEdit", err.Error())
}
}
}
}
// RunAddLog 任务执行日志
func (j *JobsGorm) RunAddLog(id uint, runId string) error {
return j.gormClient.Db.Create(&jobs_gorm_model.TaskLogRun{
TaskId: id,
RunId: runId,
InsideIp: j.config.insideIp,
OutsideIp: j.config.outsideIp,
Os: j.config.os,
Arch: j.config.arch,
Gomaxprocs: j.config.maxProCs,
GoVersion: j.config.version,
MacAddrs: j.config.macAddrS,
}).Error
}
// ConfigCreateInCustomId 创建正在运行任务
type ConfigCreateInCustomId struct {
Tx *gorm.DB // 驱动
@ -118,13 +20,13 @@ type ConfigCreateInCustomId struct {
Type string // 类型
TypeName string // 类型名称
SpecifyIp string // 指定外网IP
CurrentIp string // 当前ip
CurrentIp string // 当前外网IP
}
// CreateInCustomId 创建正在运行任务
func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
func (c *Client) CreateInCustomId(ctx context.Context, config *ConfigCreateInCustomId) error {
if config.CurrentIp == "" {
config.CurrentIp = j.config.outsideIp
config.CurrentIp = c.config.systemOutsideIp
}
err := config.Tx.Create(&jobs_gorm_model.Task{
Status: TASK_IN,
@ -139,6 +41,7 @@ func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
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()))
@ -156,17 +59,17 @@ type ConfigCreateInCustomIdOnly struct {
Type string // 类型
TypeName string // 类型名称
SpecifyIp string // 指定外网IP
CurrentIp string // 当前ip
CurrentIp string // 当前外网IP
}
// CreateInCustomIdOnly 创建正在运行唯一任务
func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) error {
query := j.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
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 errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
return TaskIsExist
}
if config.CurrentIp == "" {
config.CurrentIp = j.config.outsideIp
config.CurrentIp = c.config.systemOutsideIp
}
err := config.Tx.Create(&jobs_gorm_model.Task{
Status: TASK_IN,
@ -181,6 +84,7 @@ func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) erro
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()))
@ -199,13 +103,13 @@ type ConfigCreateInCustomIdMaxNumber struct {
Type string // 类型
TypeName string // 类型名称
SpecifyIp string // 指定外网IP
CurrentIp string // 当前ip
CurrentIp string // 当前外网IP
}
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNumber) error {
func (c *Client) CreateInCustomIdMaxNumber(ctx context.Context, config *ConfigCreateInCustomIdMaxNumber) error {
if config.CurrentIp == "" {
config.CurrentIp = j.config.outsideIp
config.CurrentIp = c.config.systemOutsideIp
}
err := config.Tx.Create(&jobs_gorm_model.Task{
Status: TASK_IN,
@ -221,6 +125,7 @@ func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNu
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()))
@ -239,17 +144,17 @@ type ConfigCreateInCustomIdMaxNumberOnly struct {
Type string // 类型
TypeName string // 类型名称
SpecifyIp string // 指定外网IP
CurrentIp string // 当前ip
CurrentIp string // 当前外网IP
}
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdMaxNumberOnly) error {
query := j.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
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 errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
return TaskIsExist
}
if config.CurrentIp == "" {
config.CurrentIp = j.config.outsideIp
config.CurrentIp = c.config.systemOutsideIp
}
err := config.Tx.Create(&jobs_gorm_model.Task{
Status: TASK_IN,
@ -265,6 +170,7 @@ func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdM
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()))

@ -0,0 +1,50 @@
package gojobs
import (
"context"
"errors"
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gostring"
"github.com/dtapps/go-library/utils/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
}

@ -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("任务已存在")
)

@ -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
}

@ -0,0 +1,196 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/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,21 +0,0 @@
package gojobs
import (
"context"
"fmt"
"github.com/dtapps/go-library/utils/goip"
)
var ip string
func configIp() {
ip = goip.GetOutsideIp(context.Background())
}
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,56 +0,0 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/pb"
"google.golang.org/grpc"
"log"
)
// CronConfig 定时任务配置
type CronConfig struct {
Address string // 服务端口 127.0.0.1:8888
}
// GrpcCron 定时任务
type GrpcCron struct {
CronConfig // 配置
Pub pb.PubSubClient // 订阅
Conn *grpc.ClientConn // 链接信息
}
// NewGrpcCron 创建定时任务
func NewGrpcCron(config *CronConfig) *GrpcCron {
if config.Address == "" {
panic("[定时任务]请填写服务端口")
}
c := &GrpcCron{}
c.Address = config.Address
var err error
// 建立连接 获取client
c.Conn, err = grpc.Dial(c.Address, grpc.WithInsecure())
if err != nil {
panic("[定时任务]{连接失败}" + err.Error())
}
// 新建一个客户端
c.Pub = pb.NewPubSubClient(c.Conn)
return c
}
// Send 发送
func (c *GrpcCron) Send(in *pb.PublishRequest) (*pb.PublishResponse, error) {
log.Printf("[定时任务]{广播开始}编号:%s 类型:%s ip%s\n", in.GetId(), in.GetValue(), in.GetIp())
stream, err := c.Pub.Publish(context.Background(), in)
if err != nil {
log.Printf("[定时任务]{广播失败}编号:%s %v\n", in.GetId(), err)
}
log.Printf("[定时任务]{广播成功}编号:%s 类型:%s ip%s\n", in.GetId(), in.GetValue(), in.GetIp())
return stream, err
}

@ -1,84 +0,0 @@
package gojobs
import (
"errors"
"github.com/dtapps/go-library/utils/gojobs/pb"
"github.com/dtapps/go-library/utils/gojobs/pubsub"
"google.golang.org/grpc"
"log"
"net"
"strings"
"time"
)
// ServerConfig 服务配置
type ServerConfig struct {
PublishTimeout time.Duration // 控制发布时最大阻塞时间
PubBuffer int // 缓冲区大小控制每个订阅者的chan缓冲区大小
Address string // 服务端口 0.0.0.0:8888
}
// Server 服务
type Server struct {
ServerConfig // 配置
Pub *pubsub.Publisher // 订阅
Conn *grpc.Server // 链接信息
}
// NewServer 创建服务和注册
func NewServer(config *ServerConfig) *Server {
if config.Address == "" {
panic("[服务中转]请填写服务端口")
}
s := &Server{}
s.PublishTimeout = config.PublishTimeout
s.PubBuffer = config.PubBuffer
s.Address = config.Address
s.Pub = pubsub.NewPublisher(config.PublishTimeout, config.PubBuffer)
// 创建gRPC服务器
s.Conn = grpc.NewServer()
// 注册
pb.RegisterPubSubServer(s.Conn, pb.NewPubSubServerService())
return s
}
// StartCron 启动定时任务
func (s *Server) StartCron() {
cron := s.Pub.SubscribeTopic(func(v interface{}) bool {
if key, ok := v.(string); ok {
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
})
go func() {
log.Println("crontopic:", <-cron)
}()
}
// StartUp 启动服务
func (s *Server) StartUp() {
// 监听本地端口
lis, err := net.Listen("tcp", s.Address)
if err != nil {
panic(errors.New("[服务中转]{创建监听失败}" + err.Error()))
}
log.Println("[服务中转]{监听}", lis.Addr())
// 启动grpc
err = s.Conn.Serve(lis)
if err != nil {
panic(errors.New("[服务中转]{创建服务失败}" + err.Error()))
}
}

@ -1,76 +0,0 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/pb"
"github.com/dtapps/go-library/utils/gostring"
"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: gostring.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: gostring.GetUuId(),
Value: prefixSprintf(w.ClientIp),
Ip: w.ClientIp,
})
if err != nil {
panic("[工作线]{启动任务失败}" + err.Error())
}
return stream
}

@ -1,5 +1,10 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
)
const (
TASK_IN = "IN" // 任务运行
TASK_SUCCESS = "SUCCESS" // 任务完成
@ -11,15 +16,15 @@ const (
// Cron
type jobs interface {
// Run 运行
Run(info interface{}, status int, desc string)
// RunAddLog 任务执行日志
RunAddLog(id uint, runId string)
Run(ctx context.Context, info jobs_gorm_model.Task, status int, result string)
// CreateInCustomId 创建正在运行任务
CreateInCustomId()
CreateInCustomId(ctx context.Context, config *ConfigCreateInCustomId) error
// CreateInCustomIdOnly 创建正在运行唯一任务
CreateInCustomIdOnly()
CreateInCustomIdOnly(ctx context.Context, config *ConfigCreateInCustomIdOnly) error
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
CreateInCustomIdMaxNumber()
CreateInCustomIdMaxNumber(ctx context.Context, config *ConfigCreateInCustomIdMaxNumber) error
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
CreateInCustomIdMaxNumberOnly()
CreateInCustomIdMaxNumberOnly(ctx context.Context, config *ConfigCreateInCustomIdMaxNumberOnly) error
// CreateWaitCustomId 创建正在运行任务
CreateWaitCustomId(ctx context.Context, config *ConfigCreateWaitCustomId) error
}

@ -1,114 +0,0 @@
package gojobs
import (
"context"
"errors"
"fmt"
"github.com/dtapps/go-library"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goarray"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/golock"
"log"
"runtime"
)
type JobsGormConfig struct {
GormClient *dorm.GormClient // 数据库驱动
RedisClient *dorm.RedisClient // 缓存数据库驱动
CurrentIp string // 当前ip
LockKeyPrefix string // 锁Key前缀 xxx_lock
LockKeySeparator string // 锁Key分隔符 :
CornKeyPrefix string // 任务Key前缀 xxx_cron
CornKeyCustom string // 任务Key自定义 xxx_cron_自定义 xxx_cron_自定义_*
Debug bool // 调试
}
// JobsGorm Gorm数据库驱动
type JobsGorm struct {
gormClient *dorm.GormClient // 数据库驱动
redisClient *dorm.RedisClient // 缓存驱动
lockClient *golock.LockRedis // 锁驱动
config struct {
debug bool // 调试
runVersion string // 运行版本
os string // 系统类型
arch string // 系统架构
maxProCs int // CPU核数
version string // GO版本
macAddrS string // Mac地址
insideIp string // 内网ip
outsideIp string // 外网ip
lockKeyPrefix string // 锁Key前缀 xxx_lock
lockKeySeparator string // 锁Key分隔符 :
cornKeyPrefix string // 任务Key前缀 xxx_cron
cornKeyCustom string // 任务Key自定义
}
}
// NewJobsGorm 初始化
func NewJobsGorm(config *JobsGormConfig) (*JobsGorm, error) {
// 判断
if config.LockKeyPrefix == "" {
return nil, errors.New("需要配置锁Key前缀")
}
if config.LockKeySeparator == "" {
return nil, errors.New("需要配置锁Key分隔符")
}
if config.CornKeyPrefix == "" {
return nil, errors.New("需要配置任务Key前缀")
}
if config.CornKeyCustom == "" {
return nil, errors.New("需要配置任务Key自定义")
}
if config.CurrentIp == "" {
return nil, errors.New("需要配置当前的IP")
}
if config.GormClient == nil {
return nil, errors.New("需要配置数据库驱动")
}
if config.RedisClient == nil {
return nil, errors.New("需要配置缓存数据库驱动")
}
c := &JobsGorm{}
c.gormClient = config.GormClient
c.redisClient = config.RedisClient
c.config.outsideIp = config.CurrentIp
c.config.lockKeyPrefix = config.LockKeyPrefix
c.config.lockKeySeparator = config.LockKeySeparator
c.config.cornKeyPrefix = config.CornKeyPrefix
c.config.cornKeyCustom = config.CornKeyCustom
c.config.debug = config.Debug
// 锁
c.lockClient = golock.NewLockRedis(c.redisClient)
// 配置信息
c.config.runVersion = go_library.Version()
c.config.os = runtime.GOOS
c.config.arch = runtime.GOARCH
c.config.maxProCs = runtime.GOMAXPROCS(0)
c.config.version = runtime.Version()
c.config.macAddrS = goarray.TurnString(goip.GetMacAddr(context.Background()))
c.config.insideIp = goip.GetInsideIp(context.Background())
// 创建模型
err := c.gormClient.Db.AutoMigrate(
&jobs_gorm_model.Task{},
&jobs_gorm_model.TaskLog{},
&jobs_gorm_model.TaskLogRun{},
&jobs_gorm_model.TaskIp{},
)
if err != nil {
return nil, errors.New(fmt.Sprintf("创建任务模型失败:%v\n", err))
}
if c.config.debug == true {
log.Printf("JOBS配置%+v\n", c.config)
}
return c, nil
}

@ -1,40 +0,0 @@
package gojobs
import (
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gotime"
"gorm.io/gorm"
"log"
)
// CheckManyTask 多任务检查
func (j *JobsGorm) CheckManyTask(tx *gorm.DB, vs []jobs_gorm_model.Task) {
if len(vs) > 0 {
for _, v := range vs {
diffInSecondWithAbs := gotime.Current().DiffInSecondWithAbs(gotime.SetCurrent(v.UpdatedAt).Time)
if diffInSecondWithAbs >= v.Frequency*3 {
if j.config.debug == true {
log.Printf("每隔%v秒任务%v相差%v秒\n", v.Frequency, v.Id, diffInSecondWithAbs)
}
err := tx.Where("task_id = ?", v.Id).Where("run_id = ?", v.RunId).Delete(&jobs_gorm_model.TaskLogRun{}).Error
if err != nil {
log.Println("删除失败", err.Error())
}
}
}
}
}
// CheckSingleTask 单任务检查
func (j *JobsGorm) CheckSingleTask(tx *gorm.DB, v jobs_gorm_model.Task) {
diffInSecondWithAbs := gotime.Current().DiffInSecondWithAbs(gotime.SetCurrent(v.UpdatedAt).Time)
if diffInSecondWithAbs >= v.Frequency*3 {
if j.config.debug == true {
log.Printf("每隔%v秒任务%v相差%v秒\n", v.Frequency, v.Id, diffInSecondWithAbs)
}
err := tx.Where("task_id = ?", v.Id).Where("run_id = ?", v.RunId).Delete(&jobs_gorm_model.TaskLogRun{}).Error
if err != nil {
log.Println("删除失败", err.Error())
}
}
}

@ -1,21 +0,0 @@
package gojobs
import (
"github.com/go-redis/redis/v9"
"gorm.io/gorm"
)
// GetDb 获取数据库驱动
func (j *JobsGorm) GetDb() *gorm.DB {
return j.gormClient.Db
}
// GetRedis 获取缓存数据库驱动
func (j *JobsGorm) GetRedis() *redis.Client {
return j.redisClient.Db
}
// GetCurrentIp 获取当前ip
func (j *JobsGorm) GetCurrentIp() string {
return j.config.outsideIp
}

@ -1,21 +0,0 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"gorm.io/gorm"
)
// RefreshIp 刷新Ip
func (j *JobsGorm) RefreshIp(ctx context.Context, tx *gorm.DB) {
xip := goip.GetOutsideIp(ctx)
if j.config.outsideIp == "" || j.config.outsideIp == "0.0.0.0" {
return
}
if j.config.outsideIp == xip {
return
}
tx.Where("ips = ?", j.config.outsideIp).Delete(&jobs_gorm_model.TaskIp{}) // 删除
j.config.outsideIp = xip
}

@ -1,22 +0,0 @@
package gojobs
import (
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"time"
)
// Lock 上锁
func (j *JobsGorm) Lock(info jobs_gorm_model.Task, id any) (string, error) {
return j.lockClient.Lock(fmt.Sprintf("%s%s%v%s%v", j.config.lockKeyPrefix, j.config.lockKeySeparator, info.Type, j.config.lockKeySeparator, id), fmt.Sprintf("已在%s@%s机器上锁成功", j.config.insideIp, j.config.outsideIp), time.Duration(info.Frequency)*3*time.Second)
}
// Unlock Lock 解锁
func (j *JobsGorm) Unlock(info jobs_gorm_model.Task, id any) error {
return j.lockClient.Unlock(fmt.Sprintf("%s%s%v%s%v", j.config.lockKeyPrefix, j.config.lockKeySeparator, info.Type, j.config.lockKeySeparator, id))
}
// LockForever 永远上锁
func (j *JobsGorm) LockForever(info jobs_gorm_model.Task, id any) (string, error) {
return j.lockClient.LockForever(fmt.Sprintf("%s%s%v%s%v", j.config.lockKeyPrefix, j.config.lockKeySeparator, info.Type, j.config.lockKeySeparator, id), fmt.Sprintf("已在%s@%s机器永远上锁成功", j.config.insideIp, j.config.outsideIp))
}

@ -1,196 +0,0 @@
package gojobs
import (
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"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").
Updates(jobs_gorm_model.Task{
Frequency: frequency,
})
}
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.config.outsideIp == "" || j.config.outsideIp == "0.0.0.0" {
return false
}
tx.Where("ips = ?", j.config.outsideIp).Delete(&jobs_gorm_model.TaskIp{}) // 删除
for k, v := range ips {
if v == "" {
j.TaskIpUpdate(tx, k, j.config.outsideIp)
} else {
find := strings.Contains(v, ",")
if find == true {
// 包含
parts := strings.Split(v, ",")
for _, vv := range parts {
if vv == j.config.outsideIp {
j.TaskIpUpdate(tx, k, j.config.outsideIp)
}
}
} else {
// 不包含
if v == j.config.outsideIp {
j.TaskIpUpdate(tx, k, j.config.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
}

@ -7,28 +7,29 @@ import (
// Task 任务
type Task struct {
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
Status string `gorm:"index;comment:状态码" json:"status"` // 状态码
Params string `gorm:"comment:参数" json:"params"` // 参数
ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型
StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述
Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位)
Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数
MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数
RunId string `gorm:"index;comment:执行编号" json:"run_id"` // 执行编号
CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号
CustomSequence int64 `gorm:"index;comment:自定义顺序" json:"custom_sequence"` // 自定义顺序
Type string `gorm:"index;comment:类型" json:"type"` // 类型
TypeName string `gorm:"comment:类型名称" json:"type_name"` // 类型名称
CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP
SpecifyIp string `gorm:"index;comment:指定外网IP" json:"specify_ip"` // 指定外网IP
UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP
Result string `gorm:"comment:结果" json:"result"` // 结果
CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"deleted_at"` // 删除时间
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
Status string `gorm:"index;comment:状态码" json:"status"` // 状态码
Params string `gorm:"comment:参数" json:"params"` // 参数
ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型
StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述
Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位)
Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数
MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数
RunId string `gorm:"index;comment:执行编号" json:"run_id"` // 执行编号
CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号
CustomSequence int64 `gorm:"index;comment:自定义顺序" json:"custom_sequence"` // 自定义顺序
Type string `gorm:"index;comment:类型" json:"type"` // 类型
TypeName string `gorm:"comment:类型名称" json:"type_name"` // 类型名称
CreatedIp string `gorm:"default:0.0.0.0;comment:创建外网IP" json:"created_ip"` // 创建外网IP
SpecifyIp string `gorm:"default:0.0.0.0;index;comment:指定外网IP" json:"specify_ip"` // 指定外网IP
UpdatedIp string `gorm:"default:0.0.0.0;comment:更新外网IP" json:"updated_ip"` // 更新外网IP
Result string `gorm:"comment:结果" json:"result"` // 结果
NextRunTime time.Time `gorm:"comment:下次运行时间" json:"next_run_time"` // 下次运行时间
CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"deleted_at"` // 删除时间
}
func (m *Task) TableName() string {
func (Task) TableName() string {
return "task"
}

@ -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"
}

@ -4,14 +4,21 @@ import "time"
// TaskLog 任务日志模型
type TaskLog struct {
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
TaskId uint `gorm:"index;comment:任务编号" json:"task_id"` // 任务编号
StatusCode int `gorm:"index;comment:状态码" json:"status_code"` // 状态码
Desc string `gorm:"comment:结果" json:"desc"` // 结果
Version string `gorm:"comment:版本" json:"version"` // 版本
CreatedAt time.Time `gorm:"autoCreateTime;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,22 +0,0 @@
package jobs_gorm_model
import "time"
// TaskLogRun 任务执行日志模型
type TaskLogRun struct {
Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号
TaskId uint `gorm:"index;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 time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at"` // 创建时间
}
func (m *TaskLogRun) TableName() string {
return "task_log_run"
}

@ -1,38 +0,0 @@
package gojobs
import (
"context"
"github.com/go-redis/redis/v9"
"log"
)
// Publish 发布
// ctx 上下文
// channel 频道
// message 消息
func (j *JobsGorm) Publish(ctx context.Context, channel string, message interface{}) error {
publish, err := j.redisClient.Publish(ctx, channel, message).Result()
if j.config.debug == true {
log.Println("gojobs.Publish", channel, message, publish, err)
}
return err
}
type SubscribeResult struct {
err error
Message *redis.PubSub
}
// Subscribe 订阅
func (j *JobsGorm) Subscribe(ctx context.Context) SubscribeResult {
return SubscribeResult{
Message: j.redisClient.Subscribe(ctx, j.config.cornKeyPrefix+"_"+j.config.cornKeyCustom),
}
}
// PSubscribe 订阅,支持通配符匹配(ch_user_*)
func (j *JobsGorm) PSubscribe(ctx context.Context) SubscribeResult {
return SubscribeResult{
Message: j.redisClient.PSubscribe(ctx, j.config.cornKeyPrefix+"_"+j.config.cornKeyCustom+"_*"),
}
}

@ -1,96 +0,0 @@
package gojobs
import (
"context"
"errors"
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gostring"
"log"
"math/rand"
"time"
)
// GetIssueAddress 获取下发地址
// workers 在线列表
// v 任务信息
// ---
// address 下发地址
// err 错误信息
func (j *JobsGorm) GetIssueAddress(workers []string, v *jobs_gorm_model.Task) (address string, err error) {
var (
currentIp = "" // 当前Ip
appointIpStatus = false // 指定Ip状态
)
// 赋值ip
if v.SpecifyIp != "" {
currentIp = v.SpecifyIp
appointIpStatus = true
}
// 只有一个客户端在线
if len(workers) == 1 {
if appointIpStatus == true {
// 判断是否指定某ip执行
if gostring.Contains(workers[0], currentIp) == true {
return j.config.cornKeyPrefix + "_" + v.SpecifyIp, nil
}
return address, errors.New(fmt.Sprintf("需要执行的[%s]客户端不在线", currentIp))
}
return j.config.cornKeyPrefix + "_" + workers[0], nil
}
// 优先处理指定某ip执行
if appointIpStatus == true {
for wk, wv := range workers {
if gostring.Contains(wv, currentIp) == true {
return j.config.cornKeyPrefix + "_" + workers[wk], nil
}
}
return address, errors.New(fmt.Sprintf("需要执行的[%s]客户端不在线", currentIp))
} else {
// 随机返回一个
zxIp := workers[j.random(0, len(workers))]
if zxIp == "" {
return address, errors.New("获取执行的客户端异常")
}
address = j.config.cornKeyPrefix + "_" + zxIp
return address, err
}
}
// GetSubscribeClientList 获取在线的客户端
func (j *JobsGorm) GetSubscribeClientList(ctx context.Context) ([]string, error) {
if j.config.debug == true {
log.Printf("获取在线的客户端:%s\n", j.config.cornKeyPrefix+"_*")
}
// 扫描
values, err := j.redisClient.Keys(ctx, j.config.cornKeyPrefix+"_*").Result()
if err != nil {
if err == errors.New("ERR wrong number of arguments for 'mget' command") {
return []string{}, nil
}
return nil, errors.New(fmt.Sprintf("获取失败:%s", err.Error()))
}
client := make([]string, 0, len(values))
for _, val := range values {
client = append(client, val.(string))
}
return client, nil
}
// 随机返回一个
// min最小
// max最大
func (j *JobsGorm) random(min, max int) int {
if max-min <= 0 {
return 0
}
rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min
}

@ -1,22 +0,0 @@
package gojobs
import (
"context"
"github.com/robfig/cron/v3"
"log"
"time"
)
// Ping 心跳
func (j *JobsGorm) Ping(ctx context.Context) {
c := cron.New(cron.WithSeconds())
_, _ = c.AddFunc(GetSeconds(2).Spec(), func() {
result, err := j.redisClient.Set(ctx, j.config.cornKeyPrefix+"_"+j.config.cornKeyCustom, j.config.cornKeyCustom, 3*time.Second).Result()
if j.config.debug == true {
log.Println("JOBS心跳", j.config.cornKeyPrefix+"_"+j.config.cornKeyCustom, j.config.cornKeyCustom, result, err)
}
})
c.Start()
defer c.Stop()
select {}
}

@ -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"
}

@ -0,0 +1,24 @@
package gojobs
import (
"context"
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gotime"
"time"
)
// Lock 上锁
func (c *Client) Lock(ctx context.Context, info jobs_gorm_model.Task, id any) (string, error) {
return c.cache.redisLockClient.Lock(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, id), fmt.Sprintf("[Lock] 已在%s@%s机器上锁成功时间%v", c.config.systemInsideIp, c.config.systemOutsideIp, gotime.Current().Format()), time.Duration(info.Frequency)*3*time.Second)
}
// Unlock Lock 解锁
func (c *Client) Unlock(ctx context.Context, info jobs_gorm_model.Task, id any) error {
return c.cache.redisLockClient.Unlock(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, id))
}
// LockForever 永远上锁
func (c *Client) LockForever(ctx context.Context, info jobs_gorm_model.Task, id any) (string, error) {
return c.cache.redisLockClient.LockForever(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, id), fmt.Sprintf("[LockForever] 已在%s@%s机器永远上锁成功时间%v", c.config.systemInsideIp, c.config.systemOutsideIp, gotime.Current().Format()))
}

@ -0,0 +1,24 @@
package gojobs
import (
"context"
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gotime"
"time"
)
// LockCustomId 上锁
func (c *Client) LockCustomId(ctx context.Context, info jobs_gorm_model.Task) (string, error) {
return c.cache.redisLockClient.Lock(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, info.CustomId), fmt.Sprintf("[LockCustomId] 已在%s@%s机器上锁成功时间%v", c.config.systemInsideIp, c.config.systemOutsideIp, gotime.Current().Format()), time.Duration(info.Frequency)*3*time.Second)
}
// UnlockCustomId 解锁
func (c *Client) UnlockCustomId(ctx context.Context, info jobs_gorm_model.Task) error {
return c.cache.redisLockClient.Unlock(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, info.CustomId))
}
// LockForeverCustomId 永远上锁
func (c *Client) LockForeverCustomId(ctx context.Context, info jobs_gorm_model.Task) (string, error) {
return c.cache.redisLockClient.LockForever(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, info.CustomId), fmt.Sprintf("[LockCustomId] 已在%s@%s机器永远上锁成功时间%v", c.config.systemInsideIp, c.config.systemOutsideIp, gotime.Current().Format()))
}

@ -0,0 +1,24 @@
package gojobs
import (
"context"
"fmt"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gotime"
"time"
)
// LockId 上锁
func (c *Client) LockId(ctx context.Context, info jobs_gorm_model.Task) (string, error) {
return c.cache.redisLockClient.Lock(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, info.Id), fmt.Sprintf("[LockId] 已在%s@%s机器上锁成功时间%v", c.config.systemInsideIp, c.config.systemOutsideIp, gotime.Current().Format()), time.Duration(info.Frequency)*3*time.Second)
}
// UnlockId 解锁
func (c *Client) UnlockId(ctx context.Context, info jobs_gorm_model.Task) error {
return c.cache.redisLockClient.Unlock(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, info.Id))
}
// LockForeverId 永远上锁
func (c *Client) LockForeverId(ctx context.Context, info jobs_gorm_model.Task) (string, error) {
return c.cache.redisLockClient.LockForever(ctx, fmt.Sprintf("%s%s%v%s%v", c.cache.lockKeyPrefix, c.cache.lockKeySeparator, info.Type, c.cache.lockKeySeparator, info.Id), fmt.Sprintf("[LockForeverId] 已在%s@%s机器永远上锁成功时间%v", c.config.systemInsideIp, c.config.systemOutsideIp, gotime.Current().Format()))
}

@ -0,0 +1,66 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gotime"
"github.com/dtapps/go-library/utils/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"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/golog"
"github.com/dtapps/go-library/utils/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,77 +0,0 @@
package pb
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/pubsub"
"log"
"strings"
"time"
)
type PubSubServerService struct {
pub *pubsub.Publisher
UnimplementedPubSubServer
}
func NewPubSubServerService() *PubSubServerService {
return &PubSubServerService{
// 新建一个Publisher对象
pub: pubsub.NewPublisher(time.Millisecond*100, 10),
}
}
// Publish 实现发布方法
func (p *PubSubServerService) Publish(ctx context.Context, req *PublishRequest) (*PublishResponse, error) {
log.Printf("[服务中转]{发布}编号:%s 类型:%s ip地址%s\n", req.GetId(), req.GetValue(), req.GetIp())
// 发布消息
p.pub.Publish(req.GetValue())
return &PublishResponse{
Id: req.GetId(),
Value: req.GetValue(),
Ip: req.GetIp(),
}, nil
}
// Subscribe 实现订阅方法
func (p *PubSubServerService) Subscribe(req *SubscribeRequest, stream PubSub_SubscribeServer) error {
// SubscribeTopic 增加一个使用函数过滤器的订阅者
// func(v interface{}) 定义函数过滤的规则
// SubscribeTopic 返回一个chan interface{}
ch := p.pub.SubscribeTopic(func(v interface{}) bool {
log.Printf("[服务中转]{订阅}主题:%v\n", v)
// 接收数据是string并且key是以arg为前缀的
if key, ok := v.(string); ok {
if strings.HasPrefix(key, req.GetValue()) {
return true
}
}
return false
})
log.Printf("[服务中转]{订阅}编号:%s 类型:%s 方法:%s ip地址%s\n", req.GetId(), req.GetValue(), req.GetMethod(), req.GetIp())
log.Println("[服务中转]{订阅}工作线:", ch)
log.Println("[服务中转]{订阅}当前工作线数量:", p.pub.Len())
// 服务器遍历chan并将其中信息发送给订阅客户端
for v := range ch {
log.Println("[服务中转]{订阅}for ch", ch)
log.Println("[服务中转]{订阅}for v", v)
err := stream.Send(&SubscribeResponse{
Id: req.GetId(),
Value: req.GetValue(),
Method: req.GetMethod(),
})
if err != nil {
log.Println("[服务中转]{订阅}任务分配失败 ", err.Error())
return err
}
}
return nil
}

@ -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",
}

@ -1,125 +0,0 @@
package pubsub
import (
"sync"
"time"
)
// 等待组放在共享内存池中减少GC
var wgPool = sync.Pool{New: func() interface{} { return new(sync.WaitGroup) }}
// NewPublisher
// 第一个参数控制发布时最大阻塞时间
// 第二个参数是缓冲区大小控制每个订阅者的chan缓冲区大小
func NewPublisher(publishTimeout time.Duration, buffer int) *Publisher {
return &Publisher{
buffer: buffer,
timeout: publishTimeout,
subscribers: make(map[subscriber]topicFunc),
}
}
type subscriber chan interface{}
type topicFunc func(v interface{}) bool
type Publisher struct {
m sync.RWMutex // 控制订阅者map并发读写安全
buffer int // 每个订阅者chan缓冲区大小
timeout time.Duration // 发布阻塞超时时间
subscribers map[subscriber]topicFunc
}
// Len 返回订阅者数量
func (p *Publisher) Len() int {
p.m.RLock()
i := len(p.subscribers)
p.m.RUnlock()
return i
}
// Subscribe 无Topic订阅
func (p *Publisher) Subscribe() chan interface{} {
return p.SubscribeTopic(nil)
}
// SubscribeTopic 通过Topic订阅
func (p *Publisher) SubscribeTopic(topic topicFunc) chan interface{} {
ch := make(chan interface{}, p.buffer)
p.m.Lock()
p.subscribers[ch] = topic
p.m.Unlock()
return ch
}
// SubscribeTopicWithBuffer 通过自定义chan缓冲区大小定义新的订阅者
func (p *Publisher) SubscribeTopicWithBuffer(topic topicFunc, buffer int) chan interface{} {
ch := make(chan interface{}, buffer)
p.m.Lock()
p.subscribers[ch] = topic
p.m.Unlock()
return ch
}
// Evict 移除某个订阅者
func (p *Publisher) Evict(sub chan interface{}) {
p.m.Lock()
_, exists := p.subscribers[sub]
if exists {
delete(p.subscribers, sub)
close(sub)
}
p.m.Unlock()
}
// Publish 发布消息
func (p *Publisher) Publish(v interface{}) {
p.m.RLock()
if len(p.subscribers) == 0 {
p.m.RUnlock()
return
}
wg := wgPool.Get().(*sync.WaitGroup)
for sub, topic := range p.subscribers {
wg.Add(1)
go p.sendTopic(sub, topic, v, wg)
}
wg.Wait()
wgPool.Put(wg)
p.m.RUnlock()
}
// Close 关闭服务
func (p *Publisher) Close() {
p.m.Lock()
for sub := range p.subscribers {
delete(p.subscribers, sub)
close(sub)
}
p.m.Unlock()
}
// 真正发布消息的逻辑通过Timer根据传入的timeout控制每次发布消息最大阻塞时长
func (p *Publisher) sendTopic(sub subscriber, topic topicFunc, v interface{}, wg *sync.WaitGroup) {
defer wg.Done()
if topic != nil && !topic(v) {
return
}
// 如果接收器不可用,请在选择“不阻止”下发送
if p.timeout > 0 {
timeout := time.NewTimer(p.timeout)
defer timeout.Stop()
select {
case sub <- v:
case <-timeout.C:
}
return
}
select {
case sub <- v:
default:
}
}

@ -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,90 @@
package gojobs
import (
"context"
"errors"
"fmt"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gostring"
"math/rand"
"time"
)
// GetIssueAddress 获取下发地址
// workers 在线列表
// v 任务信息
// ---
// address 下发地址
// err 错误信息
func (c *Client) GetIssueAddress(ctx context.Context, workers []string, v *jobs_gorm_model.Task) (string, error) {
var (
currentIp = "" // 当前Ip
appointIpStatus = false // 指定Ip状态
)
if v.SpecifyIp != "" {
v.SpecifyIp = goip.IsIp(v.SpecifyIp)
}
// 赋值ip
if v.SpecifyIp != "" && v.SpecifyIp != SpecifyIpNull {
currentIp = v.SpecifyIp
appointIpStatus = true
}
// 只有一个客户端在线
if len(workers) == 1 {
if appointIpStatus {
// 判断是否指定某ip执行
if gostring.Contains(workers[0], currentIp) {
c.zapLog.WithTraceId(ctx).Sugar().Info("只有一个客户端在线指定某ip执行", workers[0], currentIp)
return workers[0], nil
}
return "", errors.New(fmt.Sprintf("需要执行的[%s]客户端不在线", currentIp))
}
return workers[0], nil
}
// 优先处理指定某ip执行
if appointIpStatus {
for wk, wv := range workers {
if gostring.Contains(wv, currentIp) {
c.zapLog.WithTraceId(ctx).Sugar().Info("优先处理指定某ip执行", workers[wk], currentIp)
return workers[wk], nil
}
}
return "", errors.New(fmt.Sprintf("需要执行的[%s]客户端不在线", currentIp))
} else {
// 随机返回一个
address := workers[c.random(0, len(workers))]
if address == "" {
return address, errors.New("获取执行的客户端异常")
}
c.zapLog.WithTraceId(ctx).Sugar().Info("随机返回一个:", address, currentIp)
return address, nil
}
}
// GetSubscribeClientList 获取在线的客户端
func (c *Client) GetSubscribeClientList(ctx context.Context) (client []string, err error) {
// 查询活跃的channel
client, err = c.cache.redisClient.PubSubChannels(ctx, c.cache.cornKeyPrefix+"_*").Result()
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("获取在线的客户端失败:%s%v", c.cache.cornKeyPrefix+"_*", err)
}
return client, err
}
// 随机返回一个
// min最小
// max最大
func (c *Client) random(min, max int) int {
if max-min <= 0 {
return 0
}
rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min
}

@ -0,0 +1,98 @@
package gojobs
import (
"context"
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
"github.com/dtapps/go-library/utils/gotime"
"github.com/dtapps/go-library/utils/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"
"github.com/dtapps/go-library/utils/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
}
Loading…
Cancel
Save