本站源代码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

67 rindas
1.5KB

  1. // Copyright 2019 Gitea. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package task
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/migrations/base"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/structs"
  12. )
  13. // taskQueue is a global queue of tasks
  14. var taskQueue Queue
  15. // Run a task
  16. func Run(t *models.Task) error {
  17. switch t.Type {
  18. case structs.TaskTypeMigrateRepo:
  19. return runMigrateTask(t)
  20. default:
  21. return fmt.Errorf("Unknow task type: %d", t.Type)
  22. }
  23. }
  24. // Init will start the service to get all unfinished tasks and run them
  25. func Init() error {
  26. switch setting.Task.QueueType {
  27. case setting.ChannelQueueType:
  28. taskQueue = NewChannelQueue(setting.Task.QueueLength)
  29. case setting.RedisQueueType:
  30. var err error
  31. addrs, pass, idx, err := parseConnStr(setting.Task.QueueConnStr)
  32. if err != nil {
  33. return err
  34. }
  35. taskQueue, err = NewRedisQueue(addrs, pass, idx)
  36. if err != nil {
  37. return err
  38. }
  39. default:
  40. return fmt.Errorf("Unsupported task queue type: %v", setting.Task.QueueType)
  41. }
  42. go func() {
  43. if err := taskQueue.Run(); err != nil {
  44. log.Error("taskQueue.Run end failed: %v", err)
  45. }
  46. }()
  47. return nil
  48. }
  49. // MigrateRepository add migration repository to task
  50. func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error {
  51. task, err := models.CreateMigrateTask(doer, u, opts)
  52. if err != nil {
  53. return err
  54. }
  55. return taskQueue.Push(task)
  56. }
上海开阖软件有限公司 沪ICP备12045867号-1