Skip to content

Commit

Permalink
Updated go version, dependencies (aws sdk), and refactoring in a sake…
Browse files Browse the repository at this point in the history
… of god.
  • Loading branch information
Maya Sergeeva committed Sep 19, 2022
1 parent 2f8eeba commit 3244f57
Show file tree
Hide file tree
Showing 23 changed files with 1,438 additions and 328 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ COPY --from=build-env /app/bin/* /app/bin/
COPY --from=build-env /app/Makefile /app/
COPY --from=build-env /app/configuration/defaults /app/configuration/defaults

CMD ["/app/bin/prerender"]
CMD ["/app/bin/prerender run"]
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
IMAGE_NAME = spacetabio/prerender-go
IMAGE_VERSION = 0.3.0
IMAGE_VERSION = 0.3.1

deps:
go mod vendor

build:
go build -o ./bin/prerender ./cmd/prerender/main.go
go build -o ./bin/prerender .
.PHONY: build

build_vendor:
go build -mod=vendor -o ./bin/prerender ./cmd/prerender/main.go
go build -mod=vendor -o ./bin/prerender .
.PHONY: build_vendor

build_for_docker:
GOOS=linux GOARCH=amd64 go build -o ./bin/prerender ./cmd/prerender/main.go
GOOS=linux GOARCH=amd64 go build -o ./bin/prerender .
.PHONY: build_for_docker

build_vendor_for_docker:
GOOS=linux GOARCH=amd64 go build -mod=vendor -o ./bin/prerender ./cmd/prerender/main.go
GOOS=linux GOARCH=amd64 go build -mod=vendor -o ./bin/prerender .
.PHONY: build_for_docker

run:
Expand Down
84 changes: 84 additions & 0 deletions cmd/main.go
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
}
65 changes: 0 additions & 65 deletions cmd/prerender/main.go

This file was deleted.

66 changes: 66 additions & 0 deletions cmd/run.go
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
}
Loading

0 comments on commit 3244f57

Please sign in to comment.