未加星标

Gores:Go 语言编写的基于 Redis 的消息队列系统

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Gores

An asynchronous job execution system based on Redis

Installation

Get the package

$ go get github.com/wang502/gores/gores

Import the package

import "github.com/wang502/gores/gores" Usage Configuration

Add a config.json in your project folder

{ "REDISURL": "127.0.0.1:6379", "REDIS_PW": "mypassword", "BLPOP_MAX_BLOCK_TIME" : 1, "MAX_WORKERS": 2, "Queues": ["queue1", "queue2"], "DispatcherTimeout": 5, "WorkerTimeout": 5 } REDISURL : Redis server address. If you run in a local Redis, the dafault host is 127.0.0.1:6379 REDIS_PW : Redis password. If the password is not set, then password can be any string. BLPOP_MAX_BLOCK_TIME : Blocking time when calling BLPOP command in Redis. MAX_WORKERS : Maximum number of concurrent workers, each worker is a separate goroutine that execute specific task on the fetched item. Queues : Array of queue names on Redis message broker. DispatcherTimeout : Duration dispatcher will wait to dispatch new job before quitting. WorkerTimeout : Duration worker will wait to process new job before quitting.

Initialize config

configPath := flag.String("c", "config.json", "path to configuration file") flag.Parse() config, err := gores.InitConfig(*configPath) Enqueue item to Redis queue

An item is a Go map. It is required to have several keys:

Name : name of the item to enqueue, items with different names are mapped to different tasks. Queue : name of the queue you want to put the item in. Args : the required arguments that you need in order for the workers to execute those tasks. Enqueue_timestamp : the Unix timestamp of when the item is enqueued. resq := gores.NewResQ(config) item := map[string]interface{}{ "Name": "Rectangle", "Queue": "TestJob", "Args": map[string]interface{}{ "Length": 10, "Width": 10, }, "Enqueue_timestamp": time.Now().Unix(), } err = resq.Enqueue(item) if err != nil { log.Fatalf("ERROR Enqueue item to ResQ") } $ go run main.go -c ./config.json -o produce Define tasks package tasks // task for item with 'Name' = 'Rectangle' // calculating the area of an rectangle by multiplying Length with Width func CalculateArea(args map[string]interface{}) error { var err error length := args["Length"] width := args["Width"] if length == nil || width == nil { err = errors.New("Map has no required attributes") return err } fmt.Printf("The area is %d\n", int(length.(float64)) * int(width.(float64))) return err } Launch workers to consume items and execute tasks tasks := map[string]interface{}{ "Item": tasks.PrintItem, "Rectangle": tasks.CalculateArea, } gores.Launch(config, &tasks) $ go run main.go -c ./config.json -o consume

The output will be:

The rectangle area is 100 Info about processed/failed job resq := gores.NewResQ(config) if resq == nil { log.Fatalf("resq is nil") } info := resq.Info() for k, v := range info { switch v.(type) { case string: fmt.Printf("%s : %s\n", k, v) case int: fmt.Printf("%s : %d\n", k, v) case int64: fmt.Printf("%s : %d\n", k, v) } }

The output will be:

Gores Info: queues : 2 workers : 0 failed : 0 host : 127.0.0.1:6379 pending : 0 processed : 1 Contribution

Please feel free to suggest new features. Also open to pull request!

本文数据库(综合)相关术语:系统安全软件

主题: RedisEDISUISUTI
分页:12
转载请注明
本文标题:Gores:Go 语言编写的基于 Redis 的消息队列系统
本站链接:http://www.codesec.net/view/524116.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(69)