mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
fix(backend/data): 修复爬虫任务取消后未关闭 ctx 的问题
- 在 crawler_api.go 文件中的多个函数中添加了 chromedp.Cancel(ctx) 调用 - 确保在任务取消时能够正确关闭 ctx,避免资源泄露
This commit is contained in:
parent
1a728672c8
commit
076dc4f9ef
@ -68,6 +68,7 @@ func (c *CrawlerApi) GetHtml(url, waitVisible string, headless bool) (string, bo
|
|||||||
defer pcancel()
|
defer pcancel()
|
||||||
ctx, cancel := chromedp.NewContext(pctx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
ctx, cancel := chromedp.NewContext(pctx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
defer chromedp.Cancel(ctx)
|
||||||
err := chromedp.Run(ctx, chromedp.Navigate(url),
|
err := chromedp.Run(ctx, chromedp.Navigate(url),
|
||||||
chromedp.WaitVisible(waitVisible, chromedp.ByQuery), // 确保 元素可见
|
chromedp.WaitVisible(waitVisible, chromedp.ByQuery), // 确保 元素可见
|
||||||
chromedp.WaitReady(waitVisible, chromedp.ByQuery), // 确保 元素准备好
|
chromedp.WaitReady(waitVisible, chromedp.ByQuery), // 确保 元素准备好
|
||||||
@ -80,6 +81,7 @@ func (c *CrawlerApi) GetHtml(url, waitVisible string, headless bool) (string, bo
|
|||||||
} else {
|
} else {
|
||||||
ctx, cancel := chromedp.NewContext(c.crawlerCtx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
ctx, cancel := chromedp.NewContext(c.crawlerCtx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
defer chromedp.Cancel(ctx)
|
||||||
err := chromedp.Run(ctx, chromedp.Navigate(url), chromedp.WaitVisible("body"), chromedp.InnerHTML("body", &htmlContent))
|
err := chromedp.Run(ctx, chromedp.Navigate(url), chromedp.WaitVisible("body"), chromedp.InnerHTML("body", &htmlContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.SugaredLogger.Error(err.Error())
|
logger.SugaredLogger.Error(err.Error())
|
||||||
@ -197,6 +199,7 @@ func (c *CrawlerApi) GetHtmlWithActions(actions *[]chromedp.Action, headless boo
|
|||||||
defer pcancel()
|
defer pcancel()
|
||||||
ctx, cancel := chromedp.NewContext(pctx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
ctx, cancel := chromedp.NewContext(pctx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
defer chromedp.Cancel(ctx)
|
||||||
|
|
||||||
err := chromedp.Run(ctx, *actions...)
|
err := chromedp.Run(ctx, *actions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -206,6 +209,7 @@ func (c *CrawlerApi) GetHtmlWithActions(actions *[]chromedp.Action, headless boo
|
|||||||
} else {
|
} else {
|
||||||
ctx, cancel := chromedp.NewContext(c.crawlerCtx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
ctx, cancel := chromedp.NewContext(c.crawlerCtx, chromedp.WithLogf(logger.SugaredLogger.Infof))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
defer chromedp.Cancel(ctx)
|
||||||
|
|
||||||
err := chromedp.Run(ctx, *actions...)
|
err := chromedp.Run(ctx, *actions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user