Skip to content

Commit

Permalink
feat:增加资源管理,能够在关闭时释放资源 fix:修复游戏达人启动chromedp资源无法及时释放
Browse files Browse the repository at this point in the history
  • Loading branch information
Azusa-Yuan committed Oct 5, 2024
1 parent 395fb86 commit ffeb4ac
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
40 changes: 38 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package main

import (
_ "MiniBot/utils/log"
"MiniBot/utils/resource"
"MiniBot/utils/schedule"
"context"
"os"
"os/signal"
"syscall"
"time"

"MiniBot/config"
"MiniBot/service/web"
"MiniBot/utils"
_ "MiniBot/utils/db"

// ---------以下插件均可通过前面加 // 注释,注释后停用并不加载插件--------- //
Expand Down Expand Up @@ -36,6 +41,8 @@ import (
"ZeroBot/message"

"MiniBot/plugin/manager"

"github.com/rs/zerolog/log"
// webctrl "github.com/FloatTech/zbputils/control/web"
// -----------------------以上为内置依赖,勿动------------------------ //
)
Expand All @@ -59,5 +66,34 @@ func main() {
r.Run("127.0.0.1:8888")
}()
}
zero.RunAndBlock(&config.Config.Z, utils.GlobalInitMutex.Unlock)
zero.Run(&config.Config.Z)

// 优雅关闭
// 创建一个 channel 用于监听退出信号
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// 等待接收到退出信号
<-quit
log.Info().Str("name", "main").Msg("Shutting down server...")

errChan := make(chan error, 1)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
go func() {
errChan <- resource.ResourceManager.Cleanup(ctx)
}()

select {
case err := <-errChan:
if err != nil {
log.Error().Str("name", "main").Err(err).Msg("")
} else {
log.Info().Str("name", "main").Msg("Server exited gracefully")
}
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
log.Error().Str("name", "main").Msg("cleanup operation timed out")
}
}
}
18 changes: 14 additions & 4 deletions plugin/dnf/service/yxdr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"MiniBot/utils/resource"
"context"
"fmt"

Expand Down Expand Up @@ -39,11 +40,20 @@ var (
chromedp.ProxyServer("http://127.0.0.1:10809"),
)

allocCtx, _ = chromedp.NewExecAllocator(context.Background(), opts...)
TemCtx, _ = chromedp.NewContext(allocCtx)
ScCtx = context.WithoutCancel(TemCtx)
allocCtx, cancel1 = chromedp.NewExecAllocator(context.Background(), opts...)
Ctx, cancel2 = chromedp.NewContext(allocCtx)
)

func init() {
resource.ResourceManager.Register(
func(ctx context.Context) error {
cancel2()
cancel1()
return nil
},
)
}

func Screenshot(server string, productType string) ([]byte, string, error) {

if _, ok := ReportRegions[server]; !ok {
Expand All @@ -53,7 +63,7 @@ func Screenshot(server string, productType string) ([]byte, string, error) {
// 导航到指定的URL
var buf []byte
url := fmt.Sprintf("https://www.yxdr.com/bijiaqi/dnf/%s/kua%s", productType, ReportRegions[server])
err := chromedp.Run(ScCtx,
err := chromedp.Run(Ctx,
chromedp.Navigate(url),
chromedp.WaitVisible("#right_m"),
chromedp.FullScreenshot(&buf, 100),
Expand Down
25 changes: 25 additions & 0 deletions utils/resource/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package resource

import "context"

// ResourceManager 管理注册的资源释放函数
type resourceManager struct {
cleanupFuncs []func(context.Context) error
}

// Register 注册释放资源的函数
func (rm *resourceManager) Register(cleanupFunc func(context.Context) error) {
rm.cleanupFuncs = append(rm.cleanupFuncs, cleanupFunc)
}

// Cleanup 执行所有注册的释放函数
func (rm *resourceManager) Cleanup(ctx context.Context) error {
for _, cleanup := range rm.cleanupFuncs {
if err := cleanup(ctx); err != nil {
return err
}
}
return nil
}

var ResourceManager = &resourceManager{}

0 comments on commit ffeb4ac

Please sign in to comment.