aboutsummaryrefslogtreecommitdiff
path: root/internal/application/application.go
blob: d61f38c0b667fd9ebd6db2302bfd1ebfbe3aa531 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package application

import (
	"context"
	"fmt"
	"log/slog"
	"os"
	"sync"

	cm "go.neonxp.ru/conf/model"
	"go.neonxp.ru/pose/internal/database"
	"go.neonxp.ru/pose/internal/model"
	"go.neonxp.ru/pose/internal/source"
	"go.neonxp.ru/pose/internal/target"
)

type Application struct {
	logger  *slog.Logger
	db      *database.DB
	sources []Source
	targets []Target
	wg      sync.WaitGroup
}

func New(cfg cm.Group, logger *slog.Logger) (*Application, error) {
	dbfile := cfg.Get("db_file").StringExt("", os.LookupEnv)
	db, err := database.New(dbfile)
	if err != nil {
		return nil, fmt.Errorf("failed create db: %w", err)
	}

	app := &Application{
		logger:  logger,
		db:      db,
		sources: []Source{},
		targets: []Target{},
		wg:      sync.WaitGroup{},
	}

	//Build sources
	for _, it := range cfg.Get("sources").Group() {
		name := it.String()
		switch it.Name {
		case cm.Ident("feed"):
			src, err := source.NewFeed(
				it.Group(),
				app.logger.With(slog.String("source", name)),
			)
			if err != nil {
				return nil, err
			}
			app.sources = append(app.sources, src)
		default:
			return nil, fmt.Errorf("unknown source type: %s", it.Name)
		}
	}

	// Build targets
	for _, it := range cfg.Get("targets").Group() {
		name := it.String()
		switch it.Name {
		case cm.Ident("telegram"):
			tgt, err := target.NewTelegram(
				it.Group(),
				app.logger.With(slog.String("target", name)),
			)
			if err != nil {
				return nil, err
			}
			app.targets = append(app.targets, tgt)
		default:
			return nil, fmt.Errorf("unknown target type: %s", it.Name)
		}
	}

	return app, nil
}

func (a *Application) Run(ctx context.Context) error {
	defer a.wg.Wait()

	// Сборный канал со всех источников
	inChan := a.inChan(ctx)

	// Срез каналов всех потребителей
	outChans := a.outChans(ctx)

	// Перекладывание сообщений из inChan во все outChans
	for it := range inChan {
		for _, ch := range outChans {
			select {
			case ch <- it:
				slog.InfoContext(ctx, "trying send feed item to target", slog.String("id", it.ID))
			default:
				slog.WarnContext(ctx, "feed item ignored because no free targets")
			}
		}
	}

	return nil
}

func (a *Application) Close() error {
	return a.db.Close()
}

func (a *Application) inChan(ctx context.Context) chan model.Item {
	inChan := make(chan model.Item, 1)
	a.wg.Go(func() {
		<-ctx.Done()
		close(inChan)
	})

	for _, s := range a.sources {
		a.wg.Go(func() {
			for it := range s.Retrive(ctx) {
				if a.db.Exists(it.ID) {
					continue
				}
				if err := a.db.Append(it.ID); err != nil {
					a.logger.ErrorContext(ctx, "failed to add item to deduplication db", slog.Any("err", err))
					continue
				}

				inChan <- it
			}
		})
	}

	return inChan
}

func (a *Application) outChans(ctx context.Context) []chan<- model.Item {
	outChans := make([]chan<- model.Item, len(a.targets))

	for i, t := range a.targets {
		outChans[i] = t.Send(ctx)
	}
	return outChans
}

type Source interface {
	Retrive(ctx context.Context) <-chan model.Item
}

type Target interface {
	Send(ctx context.Context) chan<- model.Item
}