From 2cef8ca20ef17ddba830c93974ef430074986497 Mon Sep 17 00:00:00 2001 From: runstp Date: Mon, 16 Jan 2023 22:05:44 +0800 Subject: [PATCH] =?UTF-8?q?[feature]=20=E6=96=B0=E5=A2=9Epicture=E5=88=B0?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/spider/main.go | 22 +++++++++++++++++++++- internal/app/spider/picture.go | 28 ++++++++++++++-------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/cmd/spider/main.go b/cmd/spider/main.go index 6a8c1ad..5abf576 100644 --- a/cmd/spider/main.go +++ b/cmd/spider/main.go @@ -22,13 +22,21 @@ func newSpider() fx.Option { video_analysis.Provide(), fx.Provide(spider.NewVideo), fx.Provide(spider.NewUpdate), + fx.Provide(spider.NewPicture), fx.Provide(bilibili.NewSDK), fx.Provide(health.NewCheckServer), fx.Invoke(lc), ) } -func lc(lifecycle fx.Lifecycle, spiderVideo *spider.Video, spiderUpdate *spider.Update, checkServer *health.CheckServer, shutdown fx.Shutdowner) { +func lc( + lifecycle fx.Lifecycle, + spiderVideo *spider.Video, + spiderUpdate *spider.Update, + spiderPicture *spider.Picture, + checkServer *health.CheckServer, + shutdown fx.Shutdowner, +) { lifecycle.Append(fx.Hook{ OnStart: func(ctx context.Context) error { return spiderVideo.Run(ctx) @@ -53,6 +61,18 @@ func lc(lifecycle fx.Lifecycle, spiderVideo *spider.Video, spiderUpdate *spider. }, }) + lifecycle.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + return spiderPicture.Run(ctx) + }, + OnStop: func(ctx context.Context) error { + if err := spiderPicture.Stop(ctx); err != nil { + return err + } + return shutdown.Shutdown() + }, + }) + lifecycle.Append(fx.Hook{ OnStart: func(ctx context.Context) error { go func() { diff --git a/internal/app/spider/picture.go b/internal/app/spider/picture.go index ea6c0ca..013365b 100644 --- a/internal/app/spider/picture.go +++ b/internal/app/spider/picture.go @@ -32,15 +32,15 @@ func NewPicture(db *gorm.DB, logger *zap.Logger, sdk *bilibili.SDK) *Picture { } } -func (v *Picture) Stop(ctx context.Context) error { - v.logger.Info("stopping spider server") +func (p *Picture) Stop(ctx context.Context) error { + p.logger.Info("stopping spider server") for { select { case <-ctx.Done(): return errors.New("shutdown spider server timeout") default: - if err := v.stop(); err != nil { + if err := p.stop(); err != nil { return errors.Wrap(err, "shutdown spider server error") } return nil @@ -48,19 +48,19 @@ func (v *Picture) Stop(ctx context.Context) error { } } -func (v *Picture) stop() error { - v.stopChan <- true - v.isRunning = false +func (p *Picture) stop() error { + p.stopChan <- true + p.isRunning = false return nil } -func (v *Picture) Run(ctx context.Context) error { +func (p *Picture) Run(ctx context.Context) error { tk := time.NewTicker(60 * time.Minute) - v.isRunning = true + p.isRunning = true go func() { - if err := v.spider(); err != nil { - v.logger.Error("start spider server error", zap.Error(err)) + if err := p.spider(); err != nil { + p.logger.Error("start spider server error", zap.Error(err)) } }() @@ -68,11 +68,11 @@ func (v *Picture) Run(ctx context.Context) error { for { select { case <-_tk.C: - v.logger.Info("[tick] picture spider", zap.Time("time", time.Now())) - if err := v.spider(); err != nil { - v.logger.Error("start picture server error", zap.Error(err)) + p.logger.Info("[tick] picture spider", zap.Time("time", time.Now())) + if err := p.spider(); err != nil { + p.logger.Error("start picture server error", zap.Error(err)) } - case <-v.stopChan: + case <-p.stopChan: return } }