parent
c77bfce263
commit
1602f17a51
@ -1,5 +1,5 @@
|
||||
package go_library
|
||||
|
||||
func Version() string {
|
||||
return "1.0.54"
|
||||
return "1.0.55"
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
package goheader
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Headers map[string]interface{}
|
||||
|
||||
func NewHeaders() Headers {
|
||||
P := make(Headers)
|
||||
return P
|
||||
}
|
||||
|
||||
func (p Headers) Set(key string, value interface{}) {
|
||||
p[key] = value
|
||||
}
|
||||
|
||||
func (p Headers) SetHeaders(headers Headers) {
|
||||
for key, value := range headers {
|
||||
p[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
func (p Headers) GetQuery() string {
|
||||
u := url.Values{}
|
||||
for k, v := range p {
|
||||
u.Set(k, GetHeadersString(v))
|
||||
}
|
||||
return u.Encode()
|
||||
}
|
||||
|
||||
func GetHeadersString(i interface{}) string {
|
||||
switch v := i.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
default:
|
||||
bytes, _ := json.Marshal(v)
|
||||
return string(bytes)
|
||||
}
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 66 MiB After Width: | Height: | Size: 65 MiB |
Binary file not shown.
Binary file not shown.
@ -1,21 +0,0 @@
|
||||
package gomac
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func GetMacAddrs() (macAddrs []string) {
|
||||
netInterfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return macAddrs
|
||||
}
|
||||
for _, netInterface := range netInterfaces {
|
||||
macAddr := netInterface.HardwareAddr.String()
|
||||
if len(macAddr) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
macAddrs = append(macAddrs, macAddr)
|
||||
}
|
||||
return macAddrs
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
CodeAbnormal = 0 // 异常
|
||||
CodeError = http.StatusInternalServerError // 失败
|
||||
CodeSuccess = http.StatusOK // 成功
|
||||
CodeEnd = http.StatusCreated // 结束
|
||||
)
|
||||
|
||||
// 每隔n秒执行一次
|
||||
const specSeconds = "*/%d * * * * *"
|
||||
|
||||
// GetSpecSeconds 每隔n秒执行一次
|
||||
var GetSpecSeconds = func(n int64) string {
|
||||
if n < 0 && n > 59 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(specSeconds, n)
|
||||
}
|
||||
|
||||
// GetFrequencySeconds 每隔n秒执行一次
|
||||
var GetFrequencySeconds = func(n int64) int64 {
|
||||
if n < 0 && n > 59 {
|
||||
return -1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// 每隔n分钟执行一次
|
||||
const specMinutes = "0 */%d * * * *"
|
||||
|
||||
// GetSpecMinutes 每隔n分钟执行一次
|
||||
var GetSpecMinutes = func(n int64) string {
|
||||
if n < 0 && n > 59 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(specMinutes, n)
|
||||
}
|
||||
|
||||
// GetFrequencyMinutes 每隔n分钟执行一次
|
||||
var GetFrequencyMinutes = func(n int64) int64 {
|
||||
if n < 0 && n > 59 {
|
||||
return -1
|
||||
}
|
||||
return n * 60
|
||||
}
|
||||
|
||||
// 每天n点执行一次
|
||||
const specHour = "0 0 */%d * * *"
|
||||
|
||||
// GetSpecHour 每天n点执行一次
|
||||
var GetSpecHour = func(n int64) string {
|
||||
if n < 0 && n > 23 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(specHour, n)
|
||||
}
|
||||
|
||||
// GetFrequencyHour 每天n点执行一次
|
||||
var GetFrequencyHour = func(n int64) int64 {
|
||||
if n < 0 && n > 23 {
|
||||
return -1
|
||||
}
|
||||
return n * 60 * 60
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"github.com/dtapps/go-library/utils/goip"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RefreshIp 刷新Ip
|
||||
func (app *App) RefreshIp(tx *gorm.DB) {
|
||||
xip := goip.GetOutsideIp()
|
||||
if app.OutsideIp == "" || app.OutsideIp == "0.0.0.0" {
|
||||
return
|
||||
}
|
||||
if app.OutsideIp == xip {
|
||||
return
|
||||
}
|
||||
tx.Where("ips = ?", app.OutsideIp).Delete(&TaskIp{}) // 删除
|
||||
app.OutsideIp = xip
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dtapps/go-library/utils/dorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Lock 上锁
|
||||
func (app *App) Lock(info Task, id any) string {
|
||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||
judgeCache := app.Redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
||||
if judgeCache != "" {
|
||||
return judgeCache
|
||||
}
|
||||
app.Redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器上锁成功", app.OutsideIp), dorm.WithExpire(time.Millisecond*time.Duration(info.Frequency)*3))
|
||||
return ""
|
||||
}
|
||||
|
||||
// Unlock Lock 解锁
|
||||
func (app *App) Unlock(info Task, id any) {
|
||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||
app.Redis.NewStringOperation().Del(cacheName)
|
||||
}
|
||||
|
||||
// LockForever 永远上锁
|
||||
func (app *App) LockForever(info Task, id any) string {
|
||||
cacheName := fmt.Sprintf("cron:%v:%v", info.Type, id)
|
||||
judgeCache := app.Redis.NewStringOperation().Get(cacheName).UnwrapOr("")
|
||||
if judgeCache != "" {
|
||||
return judgeCache
|
||||
}
|
||||
app.Redis.NewStringOperation().Set(cacheName, fmt.Sprintf("已在%v机器永远上锁成功", app.OutsideIp))
|
||||
return ""
|
||||
}
|
@ -1,181 +0,0 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
TASK_IN = "IN" // 任务运行
|
||||
TASK_SUCCESS = "SUCCESS" // 任务完成
|
||||
TASK_ERROR = "ERROR" // 任务异常
|
||||
TASK_TIMEOUT = "TIMEOUT" // 任务超时
|
||||
TASK_WAIT = "WAIT" // 任务等待
|
||||
)
|
||||
|
||||
// Task 任务
|
||||
type Task struct {
|
||||
Id uint `gorm:"primaryKey" json:"id"` // 记录编号
|
||||
Status string `json:"status"` // 状态码
|
||||
Params string `json:"params"` // 参数
|
||||
ParamsType string `json:"params_type"` // 参数类型
|
||||
StatusDesc string `json:"status_desc"` // 状态描述
|
||||
Frequency int64 `json:"frequency"` // 频率(秒单位)
|
||||
Number int64 `json:"number"` // 当前次数
|
||||
MaxNumber int64 `json:"max_number"` // 最大次数
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
CustomId string `json:"custom_id"` // 自定义编号
|
||||
CustomSequence int64 `json:"custom_sequence"` // 自定义顺序
|
||||
Type string `json:"type"` // 类型
|
||||
CreatedIp string `json:"created_ip"` // 创建外网IP
|
||||
SpecifyIp string `json:"specify_ip"` // 指定外网IP
|
||||
UpdatedIp string `json:"updated_ip"` // 更新外网IP
|
||||
Result string `json:"result"` // 结果
|
||||
CreatedAt string `gorm:"type:text" json:"created_at"` // 创建时间
|
||||
UpdatedAt string `gorm:"type:text" json:"updated_at"` // 更新时间
|
||||
}
|
||||
|
||||
func (m *Task) TableName() string {
|
||||
return "task"
|
||||
}
|
||||
|
||||
// TaskTake 查询任务
|
||||
func (app *App) TaskTake(tx *gorm.DB, customId string) (result Task) {
|
||||
tx.Where("custom_id = ?", customId).Where("status = ?", TASK_IN).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskCustomIdTake 查询任务
|
||||
func (app *App) TaskCustomIdTake(tx *gorm.DB, Type, customId string) (result Task) {
|
||||
tx.Where("type = ?", Type).Where("custom_id = ?", customId).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskCustomIdTakeStatus 查询任务
|
||||
func (app *App) TaskCustomIdTakeStatus(tx *gorm.DB, Type, customId, status string) (result Task) {
|
||||
tx.Where("type = ?", Type).Where("custom_id = ?", customId).Where("status = ?", status).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskFind 查询任务
|
||||
func (app *App) TaskFind(tx *gorm.DB, frequency int64) (results []Task) {
|
||||
tx.Table("task").Select("task.*").Where("task.frequency = ?", frequency).Where("task.status = ?", TASK_IN).Where("task_ip.ips = ?", app.OutsideIp).Order("task.id asc").Joins("left join task_ip on task_ip.task_type = task.type").Find(&results)
|
||||
return app.taskFindCheck(results)
|
||||
}
|
||||
|
||||
// TaskFindAll 查询任务
|
||||
func (app *App) TaskFindAll(tx *gorm.DB, frequency int64) (results []Task) {
|
||||
tx.Where("frequency = ?", frequency).Where("status = ?", TASK_IN).Order("id asc").Find(&results)
|
||||
return results
|
||||
}
|
||||
|
||||
// 检查任务
|
||||
func (app *App) taskFindCheck(lists []Task) (results []Task) {
|
||||
for _, v := range lists {
|
||||
if v.SpecifyIp == "" {
|
||||
results = append(results, v)
|
||||
} else {
|
||||
if app.OutsideIp == v.SpecifyIp {
|
||||
results = append(results, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// TaskLog 任务日志
|
||||
type TaskLog struct {
|
||||
Id uint `gorm:"primaryKey" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
StatusCode int `json:"status_code"` // 状态码
|
||||
Desc string `json:"desc"` // 结果
|
||||
Version int `json:"version"` // 版本
|
||||
CreatedAt string `gorm:"type:text" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLog) TableName() string {
|
||||
return "task_log"
|
||||
}
|
||||
|
||||
// TaskLogRun 任务执行日志
|
||||
type TaskLogRun struct {
|
||||
Id uint `gorm:"primaryKey" json:"id"` // 记录编号
|
||||
TaskId uint `json:"task_id"` // 任务编号
|
||||
RunId string `json:"run_id"` // 执行编号
|
||||
OutsideIp string `json:"outside_ip"` // 外网ip
|
||||
InsideIp string `json:"inside_ip"` // 内网ip
|
||||
Os string `json:"os"` // 系统类型
|
||||
Arch string `json:"arch"` // 系统架构
|
||||
Gomaxprocs int `json:"gomaxprocs"` // CPU核数
|
||||
GoVersion string `json:"go_version"` // GO版本
|
||||
MacAddrs string `json:"mac_addrs"` // Mac地址
|
||||
CreatedAt string `gorm:"type:text" json:"created_at"` // 创建时间
|
||||
}
|
||||
|
||||
func (m *TaskLogRun) TableName() string {
|
||||
return "task_log_run"
|
||||
}
|
||||
|
||||
// TaskLogRunTake 查询任务执行日志
|
||||
func (app *App) TaskLogRunTake(tx *gorm.DB, taskId uint, runId string) (result TaskLogRun) {
|
||||
tx.Select("id", "os", "arch", "outside_ip", "created_at").Where("task_id = ?", taskId).Where("run_id = ?", runId).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// TaskIp 任务Ip
|
||||
type TaskIp struct {
|
||||
Id int64
|
||||
TaskType string `json:"task_type"` // 任务编号
|
||||
Ips string `json:"ips"` // 任务IP
|
||||
}
|
||||
|
||||
func (m *TaskIp) TableName() string {
|
||||
return "task_ip"
|
||||
}
|
||||
|
||||
func (app *App) TaskIpUpdate(tx *gorm.DB, taskType, ips string) *gorm.DB {
|
||||
var query TaskIp
|
||||
tx.Where("task_type = ?", taskType).Where("ips = ?", ips).Take(&query)
|
||||
if query.Id != 0 {
|
||||
return tx
|
||||
}
|
||||
updateStatus := tx.Create(&TaskIp{
|
||||
TaskType: taskType,
|
||||
Ips: ips,
|
||||
})
|
||||
if updateStatus.RowsAffected == 0 {
|
||||
log.Println("任务更新失败:", updateStatus.Error)
|
||||
}
|
||||
return updateStatus
|
||||
}
|
||||
|
||||
// TaskIpInit 实例任务ip
|
||||
func (app *App) TaskIpInit(tx *gorm.DB, ips map[string]string) bool {
|
||||
if app.OutsideIp == "" || app.OutsideIp == "0.0.0.0" {
|
||||
return false
|
||||
}
|
||||
tx.Where("ips = ?", app.OutsideIp).Delete(&TaskIp{}) // 删除
|
||||
for k, v := range ips {
|
||||
if v == "" {
|
||||
app.TaskIpUpdate(tx, k, app.OutsideIp)
|
||||
} else {
|
||||
find := strings.Contains(v, ",")
|
||||
if find == true {
|
||||
// 包含
|
||||
parts := strings.Split(v, ",")
|
||||
for _, vv := range parts {
|
||||
if vv == app.OutsideIp {
|
||||
app.TaskIpUpdate(tx, k, app.OutsideIp)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 不包含
|
||||
if v == app.OutsideIp {
|
||||
app.TaskIpUpdate(tx, k, app.OutsideIp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package jobs
|
||||
|
||||
var ParamsOrderType = "order"
|
||||
|
||||
// ParamsOrderId 订单任务
|
||||
type ParamsOrderId struct {
|
||||
OrderId string `json:"order_id,omitempty"`
|
||||
}
|
||||
|
||||
var ParamsMerchantGoldenBeanType = "merchant.golden_bean"
|
||||
|
||||
var ParamsNewServiceType = "new_service"
|
||||
|
||||
// ParamsTaskId 企业自定义任务
|
||||
type ParamsTaskId struct {
|
||||
TaskId int64 `json:"task_id,omitempty"`
|
||||
}
|
||||
|
||||
var ParamsNewServiceNextType = "new_service.next"
|
||||
|
||||
// ParamsTaskIdNext 企业自定义下一步任务
|
||||
type ParamsTaskIdNext struct {
|
||||
TaskId int64 `json:"task_id,omitempty"`
|
||||
MerchantUserId int64 `json:"merchant_user_id,omitempty"`
|
||||
CurrentNumber int `json:"current_number,omitempty"`
|
||||
MaxNumber int `json:"max_number,omitempty"`
|
||||
}
|
||||
|
||||
var ParamsTeamInvType = "team.inv"
|
@ -1,25 +0,0 @@
|
||||
package jobs
|
||||
|
||||
func GetTypeApiPaySubmit(Type string) string {
|
||||
return "api.pay.submit." + Type
|
||||
}
|
||||
|
||||
func GetTypeWechatRefundsSubmit(Type string) string {
|
||||
return "wechat.refunds.submit." + Type
|
||||
}
|
||||
|
||||
func GetTypeWechatRefundsQuery(Type string) string {
|
||||
return "wechat.refunds.query." + Type
|
||||
}
|
||||
|
||||
func GetTypeGoldenBeansIssue(Type string) string {
|
||||
return "golden_beans.issue." + Type
|
||||
}
|
||||
|
||||
func GetTypeGoldenBeansRefunds(Type string) string {
|
||||
return "golden_beans.refunds." + Type
|
||||
}
|
||||
|
||||
func GetTypeCustomerAuto(Type string) string {
|
||||
return "customer.auto." + Type
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package jobs
|
||||
|
||||
const Version = "1.0.16"
|
Loading…
Reference in new issue