Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "time"
  7.  
  8. "github.com/lib/pq"
  9. )
  10.  
  11. func waitForNotification(l *pq.Listener) {
  12. for {
  13. select {
  14. case ch := <-l.Notify:
  15. fmt.Printf("received notification, new cdr available %#v\n", ch)
  16. return
  17. case <-time.After(90 * time.Second):
  18. go func() {
  19. l.Ping()
  20. }()
  21. // Check if there's more work available, just in case it takes
  22. // a while for the Listener to notice connection loss and
  23. // reconnect.
  24. // fmt.Println("received no work for 90 seconds, checking for new work")
  25. return
  26. }
  27. }
  28. }
  29.  
  30. func reportProblem(ev pq.ListenerEventType, err error) {
  31. fmt.Printf("%+v, err: %s", ev, err)
  32. }
  33.  
  34. func main() {
  35. conninfo := "host=localhost user=postgres dbname=tts sslmode=disable"
  36.  
  37. db, err := sql.Open("postgres", conninfo)
  38. if err != nil {
  39. panic(err)
  40. }
  41.  
  42. fmt.Println(db, conninfo)
  43.  
  44. reportProblem := func(ev pq.ListenerEventType, err error) {
  45. if err != nil {
  46. fmt.Println(err.Error())
  47. }
  48. }
  49.  
  50. listener := pq.NewListener(conninfo, 10*time.Second, time.Minute, reportProblem)
  51. err = listener.Listen("cdr_created")
  52. if err != nil {
  53. panic(err)
  54. }
  55.  
  56. fmt.Println("entering main loop")
  57. for {
  58. // process all available work before waiting for notifications
  59. //getWork(db)
  60. waitForNotification(listener)
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement