This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/cmd/twhelp/internal/job/job.go
Dawid Wysokiński 9c1b580adb
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
refactor: adapters - rename structs/files
2023-07-22 08:22:28 +02:00

170 lines
4.2 KiB
Go

package job
import (
"context"
"fmt"
"time"
"gitea.dwysokinski.me/twhelp/core/cmd/twhelp/internal"
"gitea.dwysokinski.me/twhelp/core/internal/adapter"
"gitea.dwysokinski.me/twhelp/core/internal/service"
"github.com/garsue/watermillzap"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
)
const (
jobDefaultTimeout = 30 * time.Second
)
func New() *cli.Command {
return &cli.Command{
Name: "job",
Usage: "Runs various jobs",
Subcommands: []*cli.Command{
newCreateCommand(),
newUpdateCommand(),
newCleanUpCommand(),
},
}
}
func newCreateCommand() *cli.Command {
return &cli.Command{
Name: "create",
Subcommands: []*cli.Command{
newCreateSnapshotsCommand(),
},
}
}
func newCreateSnapshotsCommand() *cli.Command {
return &cli.Command{
Name: "snapshots",
Usage: "Launches snapshot update",
Action: func(c *cli.Context) error {
return runJob(
c,
jobDefaultTimeout,
func(ctx context.Context, job *service.Job) error {
if err := job.CreateSnapshots(ctx); err != nil {
return fmt.Errorf("Job.CreateSnapshots: %w", err)
}
return nil
},
)
},
}
}
func newUpdateCommand() *cli.Command {
return &cli.Command{
Name: "update",
Subcommands: []*cli.Command{
newUpdateDataCommand(),
newUpdateEnnoblementsCommand(),
},
}
}
func newUpdateDataCommand() *cli.Command {
return &cli.Command{
Name: "data",
Usage: "Launches data update (servers, players, tribes, villages)",
Action: func(c *cli.Context) error {
return runJob(
c,
jobDefaultTimeout,
func(ctx context.Context, job *service.Job) error {
if err := job.UpdateData(ctx); err != nil {
return fmt.Errorf("Job.UpdateData: %w", err)
}
return nil
},
)
},
}
}
func newUpdateEnnoblementsCommand() *cli.Command {
return &cli.Command{
Name: "ennoblements",
Usage: "Launches ennoblement update",
Action: func(c *cli.Context) error {
return runJob(
c,
jobDefaultTimeout,
func(ctx context.Context, job *service.Job) error {
if err := job.UpdateEnnoblements(ctx); err != nil {
return fmt.Errorf("Job.UpdateEnnoblements: %w", err)
}
return nil
},
)
},
}
}
func newCleanUpCommand() *cli.Command {
return &cli.Command{
Name: "cleanup",
Usage: "Launches database cleanup",
Action: func(c *cli.Context) error {
return runJob(
c,
time.Hour,
func(ctx context.Context, job *service.Job) error {
if err := job.CleanUp(ctx); err != nil {
return fmt.Errorf("Job.CleanUp: %w", err)
}
return nil
},
)
},
}
}
type runJobFunc func(ctx context.Context, job *service.Job) error
func runJob(c *cli.Context, timeout time.Duration, f runJobFunc) error {
db, err := internal.NewBunDB()
if err != nil {
return fmt.Errorf("internal.NewBunDB: %w", err)
}
defer func() {
_ = db.Close()
}()
publisher, err := internal.NewAMQPPublisher(watermillzap.NewLogger(zap.L()))
if err != nil {
return fmt.Errorf("internal.NewAMQPPublisher: %w", err)
}
defer func() {
_ = publisher.Close()
}()
marshaler := internal.NewWatermillPublisherMarshaler()
ctx, cancel := context.WithTimeout(c.Context, timeout)
defer cancel()
client := internal.NewTWClient(c.App.Version)
versionSvc := service.NewVersion(adapter.NewVersionBunRepository(db))
serverSvc := service.NewServer(adapter.NewServerBunRepository(db), client)
tribeChangeSvc := service.NewTribeChange(adapter.NewTribeChangeBunRepository(db))
tribeSvc := service.NewTribe(adapter.NewTribeBunRepository(db), client)
tribeSnapshotSvc := service.NewTribeSnapshot(adapter.NewTribeSnapshotBunRepository(db), tribeSvc)
playerSvc := service.NewPlayer(adapter.NewPlayerBunRepository(db), tribeChangeSvc, client)
playerSnapshotSvc := service.NewPlayerSnapshot(adapter.NewPlayerSnapshotBunRepository(db), playerSvc)
ennoblementSvc := service.NewEnnoblement(adapter.NewEnnoblementBunRepository(db), client)
return f(ctx, service.NewJob(
versionSvc,
serverSvc,
adapter.NewServerWatermillPublisher(publisher, marshaler),
adapter.NewEnnoblementWatermillPublisher(publisher, marshaler),
adapter.NewSnapshotWatermillPublisher(publisher, marshaler),
[]service.CleanUper{playerSnapshotSvc, tribeSnapshotSvc, ennoblementSvc},
))
}