refactor: add cronservice
This commit is contained in:
parent
8f76f0c55e
commit
f077ffd7a7
10 changed files with 192 additions and 93 deletions
|
@ -1,11 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/router"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -14,9 +10,7 @@ func main() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
// Конфигурация сервера
|
||||
router := router.New(app).Setup()
|
||||
app.Init()
|
||||
|
||||
log.Printf("Сервер запущен на порту: %s", app.Config.Port)
|
||||
http.ListenAndServe(":"+app.Config.Port, router)
|
||||
app.Run()
|
||||
}
|
||||
|
|
|
@ -2,35 +2,98 @@ package app
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/router"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/cronservice"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/reposervice"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Db *gorm.DB
|
||||
Config *config.Config
|
||||
db *gorm.DB
|
||||
config *config.Config
|
||||
|
||||
repo *reposervice.Service
|
||||
taskService *taskservice.Service
|
||||
cron *cronservice.Service
|
||||
|
||||
taskController *taskcontroller.TaskController
|
||||
router *router.Router
|
||||
}
|
||||
|
||||
func createDBConnection(cfg *config.Config) (*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPassword, cfg.DBName)
|
||||
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
}
|
||||
|
||||
func New() (*App, error) {
|
||||
cfg := config.New()
|
||||
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPassword, cfg.DBName)
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
db, err := createDBConnection(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
|
||||
|
||||
db.FirstOrCreate(&models.ALTRepo{
|
||||
Name: "Sisyphus",
|
||||
})
|
||||
|
||||
return &App{
|
||||
Config: cfg,
|
||||
Db: db,
|
||||
}, nil
|
||||
app := App{
|
||||
config: cfg,
|
||||
db: db,
|
||||
}
|
||||
|
||||
app.createServices()
|
||||
app.createControllers()
|
||||
app.createRouter()
|
||||
|
||||
return &app, nil
|
||||
}
|
||||
|
||||
func (app *App) createServices() {
|
||||
app.repo = reposervice.New(
|
||||
app.db,
|
||||
app.config,
|
||||
)
|
||||
app.taskService = taskservice.New(
|
||||
app.db,
|
||||
app.config,
|
||||
)
|
||||
app.cron = cronservice.New(
|
||||
app.repo,
|
||||
)
|
||||
}
|
||||
|
||||
func (app *App) createControllers() {
|
||||
app.taskController = taskcontroller.New(
|
||||
app.taskService,
|
||||
)
|
||||
}
|
||||
|
||||
func (app *App) createRouter() {
|
||||
app.router = router.New(
|
||||
app.config,
|
||||
app.taskController,
|
||||
)
|
||||
}
|
||||
|
||||
func (app *App) initServices() {
|
||||
app.cron.SetupCronJobs()
|
||||
app.cron.Start()
|
||||
}
|
||||
|
||||
func (app *App) Init() {
|
||||
app.initServices()
|
||||
}
|
||||
|
||||
func (app *App) Run() {
|
||||
log.Printf("Сервер запущен на порту: %s", app.config.Port)
|
||||
http.ListenAndServe(":"+app.config.Port, app.router.Setup())
|
||||
}
|
||||
|
|
|
@ -28,3 +28,7 @@ func New() *Config {
|
|||
|
||||
return config
|
||||
}
|
||||
|
||||
func (c *Config) GetUploadDir() string {
|
||||
return c.UploadDir
|
||||
}
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
package taskcontroller
|
||||
|
||||
import (
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
|
||||
)
|
||||
|
||||
type TaskController struct {
|
||||
app *app.App
|
||||
taskService *taskservice.Service
|
||||
}
|
||||
|
||||
func New(app *app.App, taskService *taskservice.Service) *TaskController {
|
||||
func New(taskService *taskservice.Service) *TaskController {
|
||||
return &TaskController{
|
||||
app: app,
|
||||
taskService: taskService,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,25 +3,27 @@ package router
|
|||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
httpSwagger "github.com/swaggo/http-swagger"
|
||||
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/middlewares"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/reposervice"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
|
||||
|
||||
_ "code.alt-gnome.ru/aides-infra/aides-repo-api/docs"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
app *app.App
|
||||
config *config.Config
|
||||
taskController *taskcontroller.TaskController
|
||||
}
|
||||
|
||||
func New(app *app.App) *Router {
|
||||
func New(
|
||||
config *config.Config,
|
||||
taskController *taskcontroller.TaskController,
|
||||
) *Router {
|
||||
return &Router{
|
||||
app: app,
|
||||
config: config,
|
||||
taskController: taskController,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,41 +31,14 @@ func (r *Router) Setup() *chi.Mux {
|
|||
router := chi.NewRouter()
|
||||
router.Use(middleware.Logger)
|
||||
|
||||
taskService := taskservice.New(
|
||||
r.app,
|
||||
)
|
||||
|
||||
repoService := reposervice.New(r.app)
|
||||
repoService.ForceUpdate()
|
||||
|
||||
s, _ := gocron.NewScheduler()
|
||||
defer func() { _ = s.Shutdown() }()
|
||||
|
||||
_, _ = s.NewJob(
|
||||
gocron.CronJob(
|
||||
"0 4 * * *",
|
||||
false,
|
||||
),
|
||||
gocron.NewTask(
|
||||
func() {
|
||||
repoService.ForceUpdate()
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
taskController := taskcontroller.New(
|
||||
r.app,
|
||||
taskService,
|
||||
)
|
||||
|
||||
authGuard := middlewares.CreateAuthGuard(r.app.Config)
|
||||
authGuard := middlewares.CreateAuthGuard(r.config)
|
||||
|
||||
router.Get("/swagger/*", httpSwagger.WrapHandler)
|
||||
|
||||
router.Route("/tasks", func(taskRouter chi.Router) {
|
||||
taskRouter.With(authGuard).Post("/", taskController.Create)
|
||||
taskRouter.With(authGuard).Post("/", r.taskController.Create)
|
||||
taskRouter.Route("/{taskID}", func(sTaskRouter chi.Router) {
|
||||
sTaskRouter.With(authGuard).Post("/upload", taskController.Upload)
|
||||
sTaskRouter.With(authGuard).Post("/upload", r.taskController.Upload)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
55
internal/services/cronservice/service.go
Normal file
55
internal/services/cronservice/service.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package cronservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
)
|
||||
|
||||
type RepoService interface {
|
||||
ForceUpdate()
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repoService RepoService
|
||||
|
||||
scheduler gocron.Scheduler
|
||||
}
|
||||
|
||||
func New(repo RepoService) *Service {
|
||||
scheduler, err := gocron.NewScheduler(gocron.WithLocation(time.UTC))
|
||||
if err != nil {
|
||||
log.Fatalf("Не удалось создать планировщик: %v", err)
|
||||
}
|
||||
|
||||
return &Service{
|
||||
scheduler: scheduler,
|
||||
repoService: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) SetupCronJobs() {
|
||||
_, err := s.scheduler.NewJob(
|
||||
gocron.CronJob("* * * * *", false),
|
||||
gocron.NewTask(func() {
|
||||
fmt.Println("Cron run!")
|
||||
s.repoService.ForceUpdate()
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Не удалось создать задание cron: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Start() {
|
||||
s.scheduler.Start()
|
||||
}
|
||||
|
||||
func (s *Service) Shutdown() {
|
||||
err := s.scheduler.Shutdown()
|
||||
if err != nil {
|
||||
log.Printf("Не удалось корректно завершить работу планировщика: %v", err)
|
||||
}
|
||||
}
|
|
@ -7,18 +7,23 @@ import (
|
|||
"path"
|
||||
"strconv"
|
||||
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
app *app.App
|
||||
type Config interface {
|
||||
GetUploadDir() string
|
||||
}
|
||||
|
||||
func New(app *app.App) *Service {
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
config Config
|
||||
}
|
||||
|
||||
func New(db *gorm.DB, cfg Config) *Service {
|
||||
return &Service{
|
||||
app: app,
|
||||
db: db,
|
||||
config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,11 +77,11 @@ func (s *Service) ForceUpdate() {
|
|||
Name: "Sisyphus",
|
||||
}
|
||||
|
||||
s.app.Db.
|
||||
s.db.
|
||||
Where(&altRepo).
|
||||
First(&altRepo)
|
||||
|
||||
s.app.Db.Debug().
|
||||
s.db.Debug().
|
||||
Model(&models.GitRepoAltRepoTask{}).
|
||||
Select("tasks.*").
|
||||
Joins("JOIN tasks ON tasks.id = git_repo_alt_repo_tasks.last_task_id").
|
||||
|
@ -86,7 +91,7 @@ func (s *Service) ForceUpdate() {
|
|||
Preload("Files").
|
||||
Find(&tasks)
|
||||
|
||||
repoPath := path.Join(s.app.Config.UploadDir, "future_repo", "Sisyphus")
|
||||
repoPath := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus")
|
||||
|
||||
os.MkdirAll(
|
||||
repoPath,
|
||||
|
@ -102,14 +107,14 @@ func (s *Service) ForceUpdate() {
|
|||
|
||||
for _, el := range tasks {
|
||||
taskPath := path.Join(
|
||||
s.app.Config.UploadDir, "tasks", strconv.FormatUint(uint64(el.ID), 10),
|
||||
s.config.GetUploadDir(), "tasks", strconv.FormatUint(uint64(el.ID), 10),
|
||||
)
|
||||
|
||||
for _, fileInfo := range el.Files {
|
||||
localFilePath := path.Join(
|
||||
strconv.FormatUint(uint64(el.ID), 10), fileInfo.Name,
|
||||
)
|
||||
symLink := path.Join(s.app.Config.UploadDir, "future_repo", "Sisyphus", fileInfo.Arch, "RPMS.aides", fileInfo.Name)
|
||||
symLink := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus", fileInfo.Arch, "RPMS.aides", fileInfo.Name)
|
||||
targetPath := path.Join("../../../../tasks/", localFilePath)
|
||||
createSymlink(targetPath, symLink)
|
||||
|
||||
|
@ -121,7 +126,7 @@ func (s *Service) ForceUpdate() {
|
|||
runGenbasedir(repoPath, arch, repoName)
|
||||
}
|
||||
|
||||
s.app.Db.Debug().
|
||||
s.db.Debug().
|
||||
Model(&models.GitRepoAltRepoTask{}).
|
||||
Where(&models.GitRepoAltRepoTask{
|
||||
ALTRepoID: altRepo.ID,
|
||||
|
@ -130,11 +135,11 @@ func (s *Service) ForceUpdate() {
|
|||
"current_task_id", gorm.Expr("last_task_id"),
|
||||
)
|
||||
|
||||
os.MkdirAll(path.Join(s.app.Config.UploadDir, "repo"), os.ModePerm)
|
||||
os.MkdirAll(path.Join(s.config.GetUploadDir(), "repo"), os.ModePerm)
|
||||
|
||||
aPath := path.Join(s.app.Config.UploadDir, "future_repo", "Sisyphus")
|
||||
bPath := path.Join(s.app.Config.UploadDir, "repo", "Sisyphus")
|
||||
cPath := path.Join(s.app.Config.UploadDir, "repo", ".Sisyphus")
|
||||
aPath := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus")
|
||||
bPath := path.Join(s.config.GetUploadDir(), "repo", "Sisyphus")
|
||||
cPath := path.Join(s.config.GetUploadDir(), "repo", ".Sisyphus")
|
||||
|
||||
if _, err := os.Stat(bPath); err == nil {
|
||||
fmt.Printf("Moving %s to %s\n", bPath, cPath)
|
||||
|
@ -152,5 +157,5 @@ func (s *Service) ForceUpdate() {
|
|||
|
||||
}
|
||||
|
||||
os.RemoveAll(path.Join(s.app.Config.UploadDir, "future_repo"))
|
||||
os.RemoveAll(path.Join(s.config.GetUploadDir(), "future_repo"))
|
||||
}
|
||||
|
|
|
@ -8,14 +8,14 @@ func (s *Service) Create(repo string) (*models.Task, error) {
|
|||
taskRepo := models.GitRepo{
|
||||
Name: repo,
|
||||
}
|
||||
s.app.Db.Debug().
|
||||
s.db.Debug().
|
||||
Where(&taskRepo).
|
||||
FirstOrCreate(&taskRepo)
|
||||
|
||||
altRepo := models.ALTRepo{
|
||||
Name: "Sisyphus",
|
||||
}
|
||||
s.app.Db.
|
||||
s.db.
|
||||
Where(&altRepo).
|
||||
FirstOrCreate(&altRepo)
|
||||
|
||||
|
@ -25,7 +25,7 @@ func (s *Service) Create(repo string) (*models.Task, error) {
|
|||
Type: models.TypeUpsert,
|
||||
}
|
||||
|
||||
result := s.app.Db.Create(&task)
|
||||
result := s.db.Create(&task)
|
||||
|
||||
return &task, result.Error
|
||||
}
|
||||
|
|
|
@ -5,22 +5,28 @@ import (
|
|||
"path"
|
||||
"strconv"
|
||||
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
|
||||
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
app *app.App
|
||||
type Config interface {
|
||||
GetUploadDir() string
|
||||
}
|
||||
|
||||
func New(app *app.App) *Service {
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
config Config
|
||||
}
|
||||
|
||||
func New(db *gorm.DB, config Config) *Service {
|
||||
return &Service{
|
||||
app: app,
|
||||
db: db,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) onTaskComplete(task *models.Task) error {
|
||||
if err := s.app.Db.Save(&task).Error; err != nil {
|
||||
if err := s.db.Save(&task).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -28,13 +34,13 @@ func (s *Service) onTaskComplete(task *models.Task) error {
|
|||
ALTRepoID: 1,
|
||||
RepoID: task.RepoID,
|
||||
}
|
||||
s.app.Db.Debug().
|
||||
s.db.Debug().
|
||||
Where(&grart).
|
||||
FirstOrCreate(&grart)
|
||||
|
||||
grart.LastTaskID = &task.ID
|
||||
|
||||
if err := s.app.Db.Save(&grart).Error; err != nil {
|
||||
if err := s.db.Save(&grart).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -53,7 +59,7 @@ func (s *Service) tasksCleanup(r *models.GitRepoAltRepoTask, N int) {
|
|||
}
|
||||
|
||||
var lastNTaskIDs []uint
|
||||
s.app.Db.
|
||||
s.db.
|
||||
Debug().
|
||||
Table("tasks").
|
||||
Select("id").
|
||||
|
@ -67,7 +73,7 @@ func (s *Service) tasksCleanup(r *models.GitRepoAltRepoTask, N int) {
|
|||
excludedTaskIDs = append(excludedTaskIDs, lastNTaskIDs...)
|
||||
|
||||
var taskIDsToDelete []int
|
||||
s.app.Db.
|
||||
s.db.
|
||||
Debug().
|
||||
Model(&models.Task{}).
|
||||
Select("id").
|
||||
|
@ -77,7 +83,7 @@ func (s *Service) tasksCleanup(r *models.GitRepoAltRepoTask, N int) {
|
|||
Pluck("id", &taskIDsToDelete)
|
||||
|
||||
if len(taskIDsToDelete) > 0 {
|
||||
s.app.Db.
|
||||
s.db.
|
||||
Debug().
|
||||
Model(&models.Task{}).
|
||||
Where("id IN ?", taskIDsToDelete).
|
||||
|
@ -91,7 +97,7 @@ func (s *Service) tasksCleanup(r *models.GitRepoAltRepoTask, N int) {
|
|||
|
||||
func (s *Service) removeTaskFiles(taskId int) {
|
||||
taskFolderPath := path.Join(
|
||||
s.app.Config.UploadDir,
|
||||
s.config.GetUploadDir(),
|
||||
"tasks",
|
||||
strconv.Itoa(taskId),
|
||||
)
|
||||
|
|
|
@ -41,7 +41,7 @@ func (s *Service) Upload(input *TaskUploadInput) error {
|
|||
files := input.Files
|
||||
|
||||
task := models.Task{}
|
||||
result := s.app.Db.Preload("Repo").Where(
|
||||
result := s.db.Preload("Repo").Where(
|
||||
"id = ?", taskID,
|
||||
).First(&task)
|
||||
/*.Where(
|
||||
|
@ -56,7 +56,7 @@ func (s *Service) Upload(input *TaskUploadInput) error {
|
|||
}
|
||||
|
||||
localPath := path.Join(input.TaskID)
|
||||
taskFolderPath := path.Join(s.app.Config.UploadDir, "tasks", localPath)
|
||||
taskFolderPath := path.Join(s.config.GetUploadDir(), "tasks", localPath)
|
||||
os.MkdirAll(taskFolderPath, os.ModePerm)
|
||||
|
||||
for _, fileHeader := range files {
|
||||
|
@ -103,7 +103,7 @@ func (s *Service) Upload(input *TaskUploadInput) error {
|
|||
Task: task,
|
||||
}
|
||||
|
||||
if err := s.app.Db.Save(&f).Error; err != nil {
|
||||
if err := s.db.Save(&f).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue