快速开始api开发(五)定时任务 crontab 和 队列 queue

本文需要介绍 gotools 工具

需要使用的工具命令:gotools queue -h
需要使用的工具命令:gotools crontab -h
代码地址:github.com/wuyan94zl/gotools

队列

生成

执行:gotools queue --name register

目录

queue
    |-- register
        |-- register.go
    |-- queue.go

编写逻辑

修改:queue/register/register.go

package register

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/hibiken/asynq"
    "github.com/wuyan94zl/example-api/container"
    "github.com/wuyan94zl/example-api/models/user"
    "github.com/wuyan94zl/gotools/utils"
)

func Handle(ctx context.Context, t *asynq.Task) error {
    params := Params{}
    err := json.Unmarshal(t.Payload(), &params)
    if err != nil {
        return err
    }
    Do(ctx, params)
    return nil
}

const QueueKey = "key" // todo 自定义队列key

type Params struct {
    // todo 自定义队列参数结构体
    Nickname string `json:"nickname"`
    LoginID  string `json:"login_id"`
    Password string `json:"password"`
}

func Do(ctx context.Context, params Params) {
    // todo 队列业务逻辑处理
    u := user.Users{
        Nickname: params.Nickname,
        LoginID:  params.LoginID,
        Password: utils.Md5ByString(params.Password),
    }
    info, err := container.Instance().UserModel.Insert(ctx, &u)
    fmt.Println(info, err)
}

队列逻辑为:添加注册一个用户

定时任务

生成

执行:gotools crontab -n register

目录

crontab
    |-- register
        |-- cronjob.go
    |-- crontab.go

编写逻辑

修改:crontab/register/cronjob.go

package register

import (
    "fmt"
    "github.com/wuyan94zl/example-api/queue"
    "github.com/wuyan94zl/example-api/queue/register"
    "time"
)

const Spec = "0 * * * * *" // todo 设置定时时间 秒 分 时 日 月 周

func NewJob() *Job {
    return &Job{}
}

type Job struct{}

func (j *Job) Run() {
    // todo 定时处理逻辑
    params := register.Params{
        Nickname: fmt.Sprintf("无言%s", time.Now().Format("01021504")),
        LoginID:  fmt.Sprintf("login%s", time.Now().Format("01021504")),
        Password: "123456",
    }
    queue.Add(register.QueueKey, params)
}

逻辑为:每分钟向添加用户队列发送一个消息

启动队列和定时任务 (仅操作一次)

修改:main.go

package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/wuyan94zl/example-api/config"
    "github.com/wuyan94zl/example-api/container"
    "github.com/wuyan94zl/example-api/crontab"
    "github.com/wuyan94zl/example-api/queue"
    "github.com/wuyan94zl/example-api/router"
    "github.com/wuyan94zl/gotools/utils"
)

func main() {
    c := new(config.Config)
    utils.MustConfig("/config.yaml", c)

    container.NewContainer(c.Container)

    go queue.NewInstance(c.Container.Redis.Host, c.Container.Redis.Pass).Start() // 增加启动队列代码
    go crontab.NewInstance().Start() // 增加启动定时任务代码

    app := gin.Default()
    group := app.Group("")
    router.RegisterHandlers(group)
    app.Run(fmt.Sprintf("%s:%d", c.Host, c.Port))
}

go-zero 启动

    group := service.NewServiceGroup()
    defer group.Stop()
    group.Add(queue.NewInstance(host, pass)) // 增加启动队列代码
    group.Add(crontab.NewInstance()) // 增加启动定时任务代码
    group.Start()

结束

执行:go mod tidy && go run main.go
验证是否每分钟会注册一个用户

下节 对象存储/日志

本作品采用《CC 协议》,转载必须注明作者和本文链接
没有造飞机的格局,怎么能拧得好螺丝。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!