Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

schedule.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "fmt"
  7. "time"
  8. "code.gitea.io/gitea/models/db"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. webhook_module "code.gitea.io/gitea/modules/webhook"
  13. "github.com/robfig/cron/v3"
  14. )
  15. // ActionSchedule represents a schedule of a workflow file
  16. type ActionSchedule struct {
  17. ID int64
  18. Title string
  19. Specs []string
  20. RepoID int64 `xorm:"index"`
  21. Repo *repo_model.Repository `xorm:"-"`
  22. OwnerID int64 `xorm:"index"`
  23. WorkflowID string
  24. TriggerUserID int64
  25. TriggerUser *user_model.User `xorm:"-"`
  26. Ref string
  27. CommitSHA string
  28. Event webhook_module.HookEventType
  29. EventPayload string `xorm:"LONGTEXT"`
  30. Content []byte
  31. Created timeutil.TimeStamp `xorm:"created"`
  32. Updated timeutil.TimeStamp `xorm:"updated"`
  33. }
  34. func init() {
  35. db.RegisterModel(new(ActionSchedule))
  36. }
  37. // GetSchedulesMapByIDs returns the schedules by given id slice.
  38. func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
  39. schedules := make(map[int64]*ActionSchedule, len(ids))
  40. return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
  41. }
  42. // GetReposMapByIDs returns the repos by given id slice.
  43. func GetReposMapByIDs(ctx context.Context, ids []int64) (map[int64]*repo_model.Repository, error) {
  44. repos := make(map[int64]*repo_model.Repository, len(ids))
  45. return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
  46. }
  47. var cronParser = cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
  48. // CreateScheduleTask creates new schedule task.
  49. func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
  50. // Return early if there are no rows to insert
  51. if len(rows) == 0 {
  52. return nil
  53. }
  54. // Begin transaction
  55. ctx, committer, err := db.TxContext(ctx)
  56. if err != nil {
  57. return err
  58. }
  59. defer committer.Close()
  60. // Loop through each schedule row
  61. for _, row := range rows {
  62. // Create new schedule row
  63. if err = db.Insert(ctx, row); err != nil {
  64. return err
  65. }
  66. // Loop through each schedule spec and create a new spec row
  67. now := time.Now()
  68. for _, spec := range row.Specs {
  69. // Parse the spec and check for errors
  70. schedule, err := cronParser.Parse(spec)
  71. if err != nil {
  72. continue // skip to the next spec if there's an error
  73. }
  74. // Insert the new schedule spec row
  75. if err = db.Insert(ctx, &ActionScheduleSpec{
  76. RepoID: row.RepoID,
  77. ScheduleID: row.ID,
  78. Spec: spec,
  79. Next: timeutil.TimeStamp(schedule.Next(now).Unix()),
  80. }); err != nil {
  81. return err
  82. }
  83. }
  84. }
  85. // Commit transaction
  86. return committer.Commit()
  87. }
  88. func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error {
  89. ctx, committer, err := db.TxContext(ctx)
  90. if err != nil {
  91. return err
  92. }
  93. defer committer.Close()
  94. if _, err := db.GetEngine(ctx).Delete(&ActionSchedule{RepoID: id}); err != nil {
  95. return err
  96. }
  97. if _, err := db.GetEngine(ctx).Delete(&ActionScheduleSpec{RepoID: id}); err != nil {
  98. return err
  99. }
  100. return committer.Commit()
  101. }
  102. func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) error {
  103. // If actions disabled when there is schedule task, this will remove the outdated schedule tasks
  104. // There is no other place we can do this because the app.ini will be changed manually
  105. if err := DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
  106. return fmt.Errorf("DeleteCronTaskByRepo: %v", err)
  107. }
  108. // cancel running cron jobs of this repository and delete old schedules
  109. if err := CancelPreviousJobs(
  110. ctx,
  111. repo.ID,
  112. repo.DefaultBranch,
  113. "",
  114. webhook_module.HookEventSchedule,
  115. ); err != nil {
  116. return fmt.Errorf("CancelPreviousJobs: %v", err)
  117. }
  118. return nil
  119. }