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 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }
  17. // BaseConfig represents the basic config for a Cron task
  18. type BaseConfig struct {
  19. Enabled bool
  20. RunAtStart bool
  21. Schedule string
  22. }
  23. // OlderThanConfig represents a cron task with OlderThan setting
  24. type OlderThanConfig struct {
  25. BaseConfig
  26. OlderThan time.Duration
  27. }
  28. // UpdateExistingConfig represents a cron task with UpdateExisting setting
  29. type UpdateExistingConfig struct {
  30. BaseConfig
  31. UpdateExisting bool
  32. }
  33. // GetSchedule returns the schedule for the base config
  34. func (b *BaseConfig) GetSchedule() string {
  35. return b.Schedule
  36. }
  37. // IsEnabled returns the enabled status for the config
  38. func (b *BaseConfig) IsEnabled() bool {
  39. return b.Enabled
  40. }
  41. // DoRunAtStart returns whether the task should be run at the start
  42. func (b *BaseConfig) DoRunAtStart() bool {
  43. return b.RunAtStart
  44. }
  45. // FormatMessage returns a message for the task
  46. func (b *BaseConfig) FormatMessage(name, status string, doer *models.User, args ...interface{}) string {
  47. realArgs := make([]interface{}, 0, len(args)+2)
  48. realArgs = append(realArgs, i18n.Tr("en-US", "admin.dashboard."+name))
  49. if doer == nil {
  50. realArgs = append(realArgs, "(Cron)")
  51. } else {
  52. realArgs = append(realArgs, doer.Name)
  53. }
  54. if len(args) > 0 {
  55. realArgs = append(realArgs, args...)
  56. }
  57. if doer == nil || (doer.ID == -1 && doer.Name == "(Cron)") {
  58. return i18n.Tr("en-US", "admin.dashboard.cron."+status, realArgs...)
  59. }
  60. return i18n.Tr("en-US", "admin.dashboard.task."+status, realArgs...)
  61. }