Skip to content

Commit

Permalink
Restructured the repository. Added a few todos, some of the component…
Browse files Browse the repository at this point in the history
…s need to be refactored.
  • Loading branch information
xBlaz3kx committed Aug 4, 2024
1 parent d3122d8 commit 828f1e0
Show file tree
Hide file tree
Showing 41 changed files with 423 additions and 299 deletions.
13 changes: 6 additions & 7 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"context"
"errors"
"fmt"
"github.com/GLCharge/otelzap"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/GLCharge/otelzap"
"github.com/ardanlabs/conf/v3"
"github.com/xBlaz3kx/distributed-scheduler/foundation/database"
"github.com/xBlaz3kx/distributed-scheduler/foundation/logger"
"github.com/xBlaz3kx/distributed-scheduler/handlers"
api "github.com/xBlaz3kx/distributed-scheduler/internal/api/http"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/database"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/logger"
"go.uber.org/zap"
)

Expand All @@ -31,7 +31,6 @@ func main() {

if err := run(log); err != nil {
log.Error("startup error", zap.Error(err))
log.Sync()
os.Exit(1)
}
}
Expand Down Expand Up @@ -123,10 +122,10 @@ func run(log *otelzap.Logger) error {
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)

apiMux := handlers.APIMux(handlers.APIMuxConfig{
apiMux := api.Api(api.APIMuxConfig{
Log: log,
DB: db,
OpenApi: handlers.OpenApiConfig{
OpenApi: api.OpenApiConfig{
Enabled: cfg.OpenAPI.Enable,
Scheme: cfg.OpenAPI.Scheme,
Host: cfg.OpenAPI.Host,
Expand Down
15 changes: 15 additions & 0 deletions cmd/manager/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "github.com/spf13/cobra"

var rootCmd = &cobra.Command{
Use: "scheduler",
Short: "Scheduler manager",
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
panic(err)
}
}
63 changes: 28 additions & 35 deletions cmd/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"time"

"github.com/GLCharge/otelzap"
"github.com/xBlaz3kx/distributed-scheduler/handlers"

"github.com/ardanlabs/conf/v3"
"github.com/xBlaz3kx/distributed-scheduler/executor"
"github.com/xBlaz3kx/distributed-scheduler/foundation/database"
"github.com/xBlaz3kx/distributed-scheduler/foundation/logger"
"github.com/xBlaz3kx/distributed-scheduler/runner"
"github.com/xBlaz3kx/distributed-scheduler/service/job"
"github.com/xBlaz3kx/distributed-scheduler/store/postgres"
api "github.com/xBlaz3kx/distributed-scheduler/internal/api/http"
"github.com/xBlaz3kx/distributed-scheduler/internal/executor"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/database"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/logger"
"github.com/xBlaz3kx/distributed-scheduler/internal/runner"
"github.com/xBlaz3kx/distributed-scheduler/internal/service/job"
"github.com/xBlaz3kx/distributed-scheduler/internal/store/postgres"
"go.uber.org/zap"
)

Expand All @@ -43,34 +42,28 @@ func main() {
}
}

type Configuration struct {
conf.Version
Web struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:8000"`
}
DB database.Config
ID string `conf:"default:instance1"`
Interval time.Duration `conf:"default:10s"`
MaxConcurrentJobs int `conf:"default:100"`
MaxJobLockTime time.Duration `conf:"default:1m"`
}

func run(log *otelzap.Logger) error {

// -------------------------------------------------------------------------
// Configuration

cfg := struct {
conf.Version
Web struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:10s"`
IdleTimeout time.Duration `conf:"default:120s"`
ShutdownTimeout time.Duration `conf:"default:20s"`
APIHost string `conf:"default:0.0.0.0:8000"`
}
DB struct {
User string `conf:"default:scheduler"`
Password string `conf:"default:scheduler,mask"`
Host string `conf:"default:localhost:5436"`
Name string `conf:"default:scheduler"`
MaxIdleConns int `conf:"default:3"`
MaxOpenConns int `conf:"default:2"`
DisableTLS bool `conf:"default:true"`
}
ID string `conf:"default:instance1"`
Interval time.Duration `conf:"default:10s"`
MaxConcurrentJobs int `conf:"default:100"`
MaxJobLockTime time.Duration `conf:"default:1m"`
}{
cfg := Configuration{
Version: conf.Version{
Build: build,
Desc: "copyright information here",
Expand Down Expand Up @@ -135,7 +128,7 @@ func run(log *otelzap.Logger) error {

executorFactory := executor.NewFactory(&http.Client{Timeout: 30 * time.Second})

runnner := runner.New(runner.Config{
runner := runner.New(runner.Config{
JobService: jobService,
Log: log,
ExecutorFactory: executorFactory,
Expand All @@ -145,11 +138,11 @@ func run(log *otelzap.Logger) error {
JobLockDuration: cfg.MaxJobLockTime,
})

runnner.Start()
runner.Start()

//
// API
apiMux := handlers.RunnerAPI(handlers.APIMuxConfig{
apiMux := api.RunnerAPI(api.APIMuxConfig{
Log: log,
DB: db,
})
Expand Down Expand Up @@ -183,7 +176,7 @@ func run(log *otelzap.Logger) error {
defer cancel()

// stop the runner
runnner.Stop(ctx)
runner.Stop(ctx)
}

return nil
Expand Down
16 changes: 9 additions & 7 deletions cmd/tooling/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/xBlaz3kx/distributed-scheduler/foundation/database"
"github.com/xBlaz3kx/distributed-scheduler/foundation/database/dbmigrate"
"time"

"github.com/GLCharge/otelzap"
"github.com/spf13/cobra"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/database"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/database/dbmigrate"
)

var migrateCmd = &cobra.Command{
Expand All @@ -29,9 +30,10 @@ func init() {
}

func migrateRun(cmd *cobra.Command, args []string) {
logger := otelzap.L().Sugar()
db, err := database.Open(dbConfig)
if err != nil {
fmt.Printf("open database: %v", err)
logger.Fatalf("unable to create database connection: %v", err)
return
}
defer db.Close()
Expand All @@ -40,9 +42,9 @@ func migrateRun(cmd *cobra.Command, args []string) {
defer cancel()

if err := dbmigrate.Migrate(ctx, db); err != nil {
fmt.Printf("migrate database: %v", err)
logger.Fatalf("unable to migrate the database: %v", err)
return
}

fmt.Println("migrations complete")
logger.Info("Database migrations complete!")
}
15 changes: 14 additions & 1 deletion cmd/tooling/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package cmd

import (
"github.com/spf13/cobra"
"os"

"github.com/GLCharge/otelzap"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/xBlaz3kx/distributed-scheduler/internal/pkg/logger"
)

var rootCmd = &cobra.Command{
Use: "scheduler",
Short: "CLI tool for managing the scheduler.",
}

func setupLogging() {
logLevel := viper.GetString("log.level")
logger, err := logger.New(logLevel)
if err == nil {
otelzap.ReplaceGlobals(logger)
}
}

func Execute() {
cobra.OnInitialize(setupLogging)
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
27 changes: 20 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/xBlaz3kx/distributed-scheduler

go 1.20
go 1.22.1

replace github.com/GLCharge/otelzap v0.0.0-20230904131944-57dc7c9994a9 => github.com/xBlaz3kx/otelzap v0.0.0-20230904131944-57dc7c9994a9

Expand All @@ -13,45 +13,58 @@ require (
github.com/lib/pq v1.10.9
github.com/samber/lo v1.46.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.19.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/vearne/gin-timeout v0.2.0
)

require (
github.com/DATA-DOG/go-sqlmock v1.5.2 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.8 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.2.0 // indirect
go.opentelemetry.io/otel v1.15.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

require (
github.com/ardanlabs/conf/v3 v3.1.5
github.com/bytedance/sonic v1.11.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.10.0
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.3.0
github.com/google/uuid v1.4.0
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1
Expand All @@ -64,7 +77,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rabbitmq/amqp091-go v1.8.1
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.9.0
Expand Down
Loading

0 comments on commit 828f1e0

Please sign in to comment.