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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cron
  4. import (
  5. "time"
  6. "code.gitea.io/gitea/modules/translation"
  7. )
  8. // Config represents a basic configuration interface that cron task
  9. type Config interface {
  10. IsEnabled() bool
  11. DoRunAtStart() bool
  12. GetSchedule() string
  13. FormatMessage(locale translation.Locale, name, status, doer string, args ...any) string
  14. DoNoticeOnSuccess() bool
  15. }
  16. // BaseConfig represents the basic config for a Cron task
  17. type BaseConfig struct {
  18. Enabled bool
  19. RunAtStart bool
  20. Schedule string
  21. NoticeOnSuccess bool
  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. // CleanupHookTaskConfig represents a cron task with settings to cleanup hook_task
  34. type CleanupHookTaskConfig struct {
  35. BaseConfig
  36. CleanupType string
  37. OlderThan time.Duration
  38. NumberToKeep int
  39. }
  40. // GetSchedule returns the schedule for the base config
  41. func (b *BaseConfig) GetSchedule() string {
  42. return b.Schedule
  43. }
  44. // IsEnabled returns the enabled status for the config
  45. func (b *BaseConfig) IsEnabled() bool {
  46. return b.Enabled
  47. }
  48. // DoRunAtStart returns whether the task should be run at the start
  49. func (b *BaseConfig) DoRunAtStart() bool {
  50. return b.RunAtStart
  51. }
  52. // DoNoticeOnSuccess returns whether a success notice should be posted
  53. func (b *BaseConfig) DoNoticeOnSuccess() bool {
  54. return b.NoticeOnSuccess
  55. }
  56. // FormatMessage returns a message for the task
  57. // Please note the `status` string will be concatenated with `admin.dashboard.cron.` and `admin.dashboard.task.` to provide locale messages. Similarly `name` will be composed with `admin.dashboard.` to provide the locale name for the task.
  58. func (b *BaseConfig) FormatMessage(locale translation.Locale, name, status, doer string, args ...any) string {
  59. realArgs := make([]any, 0, len(args)+2)
  60. realArgs = append(realArgs, locale.Tr("admin.dashboard."+name))
  61. if doer == "" {
  62. realArgs = append(realArgs, "(Cron)")
  63. } else {
  64. realArgs = append(realArgs, doer)
  65. }
  66. if len(args) > 0 {
  67. realArgs = append(realArgs, args...)
  68. }
  69. if doer == "" {
  70. return locale.Tr("admin.dashboard.cron."+status, realArgs...)
  71. }
  72. return locale.Tr("admin.dashboard.task."+status, realArgs...)
  73. }