1 Commits

Author SHA1 Message Date
Adrian Zürcher
7fec8e1101 fix orphan instances 2025-12-29 17:15:52 +01:00
3 changed files with 31 additions and 16 deletions

View File

@@ -17,11 +17,12 @@ import (
// html to pdf converter structure for // html to pdf converter structure for
type Converter struct { type Converter struct {
chromePath string chromePath string
ctx context.Context allocCtx context.Context
cancel context.CancelFunc allocCancel context.CancelFunc // Cancels the whole Chrome process manager
allocCancel context.CancelFunc browserCtx context.Context // The specific browser instance
progress func(progress int) browserCancel context.CancelFunc // Closes the browser
progress func(progress int)
} }
// NewConverter starts a new converter instance with a chrome headless shell executable // NewConverter starts a new converter instance with a chrome headless shell executable
@@ -43,9 +44,17 @@ func NewConverter(chromePath string) (*Converter, error) {
opts = append(opts, platformOptions()) opts = append(opts, platformOptions())
var allocCtx context.Context c.allocCtx, c.allocCancel = chromedp.NewExecAllocator(context.Background(), opts...)
allocCtx, c.cancel = chromedp.NewExecAllocator(context.Background(), opts...) c.browserCtx, c.browserCancel = chromedp.NewContext(c.allocCtx)
c.ctx, c.allocCancel = chromedp.NewContext(allocCtx)
// 5. "Warm up" the browser to ensure the executable actually runs
// This catches "file not found" or permission errors immediately
err = chromedp.Run(c.browserCtx)
if err != nil {
c.Close() // Cleanup if start fails
return nil, fmt.Errorf("failed to start chrome: %w", err)
}
return c, nil return c, nil
} }
@@ -82,11 +91,11 @@ func (c *Converter) Convert(files ...models.File) error {
htmlURL.WriteString(filepath.ToSlash(absPath)) htmlURL.WriteString(filepath.ToSlash(absPath))
ctx, cancel := context.WithTimeout(c.ctx, 60*time.Second) taskCtx, taskCancel := chromedp.NewContext(c.browserCtx)
defer cancel() timeoutCtx, timeoutCancel := context.WithTimeout(taskCtx, 60*time.Second)
var pdfData []byte var pdfData []byte
err = chromedp.Run(ctx, err = chromedp.Run(timeoutCtx,
chromedp.Navigate(htmlURL.String()), chromedp.Navigate(htmlURL.String()),
chromedp.WaitReady("body", chromedp.ByQuery), chromedp.WaitReady("body", chromedp.ByQuery),
chromedp.ActionFunc(func(ctx context.Context) error { chromedp.ActionFunc(func(ctx context.Context) error {
@@ -103,24 +112,27 @@ func (c *Converter) Convert(files ...models.File) error {
}), }),
) )
timeoutCancel()
taskCancel()
if err != nil { if err != nil {
c.cancel()
return err return err
} }
// Save PDF to file // Save PDF to file
if err := os.WriteFile(f.Output, pdfData, 0644); err != nil { if err := os.WriteFile(f.Output, pdfData, 0644); err != nil {
c.cancel()
return err return err
} }
} }
c.cancel()
return nil return nil
} }
func (c *Converter) Close() { func (c *Converter) Close() {
if c.cancel != nil { // Close browser first, then allocator
c.cancel() if c.browserCancel != nil {
c.browserCancel()
}
if c.allocCancel != nil {
c.allocCancel()
} }
} }

View File

@@ -15,6 +15,8 @@ func Convert(chromePath, inputFile, outputFile string) error {
if err != nil { if err != nil {
return err return err
} }
defer c.Close()
return c.Convert(input) return c.Convert(input)
} }

View File

@@ -46,6 +46,7 @@ func TestConvertFiles(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close()
c.SetProgressCallback(func(progress int) { c.SetProgressCallback(func(progress int) {
fmt.Println(progress) fmt.Println(progress)