-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated go version, dependencies (aws sdk), and refactoring in a sake…
… of god.
- Loading branch information
Maya Sergeeva
committed
Sep 19, 2022
1 parent
2f8eeba
commit 3244f57
Showing
23 changed files
with
1,438 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spacetab-io/commands-go" | ||
"github.com/spacetab-io/configuration-go/stage" | ||
"github.com/spacetab-io/prerender-go/configuration" | ||
"github.com/spacetab-io/prerender-go/pkg/log" | ||
"github.com/spacetab-io/prerender-go/pkg/repository" | ||
"github.com/spacetab-io/prerender-go/pkg/service" | ||
"github.com/spacetab-io/prerender-go/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
rootCmd = &cobra.Command{ | ||
Use: "prerender", | ||
Short: "Prerender service", | ||
} | ||
runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run url parsing and prerend pages with storing html in storage", | ||
RunE: run, | ||
} | ||
) | ||
|
||
func Execute() { | ||
envStage := stage.NewEnvStage("development") | ||
|
||
config, err := configuration.Init(envStage, os.Getenv("CONFIG_PATH")) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("config init error") | ||
} | ||
|
||
if err := log.Init(&config.Log, envStage.String(), config.Info.GetAlias(), config.Info.GetVersion()); err != nil { | ||
log.Fatal().Err(err).Msg("logs init fail") | ||
} | ||
|
||
rootCmd.AddCommand(commands.VersionCmd, runCmd) | ||
|
||
log.Info().Msg(config.Info.Summary()) | ||
|
||
if err := rootCmd.ExecuteContext(setCtx(*config)); err != nil { | ||
os.Exit(commands.CmdFailureCode) | ||
} | ||
} | ||
|
||
func setCtx(config configuration.Config) context.Context { | ||
ctx := context.Background() | ||
ctx = context.WithValue(ctx, commands.CommandContextObjectKeyConfig, &config) | ||
|
||
return ctx | ||
} | ||
|
||
func getConfigs(ctx context.Context) ( | ||
*configuration.Config, | ||
error, | ||
) { | ||
cfg, ok := ctx.Value(commands.CommandContextObjectKeyConfig).(*configuration.Config) | ||
if !ok { | ||
return nil, fmt.Errorf("%w: config (%s)", commands.ErrBadContextValue, commands.CommandContextObjectKeyConfig) | ||
} | ||
|
||
return cfg, nil | ||
} | ||
|
||
func initCfgAndService(cmd *cobra.Command) (*configuration.Config, service.Service, error) { | ||
cfg, err := getConfigs(cmd.Context()) | ||
if err != nil { | ||
return nil, nil, utils.WrappedError("run", "getConfigs", err) | ||
} | ||
|
||
repo, err := repository.NewRepository(cfg.Storage) | ||
if err != nil { | ||
log.Error().Err(err).Msg("repo init error") | ||
|
||
return nil, nil, utils.WrappedError("initCfgAndService", "NewRepository", err) | ||
} | ||
|
||
return cfg, service.NewService(repo, cfg.Prerender, cfg.Storage), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"runtime" | ||
"time" | ||
|
||
"github.com/spacetab-io/prerender-go/configuration" | ||
"github.com/spacetab-io/prerender-go/pkg/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func run(cmd *cobra.Command, _ []string) error { | ||
cfg, srv, err := initCfgAndService(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
links, err := srv.GetLinksForRender() | ||
if err != nil { | ||
log.Error().Err(err).Msg("get links for render error") | ||
|
||
return err | ||
} | ||
|
||
timeStart := time.Now() | ||
|
||
log.Info().Int("links counts", len(links)). | ||
Str("lookup strategy", cfg.Prerender.Lookup.Type). | ||
Str("render wait strategy", cfg.Prerender.WaitFor). | ||
Msg("start rendering pages") | ||
pages, err := srv.PreparePages(links) | ||
if err != nil { | ||
log.Error().Err(err).Msg("prepare pages error") | ||
|
||
return err | ||
} | ||
|
||
maxWorkers := countMaxWorkers(cfg) | ||
|
||
if err := srv.RenderPages(pages, maxWorkers); err != nil { | ||
log.Error().Err(err).Msg("render pages error") | ||
|
||
return err | ||
} | ||
|
||
timeEnd := time.Now() | ||
|
||
srv.PrepareRenderReport(pages, timeEnd.Sub(timeStart), maxWorkers) | ||
|
||
return nil | ||
} | ||
|
||
func countMaxWorkers(cfg *configuration.Config) int { | ||
numprocs := runtime.GOMAXPROCS(runtime.NumCPU()) | ||
maxWorkers := 2 * numprocs // nolint:gomnd | ||
|
||
if cfg.Prerender.ConcurrentLimit == 0 { | ||
return numprocs | ||
} | ||
|
||
if cfg.Prerender.ConcurrentLimit > maxWorkers { | ||
return maxWorkers | ||
} | ||
|
||
return cfg.Prerender.ConcurrentLimit | ||
} |
Oops, something went wrong.