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.

79 lines
2.1 KiB

  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. // GetSchedule returns the schedule for the base config
  36. func (b *BaseConfig) GetSchedule() string {
  37. return b.Schedule
  38. }
  39. // IsEnabled returns the enabled status for the config
  40. func (b *BaseConfig) IsEnabled() bool {
  41. return b.Enabled
  42. }
  43. // DoRunAtStart returns whether the task should be run at the start
  44. func (b *BaseConfig) DoRunAtStart() bool {
  45. return b.RunAtStart
  46. }
  47. // DoNoticeOnSuccess returns whether a success notice should be posted
  48. func (b *BaseConfig) DoNoticeOnSuccess() bool {
  49. return !b.NoSuccessNotice
  50. }
  51. // FormatMessage returns a message for the task
  52. func (b *BaseConfig) FormatMessage(name, status string, doer *models.User, args ...interface{}) string {
  53. realArgs := make([]interface{}, 0, len(args)+2)
  54. realArgs = append(realArgs, i18n.Tr("en-US", "admin.dashboard."+name))
  55. if doer == nil {
  56. realArgs = append(realArgs, "(Cron)")
  57. } else {
  58. realArgs = append(realArgs, doer.Name)
  59. }
  60. if len(args) > 0 {
  61. realArgs = append(realArgs, args...)
  62. }
  63. if doer == nil || (doer.ID == -1 && doer.Name == "(Cron)") {
  64. return i18n.Tr("en-US", "admin.dashboard.cron."+status, realArgs...)
  65. }
  66. return i18n.Tr("en-US", "admin.dashboard.task."+status, realArgs...)
  67. }