Verified Commit a890edf6 authored by Marcel's avatar Marcel

Use chrome pools for better consistent performance

parent 25f9c08f
......@@ -4,13 +4,12 @@ import (
"context"
"fmt"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/runner"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gitlab.com/iotssl/luftdaten_screenshot_tool/actions"
"log"
"os"
"runtime"
"sync"
)
var FolderPath string
......@@ -25,7 +24,7 @@ var rootCmd = &cobra.Command{
Use: "luftdaten_screenshot_tool",
Short: "LuftdatenImage is a simple luftdaten.info web screenshoter",
Long: `A simple headless screenshot Tool for luftdaten.info build with golang and Google Chrome`,
Version: "0.1.0",
Version: "0.2.0",
RunE: Run,
}
......@@ -37,62 +36,21 @@ func Run(cmd *cobra.Command, args []string) error {
defer cancel()
// create chrome instance
var opts chromedp.Option
if Headless {
if runtime.GOOS == "windows" {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-sandbox", true),
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.UserDataDir("./tmp"),
runner.RemoteDebuggingPort(9222),
)
} else {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-sandbox", true),
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.RemoteDebuggingPort(9222),
)
}
} else {
if runtime.GOOS == "windows" {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.UserDataDir("./tmp"),
runner.RemoteDebuggingPort(9222),
)
} else {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.RemoteDebuggingPort(9222),
)
}
}
pool, err := chromedp.NewPool()
c, err := chromedp.New(ctxt, opts)
if err != nil {
return errors.WithMessage(err, "Failed to call New")
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
err := takeScreenshot(ctxt, &wg, pool)
if err != nil {
fmt.Println(err)
}
}()
// run task list
err = c.Run(ctxt, actions.Screenshot(`http://deutschland.maps.luftdaten.info/`, FolderPath))
if err != nil {
return errors.WithMessage(err, "Failed to call Screenshot")
}
// wait for to finish
wg.Wait()
// shutdown chrome
err = c.Shutdown(ctxt)
err = pool.Shutdown()
if err != nil {
return errors.WithMessage(err, "Failed to call Shutdown")
}
......@@ -106,6 +64,24 @@ func Run(cmd *cobra.Command, args []string) error {
return nil
}
func takeScreenshot(ctxt context.Context, wg *sync.WaitGroup, pool *chromedp.Pool) error {
defer wg.Done()
// allocate
c, err := pool.Allocate(ctxt)
if err != nil {
return errors.WithMessage(err, "Failed to call Allocate")
}
defer c.Release()
// run tasks
err = c.Run(ctxt, actions.Screenshot(`http://deutschland.maps.luftdaten.info/`, FolderPath))
if err != nil {
return errors.WithMessage(err, "Failed to call Run")
}
return nil
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment