From a32b1ef69ba2a67b5f0d0b0448b7594e68d1d1d7 Mon Sep 17 00:00:00 2001 From: nquidox Date: Sun, 1 Mar 2026 01:01:06 +0300 Subject: [PATCH 1/3] CloseAndRecv in send --- internal/app/app.go | 2 +- internal/remote/interface.go | 2 +- internal/remote/send.go | 17 +++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index c7359e1..4f27a7d 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -141,7 +141,7 @@ func (app *App) Run(ctx context.Context) { l := len(sendData) if l > 0 { log.WithField("length", l).Debug("Sending parsed data") - app.network.SendResult(apiClient, sendData) + app.network.SendResult(ctx, apiClient, sendData) sendData = sendData[:0] } } diff --git a/internal/remote/interface.go b/internal/remote/interface.go index b34de33..210d214 100644 --- a/internal/remote/interface.go +++ b/internal/remote/interface.go @@ -8,5 +8,5 @@ import ( type Handler interface { RequestTasks(ctx context.Context, client pb.TaskProcessorClient) []shared.TaskResponse - SendResult(client pb.TaskProcessorClient, tasksDone []shared.TaskResult) + SendResult(ctx context.Context, client pb.TaskProcessorClient, tasksDone []shared.TaskResult) } diff --git a/internal/remote/send.go b/internal/remote/send.go index 6e9c148..046fadf 100644 --- a/internal/remote/send.go +++ b/internal/remote/send.go @@ -5,10 +5,14 @@ import ( log "github.com/sirupsen/logrus" "task-processor/internal/shared" pb "task-processor/proto/taskProcessor" + "time" ) -func (n *Network) SendResult(client pb.TaskProcessorClient, tasksDone []shared.TaskResult) { - stream, err := client.SendResult(context.Background()) +func (n *Network) SendResult(ctx context.Context, client pb.TaskProcessorClient, tasksDone []shared.TaskResult) { + sendCtx, cancel := context.WithTimeout(ctx, time.Second*60) + defer cancel() + + stream, err := client.SendResult(sendCtx) if err != nil { log.Fatalf("Error calling PostMerch: %v", err) } @@ -29,7 +33,12 @@ func (n *Network) SendResult(client pb.TaskProcessorClient, tasksDone []shared.T } } - if err = stream.CloseSend(); err != nil { - log.Fatalf("Error closing stream: %v", err) + //if err = stream.CloseSend(); err != nil { + // log.Fatalf("Error closing stream: %v", err) + //} + + _, err = stream.CloseAndRecv() + if err != nil { + log.Fatalf("Error receiving response: %v", err) } } From 532386222bcc3dce1da70c53e99911223babb049 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 18 Mar 2026 20:34:22 +0300 Subject: [PATCH 2/3] setup browser --- internal/parsers/mandarake/handleTasks.go | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/parsers/mandarake/handleTasks.go b/internal/parsers/mandarake/handleTasks.go index 1ddd10a..8596747 100644 --- a/internal/parsers/mandarake/handleTasks.go +++ b/internal/parsers/mandarake/handleTasks.go @@ -10,8 +10,39 @@ import ( "task-processor/internal/shared" ) +func (s *Parser) setupBrowser(ctx context.Context) (string, error) { + allocCtx, allocCancel := chromedp.NewRemoteAllocator(ctx, s.externalBrowser) + defer allocCancel() + + pageCtx, pageCancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(func(string, ...any) {})) + defer pageCancel() + + copyright := "No copyright div found." + + if err := chromedp.Run(pageCtx, + chromedp.Navigate("https://www.mandarake.co.jp/"), + chromedp.WaitReady("body", chromedp.ByQuery), + chromedp.Text(`div.copyright`, ©right, chromedp.ByQuery, chromedp.AtLeast(0)), + chromedp.Navigate("https://www.mandarake.co.jp/index2.html"), + chromedp.WaitReady("body", chromedp.ByQuery), + ); err != nil { + log.WithError(err).Error(logHeader + logGetPrice + "failed to get single price tag") + return copyright, err + } + + return copyright, nil +} + func (s *Parser) HandleTasks(ctx context.Context, tasks []shared.Task, sender chan shared.TaskResult, state *appState.State) { - log.Infof("%v %v handling tasks", logHeader, logWorker) + log.Infof("%v Start handling tasks", logHeader) + log.Infof("%v Setting up browser", logHeader) + cr, err := s.setupBrowser(ctx) + if err != nil { + log.WithError(err).Error(logHeader + logGetPrice + "failed to setup browser") + } + log.WithField("Copyright message", cr).Infof("%v Finished setting up browser.", logHeader) + + log.Infof("%v %v processing tasks...", logHeader, logWorker) allocCtx, allocCancel := chromedp.NewRemoteAllocator(ctx, s.externalBrowser) defer allocCancel() From 1e331cada590fea93f47e1fb1d13852478a902ac Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 18 Mar 2026 20:34:33 +0300 Subject: [PATCH 3/3] gin logger --- pkg/router/handler.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/router/handler.go b/pkg/router/handler.go index 5ede9de..0f924cc 100644 --- a/pkg/router/handler.go +++ b/pkg/router/handler.go @@ -21,7 +21,7 @@ type Deps struct { const pkgLogHeader string = "Router |" func NewHandler(deps Deps) *Handler { - engine := gin.Default() + engine := gin.New() if deps.GinMode == "release" { gin.SetMode(gin.ReleaseMode) @@ -32,17 +32,12 @@ func NewHandler(deps Deps) *Handler { } } - engine.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"msg": "v2"}) }) + logGroup := engine.Group("") + logGroup.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"msg": "v2"}) }) p := ginprometheus.NewPrometheus("gin") p.Use(engine) - engine.Use(gin.LoggerWithConfig(gin.LoggerConfig{ - Skip: func(c *gin.Context) bool { - return c.Request.URL.Path == "/metrics" - }, - })) - srv := http.Server{ Addr: deps.Addr, Handler: engine,