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