first commit
This commit is contained in:
19
internal/pubsub/publisher.go
Normal file
19
internal/pubsub/publisher.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
sqlwatermill "github.com/ThreeDotsLabs/watermill-sql/pkg/sql"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
func NewPublisher(db *sql.DB, logger watermill.LoggerAdapter) (message.Publisher, error) {
|
||||
return sqlwatermill.NewPublisher(
|
||||
db,
|
||||
sqlwatermill.PublisherConfig{
|
||||
SchemaAdapter: sqlwatermill.DefaultPostgreSQLSchema{},
|
||||
},
|
||||
logger,
|
||||
)
|
||||
}
|
||||
23
internal/pubsub/subscriber.go
Normal file
23
internal/pubsub/subscriber.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"database/sql"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
sqlwatermill "github.com/ThreeDotsLabs/watermill-sql/pkg/sql"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
func NewSubscriber(db *sql.DB, logger watermill.LoggerAdapter) (message.Subscriber, error) {
|
||||
return sqlwatermill.NewSubscriber(
|
||||
db,
|
||||
sqlwatermill.SubscriberConfig{
|
||||
SchemaAdapter: sqlwatermill.DefaultPostgreSQLSchema{},
|
||||
OffsetsAdapter: sqlwatermill.DefaultPostgreSQLOffsetsAdapter{},
|
||||
PollInterval: 3 * time.Second,
|
||||
},
|
||||
logger,
|
||||
)
|
||||
}
|
||||
40
internal/pubsub/table.go
Normal file
40
internal/pubsub/table.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func CreateTopicTables(db *sql.DB, topic string) {
|
||||
topicTable := fmt.Sprintf("watermill_%s", topic)
|
||||
offsetTable := fmt.Sprintf("watermill_offsets_%s", topic)
|
||||
|
||||
_, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
"offset" BIGSERIAL PRIMARY KEY,
|
||||
uuid UUID NOT NULL,
|
||||
payload BYTEA NOT NULL,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
`, topicTable))
|
||||
if err != nil {
|
||||
log.Fatalf("❌ Failed to ensure topic table %s: %v", topicTable, err)
|
||||
}
|
||||
|
||||
_, err = db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
consumer_group VARCHAR(255) NOT NULL,
|
||||
"offset" BIGINT NOT NULL DEFAULT 0,
|
||||
offset_acked BIGINT NOT NULL DEFAULT 0,
|
||||
offset_consumed BIGINT NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (consumer_group)
|
||||
);
|
||||
`, offsetTable))
|
||||
if err != nil {
|
||||
log.Fatalf("❌ Failed to ensure offsets table %s: %v", offsetTable, err)
|
||||
}
|
||||
|
||||
log.Printf("✅ Watermill tables ensured for topic: %s", topic)
|
||||
}
|
||||
Reference in New Issue
Block a user