mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.3 KiB
131 lines
3.3 KiB
5 years ago
|
// Copyright 2019 Gitea. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package task
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/graceful"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/json"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
3 years ago
|
base "code.gitea.io/gitea/modules/migration"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/queue"
|
||
5 years ago
|
repo_module "code.gitea.io/gitea/modules/repository"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/secret"
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/structs"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
5 years ago
|
)
|
||
|
|
||
|
// taskQueue is a global queue of tasks
|
||
5 years ago
|
var taskQueue queue.Queue
|
||
5 years ago
|
|
||
|
// Run a task
|
||
|
func Run(t *models.Task) error {
|
||
|
switch t.Type {
|
||
|
case structs.TaskTypeMigrateRepo:
|
||
|
return runMigrateTask(t)
|
||
|
default:
|
||
5 years ago
|
return fmt.Errorf("Unknown task type: %d", t.Type)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Init will start the service to get all unfinished tasks and run them
|
||
|
func Init() error {
|
||
5 years ago
|
taskQueue = queue.CreateQueue("task", handle, &models.Task{})
|
||
|
|
||
|
if taskQueue == nil {
|
||
|
return fmt.Errorf("Unable to create Task Queue")
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
go graceful.GetManager().RunWithShutdownFns(taskQueue.Run)
|
||
5 years ago
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func handle(data ...queue.Data) {
|
||
|
for _, datum := range data {
|
||
|
task := datum.(*models.Task)
|
||
|
if err := Run(task); err != nil {
|
||
|
log.Error("Run task failed: %v", err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// MigrateRepository add migration repository to task
|
||
|
func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error {
|
||
5 years ago
|
task, err := CreateMigrateTask(doer, u, opts)
|
||
5 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return taskQueue.Push(task)
|
||
|
}
|
||
5 years ago
|
|
||
|
// CreateMigrateTask creates a migrate task
|
||
|
func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.Task, error) {
|
||
4 years ago
|
// encrypt credentials for persistence
|
||
|
var err error
|
||
|
opts.CloneAddrEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.CloneAddr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
opts.CloneAddr = util.NewStringURLSanitizer(opts.CloneAddr, true).Replace(opts.CloneAddr)
|
||
4 years ago
|
opts.AuthPasswordEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthPassword)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
opts.AuthPassword = ""
|
||
|
opts.AuthTokenEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthToken)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
opts.AuthToken = ""
|
||
5 years ago
|
bs, err := json.Marshal(&opts)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
var task = &models.Task{
|
||
5 years ago
|
DoerID: doer.ID,
|
||
|
OwnerID: u.ID,
|
||
|
Type: structs.TaskTypeMigrateRepo,
|
||
|
Status: structs.TaskStatusQueue,
|
||
|
PayloadContent: string(bs),
|
||
|
}
|
||
|
|
||
3 years ago
|
if err := models.CreateTask(task); err != nil {
|
||
5 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
|
repo, err := repo_module.CreateRepository(doer, u, models.CreateRepoOptions{
|
||
|
Name: opts.RepoName,
|
||
|
Description: opts.Description,
|
||
|
OriginalURL: opts.OriginalURL,
|
||
|
GitServiceType: opts.GitServiceType,
|
||
|
IsPrivate: opts.Private,
|
||
|
IsMirror: opts.Mirror,
|
||
|
Status: models.RepositoryBeingMigrated,
|
||
|
})
|
||
|
if err != nil {
|
||
|
task.EndTime = timeutil.TimeStampNow()
|
||
|
task.Status = structs.TaskStatusFailed
|
||
|
err2 := task.UpdateCols("end_time", "status")
|
||
|
if err2 != nil {
|
||
|
log.Error("UpdateCols Failed: %v", err2.Error())
|
||
|
}
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
task.RepoID = repo.ID
|
||
|
if err = task.UpdateCols("repo_id"); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
return task, nil
|
||
5 years ago
|
}
|