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

Function ExamplePublisher

example_publisher_test.go:17–53  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15}
16
17func ExamplePublisher() {
18 db, err := sql.Open("postgres", "user=postgres password=postgres host=localhost port=5432 dbname=postgres")
19 if err != nil {
20 log.Fatal("Error opening database:", err)
21 }
22 defer db.Close()
23 const queueName = "test_queue"
24 p := pgq.NewPublisher(db)
25 payload, _ := json.Marshal(PayloadStruct{Foo: "bar"})
26 messages := []*pgq.MessageOutgoing{
27 {
28 Metadata: pgq.Metadata{
29 "version": "1.0",
30 },
31 Payload: json.RawMessage(payload),
32 },
33 {
34 Metadata: pgq.Metadata{
35 "version": "1.0",
36 },
37 Payload: json.RawMessage(payload),
38 },
39 {
40 Metadata: pgq.Metadata{
41 "version": "1.0",
42 },
43 Payload: json.RawMessage(payload),
44 },
45 }
46 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
47 defer cancel()
48 ids, err := p.Publish(ctx, queueName, messages...)
49 if err != nil {
50 log.Fatal("Error publishing message:", err)
51 }
52 log.Println("Published messages with ids:", ids)
53}

Callers

nothing calls this directly

Calls 3

PublishMethod · 0.95
NewPublisherFunction · 0.92
FatalMethod · 0.65

Tested by

no test coverage detected