Created
May 3, 2015 13:32
-
-
Save dvirsky/ef97023d23a34430a02a to your computer and use it in GitHub Desktop.
Disque Usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import( | |
"fmt" | |
"time" | |
"github.com/EverythingMe/go-disque/disque" | |
"github.com/garyburd/redigo/redis" | |
) | |
// getPool creates a client pool that selects disque nodes on random | |
func getPool(addrs ...string) *disque.Pool { | |
pool := disque.NewPool(disque.DialFunc(func(addr string) (redis.Conn, error) { | |
return redis.Dial("tcp", addr) | |
}), addrs...) | |
pool.RefreshNodes() | |
return pool | |
} | |
// Enqueuing example | |
func ExampleEnqueue() { | |
pool := getPool("127.0.0.1:7711") | |
client, err := pool.Get() | |
if err != nil { | |
panic(err) | |
} | |
defer client.Close() | |
// Create an "add" request with optional parameters. | |
ja := disque.AddRequest{ | |
Job: disque.Job{ | |
Queue: "myQueye", | |
Data: []byte("foo bar"), | |
}, | |
Timeout: time.Millisecond * 100, | |
} | |
// Add the job to the queue | |
if _, err := client.Add(ja); err != nil { | |
panic(err) | |
} | |
} | |
// Getting a job example | |
func ExampleGet(ch chan bool) { | |
pool := getPool("127.0.0.1:7711") | |
client, err := pool.Get() | |
if err != nil { | |
panic(err) | |
} | |
defer client.Close() | |
// Get one job | |
job, err := client.Get(time.Second, "myQueye") | |
if err != nil { | |
panic(err) | |
} | |
// the job data is "foo bar" | |
fmt.Println(string(job.Data)) | |
ch <- true | |
} | |
func main(){ | |
ch := make(chan bool) | |
go ExampleGet(ch) | |
go ExampleEnqueue() | |
<-ch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment