Skip to content

Commit 9a229c3

Browse files
authored
Add 0.5.0 upgrade hook (#107)
1 parent d443c5d commit 9a229c3

File tree

12 files changed

+636
-68
lines changed

12 files changed

+636
-68
lines changed

internal/app/appfx/module.go

-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/bitmagnet-io/bitmagnet/internal/classifier/classifierfx"
1010
"github.com/bitmagnet-io/bitmagnet/internal/database/databasefx"
1111
"github.com/bitmagnet-io/bitmagnet/internal/database/migrations"
12-
"github.com/bitmagnet-io/bitmagnet/internal/database/search/warmer"
1312
"github.com/bitmagnet-io/bitmagnet/internal/dhtcrawler/dhtcrawlerfx"
1413
"github.com/bitmagnet-io/bitmagnet/internal/gql/gqlfx"
1514
"github.com/bitmagnet-io/bitmagnet/internal/importer/importerfx"
@@ -51,8 +50,5 @@ func New() fx.Option {
5150
),
5251
fx.Provide(webui.New),
5352
fx.Decorate(migrations.NewDecorator),
54-
fx.Decorate(
55-
warmer.NewDecorator,
56-
),
5753
)
5854
}

internal/boilerplate/worker/worker.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ import (
1212
type RegistryParams struct {
1313
fx.In
1414
fx.Shutdowner
15-
Workers []Worker `group:"workers"`
16-
Logger *zap.SugaredLogger
15+
Workers []Worker `group:"workers"`
16+
Decorators []Decorator `group:"worker_decorators"`
17+
Logger *zap.SugaredLogger
1718
}
1819

1920
type RegistryResult struct {
2021
fx.Out
2122
Registry Registry
2223
}
2324

24-
func NewRegistry(p RegistryParams) RegistryResult {
25+
func NewRegistry(p RegistryParams) (RegistryResult, error) {
2526
r := &registry{
2627
mutex: &sync.RWMutex{},
2728
workers: make(map[string]Worker),
@@ -30,7 +31,12 @@ func NewRegistry(p RegistryParams) RegistryResult {
3031
for _, w := range p.Workers {
3132
r.workers[w.Key()] = w
3233
}
33-
return RegistryResult{Registry: r}
34+
for _, d := range p.Decorators {
35+
if err := r.decorate(d.Key, d.Decorate); err != nil {
36+
return RegistryResult{}, err
37+
}
38+
}
39+
return RegistryResult{Registry: r}, nil
3440
}
3541

3642
type Registry interface {
@@ -41,20 +47,25 @@ type Registry interface {
4147
DisableAll()
4248
Start(ctx context.Context) error
4349
Stop(ctx context.Context) error
44-
Decorate(name string, fn Decorator) error
50+
decorate(name string, fn DecorateFunction) error
4551
}
4652

4753
type Worker interface {
4854
Key() string
4955
Enabled() bool
5056
Started() bool
51-
Decorate(Decorator) Worker
5257
_hook() fx.Hook
5358
setEnabled(enabled bool)
5459
setStarted(started bool)
60+
decorate(DecorateFunction) Worker
5561
}
5662

57-
type Decorator func(fx.Hook) fx.Hook
63+
type DecorateFunction func(fx.Hook) fx.Hook
64+
65+
type Decorator struct {
66+
Key string
67+
Decorate DecorateFunction
68+
}
5869

5970
type worker struct {
6071
key string
@@ -82,7 +93,7 @@ func (w *worker) Started() bool {
8293
return w.started
8394
}
8495

85-
func (w *worker) Decorate(fn Decorator) Worker {
96+
func (w *worker) decorate(fn DecorateFunction) Worker {
8697
return &worker{
8798
key: w.key,
8899
hook: fn(fx.Hook{
@@ -210,11 +221,11 @@ func (r *registry) Stop(ctx context.Context) error {
210221
return nil
211222
}
212223

213-
func (r *registry) Decorate(name string, fn Decorator) error {
224+
func (r *registry) decorate(name string, fn DecorateFunction) error {
214225
r.mutex.Lock()
215226
defer r.mutex.Unlock()
216227
if w, ok := r.workers[name]; ok {
217-
r.workers[name] = w.Decorate(fn)
228+
r.workers[name] = w.decorate(fn)
218229
return nil
219230
}
220231
return fmt.Errorf("worker %s not found", name)

internal/database/dao/gen.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)