code.gitea.io/gitea@v1.21.7/services/cron/setting.go (about)

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