Browse Source

update

master
dengbiao 1 month ago
parent
commit
c31c42edfa
9 changed files with 275 additions and 4 deletions
  1. +3
    -3
      Dockerfile-task
  2. +2
    -0
      app/task/init.go
  3. +2
    -0
      app/task/md/cron_key.go
  4. +81
    -0
      app/task/svc/svc_agent_settlement_state.go
  5. +81
    -0
      app/task/svc/svc_medium_settlement_state.go
  6. +23
    -0
      app/task/task_agent_settlement_state.go
  7. +23
    -0
      app/task/task_medium_settlement_state.go
  8. +58
    -0
      k8s/task-prd.yaml
  9. +2
    -1
      main.go

+ 3
- 3
Dockerfile-task View File

@@ -13,7 +13,7 @@ WORKDIR /go/release
ADD . .

# 编译:把main.go编译成可执行的二进制文件,命名为zyos
RUN GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -tags netgo -ldflags="-s -w" -installsuffix cgo -o zyos_mall_task cmd/task/main.go
RUN GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -tags netgo -ldflags="-s -w" -installsuffix cgo -o advertisement-super-task cmd/task/main.go

FROM ubuntu:xenial as prod
LABEL maintainer="wuhanqin"
@@ -25,10 +25,10 @@ RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
# 在build阶段复制可执行的go二进制文件app
COPY --from=build /go/release/zyos_mall_task ./zyos_mall_task
COPY --from=build /go/release/advertisement-super-task ./advertisement-super-task

COPY --from=build /go/release/etc/task.yml /var/zyos/task.yml

# 启动服务
CMD ["./zyos_mall_task","-c","/var/zyos/task.yml"]
CMD ["./advertisement-super-task","-c","/var/zyos/task.yml"]


+ 2
- 0
app/task/init.go View File

@@ -88,4 +88,6 @@ func doTask(fnName string) func() {
// 增加自动任务队列
func initTasks() {
jobs[taskMd.CronCalcMasterDataStatistics] = taskCalcMasterDataStatistics // 计算站长数据统计
jobs[taskMd.CronMediumSettlementState] = taskMediumSettlementState // 媒体结算单状态
jobs[taskMd.CronAgentSettlementState] = taskAgentSettlementState // 代理结算单状态
}

+ 2
- 0
app/task/md/cron_key.go View File

@@ -2,4 +2,6 @@ package md

const (
CronCalcMasterDataStatistics = "cron_calc_master_data_statistics" // 计算站长数据统计
CronMediumSettlementState = "cron_medium_settlement_state" // 媒体结算单状态
CronAgentSettlementState = "cron_agent_settlement_state" // 代理结算单状态
)

+ 81
- 0
app/task/svc/svc_agent_settlement_state.go View File

@@ -0,0 +1,81 @@
package svc

import (
"applet/app/utils/cache"
"applet/app/utils/logx"
model2 "code.fnuoos.com/zhimeng/model.git/src/super/model"
"errors"
"fmt"
"time"
"xorm.io/xorm"
)

const AgentSettlementStateLockKey = "agent_settlement_state_lock_key"
const AgentSettlementStateLockKeyValue = "running"

func AgentSettlementState(eg *xorm.Engine) {
now := time.Now()
if now.Hour() > 1 && now.Hour() > 8 {
//TODO::只在凌晨 1 点 ~ 凌晨 8 点运行
fmt.Println("err>>>>>>>", errors.New("非运行时间"))
logx.Warn(errors.New("非运行时间"))
return
}

//TODO::增加“悲观锁”防止串行
getString, _ := cache.GetString(AgentSettlementStateLockKey)
//if err != nil {
// return err
//}
if getString == AgentSettlementStateLockKeyValue {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
logx.Warn(errors.New("上一次结算未执行完"))
return
}
cache.SetEx(AgentSettlementStateLockKey, AgentSettlementStateLockKeyValue, 3600*8) //8小时

fmt.Println("calc master data statistics...")
defer func() {
if err := recover(); err != nil {
_ = logx.Error(err)
}
}()

today := now.Format("2006-01-02")

//1、查询"日结"结算单 agent_settlement 数据
_, err := eg.Where("kind = ?", 1).And("start_date != ?", today).Update(&model2.AgentSettlement{
State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
EndDate: today,
})
if err != nil {
fmt.Println("err>>>>>>>", err.Error())
logx.Warn(err)
}

//2、查询"周结"结算单 agent_settlement 数据
weekday := now.Weekday()
if weekday == time.Monday {
_, err = eg.Where("kind = ?", 2).And("start_date != ?", today).Update(&model2.AgentSettlement{
State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
EndDate: today,
})
if err != nil {
fmt.Println("err>>>>>>>", err.Error())
logx.Warn(err)
}
}

//3、查询"月结"结算单 agent_settlement 数据
if now.Day() == 1 {
_, err = eg.Where("kind = ?", 3).And("start_date != ?", today).Update(&model2.AgentSettlement{
State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
EndDate: today,
})
if err != nil {
fmt.Println("err>>>>>>>", err.Error())
logx.Warn(err)
}
}
return
}

+ 81
- 0
app/task/svc/svc_medium_settlement_state.go View File

@@ -0,0 +1,81 @@
package svc

import (
"applet/app/utils/cache"
"applet/app/utils/logx"
model2 "code.fnuoos.com/zhimeng/model.git/src/super/model"
"errors"
"fmt"
"time"
"xorm.io/xorm"
)

const MediumSettlementStateLockKey = "medium_settlement_state_lock_key"
const MediumSettlementStateLockKeyValue = "running"

func MediumSettlementState(eg *xorm.Engine) {
now := time.Now()
if now.Hour() > 1 && now.Hour() > 8 {
//TODO::只在凌晨 1 点 ~ 凌晨 8 点运行
fmt.Println("err>>>>>>>", errors.New("非运行时间"))
logx.Warn(errors.New("非运行时间"))
return
}

//TODO::增加“悲观锁”防止串行
getString, _ := cache.GetString(MediumSettlementStateLockKey)
//if err != nil {
// return err
//}
if getString == MediumSettlementStateLockKeyValue {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
logx.Warn(errors.New("上一次结算未执行完"))
return
}
cache.SetEx(MediumSettlementStateLockKey, MediumSettlementStateLockKeyValue, 3600*8) //8小时

fmt.Println("calc master data statistics...")
defer func() {
if err := recover(); err != nil {
_ = logx.Error(err)
}
}()

today := now.Format("2006-01-02")

//1、查询"日结"结算单 medium_settlement 数据
_, err := eg.Where("kind = ?", 1).And("start_date != ?", today).Update(&model2.MediumSettlement{
State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
EndDate: today,
})
if err != nil {
fmt.Println("err>>>>>>>", err.Error())
logx.Warn(err)
}

//2、查询"周结"结算单 medium_settlement 数据
weekday := now.Weekday()
if weekday == time.Monday {
_, err = eg.Where("kind = ?", 2).And("start_date != ?", today).Update(&model2.MediumSettlement{
State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
EndDate: today,
})
if err != nil {
fmt.Println("err>>>>>>>", err.Error())
logx.Warn(err)
}
}

//3、查询"月结"结算单 medium_settlement 数据
if now.Day() == 1 {
_, err = eg.Where("kind = ?", 3).And("start_date != ?", today).Update(&model2.MediumSettlement{
State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订)
EndDate: today,
})
if err != nil {
fmt.Println("err>>>>>>>", err.Error())
logx.Warn(err)
}
}
return
}

+ 23
- 0
app/task/task_agent_settlement_state.go View File

@@ -0,0 +1,23 @@
package task

import (
"applet/app/task/svc"
"math/rand"
"time"
"xorm.io/xorm"
)

// 计算站长数据统计
func taskAgentSettlementState(eg *xorm.Engine) {
for {
if len(ch) > workerNum {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
} else {
goto START
}
}
START:
ch <- 1
svc.AgentSettlementState(eg)
<-ch
}

+ 23
- 0
app/task/task_medium_settlement_state.go View File

@@ -0,0 +1,23 @@
package task

import (
"applet/app/task/svc"
"math/rand"
"time"
"xorm.io/xorm"
)

// 计算站长数据统计
func taskMediumSettlementState(eg *xorm.Engine) {
for {
if len(ch) > workerNum {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
} else {
goto START
}
}
START:
ch <- 1
svc.MediumSettlementState(eg)
<-ch
}

+ 58
- 0
k8s/task-prd.yaml View File

@@ -0,0 +1,58 @@
kind: Deployment
apiVersion: apps/v1
metadata:
name: advertisement-super-task
namespace: advertisement
labels:
app: advertisement-super-task
annotations:
kubesphere.io/creator: dengbiao
kubesphere.io/description: 广告联盟总站长定时任务
spec:
replicas: 1
selector:
matchLabels:
app: advertisement-super-task
template:
metadata:
creationTimestamp: null
labels:
app: advertisement-super-task
spec:
volumes:
- name: host-time
hostPath:
path: /etc/localtime
type: ''
- name: advertisement-super-task-cfg
configMap:
name: advertisement-super-task
items:
- key: task.yml
path: task.yml
defaultMode: 420
containers:
- name: advertisement-super-task
image: >-
registry.cn-shenzhen.aliyuncs.com/fnuoos-prd/advertisement-super-task:0.1
resources: {}
volumeMounts:
- name: host-time
readOnly: true
mountPath: /etc/localtime
- name: advertisement-super-task-cfg
readOnly: true
mountPath: /var/zyos/task.yml
subPath: task.yml
terminationMessagePolicy: File
imagePullPolicy: Always
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600

+ 2
- 1
main.go View File

@@ -3,6 +3,7 @@ package main
import (
db "code.fnuoos.com/zhimeng/model.git/src"
"context"
"errors"
"fmt"
"log"
"net/http"
@@ -75,7 +76,7 @@ func main() {
// Initializing the server in a goroutine so that it won't block the graceful shutdown handling below
go func() { //协程启动监听http服务
fmt.Println("Listening and serving HTTP on " + cfg.SrvAddr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("listen: %s\n", err)
}
}()


Loading…
Cancel
Save