(ctx context.Context, msg *pgq.MessageIncoming)
| 16 | type Handler struct{} |
| 17 | |
| 18 | func (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 | |
| 50 | func ExampleConsumer() { |
| 51 | db, err := sql.Open("postgres", "user=postgres password=postgres host=localhost port=5432 dbname=postgres") |
nothing calls this directly
no outgoing calls
no test coverage detected