10
用 Golang 跑「队列任务」,也可以像 Laravel 一样简单
source link: https://studygolang.com/articles/35668
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
用 Golang 跑「队列任务」,也可以像 Laravel 一样简单
hwbrzzl · 3天之前 · 670 次点击 · 预计阅读时间 2 分钟 · 大约8小时之前 开始浏览话说用 Golang 跑「队列任务」需要几步?使用 Goravel ,四步不能再多了!
第一步:生成任务类
go run . artisan make:job HelloWorld
任务类结构
package jobs
type HelloWorld struct {
}
//Signature The name and signature of the job.
func (receiver *HelloWorld) Signature() string {
return "process_podcast"
}
//Handle Execute the job.
func (receiver *HelloWorld) Handle(args ...interface{}) error {
return nil
}
第二步:注册任务
注册到 app/provides/queue_service_provider.go
func (receiver *QueueServiceProvider) Jobs() []queue.Job {
return []queue.Job{
&jobs.HelloWorld{},
}
}
第三步:启动队列服务器
在根目录下 main.go
中启动队列服务器
package main
import (
"github.com/goravel/framework/support/facades"
"goravel/bootstrap"
)
func main() {
// This bootstraps the framework and gets it ready for use.
bootstrap.Boot()
// Start queue server by facades.Queue.
go facades.Queue.Worker(queue.Args{}).Run()
select {}
}
第四步:调度任务
err := facades.Queue.Job(&jobs.HelloWorld{}, []queue.Arg{}).Dispatch()
OK, Over. 更多功能详见文档,基本用法与 Laravel 保持一致,小伙伴们开始愉快的搞事情吧!
关于 Goravel
Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框架。 作为一个起始脚手架帮助 Golang 开发者快速构建自己的应用。
项目地址:https://github.com/goravel/goravel
文档地址:www.goravel.dev
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK