summaryrefslogtreecommitdiffstats
path: root/modules/task/task.go
blob: 64744afe7a4c7d6c57d0fdb5c85e519b0aa0dcb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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/log"
	"code.gitea.io/gitea/modules/migrations/base"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/structs"
)

// taskQueue is a global queue of tasks
var taskQueue Queue

// Run a task
func Run(t *models.Task) error {
	switch t.Type {
	case structs.TaskTypeMigrateRepo:
		return runMigrateTask(t)
	default:
		return fmt.Errorf("Unknow task type: %d", t.Type)
	}
}

// Init will start the service to get all unfinished tasks and run them
func Init() error {
	switch setting.Task.QueueType {
	case setting.ChannelQueueType:
		taskQueue = NewChannelQueue(setting.Task.QueueLength)
	case setting.RedisQueueType:
		var err error
		addrs, pass, idx, err := parseConnStr(setting.Task.QueueConnStr)
		if err != nil {
			return err
		}
		taskQueue, err = NewRedisQueue(addrs, pass, idx)
		if err != nil {
			return err
		}
	default:
		return fmt.Errorf("Unsupported task queue type: %v", setting.Task.QueueType)
	}

	go func() {
		if err := taskQueue.Run(); err != nil {
			log.Error("taskQueue.Run end failed: %v", err)
		}
	}()

	return nil
}

// 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)
}