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.

setting.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2020 The Gitea Authors. 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 cron
  5. import (
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "github.com/unknwon/i18n"
  9. )
  10. // Config represents a basic configuration interface that cron task
  11. type Config interface {
  12. IsEnabled() bool
  13. DoRunAtStart() bool
  14. GetSchedule() string
  15. FormatMessage(name, status string, doer *models.User, args ...interface{}) string
  16. DoNoticeOnSuccess() bool
  17. }
  18. // BaseConfig represents the basic config for a Cron task
  19. type BaseConfig struct {
  20. Enabled bool
  21. RunAtStart bool
  22. Schedule string
  23. NoSuccessNotice bool
  24. }
  25. // OlderThanConfig represents a cron task with OlderThan setting
  26. type OlderThanConfig struct {
  27. BaseConfig
  28. OlderThan time.Duration
  29. }
  30. // UpdateExistingConfig represents a cron task with UpdateExisting setting
  31. type UpdateExistingConfig struct {
  32. BaseConfig
  33. UpdateExisting bool
  34. }
  35. // CleanupHookTaskConfig represents a cron task with settings to cleanup hook_task
  36. type CleanupHookTaskConfig struct {
  37. BaseConfig
  38. CleanupType string
  39. OlderThan time.Duration
  40. NumberToKeep int
  41. }
  42. // GetSchedule returns the schedule for the base config
  43. func (b *BaseConfig) GetSchedule() string {
  44. return b.Schedule
  45. }
  46. // IsEnabled returns the enabled status for the config
  47. func (b *BaseConfig) IsEnabled() bool {
  48. return b.Enabled
  49. }
  50. // DoRunAtStart returns whether the task should be run at the start
  51. func (b *BaseConfig) DoRunAtStart() bool {
  52. return b.RunAtStart
  53. }
  54. // DoNoticeOnSuccess returns whether a success notice should be posted
  55. func (b *BaseConfig) DoNoticeOnSuccess() bool {
  56. return !b.NoSuccessNotice
  57. }
  58. // FormatMessage returns a message for the task
  59. func (b *BaseConfig) FormatMessage(name, status string, doer *models.User, args ...interface{}) string {
  60. realArgs := make([]interface{}, 0, len(args)+2)
  61. realArgs = append(realArgs, i18n.Tr("en-US", "admin.dashboard."+name))
  62. if doer == nil {
  63. realArgs = append(realArgs, "(Cron)")
  64. } else {
  65. realArgs = append(realArgs, doer.Name)
  66. }
  67. if len(args) > 0 {
  68. realArgs = append(realArgs, args...)
  69. }
  70. if doer == nil || (doer.ID == -1 && doer.Name == "(Cron)") {
  71. return i18n.Tr("en-US", "admin.dashboard.cron."+status, realArgs...)
  72. }
  73. return i18n.Tr("en-US", "admin.dashboard.task."+status, realArgs...)
  74. }