gitea/modules/task/task.go

63 lines
1.4 KiB
Go
Raw Normal View History

2019-10-13 15:23:14 +02:00
// 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"
"code.gitea.io/gitea/modules/graceful"
2019-10-13 15:23:14 +02:00
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/queue"
2019-10-13 15:23:14 +02:00
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
)
// taskQueue is a global queue of tasks
var taskQueue queue.Queue
2019-10-13 15:23:14 +02:00
// Run a task
func Run(t *models.Task) error {
switch t.Type {
case structs.TaskTypeMigrateRepo:
return runMigrateTask(t)
default:
return fmt.Errorf("Unknown task type: %d", t.Type)
2019-10-13 15:23:14 +02:00
}
}
// Init will start the service to get all unfinished tasks and run them
func Init() error {
taskQueue = setting.CreateQueue("task", handle, &models.Task{})
if taskQueue == nil {
return fmt.Errorf("Unable to create Task Queue")
2019-10-13 15:23:14 +02:00
}
go graceful.GetManager().RunWithShutdownFns(taskQueue.Run)
2019-10-13 15:23:14 +02:00
return nil
}
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)
}
}
}
2019-10-13 15:23:14 +02:00
// MigrateRepository add migration repository to task
func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error {
task, err := models.CreateMigrateTask(doer, u, opts)
if err != nil {
return err
}
return taskQueue.Push(task)
}