MCPcopy Create free account
hub / github.com/dataddo/pgq / HandleMessage

Method HandleMessage

example_consumer_test.go:18–48  ·  view source on GitHub ↗
(ctx context.Context, msg *pgq.MessageIncoming)

Source from the content-addressed store, hash-verified

16type Handler struct{}
17
18func (h *Handler) HandleMessage(ctx context.Context, msg *pgq.MessageIncoming) (res bool, err error) {
19 defer func() {
20 r := recover()
21 if r == nil {
22 return
23 }
24 log.Println("Recovered in 'Handler.HandleMessage()'", r)
25 // nack the message, it will be retried
26 res = pgq.MessageNotProcessed
27 if e, ok := r.(error); ok {
28 err = e
29 } else {
30 err = fmt.Errorf("%v", r)
31 }
32 }()
33 if msg.Metadata["heaviness"] == "heavy" {
34 // nack the message, it will be retried
35 // Message won't contain error detail in the database.
36 return pgq.MessageNotProcessed, nil
37 }
38 var myPayload struct {
39 Foo string `json:"foo"`
40 }
41 if err := json.Unmarshal(msg.Payload, &myPayload); err != nil {
42 // discard the message, it will not be retried
43 // Message will contain error detail in the database.
44 return pgq.MessageProcessed, fmt.Errorf("invalid payload: %v", err)
45 }
46 // doSomethingWithThePayload(ctx, myPayload)
47 return pgq.MessageProcessed, nil
48}
49
50func ExampleConsumer() {
51 db, err := sql.Open("postgres", "user=postgres password=postgres host=localhost port=5432 dbname=postgres")

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected