-
Notifications
You must be signed in to change notification settings - Fork 0
/
article_consumer.go
106 lines (91 loc) · 3.13 KB
/
article_consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package worker
import (
"context"
"database/sql"
"errors"
"log"
"os"
"time"
nats "github.com/nats-io/nats.go"
"github.com/romashorodok/news-tracker/backend/internal/service"
"github.com/romashorodok/news-tracker/backend/internal/storage"
"github.com/romashorodok/news-tracker/pkg/natsinfo"
"go.uber.org/fx"
)
type articleConsumerWorker struct {
js nats.JetStreamContext
articleService *service.ArticleService
}
func (a *articleConsumerWorker) handler(ctx context.Context) func(msg *nats.Msg) {
return func(msg *nats.Msg) {
var article natsinfo.Article
if err := article.Unmarshal(msg.Data); err != nil {
log.Println("Unable deserialize %s article payload. Err:%s", msg.Subject, err)
_ = msg.Ack()
}
articleID, err := a.articleService.GetArticleIDByTitleAndOrigin(ctx, storage.GetArticleIDByTitleAndOriginParams{
Title: article.Title,
Origin: article.Origin,
})
if errors.Is(err, sql.ErrNoRows) {
if articleID, err := a.articleService.NewArticle(ctx, service.NewArticleParams{
Article: storage.NewArticleParams{
Title: article.Title,
Preface: article.Preface,
Content: article.Content,
Origin: article.Origin,
ViewersCount: int32(article.ViewersCount),
PublishedAt: article.PublishedAt,
},
MainImageURL: article.MainImage,
ContentImagesURLs: article.ContentImages,
}); err == nil {
log.Printf("Created article %d.", articleID)
_ = msg.Ack()
return
}
} else if err != nil {
log.Printf("Unexpected database error for Title:%s Origin:%s. Err:%s", article.Title, article.Origin, err)
return
}
if err = a.articleService.UpdateArticleStats(ctx, storage.UpdateArticleStatsParams{
ViewersCount: int32(article.ViewersCount),
UpdatedAt: time.Now(),
ID: articleID,
}); err != nil {
log.Printf("Unable update article for Title:%s Origin:%s. Err:%s", article.Title, article.Origin, err)
return
}
log.Printf("Update article %d stats.", articleID)
_ = msg.Ack()
}
}
func (a *articleConsumerWorker) start(ctx context.Context) {
if _, err := natsinfo.CreateOrUpdateStream(a.js, natsinfo.ARTICLES_STREAM_CONFIG); err != nil {
log.Panicf("unable set-up nats %s stream. Err:%s", natsinfo.ARTICLES_STREAM_CONFIG.Name, err)
os.Exit(1)
}
queueGroup := "backend-articles-consumer"
stream, subject, subOpts, config := natsinfo.ArticlesStream_NewArticleConsumerConfig(queueGroup)
if _, err := natsinfo.CreateOrUpdateConsumer(a.js, stream, config); err != nil {
log.Panicf("unable set-up nats %s consumer. Err:%s", queueGroup, err)
os.Exit(1)
}
if _, err := a.js.QueueSubscribe(subject, queueGroup, a.handler(ctx), subOpts...); err != nil {
log.Panicf("unable start nats %s consumer. Err:%s", queueGroup, err)
os.Exit(1)
}
<-ctx.Done()
}
type StartArticleConsumerWorkerParams struct {
fx.In
JS nats.JetStreamContext
ArticleService *service.ArticleService
}
func StartArticleConsumerWorker(params StartArticleConsumerWorkerParams) {
worker := &articleConsumerWorker{
js: params.JS,
articleService: params.ArticleService,
}
go worker.start(context.Background())
}