Skip to content

Commit

Permalink
chore: do not start server on config-db run <scraper.yml> (#977)
Browse files Browse the repository at this point in the history
* chore: do not start serve on config-db run <>

* chore: use debug port
  • Loading branch information
yashmehrotra authored Oct 8, 2024
1 parent 79211a6 commit 74a59cf
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

var outputDir string
var filename string
var debugPort int

// Run ...
var Run = &cobra.Command{
Expand All @@ -47,38 +48,37 @@ var Run = &cobra.Command{
dutyCtx = c
}

e := echo.New()

e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.SetRequest(c.Request().WithContext(dutyCtx.Wrap(c.Request().Context())))
return next(c)
}
})

dutyEcho.AddDebugHandlers(dutyCtx, e, func(next echo.HandlerFunc) echo.HandlerFunc { return next })

shutdown.AddHook(func() {
ctx, cancel := gocontext.WithTimeout(gocontext.Background(), 1*time.Minute)
defer cancel()

if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
})

shutdown.WaitForSignal()

go func() {
if err := e.Start(fmt.Sprintf(":%d", httpPort)); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
}()

if dutyapi.DefaultConfig.ConnectionString == "" && outputDir == "" {
logger.Fatalf("skipping export: neither --output-dir nor --db is specified")
}

if debugPort >= 0 {
e := echo.New()
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.SetRequest(c.Request().WithContext(dutyCtx.Wrap(c.Request().Context())))
return next(c)
}
})
dutyEcho.AddDebugHandlers(dutyCtx, e, func(next echo.HandlerFunc) echo.HandlerFunc { return next })

shutdown.AddHook(func() {
ctx, cancel := gocontext.WithTimeout(gocontext.Background(), 1*time.Minute)
defer cancel()

if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
})
shutdown.WaitForSignal()

go func() {
if err := e.Start(fmt.Sprintf(":%d", debugPort)); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
}()
}

for i := range scraperConfigs {
ctx, cancel, cancelTimeout := api.NewScrapeContext(dutyCtx).WithScrapeConfig(&scraperConfigs[i]).
WithTimeout(dutyCtx.Properties().Duration("scraper.timeout", 4*time.Hour))
Expand Down Expand Up @@ -165,4 +165,5 @@ func exportResource(resource v1.ScrapeResult, filename, outputDir string) error
func init() {
Run.Flags().StringVarP(&outputDir, "output-dir", "o", "", "The output folder for configurations")
Run.Flags().StringVarP(&filename, "filename", "f", ".id", "The filename to save seach resource under")
Run.Flags().IntVar(&debugPort, "debug-port", -1, "Start an HTTP server to use the /debug routes, Use -1 to disable and 0 to pick a free port")
}

0 comments on commit 74a59cf

Please sign in to comment.