mirror of
https://git.ptzo.gdn/feditools/relay.git
synced 2024-09-21 09:27:12 +00:00
Pause for Delivery Errors (#98)
This commit is contained in:
parent
585efa2b05
commit
293e27aceb
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/feditools/relay/cmd/relay/action"
|
||||
"github.com/feditools/relay/internal/clock"
|
||||
@ -14,6 +15,7 @@ import (
|
||||
"github.com/feditools/relay/internal/language"
|
||||
"github.com/feditools/relay/internal/logic/logic1"
|
||||
"github.com/feditools/relay/internal/runner/faktory"
|
||||
"github.com/feditools/relay/internal/scheduler"
|
||||
"github.com/feditools/relay/internal/token"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/uptrace/uptrace-go/uptrace"
|
||||
@ -156,6 +158,16 @@ var Start action.Action = func(topCtx context.Context) error {
|
||||
logicMod.SetRunner(runnerMod)
|
||||
runnerMod.Start(ctx)
|
||||
|
||||
// create scheduler
|
||||
schedulerMod, err := scheduler.New(runnerMod)
|
||||
if err != nil {
|
||||
l.Errorf("scheduler: %s", err.Error())
|
||||
cancel()
|
||||
|
||||
return err
|
||||
}
|
||||
logicMod.SetScheduler(schedulerMod)
|
||||
|
||||
// create http server
|
||||
l.Debug("creating http server")
|
||||
server, err := newHttpServer(
|
||||
@ -182,6 +194,13 @@ var Start action.Action = func(topCtx context.Context) error {
|
||||
stopSigChan := make(chan os.Signal)
|
||||
signal.Notify(stopSigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// start scheduler
|
||||
go func(s *scheduler.Module, errChan chan error) {
|
||||
l.Debug("starting scheduler server")
|
||||
s.Start()
|
||||
errChan <- errors.New("scheduler stopped")
|
||||
}(schedulerMod, errChan)
|
||||
|
||||
// start webserver
|
||||
go func(s *http.Server, errChan chan error) {
|
||||
l.Debug("starting http server")
|
||||
|
2
go.mod
2
go.mod
@ -7,6 +7,7 @@ require (
|
||||
github.com/contribsys/faktory v1.6.1
|
||||
github.com/contribsys/faktory_worker_go v1.6.0
|
||||
github.com/feditools/go-lib v0.16.4
|
||||
github.com/go-co-op/gocron v1.16.2
|
||||
github.com/go-fed/activity v1.0.0
|
||||
github.com/go-fed/httpsig v1.1.0
|
||||
github.com/go-playground/validator/v10 v10.11.0
|
||||
@ -86,6 +87,7 @@ require (
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/spf13/afero v1.8.2 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
|
4
go.sum
4
go.sum
@ -116,6 +116,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
|
||||
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
|
||||
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/go-co-op/gocron v1.16.2 h1:p9ghzsN5PqqPyWXYDO2JlvD1DOUNT8pPSyGYC62XBcY=
|
||||
github.com/go-co-op/gocron v1.16.2/go.mod h1:W/N9G7bntRo5fVQlmjncvqSt74jxCxHfjyHlgcB33T8=
|
||||
github.com/go-fed/activity v1.0.0 h1:j7w3auHZnVCjUcgA1mE+UqSOjFBhvW2Z2res3vNol+o=
|
||||
github.com/go-fed/activity v1.0.0/go.mod h1:v4QoPaAzjWZ8zN2VFVGL5ep9C02mst0hQYHUpQwso4Q=
|
||||
github.com/go-fed/httpsig v0.1.1-0.20190914113940-c2de3672e5b5/go.mod h1:T56HUNYZUQ1AGUzhAYPugZfp36sKApVnGBgKlIY+aIE=
|
||||
@ -391,6 +393,8 @@ github.com/rbcervilla/redisstore/v8 v8.1.0 h1:YmNOHjAIb7+DLbqLPxSFAxmbtXbDgFcY2/
|
||||
github.com/rbcervilla/redisstore/v8 v8.1.0/go.mod h1:JGDqTj9JQ28J1c+2u3iEnOUBC7W5WMW/YRKLqRm0pOk=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
|
@ -21,7 +21,6 @@ func init() {
|
||||
|
||||
up := func(ctx context.Context, db *bun.DB) error {
|
||||
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
|
||||
for _, c := range addColumns {
|
||||
query := tx.NewAddColumn().
|
||||
Model(c.Model).
|
||||
@ -42,6 +41,20 @@ func init() {
|
||||
|
||||
down := func(ctx context.Context, db *bun.DB) error {
|
||||
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
for _, c := range addColumns {
|
||||
query := tx.NewDropColumn().
|
||||
Model(c.Model).
|
||||
ColumnExpr(fmt.Sprintf("%s %s", c.Name, c.Type))
|
||||
|
||||
l.Infof(DroppingColumn, c.Name, query.GetTableName())
|
||||
|
||||
_, err := query.Exec(ctx)
|
||||
if err != nil {
|
||||
l.Errorf(DroppingColumnErr, c.Name, query.GetTableName(), err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
83
internal/db/bun/migrations/20220822160012_new.go
Normal file
83
internal/db/bun/migrations/20220822160012_new.go
Normal file
@ -0,0 +1,83 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/feditools/relay/internal/models"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/sqltype"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
l := logger.WithField("migration", "20220822160012")
|
||||
|
||||
addColumns := columnList{
|
||||
{
|
||||
Model: (*models.Instance)(nil),
|
||||
Name: "is_paused",
|
||||
Type: sqltype.Boolean,
|
||||
|
||||
Default: false,
|
||||
NotNull: true,
|
||||
},
|
||||
}
|
||||
|
||||
up := func(ctx context.Context, db *bun.DB) error {
|
||||
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
for _, c := range addColumns {
|
||||
var sb strings.Builder
|
||||
sb.WriteString(fmt.Sprintf("%s %s", c.Name, c.Type))
|
||||
if c.NotNull {
|
||||
sb.WriteString(" NOT NULL")
|
||||
}
|
||||
if c.Default != nil {
|
||||
sb.WriteString(" DEFAULT ?")
|
||||
}
|
||||
|
||||
query := tx.NewAddColumn().
|
||||
Model(c.Model)
|
||||
|
||||
if c.Default != nil {
|
||||
query = query.ColumnExpr(sb.String(), c.Default)
|
||||
} else {
|
||||
query = query.ColumnExpr(sb.String())
|
||||
}
|
||||
|
||||
l.Infof(CreatingColumn, c.Name, query.GetTableName())
|
||||
|
||||
_, err := query.Exec(ctx)
|
||||
if err != nil {
|
||||
l.Errorf(CreatingColumnErr, c.Name, query.GetTableName(), err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
down := func(ctx context.Context, db *bun.DB) error {
|
||||
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
for _, c := range addColumns {
|
||||
query := tx.NewDropColumn().
|
||||
Model(c.Model).
|
||||
ColumnExpr(fmt.Sprintf("%s %s", c.Name, c.Type))
|
||||
|
||||
l.Infof(DroppingColumn, c.Name, query.GetTableName())
|
||||
|
||||
_, err := query.Exec(ctx)
|
||||
if err != nil {
|
||||
l.Errorf(DroppingColumnErr, c.Name, query.GetTableName(), err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
if err := Migrations.Register(up, down); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
@ -3,6 +3,8 @@ package migrations
|
||||
const (
|
||||
CreatingColumn = "creating column '%s' in table `%s`"
|
||||
CreatingColumnErr = "can't create column '%s' in table `%s`: %s"
|
||||
DroppingColumn = "dropping column '%s' in table `%s`"
|
||||
DroppingColumnErr = "can't drop column '%s' in table `%s`: %s"
|
||||
CreatingTable = "creating table '%s'"
|
||||
CreatingTableErr = "can't create table '%s': %s"
|
||||
DroppingTable = "dropping table '%s'"
|
||||
|
@ -6,4 +6,7 @@ type column struct {
|
||||
Model interface{}
|
||||
Name string
|
||||
Type string
|
||||
|
||||
Default interface{}
|
||||
NotNull bool
|
||||
}
|
||||
|
20
internal/http/template/admin_jobs.go
Normal file
20
internal/http/template/admin_jobs.go
Normal file
@ -0,0 +1,20 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
libtemplate "github.com/feditools/go-lib/template"
|
||||
"github.com/feditools/relay/internal/logic"
|
||||
)
|
||||
|
||||
// AdminJobsName is the name of the admin jobs template.
|
||||
const AdminJobsName = "admin_jobs"
|
||||
|
||||
// AdminJobs contains the variables for the admin jobs template.
|
||||
type AdminJobs struct {
|
||||
Common
|
||||
|
||||
Jobs []logic.SchedulerJob
|
||||
|
||||
FormRunJobError *libtemplate.Alert
|
||||
FormRunJobAction libtemplate.FormInput
|
||||
FormRunJobJob FormSelect
|
||||
}
|
@ -1,17 +1,20 @@
|
||||
package template
|
||||
|
||||
type FormInput struct {
|
||||
Class string
|
||||
ID string
|
||||
Name string
|
||||
Placeholder string
|
||||
Type string
|
||||
Value string
|
||||
import libtemplate "github.com/feditools/go-lib/template"
|
||||
|
||||
Validation *FormInputValidation
|
||||
type FormSelect struct {
|
||||
ID string
|
||||
Name string
|
||||
Label *libtemplate.FormLabel
|
||||
Options []FormSelectOption
|
||||
Disabled bool
|
||||
Required bool
|
||||
|
||||
Validation *libtemplate.FormValidation
|
||||
}
|
||||
|
||||
type FormInputValidation struct {
|
||||
Message string
|
||||
Valid bool
|
||||
type FormSelectOption struct {
|
||||
Text string
|
||||
Value string
|
||||
Selected bool
|
||||
}
|
||||
|
77
internal/http/webapp/admin_job.go
Normal file
77
internal/http/webapp/admin_job.go
Normal file
@ -0,0 +1,77 @@
|
||||
package webapp
|
||||
|
||||
import (
|
||||
libtemplate "github.com/feditools/go-lib/template"
|
||||
"github.com/feditools/relay/internal/http/template"
|
||||
"github.com/feditools/relay/internal/language"
|
||||
"github.com/feditools/relay/internal/scheduler"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// AdminJobGetHandler serves the home page.
|
||||
func (m *Module) AdminJobGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
m.displayAdminJobs(w, r, displayAdminJobConfig{})
|
||||
}
|
||||
|
||||
type displayAdminJobConfig struct {
|
||||
Alerts *[]libtemplate.Alert
|
||||
|
||||
FormRunJobError *libtemplate.Alert
|
||||
FormRunJobJobValue string
|
||||
FormRunJobJobValidation *libtemplate.FormValidation
|
||||
}
|
||||
|
||||
func (m *Module) displayAdminJobs(w http.ResponseWriter, r *http.Request, config displayAdminJobConfig) {
|
||||
//l := logger.WithField("func", "displayAdminInstance")
|
||||
|
||||
// get localizer
|
||||
localizer := r.Context().Value(ContextKeyLocalizer).(*language.Localizer) //nolint
|
||||
|
||||
// Init template variables
|
||||
tmplVars := &template.AdminJobs{
|
||||
Common: template.Common{
|
||||
PageTitle: localizer.TextInstances().String(),
|
||||
},
|
||||
|
||||
FormRunJobError: config.FormRunJobError,
|
||||
FormRunJobAction: libtemplate.FormInput{
|
||||
ID: "formRunJobInputAction",
|
||||
Type: libtemplate.FormInputTypeHidden,
|
||||
Name: FormAction,
|
||||
Value: ActionAdd,
|
||||
},
|
||||
FormRunJobJob: template.FormSelect{
|
||||
ID: "formRunJobInputJob",
|
||||
Name: FormJob,
|
||||
Label: &libtemplate.FormLabel{
|
||||
Text: localizer.TextJob(),
|
||||
Class: "form-label",
|
||||
},
|
||||
Disabled: false,
|
||||
Required: true,
|
||||
Options: []template.FormSelectOption{
|
||||
{},
|
||||
{
|
||||
Text: scheduler.TagMaintDeliveryErrorTimeout,
|
||||
Value: scheduler.TagMaintDeliveryErrorTimeout,
|
||||
},
|
||||
},
|
||||
Validation: config.FormRunJobJobValidation,
|
||||
},
|
||||
}
|
||||
|
||||
// alerts
|
||||
tmplVars.Alerts = config.Alerts
|
||||
|
||||
// get jobs
|
||||
jobs, err := m.logic.GetSchedulerJobs()
|
||||
if err != nil {
|
||||
m.returnErrorPage(w, r, http.StatusInternalServerError, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
tmplVars.Jobs = jobs
|
||||
|
||||
m.executeTemplate(w, r, template.AdminJobsName, tmplVars)
|
||||
}
|
@ -16,6 +16,8 @@ const (
|
||||
ActionEdit = "edit"
|
||||
// ActionImport is the value for an import action.
|
||||
ActionImport = "import"
|
||||
// ActionRunJob is the value for a run job action.
|
||||
ActionRunJob = "run-job"
|
||||
|
||||
// CSVHeaderBlockSubdomains is the value for a Block Subdomains csv header.
|
||||
CSVHeaderBlockSubdomains = "Block Subdomains"
|
||||
@ -32,6 +34,8 @@ const (
|
||||
FormFile = "file"
|
||||
// FormHomeBody is the key for a home body form field.
|
||||
FormHomeBody = "home-body"
|
||||
// FormJob is the key for a job form field.
|
||||
FormJob = "job"
|
||||
// FormRobotsAllowIndexing is the key for an allow indexing form field.
|
||||
FormRobotsAllowIndexing = "robots-allow-indexing"
|
||||
// FormObfuscatedDomain is the key for a domain form field.
|
||||
|
@ -41,6 +41,7 @@ func (m *Module) Route(s *http.Server) error {
|
||||
admin.HandleFunc(path.AppAdminSubHome, m.AdminHomePostHandler).Methods(nethttp.MethodPost)
|
||||
admin.HandleFunc(path.AppAdminSubInstance, m.AdminInstanceGetHandler).Methods(nethttp.MethodGet)
|
||||
admin.HandleFunc(path.AppAdminSubInstanceView, m.AdminInstanceViewGetHandler).Methods(nethttp.MethodGet)
|
||||
admin.HandleFunc(path.AppAdminSubJob, m.AdminJobGetHandler).Methods(nethttp.MethodGet)
|
||||
admin.HandleFunc(path.AppAdminSubStatistics, m.AdminStatisticsGetHandler).Methods(nethttp.MethodGet)
|
||||
|
||||
return nil
|
||||
|
@ -175,6 +175,12 @@ func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) {
|
||||
{
|
||||
Divider: true,
|
||||
},
|
||||
{
|
||||
Text: l.TextJobs().String(),
|
||||
MatchStr: path.ReAppAdminJobPre,
|
||||
FAIcon: "chart-line",
|
||||
URL: path.AppAdminJob,
|
||||
},
|
||||
{
|
||||
Text: l.TextStatistics().String(),
|
||||
MatchStr: path.ReAppAdminStatisticsPre,
|
||||
|
@ -3,6 +3,7 @@ package language
|
||||
import (
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"golang.org/x/text/language"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Localizer returns translated phrases.
|
||||
@ -28,3 +29,6 @@ func (l *LocalizedString) Language() language.Tag { return l.language }
|
||||
|
||||
// String returns the localized string.
|
||||
func (l *LocalizedString) String() string { return l.string }
|
||||
|
||||
// Lower returns the localized string in lower-case.
|
||||
func (l *LocalizedString) Lower() string { return strings.ToLower(l.string) }
|
||||
|
39
internal/language/text_j.go
Normal file
39
internal/language/text_j.go
Normal file
@ -0,0 +1,39 @@
|
||||
package language
|
||||
|
||||
import "github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
|
||||
// TextJob returns a translated phrase.
|
||||
func (l *Localizer) TextJob() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "Job",
|
||||
Other: "Job",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextJob").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
||||
|
||||
// TextJobs returns a translated phrase.
|
||||
func (l *Localizer) TextJobs() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "Jobs",
|
||||
Other: "Jobs",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextJobs").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
71
internal/language/text_j_test.go
Normal file
71
internal/language/text_j_test.go
Normal file
@ -0,0 +1,71 @@
|
||||
package language
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/text/language"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLocalizer_TextJob(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Job",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextJob, table)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizer_TextJobs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Jobs",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextJobs, table)
|
||||
})
|
||||
}
|
||||
}
|
@ -2,6 +2,24 @@ package language
|
||||
|
||||
import "github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
|
||||
// TextLastRun returns a translated phrase.
|
||||
func (l *Localizer) TextLastRun() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "LastRun",
|
||||
Other: "Last Run",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextLastRun").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
||||
|
||||
// TextLinear returns a translated phrase.
|
||||
func (l *Localizer) TextLinear() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
|
@ -7,6 +7,38 @@ import (
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func TestLocalizer_TextLastRun(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Last Run",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextLastRun, table)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizer_TextLinear(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -2,6 +2,24 @@ package language
|
||||
|
||||
import "github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
|
||||
// TextNextRun returns a translated phrase.
|
||||
func (l *Localizer) TextNextRun() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "NextRun",
|
||||
Other: "Next Run",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextNextRun").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
||||
|
||||
// TextNotifications returns a translated phrase.
|
||||
func (l *Localizer) TextNotifications() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
|
@ -6,6 +6,38 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLocalizer_TextNextRun(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Next Run",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextNextRun, table)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizer_TextNotifications(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
21
internal/language/text_p.go
Normal file
21
internal/language/text_p.go
Normal file
@ -0,0 +1,21 @@
|
||||
package language
|
||||
|
||||
import "github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
|
||||
// TextPaused returns a translated phrase.
|
||||
func (l *Localizer) TextPaused() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "Paused",
|
||||
Other: "Paused",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextPaused").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
39
internal/language/text_p_test.go
Normal file
39
internal/language/text_p_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
package language
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/text/language"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLocalizer_TextPaused(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Paused",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextPaused, table)
|
||||
})
|
||||
}
|
||||
}
|
@ -55,3 +55,39 @@ func (l *Localizer) TextRepo() *LocalizedString {
|
||||
string: text,
|
||||
}
|
||||
}
|
||||
|
||||
// TextRunJob returns a translated phrase.
|
||||
func (l *Localizer) TextRunJob() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "RunJob",
|
||||
Other: "Run Job",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextRunJob").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
||||
|
||||
// TextRunning returns a translated phrase.
|
||||
func (l *Localizer) TextRunning() *LocalizedString {
|
||||
text, tag, err := l.localizer.LocalizeWithTag(&i18n.LocalizeConfig{
|
||||
DefaultMessage: &i18n.Message{
|
||||
ID: "Running",
|
||||
Other: "Running",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.WithField("func", "TextRunning").Warningf(missingTranslationWarning, err.Error())
|
||||
}
|
||||
|
||||
return &LocalizedString{
|
||||
language: tag,
|
||||
string: text,
|
||||
}
|
||||
}
|
||||
|
@ -102,3 +102,67 @@ func TestLocalizer_TextRepo(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizer_TextRunJob(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Run Job",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextRunJob, table)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizer_TextRunning(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tables := []testTextTable{
|
||||
{
|
||||
inputLang: language.English,
|
||||
outputString: "Running",
|
||||
outputLang: language.English,
|
||||
},
|
||||
}
|
||||
|
||||
langMod, _ := New()
|
||||
for i, table := range tables {
|
||||
i := i
|
||||
table := table
|
||||
|
||||
name := fmt.Sprintf(testTranslatedTo, i, table.inputLang)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
localizer, err := langMod.NewLocalizer(table.inputLang.String())
|
||||
if err != nil {
|
||||
t.Errorf(testCantGetLocalizer, i, table.inputLang, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
testText(t, i, localizer.TextRunning, table)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
18
internal/logic/block.go
Normal file
18
internal/logic/block.go
Normal file
@ -0,0 +1,18 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/feditools/relay/internal/models"
|
||||
)
|
||||
|
||||
type Block interface {
|
||||
AddBlock(ctx context.Context, account *models.Account, block *models.Block) error
|
||||
AddBlocks(ctx context.Context, account *models.Account, blocks ...*models.Block) error
|
||||
DeleteBlock(ctx context.Context, account *models.Account, block *models.Block) error
|
||||
GetBlockList(ctx context.Context) (*[]string, error)
|
||||
IsDomainBlocked(ctx context.Context, d string) (bool, error)
|
||||
ProcessBlockAdd(ctx context.Context, blockID int64) error
|
||||
ProcessBlockDelete(ctx context.Context, blockID int64) error
|
||||
ProcessBlockUpdate(ctx context.Context, blockID int64) error
|
||||
UpdateBlock(ctx context.Context, account *models.Account, changes []models.LogEntryBlockUpdateChange, block *models.Block) error
|
||||
}
|
14
internal/logic/instance.go
Normal file
14
internal/logic/instance.go
Normal file
@ -0,0 +1,14 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/feditools/relay/internal/models"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Instance interface {
|
||||
GetInstance(ctx context.Context, domain string) (*models.Instance, error)
|
||||
GetInstanceForActor(ctx context.Context, actorID *url.URL) (*models.Instance, error)
|
||||
GetInstanceForServerHostname(ctx context.Context, serverHostname string) (*models.Instance, error)
|
||||
GetInstanceSelf(ctx context.Context) (*models.Instance, error)
|
||||
}
|
@ -9,38 +9,21 @@ import (
|
||||
)
|
||||
|
||||
type Logic interface {
|
||||
AddBlock(ctx context.Context, account *models.Account, block *models.Block) error
|
||||
AddBlocks(ctx context.Context, account *models.Account, blocks ...*models.Block) error
|
||||
DeleteBlock(ctx context.Context, account *models.Account, block *models.Block) error
|
||||
Block
|
||||
Instance
|
||||
Metrics
|
||||
Scheduler
|
||||
|
||||
DeliverActivity(ctx context.Context, jid string, instanceID int64, activity fedihelper.Activity) error
|
||||
Domain() string
|
||||
GetBlockList(ctx context.Context) (*[]string, error)
|
||||
GetAccountConfigMap(ctx context.Context, keys ...models.ConfigKey) (*models.AccountConfigMap, error)
|
||||
GetConfigMap(ctx context.Context, keys ...models.ConfigKey) (*models.ConfigMap, error)
|
||||
GetConfigMapForAccount(ctx context.Context, accountID int64, keys ...models.ConfigKey) (*models.ConfigMap, error)
|
||||
GetInstance(ctx context.Context, domain string) (*models.Instance, error)
|
||||
GetInstanceForActor(ctx context.Context, actorID *url.URL) (*models.Instance, error)
|
||||
GetInstanceForServerHostname(ctx context.Context, serverHostname string) (*models.Instance, error)
|
||||
GetInstanceSelf(ctx context.Context) (*models.Instance, error)
|
||||
GetLoginURL(ctx context.Context, instance *models.Instance) (*url.URL, error)
|
||||
GetPeers(ctx context.Context) (*[]string, error)
|
||||
IsDomainBlocked(ctx context.Context, d string) (bool, error)
|
||||
MetricsGetAllDeliverErrorWeek(ctx context.Context, days int) (map[*models.Instance]MetricsDataPointsTime, error)
|
||||
MetricsGetAllDeliverSuccessWeek(ctx context.Context, days int) (map[*models.Instance]MetricsDataPointsTime, error)
|
||||
MetricsGetAllReceivedWeek(ctx context.Context, days int) (map[*models.Instance]MetricsDataPointsTime, error)
|
||||
MetricsGetDeliverErrorWeek(ctx context.Context, days int, instanceID int64) (MetricsDataPointsTime, error)
|
||||
MetricsGetDeliverSuccessWeek(ctx context.Context, days int, instanceID int64) (MetricsDataPointsTime, error)
|
||||
MetricsGetReceivedWeek(ctx context.Context, days int, instanceID int64) (MetricsDataPointsTime, error)
|
||||
MetricsGetReceivedTotalWeek(ctx context.Context, days int) (MetricsDataPointsTime, error)
|
||||
MetricsIncDeliverError(ctx context.Context, instanceID int64)
|
||||
MetricsIncDeliverSuccess(ctx context.Context, instanceID int64)
|
||||
MetricsIncReceived(ctx context.Context, instanceID int64)
|
||||
MaintDeliveryErrorTimeout(ctx context.Context, jid string) error
|
||||
ProcessActivity(ctx context.Context, jid string, instanceID int64, actorIRI *url.URL, activity fedihelper.Activity) error
|
||||
ProcessBlockAdd(ctx context.Context, blockID int64) error
|
||||
ProcessBlockDelete(ctx context.Context, blockID int64) error
|
||||
ProcessBlockUpdate(ctx context.Context, blockID int64) error
|
||||
ProcessConfigChanges(ctx context.Context, configChanges []*models.ConfigChange) error
|
||||
SendNotification(ctx context.Context, jid string, event models.EventType, metadata map[string]interface{}) error
|
||||
UpdateBlock(ctx context.Context, account *models.Account, changes []models.LogEntryBlockUpdateChange, block *models.Block) error
|
||||
ValidateRequest(r *http.Request, actorURI *url.URL) (bool, *fedihelper.Actor)
|
||||
}
|
||||
|
@ -29,6 +29,13 @@ func (l *Logic) DeliverActivity(ctx context.Context, jid string, instanceID int6
|
||||
return fmt.Errorf("db read: %s", err.Error())
|
||||
}
|
||||
|
||||
// drop if paused
|
||||
if instance.IsPaused {
|
||||
log.Debugf("instance %s is paused, dropping delivery", instance.Domain)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// send activity
|
||||
body, err := json.Marshal(activity)
|
||||
if err != nil {
|
||||
@ -66,6 +73,73 @@ func (l *Logic) ProcessActivity(ctx context.Context, jid string, instanceID int6
|
||||
"jid": jid,
|
||||
})
|
||||
|
||||
// get instance
|
||||
instance, err := l.db.ReadInstance(ctx, instanceID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
return fmt.Errorf("db read: %s", err.Error())
|
||||
}
|
||||
|
||||
// if paused, unpause
|
||||
if instance.IsPaused {
|
||||
instance.IsPaused = false
|
||||
|
||||
// create transaction
|
||||
txID, err := l.db.TxNew(ctx)
|
||||
if err != nil {
|
||||
msg := fmt.Errorf("new db tx: %s", err.Error())
|
||||
log.Error(msg.Error())
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
// create log entries
|
||||
logEntry, err := genLogEntryInstanceDeliveryResumed(nil, instance, models.LogEntryReasonDeliveryReceived)
|
||||
if err != nil {
|
||||
msg := fmt.Errorf("log gen: %s", err.Error())
|
||||
log.Error(msg.Error())
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
// add block to database
|
||||
if err := l.db.UpdateInstanceTX(ctx, txID, instance); err != nil {
|
||||
msg := fmt.Errorf("db update: %s", err.Error())
|
||||
log.Error(msg.Error())
|
||||
if txerr := l.db.TxRollback(ctx, txID); txerr != nil {
|
||||
msg := fmt.Errorf("error rolling back db tx: %s", err.Error())
|
||||
log.Error(msg.Error())
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
// add log entries to database
|
||||
if err := l.db.CreateLogEntryTX(ctx, txID, logEntry); err != nil {
|
||||
msg := fmt.Errorf("db create: %s", err.Error())
|
||||
log.Error(msg.Error())
|
||||
if txerr := l.db.TxRollback(ctx, txID); txerr != nil {
|
||||
msg := fmt.Errorf("error rolling back db tx: %s", err.Error())
|
||||
log.Error(msg.Error())
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
// commit transaction
|
||||
if err := l.db.TxCommit(ctx, txID); err != nil {
|
||||
msg := fmt.Errorf("error committing db tx: %s", err.Error())
|
||||
log.Errorf("error committing db tx: %s", err.Error())
|
||||
|
||||
return msg
|
||||
}
|
||||
}
|
||||
|
||||
actType, err := activity.Type()
|
||||
if err != nil {
|
||||
log.Debugf("can't get activity type: %s", err.Error())
|
||||
@ -77,13 +151,13 @@ func (l *Logic) ProcessActivity(ctx context.Context, jid string, instanceID int6
|
||||
|
||||
switch actType {
|
||||
case fedihelper.TypeAnnounce, fedihelper.TypeCreate:
|
||||
return l.doRelay(ctx, jid, instanceID, activity)
|
||||
return l.doRelay(ctx, jid, instance, activity)
|
||||
case fedihelper.TypeDelete, fedihelper.TypeUpdate:
|
||||
return l.doForward(ctx, jid, instanceID, activity)
|
||||
return l.doForward(ctx, jid, instance, activity)
|
||||
case fedihelper.TypeFollow:
|
||||
return l.doFollow(ctx, jid, instanceID, activity)
|
||||
return l.doFollow(ctx, jid, instance, activity)
|
||||
case fedihelper.TypeUndo:
|
||||
return l.doUndo(ctx, jid, instanceID, activity)
|
||||
return l.doUndo(ctx, jid, instance, activity)
|
||||
default:
|
||||
log.Debugf("unhandled activity type: %s", actType)
|
||||
|
||||
@ -91,7 +165,7 @@ func (l *Logic) ProcessActivity(ctx context.Context, jid string, instanceID int6
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logic) doFollow(ctx context.Context, jid string, instanceID int64, activity fedihelper.Activity) error {
|
||||
func (l *Logic) doFollow(ctx context.Context, jid string, instance *models.Instance, activity fedihelper.Activity) error {
|
||||
log := logger.WithFields(logrus.Fields{
|
||||
"func": "doFollow",
|
||||
"jid": jid,
|
||||
@ -113,12 +187,6 @@ func (l *Logic) doFollow(ctx context.Context, jid string, instanceID int64, acti
|
||||
}
|
||||
|
||||
// set followed
|
||||
instance, err := l.db.ReadInstance(ctx, instanceID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
return fmt.Errorf("db read: %s", err.Error())
|
||||
}
|
||||
instance.IsFollowing = true
|
||||
|
||||
// create log entries
|
||||
@ -187,7 +255,7 @@ func (l *Logic) doFollow(ctx context.Context, jid string, instanceID int64, acti
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Logic) doForward(ctx context.Context, jid string, instanceID int64, activity fedihelper.Activity) error {
|
||||
func (l *Logic) doForward(ctx context.Context, jid string, instance *models.Instance, activity fedihelper.Activity) error {
|
||||
log := logger.WithFields(logrus.Fields{
|
||||
"func": "doForward",
|
||||
"jid": jid,
|
||||
@ -208,20 +276,12 @@ func (l *Logic) doForward(ctx context.Context, jid string, instanceID int64, act
|
||||
return nil
|
||||
}
|
||||
|
||||
// get instance
|
||||
signingInstance, err := l.db.ReadInstance(ctx, instanceID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
return fmt.Errorf("db read: %s", err.Error())
|
||||
}
|
||||
|
||||
// forward activity
|
||||
log.Debugf("forwarding messagge from %s", signingInstance.ActorIRI)
|
||||
log.Debugf("forwarding messagge from %s", instance.ActorIRI)
|
||||
log.Tracef("forwarding activity: %#v", activity)
|
||||
|
||||
// read from db
|
||||
followedInstances, err := l.GetInstancesForForwarding(ctx, signingInstance.ActorIRI, activityID)
|
||||
followedInstances, err := l.GetInstancesForForwarding(ctx, instance.ActorIRI, activityID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
@ -230,10 +290,11 @@ func (l *Logic) doForward(ctx context.Context, jid string, instanceID int64, act
|
||||
log.Debugf("got %d followed instances", len(followedInstances))
|
||||
|
||||
// enqueue deliveries
|
||||
for _, instance := range followedInstances {
|
||||
err = l.runner.EnqueueDeliverActivity(ctx, instance.ID, activity)
|
||||
if err != nil {
|
||||
log.Errorf("enqueueing delivery: %s", err.Error())
|
||||
for _, followedInstance := range followedInstances {
|
||||
if !followedInstance.IsPaused {
|
||||
if err = l.runner.EnqueueDeliverActivity(ctx, followedInstance.ID, activity); err != nil {
|
||||
log.Errorf("enqueueing delivery: %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = l.cacheActivity.Add(activityID, activityID)
|
||||
@ -241,7 +302,7 @@ func (l *Logic) doForward(ctx context.Context, jid string, instanceID int64, act
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Logic) doRelay(ctx context.Context, jid string, instanceID int64, activity fedihelper.Activity) error {
|
||||
func (l *Logic) doRelay(ctx context.Context, jid string, instance *models.Instance, activity fedihelper.Activity) error {
|
||||
log := logger.WithFields(logrus.Fields{
|
||||
"func": "doRelay",
|
||||
"jid": jid,
|
||||
@ -262,16 +323,8 @@ func (l *Logic) doRelay(ctx context.Context, jid string, instanceID int64, activ
|
||||
return nil
|
||||
}
|
||||
|
||||
// get instance
|
||||
signingInstance, err := l.db.ReadInstance(ctx, instanceID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
return fmt.Errorf("db read: %s", err.Error())
|
||||
}
|
||||
|
||||
// forward activity
|
||||
log.Infof("relaying post from %s", signingInstance.ActorIRI)
|
||||
log.Infof("relaying post from %s", instance.ActorIRI)
|
||||
log.Tracef("relaying activity: %#v", activity)
|
||||
|
||||
// send announce
|
||||
@ -284,7 +337,7 @@ func (l *Logic) doRelay(ctx context.Context, jid string, instanceID int64, activ
|
||||
}
|
||||
|
||||
// read from db
|
||||
followedInstances, err := l.GetInstancesForForwarding(ctx, signingInstance.ActorIRI, objectID)
|
||||
followedInstances, err := l.GetInstancesForForwarding(ctx, instance.ActorIRI, objectID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
@ -293,8 +346,8 @@ func (l *Logic) doRelay(ctx context.Context, jid string, instanceID int64, activ
|
||||
log.Debugf("got %d followed instances", len(followedInstances))
|
||||
|
||||
// enqueue deliveries
|
||||
for _, instance := range followedInstances {
|
||||
err = l.runner.EnqueueDeliverActivity(ctx, instance.ID, outgoingActivity)
|
||||
for _, followedInstance := range followedInstances {
|
||||
err = l.runner.EnqueueDeliverActivity(ctx, followedInstance.ID, outgoingActivity)
|
||||
if err != nil {
|
||||
log.Errorf("enqueueing delivery: %s", err.Error())
|
||||
}
|
||||
@ -304,7 +357,7 @@ func (l *Logic) doRelay(ctx context.Context, jid string, instanceID int64, activ
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Logic) doUndo(ctx context.Context, jid string, instanceID int64, activity fedihelper.Activity) error {
|
||||
func (l *Logic) doUndo(ctx context.Context, jid string, instance *models.Instance, activity fedihelper.Activity) error {
|
||||
log := logger.WithFields(logrus.Fields{
|
||||
"func": "doUndo",
|
||||
"jid": jid,
|
||||
@ -318,15 +371,8 @@ func (l *Logic) doUndo(ctx context.Context, jid string, instanceID int64, activi
|
||||
|
||||
switch aType {
|
||||
case fedihelper.TypeAnnounce:
|
||||
return l.doForward(ctx, jid, instanceID, activity)
|
||||
return l.doForward(ctx, jid, instance, activity)
|
||||
case fedihelper.TypeFollow:
|
||||
// unset followed
|
||||
instance, err := l.db.ReadInstance(ctx, instanceID)
|
||||
if err != nil {
|
||||
log.Errorf("db read: %s", err.Error())
|
||||
|
||||
return fmt.Errorf("db read: %s", err.Error())
|
||||
}
|
||||
instance.IsFollowing = false
|
||||
|
||||
// create log entries
|
||||
|
@ -53,6 +53,43 @@ func genLogEntryBlockUpdate(b *models.Block, a *models.Account, changes []models
|
||||
return &logEntry, nil
|
||||
}
|
||||
|
||||
func genLogEntryInstanceDeliveryPaused(instance *models.Instance, reason models.LogEntryReason) (*models.LogEntry, error) {
|
||||
logEntry := models.LogEntry{
|
||||
Type: models.LogEntryTypeInstanceDeliveryPaused,
|
||||
}
|
||||
|
||||
err := logEntry.SetMetaData(&models.LogEntryInstanceDeliveryPaused{
|
||||
Domain: instance.Domain,
|
||||
Reason: reason,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &logEntry, nil
|
||||
}
|
||||
|
||||
func genLogEntryInstanceDeliveryResumed(account *models.Account, instance *models.Instance, reason models.LogEntryReason) (*models.LogEntry, error) {
|
||||
logEntry := models.LogEntry{
|
||||
Type: models.LogEntryTypeInstanceDeliveryResumed,
|
||||
}
|
||||
|
||||
metadata := models.LogEntryInstanceDeliveryResumed{
|
||||
Domain: instance.Domain,
|
||||
Reason: reason,
|
||||
}
|
||||
if account != nil {
|
||||
metadata.Account = account.String()
|
||||
}
|
||||
|
||||
err := logEntry.SetMetaData(&metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &logEntry, nil
|
||||
}
|
||||
|
||||
func genLogEntryInstanceJoin(i *models.Instance) (*models.LogEntry, error) {
|
||||
logEntry := models.LogEntry{
|
||||
Type: models.LogEntryTypeInstanceJoin,
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"github.com/feditools/relay/internal/notification"
|
||||
"github.com/feditools/relay/internal/path"
|
||||
"github.com/feditools/relay/internal/runner"
|
||||
"github.com/feditools/relay/internal/scheduler"
|
||||
"github.com/feditools/relay/internal/token"
|
||||
"github.com/go-fed/activity/pub"
|
||||
"github.com/go-fed/httpsig"
|
||||
@ -31,6 +32,7 @@ type Logic struct {
|
||||
kv kv.KV
|
||||
notifier notification.Notifier
|
||||
runner runner.Runner
|
||||
scheduler *scheduler.Module
|
||||
tokz *token.Tokenizer
|
||||
tracer trace.Tracer
|
||||
transport *fedihelper.Transport
|
||||
@ -132,3 +134,7 @@ func (l *Logic) SetNotifier(n notification.Notifier) {
|
||||
func (l *Logic) SetRunner(r runner.Runner) {
|
||||
l.runner = r
|
||||
}
|
||||
|
||||
func (l *Logic) SetScheduler(s *scheduler.Module) {
|
||||
l.scheduler = s
|
||||
}
|
||||
|
132
internal/logic/logic1/maintenance.go
Normal file
132
internal/logic/logic1/maintenance.go
Normal file
@ -0,0 +1,132 @@
|
||||
package logic1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/feditools/relay/internal/models"
|
||||
"github.com/sirupsen/logrus"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (l *Logic) MaintDeliveryErrorTimeout(ctx context.Context, jid string) error {
|
||||
log := logger.WithFields(logrus.Fields{
|
||||
"func": "MaintDeliveryErrorTimeout",
|
||||
"jid": jid,
|
||||
})
|
||||
|
||||
today := time.Date(time.Now().UTC().Year(), time.Now().UTC().Month(), time.Now().UTC().Day(), 0, 0, 0, 0, time.UTC)
|
||||
|
||||
deliverErrors, err := l.MetricsGetAllDeliverErrorWeek(ctx, 8)
|
||||
if err != nil {
|
||||
log.Errorf("metrics get errors: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
deliverSuccesses, err := l.MetricsGetAllDeliverSuccessWeek(ctx, 8)
|
||||
if err != nil {
|
||||
log.Errorf("metrics get successes: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
var instancesWithAllErrors []*models.Instance
|
||||
for instance, dps := range deliverErrors {
|
||||
allErrors := true
|
||||
for _, dp := range dps {
|
||||
log.Tracef("instance %s had %d errors on %s", instance.Domain, dp.Y, dp.X)
|
||||
if dp.X != today && dp.Y == 0 {
|
||||
allErrors = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allErrors {
|
||||
log.Debugf("instance %s doesn't have all errors, skipping", instance.Domain)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
hasSuccess := false
|
||||
for sIncident, dps := range deliverSuccesses {
|
||||
if instance.ID == sIncident.ID {
|
||||
for _, dp := range dps {
|
||||
log.Tracef("instance %s had %d successes on %s", instance.Domain, dp.Y, dp.X)
|
||||
if dp.X != today && dp.Y != 0 {
|
||||
hasSuccess = true
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasSuccess {
|
||||
log.Debugf("instance %s had some successes, skipping", instance.Domain)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debugf("instance %s had all errors, pausing", instance.Domain)
|
||||
if allErrors {
|
||||
instancesWithAllErrors = append(instancesWithAllErrors, instance)
|
||||
}
|
||||
}
|
||||
if len(instancesWithAllErrors) == 0 {
|
||||
log.Debugf("no instances with 1 week of errors")
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
// add to db
|
||||
|
||||
// create transaction
|
||||
txID, err := l.db.TxNew(ctx)
|
||||
if err != nil {
|
||||
log.Errorf("new db tx: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
for _, instance := range instancesWithAllErrors {
|
||||
|
||||
// create log entries
|
||||
logEntry, err := genLogEntryInstanceDeliveryPaused(instance, models.LogEntryReasonDeliveryError)
|
||||
if err != nil {
|
||||
log.Errorf("log gen: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
instance.IsPaused = true
|
||||
|
||||
// add block to database
|
||||
if err := l.db.UpdateInstanceTX(ctx, txID, instance); err != nil {
|
||||
log.Errorf("db create blocks: %s", err.Error())
|
||||
if txerr := l.db.TxRollback(ctx, txID); txerr != nil {
|
||||
log.Errorf("error rolling back db tx: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
// add log entries to database
|
||||
if err := l.db.CreateLogEntryTX(ctx, txID, logEntry); err != nil {
|
||||
log.Errorf("db create: %s", err.Error())
|
||||
if txerr := l.db.TxRollback(ctx, txID); txerr != nil {
|
||||
log.Errorf("error rolling back db tx: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
}
|
||||
|
||||
// commit transaction
|
||||
if err := l.db.TxCommit(ctx, txID); err != nil {
|
||||
log.Errorf("error committing db tx: %s", err.Error())
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
||||
|
||||
return nil // always return nil, this job is scheduled
|
||||
}
|
17
internal/logic/logic1/schduler.go
Normal file
17
internal/logic/logic1/schduler.go
Normal file
@ -0,0 +1,17 @@
|
||||
package logic1
|
||||
|
||||
import "github.com/feditools/relay/internal/logic"
|
||||
|
||||
func (l *Logic) GetSchedulerJobs() ([]logic.SchedulerJob, error) {
|
||||
jobs := l.scheduler.Jobs()
|
||||
|
||||
logicJobs := make([]logic.SchedulerJob, len(jobs))
|
||||
for i, j := range jobs {
|
||||
logicJobs[i].Name = j.Tags()[0]
|
||||
logicJobs[i].Running = j.IsRunning()
|
||||
logicJobs[i].LastRun = j.LastRun()
|
||||
logicJobs[i].NextRun = j.NextRun()
|
||||
}
|
||||
|
||||
return logicJobs, nil
|
||||
}
|
@ -1,6 +1,23 @@
|
||||
package logic
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"github.com/feditools/relay/internal/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Metrics interface {
|
||||
MetricsGetAllDeliverErrorWeek(ctx context.Context, days int) (map[*models.Instance]MetricsDataPointsTime, error)
|
||||
MetricsGetAllDeliverSuccessWeek(ctx context.Context, days int) (map[*models.Instance]MetricsDataPointsTime, error)
|
||||
MetricsGetAllReceivedWeek(ctx context.Context, days int) (map[*models.Instance]MetricsDataPointsTime, error)
|
||||
MetricsGetDeliverErrorWeek(ctx context.Context, days int, instanceID int64) (MetricsDataPointsTime, error)
|
||||
MetricsGetDeliverSuccessWeek(ctx context.Context, days int, instanceID int64) (MetricsDataPointsTime, error)
|
||||
MetricsGetReceivedWeek(ctx context.Context, days int, instanceID int64) (MetricsDataPointsTime, error)
|
||||
MetricsGetReceivedTotalWeek(ctx context.Context, days int) (MetricsDataPointsTime, error)
|
||||
MetricsIncDeliverError(ctx context.Context, instanceID int64)
|
||||
MetricsIncDeliverSuccess(ctx context.Context, instanceID int64)
|
||||
MetricsIncReceived(ctx context.Context, instanceID int64)
|
||||
}
|
||||
|
||||
type MetricsDataPointsTime []MetricsDataPointTime
|
||||
|
||||
|
14
internal/logic/schduler.go
Normal file
14
internal/logic/schduler.go
Normal file
@ -0,0 +1,14 @@
|
||||
package logic
|
||||
|
||||
import "time"
|
||||
|
||||
type Scheduler interface {
|
||||
GetSchedulerJobs() (jobs []SchedulerJob, err error)
|
||||
}
|
||||
|
||||
type SchedulerJob struct {
|
||||
Name string
|
||||
Running bool
|
||||
LastRun time.Time
|
||||
NextRun time.Time
|
||||
}
|
@ -25,6 +25,7 @@ type Instance struct {
|
||||
ActorIRI string `validate:"required,url" bun:",nullzero,notnull,unique"`
|
||||
InboxIRI string `validate:"required,url" bun:",nullzero,notnull,unique"`
|
||||
IsFollowing bool `validate:"-" bun:",notnull"`
|
||||
IsPaused bool `validate:"-" bun:",notnull"`
|
||||
OAuthClientID string `validate:"-" bun:",nullzero"`
|
||||
OAuthClientSecret string `validate:"-" bun:",nullzero"`
|
||||
BlockID int64 `validate:"-" bun:",nullzero"`
|
||||
|
@ -36,6 +36,10 @@ func (l *LogEntry) String() string {
|
||||
metadata = &LogEntryBlockDelete{}
|
||||
case LogEntryTypeBlockUpdate:
|
||||
metadata = &LogEntryBlockUpdate{}
|
||||
case LogEntryTypeInstanceDeliveryPaused:
|
||||
metadata = &LogEntryInstanceDeliveryPaused{}
|
||||
case LogEntryTypeInstanceDeliveryResumed:
|
||||
metadata = &LogEntryInstanceDeliveryResumed{}
|
||||
case LogEntryTypeInstanceJoin:
|
||||
metadata = &LogEntryInstanceJoin{}
|
||||
case LogEntryTypeInstanceLeft:
|
||||
|
@ -5,6 +5,83 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
LogEntryTypeInstanceDeliveryPaused LogEntryType = "INSTANCE_DELIVERY_PAUSED"
|
||||
LogEntryTypeInstanceDeliveryPausedString = "Delivery to instance %s was paused%s."
|
||||
)
|
||||
|
||||
var LogEntryTypeInstanceDeliveryPausedReasonString = map[LogEntryReason]string{
|
||||
LogEntryReasonDeliveryError: " due to delivery errors",
|
||||
}
|
||||
|
||||
type LogEntryInstanceDeliveryPaused struct {
|
||||
Domain string `json:"domain"`
|
||||
Reason LogEntryReason `json:"reason"`
|
||||
}
|
||||
|
||||
func (t *LogEntryInstanceDeliveryPaused) String() string {
|
||||
reason := ""
|
||||
|
||||
if r, ok := LogEntryTypeInstanceDeliveryPausedReasonString[t.Reason]; ok {
|
||||
reason = r
|
||||
}
|
||||
|
||||
return fmt.Sprintf(LogEntryTypeInstanceDeliveryPausedString, t.Domain, reason)
|
||||
}
|
||||
|
||||
func (t *LogEntryInstanceDeliveryPaused) ToJSON() (metadata string, err error) {
|
||||
jsonString, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(jsonString), nil
|
||||
}
|
||||
|
||||
func (t *LogEntryInstanceDeliveryPaused) FromJSON(metadata string) error {
|
||||
return json.Unmarshal([]byte(metadata), t)
|
||||
}
|
||||
|
||||
const (
|
||||
LogEntryTypeInstanceDeliveryResumed LogEntryType = "INSTANCE_DELIVERY_RESUMED"
|
||||
LogEntryTypeInstanceDeliveryResumedString = "Delivery to instance %s was resumed%s."
|
||||
)
|
||||
|
||||
var LogEntryTypeInstanceDeliveryResumedReasonString = map[LogEntryReason]string{
|
||||
LogEntryReasonAdminIntervention: " manually",
|
||||
LogEntryReasonDeliveryReceived: " because a delivery was received from it",
|
||||
LogEntryReasonUserIntervention: " manually",
|
||||
}
|
||||
|
||||
type LogEntryInstanceDeliveryResumed struct {
|
||||
Account string `json:"account,omitempty"`
|
||||
Domain string `json:"domain"`
|
||||
Reason LogEntryReason `json:"reason"`
|
||||
}
|
||||
|
||||
func (t *LogEntryInstanceDeliveryResumed) String() string {
|
||||
reason := ""
|
||||
|
||||
if r, ok := LogEntryTypeInstanceDeliveryResumedReasonString[t.Reason]; ok {
|
||||
reason = r
|
||||
}
|
||||
|
||||
return fmt.Sprintf(LogEntryTypeInstanceDeliveryResumedString, t.Domain, reason)
|
||||
}
|
||||
|
||||
func (t *LogEntryInstanceDeliveryResumed) ToJSON() (metadata string, err error) {
|
||||
jsonString, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(jsonString), nil
|
||||
}
|
||||
|
||||
func (t *LogEntryInstanceDeliveryResumed) FromJSON(metadata string) error {
|
||||
return json.Unmarshal([]byte(metadata), t)
|
||||
}
|
||||
|
||||
const (
|
||||
LogEntryTypeInstanceJoin LogEntryType = "INSTANCE_JOIN"
|
||||
LogEntryTypeInstanceJoinString string = "Instance %s joined."
|
||||
|
10
internal/models/log_entry_reason.go
Normal file
10
internal/models/log_entry_reason.go
Normal file
@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
type LogEntryReason string
|
||||
|
||||
const (
|
||||
LogEntryReasonAdminIntervention LogEntryReason = "ADMIN_INTERVENTION"
|
||||
LogEntryReasonDeliveryError LogEntryReason = "DELIVERY_ERROR"
|
||||
LogEntryReasonDeliveryReceived LogEntryReason = "DELIVERY_RECEIVED"
|
||||
LogEntryReasonUserIntervention LogEntryReason = "USER_INTERVENTION"
|
||||
)
|
@ -25,6 +25,8 @@ const (
|
||||
PartInbox = "inbox"
|
||||
// PartInstance is used in a path for instances.
|
||||
PartInstance = "instance"
|
||||
// PartJob is used in a path for job.
|
||||
PartJob = "job"
|
||||
// PartLog is used in a path for logs.
|
||||
PartLog = "log"
|
||||
// PartLogin is used in a path for login.
|
||||
|
@ -77,6 +77,10 @@ const (
|
||||
AppAdminPreInstanceView = AppAdmin + AppAdminSubInstance + "/"
|
||||
// AppAdminSubInstanceView is the sub path for the admin instance view page.
|
||||
AppAdminSubInstanceView = AppAdminSubInstance + "/" + VarInstance
|
||||
// AppAdminJob is the path for the admin jobs page.
|
||||
AppAdminJob = AppAdmin + AppAdminSubJob
|
||||
// AppAdminSubJob is the sub path for the admin jobs page.
|
||||
AppAdminSubJob = "/" + PartJob
|
||||
// AppAdminStatistics is the path for the admin stats page.
|
||||
AppAdminStatistics = AppAdmin + AppAdminSubStatistics
|
||||
// AppAdminSubStatistics is the sub path for the admin stats page.
|
||||
|
@ -21,6 +21,8 @@ var (
|
||||
ReAppAdminBlockPre = regexp.MustCompile(fmt.Sprintf(`^?%s`, AppAdminBlock))
|
||||
// ReAppAdminInstancesPre matches the admin instances page.
|
||||
ReAppAdminInstancesPre = regexp.MustCompile(fmt.Sprintf(`^?%s`, AppAdminInstance))
|
||||
// ReAppAdminJobPre matches the admin jobs page.
|
||||
ReAppAdminJobPre = regexp.MustCompile(fmt.Sprintf(`^?%s`, AppAdminJob))
|
||||
// ReAppAdminStatisticsPre matches the admin statistics page.
|
||||
ReAppAdminStatisticsPre = regexp.MustCompile(fmt.Sprintf(`^?%s`, AppAdminStatistics))
|
||||
|
||||
@ -28,8 +30,6 @@ var (
|
||||
ReAppBlocks = regexp.MustCompile(fmt.Sprintf(`^?%s$`, AppBlocks))
|
||||
// ReAppHome matches the Home page.
|
||||
ReAppHome = regexp.MustCompile(fmt.Sprintf(`^?%s$`, AppHome))
|
||||
// ReAppLog matches the Log page.
|
||||
ReAppLog = regexp.MustCompile(fmt.Sprintf(`^?%s$`, AppLog))
|
||||
// ReAppSettings matches the settings page.
|
||||
ReAppSettings = regexp.MustCompile(fmt.Sprintf(`^?%s$`, AppSettings))
|
||||
)
|
||||
|
@ -1,12 +1,13 @@
|
||||
package faktory
|
||||
|
||||
const (
|
||||
JobDeliverActivity = "DeliverActivity"
|
||||
JobInboxActivity = "InboxActivity"
|
||||
JobProcessBlockAdd = "ProcessBlockAdd"
|
||||
JobProcessBlockDelete = "ProcessBlockDelete"
|
||||
JobProcessBlockUpdate = "ProcessBlockUpdate"
|
||||
JobSendNotification = "SendNotification"
|
||||
JobDeliverActivity = "DeliverActivity"
|
||||
JobInboxActivity = "InboxActivity"
|
||||
JobMaintDeliveryErrorTimeout = "MaintDeliveryErrorTimeout"
|
||||
JobProcessBlockAdd = "ProcessBlockAdd"
|
||||
JobProcessBlockDelete = "ProcessBlockDelete"
|
||||
JobProcessBlockUpdate = "ProcessBlockUpdate"
|
||||
JobSendNotification = "SendNotification"
|
||||
|
||||
QueueDefault = "default" // medium
|
||||
QueueDelivery = "delivery" // low
|
||||
|
34
internal/runner/faktory/maintenance.go
Normal file
34
internal/runner/faktory/maintenance.go
Normal file
@ -0,0 +1,34 @@
|
||||
package faktory
|
||||
|
||||
import (
|
||||
"context"
|
||||
faktory "github.com/contribsys/faktory/client"
|
||||
worker "github.com/contribsys/faktory_worker_go"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (r *Runner) EnqueueMaintDeliveryErrorTimeout(_ context.Context) error {
|
||||
job := faktory.NewJob(JobMaintDeliveryErrorTimeout)
|
||||
job.Args = []interface{}{}
|
||||
|
||||
client, err := r.manager.Pool.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.Push(job)
|
||||
}
|
||||
|
||||
func (r *Runner) maintDeliveryErrorTimeout(ctx context.Context, args ...interface{}) error {
|
||||
help := worker.HelperFor(ctx)
|
||||
|
||||
l := logger.WithFields(logrus.Fields{
|
||||
"func": "maintDeliveryErrorTimeout",
|
||||
"jid": help.Jid(),
|
||||
})
|
||||
|
||||
if len(args) != 0 {
|
||||
l.Errorf("wrong number of arguments, got: %d, want: %d", len(args), 0)
|
||||
}
|
||||
|
||||
return r.logic.MaintDeliveryErrorTimeout(ctx, help.Jid())
|
||||
}
|
@ -32,6 +32,7 @@ func New(l logic.Logic) (*Runner, error) {
|
||||
mgr.Use(newRunner.Middleware)
|
||||
mgr.Register(JobDeliverActivity, newRunner.deliverActivity)
|
||||
mgr.Register(JobInboxActivity, newRunner.inboxActivity)
|
||||
mgr.Register(JobMaintDeliveryErrorTimeout, newRunner.maintDeliveryErrorTimeout)
|
||||
mgr.Register(JobProcessBlockAdd, newRunner.processBlockAdd)
|
||||
mgr.Register(JobProcessBlockDelete, newRunner.processBlockDelete)
|
||||
mgr.Register(JobProcessBlockUpdate, newRunner.processBlockUpdate)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
type Runner interface {
|
||||
EnqueueInboxActivity(ctx context.Context, instanceID int64, actorIRI string, activity fedihelper.Activity) (err error)
|
||||
EnqueueDeliverActivity(ctx context.Context, instanceID int64, activity fedihelper.Activity) (err error)
|
||||
EnqueueMaintDeliveryErrorTimeout(ctx context.Context) (err error)
|
||||
EnqueueProcessBlockAdd(ctx context.Context, blockID int64) (err error)
|
||||
EnqueueProcessBlockDelete(ctx context.Context, blockID int64) (err error)
|
||||
EnqueueProcessBlockUpdate(ctx context.Context, blockID int64) (err error)
|
||||
|
5
internal/scheduler/const.go
Normal file
5
internal/scheduler/const.go
Normal file
@ -0,0 +1,5 @@
|
||||
package scheduler
|
||||
|
||||
const (
|
||||
TagMaintDeliveryErrorTimeout = "MaintDeliveryErrorTimeout"
|
||||
)
|
9
internal/scheduler/logger.go
Normal file
9
internal/scheduler/logger.go
Normal file
@ -0,0 +1,9 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"github.com/feditools/relay/internal/log"
|
||||
)
|
||||
|
||||
type empty struct{}
|
||||
|
||||
var logger = log.WithPackageField(empty{})
|
11
internal/scheduler/maintenance.go
Normal file
11
internal/scheduler/maintenance.go
Normal file
@ -0,0 +1,11 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func (m *Module) maintDeliveryErrorTimeout() {
|
||||
if err := m.runner.EnqueueMaintDeliveryErrorTimeout(context.Background()); err != nil {
|
||||
logger.WithField("func", "maintDeliveryErrorTimeout").Error(err.Error())
|
||||
}
|
||||
}
|
37
internal/scheduler/module.go
Normal file
37
internal/scheduler/module.go
Normal file
@ -0,0 +1,37 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"github.com/feditools/relay/internal/runner"
|
||||
"github.com/go-co-op/gocron"
|
||||
"time"
|
||||
)
|
||||
|
||||
func New(r runner.Runner) (*Module, error) {
|
||||
m := &Module{
|
||||
runner: r,
|
||||
scheduler: gocron.NewScheduler(time.UTC),
|
||||
}
|
||||
|
||||
m.scheduler.TagsUnique()
|
||||
m.scheduler.SingletonModeAll()
|
||||
|
||||
_, err := m.scheduler.Every(1).Day().At("01:00").Tag(TagMaintDeliveryErrorTimeout).Do(m.maintDeliveryErrorTimeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
runner runner.Runner
|
||||
scheduler *gocron.Scheduler
|
||||
}
|
||||
|
||||
func (m *Module) Jobs() []*gocron.Job {
|
||||
return m.scheduler.Jobs()
|
||||
}
|
||||
|
||||
func (m *Module) Start() {
|
||||
m.scheduler.StartBlocking()
|
||||
}
|
50
test/fake_metrics_timeout.sh
Executable file
50
test/fake_metrics_timeout.sh
Executable file
@ -0,0 +1,50 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
METRIC_MAX=500
|
||||
|
||||
KEY_DELIVERY_S="relay:metrics:deliver:s:"
|
||||
KEY_DELIVERY_S_TOTAL="relay:metrics:deliver:st:"
|
||||
KEY_DELIVERY_E="relay:metrics:deliver:e:"
|
||||
KEY_DELIVERY_E_TOTAL="relay:metrics:deliver:et:"
|
||||
KEY_RECEIVED="relay:metrics:received:"
|
||||
KEY_RECEIVED_TOTAL="relay:metrics:receivedt:"
|
||||
|
||||
for day in {0..30}; do
|
||||
#echo $day
|
||||
|
||||
DATE1=$(date -v -${day}d +"%Y:%m:%d")
|
||||
|
||||
DELIVERY_S_TOTAL=0
|
||||
DELIVERY_E_TOTAL=0
|
||||
RECEIVED_TOTAL=0
|
||||
|
||||
for instance in {2..2}; do
|
||||
DELIVERY_S=$((1 + $RANDOM % ${METRIC_MAX}))
|
||||
|
||||
DELIVERY_S_TOTAL=$(expr ${DELIVERY_S_TOTAL} + ${DELIVERY_S})
|
||||
|
||||
docker exec -it relay_redis_1 redis-cli --pass test HSET ${KEY_DELIVERY_S}${DATE1} ${instance} ${DELIVERY_S}
|
||||
docker exec -it relay_redis_1 redis-cli --pass test HSET ${KEY_DELIVERY_E}${DATE1} ${instance} 0
|
||||
done
|
||||
|
||||
for instance in {3..3}; do
|
||||
DELIVERY_E=$((1 + $RANDOM % ${METRIC_MAX}))
|
||||
|
||||
DELIVERY_E_TOTAL=$(expr ${DELIVERY_E_TOTAL} + ${DELIVERY_E})
|
||||
|
||||
docker exec -it relay_redis_1 redis-cli --pass test HSET ${KEY_DELIVERY_S}${DATE1} ${instance} 0
|
||||
docker exec -it relay_redis_1 redis-cli --pass test HSET ${KEY_DELIVERY_E}${DATE1} ${instance} ${DELIVERY_E}
|
||||
done
|
||||
|
||||
for instance in {2..3}; do
|
||||
RECEIVED=$((1 + $RANDOM % ${METRIC_MAX}))
|
||||
|
||||
RECEIVED_TOTAL=$(expr ${RECEIVED_TOTAL} + ${RECEIVED})
|
||||
|
||||
docker exec -it relay_redis_1 redis-cli --pass test HSET ${KEY_RECEIVED}${DATE1} ${instance} ${RECEIVED}
|
||||
done
|
||||
|
||||
docker exec -it relay_redis_1 redis-cli --pass test SET ${KEY_DELIVERY_S_TOTAL}${DATE1} ${DELIVERY_S_TOTAL}
|
||||
docker exec -it relay_redis_1 redis-cli --pass test SET ${KEY_DELIVERY_E_TOTAL}${DATE1} ${DELIVERY_E_TOTAL}
|
||||
docker exec -it relay_redis_1 redis-cli --pass test SET ${KEY_RECEIVED_TOTAL}${DATE1} ${RECEIVED_TOTAL}
|
||||
done
|
19
vendor/github.com/go-co-op/gocron/.gitignore
generated
vendored
Normal file
19
vendor/github.com/go-co-op/gocron/.gitignore
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
local_testing
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
vendor/
|
||||
|
||||
# IDE project files
|
||||
.idea
|
49
vendor/github.com/go-co-op/gocron/.golangci.yaml
generated
vendored
Normal file
49
vendor/github.com/go-co-op/gocron/.golangci.yaml
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
run:
|
||||
timeout: 2m
|
||||
issues-exit-code: 1
|
||||
tests: true
|
||||
|
||||
issues:
|
||||
max-same-issues: 100
|
||||
exclude-rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- bodyclose
|
||||
- errcheck
|
||||
- gosec
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- bodyclose
|
||||
- deadcode
|
||||
- errcheck
|
||||
- gofmt
|
||||
- revive
|
||||
- gosec
|
||||
- gosimple
|
||||
- govet
|
||||
- ineffassign
|
||||
- misspell
|
||||
- staticcheck
|
||||
- structcheck
|
||||
- typecheck
|
||||
- unused
|
||||
- varcheck
|
||||
|
||||
output:
|
||||
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
|
||||
format: colored-line-number
|
||||
# print lines of code with issue, default is true
|
||||
print-issued-lines: true
|
||||
# print linter name in the end of issue text, default is true
|
||||
print-linter-name: true
|
||||
# make issues output unique by line, default is true
|
||||
uniq-by-line: true
|
||||
# add a prefix to the output file references; default is no prefix
|
||||
path-prefix: ""
|
||||
# sorts results by: filepath, line and column
|
||||
sort-results: true
|
||||
|
||||
linters-settings:
|
||||
golint:
|
||||
min-confidence: 0.8
|
73
vendor/github.com/go-co-op/gocron/CODE_OF_CONDUCT.md
generated
vendored
Normal file
73
vendor/github.com/go-co-op/gocron/CODE_OF_CONDUCT.md
generated
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone. And we mean everyone!
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and kind language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team initially on Slack to coordinate private communication. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see
|
||||
https://www.contributor-covenant.org/faq
|
40
vendor/github.com/go-co-op/gocron/CONTRIBUTING.md
generated
vendored
Normal file
40
vendor/github.com/go-co-op/gocron/CONTRIBUTING.md
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
# Contributing to gocron
|
||||
|
||||
Thank you for coming to contribute to gocron! We welcome new ideas, PRs and general feedback.
|
||||
|
||||
## Reporting Bugs
|
||||
|
||||
If you find a bug then please let the project know by opening an issue after doing the following:
|
||||
|
||||
- Do a quick search of the existing issues to make sure the bug isn't already reported
|
||||
- Try and make a minimal list of steps that can reliably reproduce the bug you are experiencing
|
||||
- Collect as much information as you can to help identify what the issue is (project version, configuration files, etc)
|
||||
|
||||
## Suggesting Enhancements
|
||||
|
||||
If you have a use case that you don't see a way to support yet, we would welcome the feedback in an issue. Before opening the issue, please consider:
|
||||
|
||||
- Is this a common use case?
|
||||
- Is it simple to understand?
|
||||
|
||||
You can help us out by doing the following before raising a new issue:
|
||||
|
||||
- Check that the feature hasn't been requested already by searching existing issues
|
||||
- Try and reduce your enhancement into a single, concise and deliverable request, rather than a general idea
|
||||
- Explain your own use cases as the basis of the request
|
||||
|
||||
## Adding Features
|
||||
|
||||
Pull requests are always welcome. However, before going through the trouble of implementing a change it's worth creating a bug or feature request issue.
|
||||
This allows us to discuss the changes and make sure they are a good fit for the project.
|
||||
|
||||
Please always make sure a pull request has been:
|
||||
|
||||
- Unit tested with `make test`
|
||||
- Linted with `make lint`
|
||||
- Vetted with `make vet`
|
||||
- Formatted with `make fmt` or validated with `make check-fmt`
|
||||
|
||||
## Writing Tests
|
||||
|
||||
Tests should follow the [table driven test pattern](https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go). See other tests in the code base for additional examples.
|
21
vendor/github.com/go-co-op/gocron/LICENSE
generated
vendored
Normal file
21
vendor/github.com/go-co-op/gocron/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2014, 辣椒面
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
12
vendor/github.com/go-co-op/gocron/Makefile
generated
vendored
Normal file
12
vendor/github.com/go-co-op/gocron/Makefile
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
.PHONY: fmt check-fmt lint vet test
|
||||
|
||||
GO_PKGS := $(shell go list -f {{.Dir}} ./...)
|
||||
|
||||
fmt:
|
||||
@go list -f {{.Dir}} ./... | xargs -I{} gofmt -w -s {}
|
||||
|
||||
lint:
|
||||
@golangci-lint run
|
||||
|
||||
test:
|
||||
@go test -race -v $(GO_FLAGS) -count=1 $(GO_PKGS)
|
132
vendor/github.com/go-co-op/gocron/README.md
generated
vendored
Normal file
132
vendor/github.com/go-co-op/gocron/README.md
generated
vendored
Normal file
@ -0,0 +1,132 @@
|
||||
# gocron: A Golang Job Scheduling Package.
|
||||
|
||||
[![CI State](https://github.com/go-co-op/gocron/workflows/Go%20Test/badge.svg)](https://github.com/go-co-op/gocron/actions?query=workflow%3A"lint") ![Go Report Card](https://goreportcard.com/badge/github.com/go-co-op/gocron) [![Go Doc](https://godoc.org/github.com/go-co-op/gocron?status.svg)](https://pkg.go.dev/github.com/go-co-op/gocron)
|
||||
|
||||
gocron is a job scheduling package which lets you run Go functions at pre-determined intervals using a simple, human-friendly syntax.
|
||||
|
||||
gocron is a Golang scheduler implementation similar to the Ruby module [clockwork](https://github.com/tomykaira/clockwork) and the Python job scheduling package [schedule](https://github.com/dbader/schedule).
|
||||
|
||||
See also these two great articles that were used for design input:
|
||||
|
||||
- [Rethinking Cron](http://adam.herokuapp.com/past/2010/4/13/rethinking_cron/)
|
||||
- [Replace Cron with Clockwork](http://adam.herokuapp.com/past/2010/6/30/replace_cron_with_clockwork/)
|
||||
|
||||
If you want to chat, you can find us at Slack! [<img src="https://img.shields.io/badge/gophers-gocron-brightgreen?logo=slack">](https://gophers.slack.com/archives/CQ7T0T1FW)
|
||||
|
||||
## Concepts
|
||||
|
||||
- **Scheduler**: The scheduler tracks all the jobs assigned to it and makes sure they are passed to the executor when ready to be run. The scheduler is able to manage overall aspects of job behavior like limiting how many jobs are running at one time.
|
||||
- **Job**: The job is simply aware of the task (go function) it's provided and is therefore only able to perform actions related to that task like preventing itself from overruning a previous task that is taking a long time.
|
||||
- **Executor**: The executor, as it's name suggests, is simply responsible for calling the task (go function) that the job hands to it when sent by the scheduler.
|
||||
|
||||
## Examples
|
||||
|
||||
```golang
|
||||
s := gocron.NewScheduler(time.UTC)
|
||||
|
||||
s.Every(5).Seconds().Do(func(){ ... })
|
||||
|
||||
// strings parse to duration
|
||||
s.Every("5m").Do(func(){ ... })
|
||||
|
||||
s.Every(5).Days().Do(func(){ ... })
|
||||
|
||||
s.Every(1).Month(1, 2, 3).Do(func(){ ... })
|
||||
|
||||
// set time
|
||||
s.Every(1).Day().At("10:30").Do(func(){ ... })
|
||||
|
||||
// set multiple times
|
||||
s.Every(1).Day().At("10:30;08:00").Do(func(){ ... })
|
||||
|
||||
s.Every(1).Day().At("10:30").At("08:00").Do(func(){ ... })
|
||||
|
||||
// Schedule each last day of the month
|
||||
s.Every(1).MonthLastDay().Do(func(){ ... })
|
||||
|
||||
// Or each last day of every other month
|
||||
s.Every(2).MonthLastDay().Do(func(){ ... })
|
||||
|
||||
// cron expressions supported
|
||||
s.Cron("*/1 * * * *").Do(task) // every minute
|
||||
|
||||
// you can start running the scheduler in two different ways:
|
||||
// starts the scheduler asynchronously
|
||||
s.StartAsync()
|
||||
// starts the scheduler and blocks current execution path
|
||||
s.StartBlocking()
|
||||
```
|
||||
|
||||
For more examples, take a look in our [go docs](https://pkg.go.dev/github.com/go-co-op/gocron#pkg-examples)
|
||||
|
||||
## Options
|
||||
|
||||
| Interval | Supported schedule options |
|
||||
| ------------ | ------------------------------------------------------------------- |
|
||||
| sub-second | `StartAt()` |
|
||||
| milliseconds | `StartAt()` |
|
||||
| seconds | `StartAt()` |
|
||||
| minutes | `StartAt()` |
|
||||
| hours | `StartAt()` |
|
||||
| days | `StartAt()`, `At()` |
|
||||
| weeks | `StartAt()`, `At()`, `Weekday()` (and all week day named functions) |
|
||||
| months | `StartAt()`, `At()` |
|
||||
|
||||
There are several options available to restrict how jobs run:
|
||||
|
||||
| Mode | Function | Behavior |
|
||||
| --------------- | ------------------------ | ------------------------------------------------------------------------------- |
|
||||
| Default | | jobs are rescheduled at every interval |
|
||||
| Job singleton | `SingletonMode()` | a long running job will not be rescheduled until the current run is completed |
|
||||
| Scheduler limit | `SetMaxConcurrentJobs()` | set a collective maximum number of concurrent jobs running across the scheduler |
|
||||
|
||||
## Tags
|
||||
|
||||
Jobs may have arbitrary tags added which can be useful when tracking many jobs.
|
||||
The scheduler supports both enforcing tags to be unique and when not unique,
|
||||
running all jobs with a given tag.
|
||||
|
||||
```golang
|
||||
s := gocron.NewScheduler(time.UTC)
|
||||
s.TagsUnique()
|
||||
|
||||
_, _ = s.Every(1).Week().Tag("foo").Do(task)
|
||||
_, err := s.Every(1).Week().Tag("foo").Do(task)
|
||||
// error!!!
|
||||
|
||||
s := gocron.NewScheduler(time.UTC)
|
||||
|
||||
s.Every(2).Day().Tag("tag").At("10:00").Do(task)
|
||||
s.Every(1).Minute().Tag("tag").Do(task)
|
||||
s.RunByTag("tag")
|
||||
// both jobs will run
|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
- Q: I'm running multiple pods on a distributed environment. How can I make a job not run once per pod causing duplication?
|
||||
- A: We recommend using your own lock solution within the jobs themselves (you could use [Redis](https://redis.io/topics/distlock), for example)
|
||||
|
||||
- Q: I've removed my job from the scheduler, but how can I stop a long-running job that has already been triggered?
|
||||
- A: We recommend using a means of canceling your job, e.g. a `context.WithCancel()`.
|
||||
|
||||
---
|
||||
|
||||
Looking to contribute? Try to follow these guidelines:
|
||||
|
||||
- Use issues for everything
|
||||
- For a small change, just send a PR!
|
||||
- For bigger changes, please open an issue for discussion before sending a PR.
|
||||
- PRs should have: tests, documentation and examples (if it makes sense)
|
||||
- You can also contribute by:
|
||||
- Reporting issues
|
||||
- Suggesting new features or enhancements
|
||||
- Improving/fixing documentation
|
||||
|
||||
---
|
||||
|
||||
## Design
|
||||
|
||||
![design-diagram](https://user-images.githubusercontent.com/19351306/110375142-2ba88680-8017-11eb-80c3-554cc746b165.png)
|
||||
|
||||
[Jetbrains](https://www.jetbrains.com/?from=gocron) supports this project with GoLand licenses. We appreciate their support for free and open source software!
|
15
vendor/github.com/go-co-op/gocron/SECURITY.md
generated
vendored
Normal file
15
vendor/github.com/go-co-op/gocron/SECURITY.md
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
The current plan is to maintain version 1 as long as possible incorporating any necessary security patches.
|
||||
|
||||
| Version | Supported |
|
||||
| ------- | ------------------ |
|
||||
| 1.x.x | :white_check_mark: |
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Vulnerabilities can be reported by [opening an issue](https://github.com/go-co-op/gocron/issues/new/choose) or reaching out on Slack: [<img src="https://img.shields.io/badge/gophers-gocron-brightgreen?logo=slack">](https://gophers.slack.com/archives/CQ7T0T1FW)
|
||||
|
||||
We will do our best to addrerss any vulnerabilites in an expeditious manner.
|
126
vendor/github.com/go-co-op/gocron/executor.go
generated
vendored
Normal file
126
vendor/github.com/go-co-op/gocron/executor.go
generated
vendored
Normal file
@ -0,0 +1,126 @@
|
||||
package gocron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/sync/semaphore"
|
||||
)
|
||||
|
||||
const (
|
||||
// RescheduleMode - the default is that if a limit on maximum
|
||||
// concurrent jobs is set and the limit is reached, a job will
|
||||
// skip it's run and try again on the next occurrence in the schedule
|
||||
RescheduleMode limitMode = iota
|
||||
|
||||
// WaitMode - if a limit on maximum concurrent jobs is set
|
||||
// and the limit is reached, a job will wait to try and run
|
||||
// until a spot in the limit is freed up.
|
||||
//
|
||||
// Note: this mode can produce unpredictable results as
|
||||
// job execution order isn't guaranteed. For example, a job that
|
||||
// executes frequently may pile up in the wait queue and be executed
|
||||
// many times back to back when the queue opens.
|
||||
WaitMode
|
||||
)
|
||||
|
||||
type executor struct {
|
||||
jobFunctions chan jobFunction
|
||||
stopCh chan struct{}
|
||||
limitMode limitMode
|
||||
maxRunningJobs *semaphore.Weighted
|
||||
}
|
||||
|
||||
func newExecutor() executor {
|
||||
return executor{
|
||||
jobFunctions: make(chan jobFunction, 1),
|
||||
stopCh: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *executor) start() {
|
||||
stopCtx, cancel := context.WithCancel(context.Background())
|
||||
runningJobsWg := sync.WaitGroup{}
|
||||
|
||||
for {
|
||||
select {
|
||||
case f := <-e.jobFunctions:
|
||||
runningJobsWg.Add(1)
|
||||
go func() {
|
||||
panicHandlerMutex.RLock()
|
||||
defer panicHandlerMutex.RUnlock()
|
||||
|
||||
if panicHandler != nil {
|
||||
defer func() {
|
||||
if r := recover(); r != interface{}(nil) {
|
||||
panicHandler(f.name, r)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
defer runningJobsWg.Done()
|
||||
|
||||
if e.maxRunningJobs != nil {
|
||||
if !e.maxRunningJobs.TryAcquire(1) {
|
||||
|
||||
switch e.limitMode {
|
||||
case RescheduleMode:
|
||||
return
|
||||
case WaitMode:
|
||||
for {
|
||||
select {
|
||||
case <-stopCtx.Done():
|
||||
return
|
||||
case <-f.ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
if e.maxRunningJobs.TryAcquire(1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defer e.maxRunningJobs.Release(1)
|
||||
}
|
||||
|
||||
runJob := func() {
|
||||
f.incrementRunState()
|
||||
callJobFunc(f.eventListeners.onBeforeJobExecution)
|
||||
callJobFuncWithParams(f.function, f.parameters)
|
||||
callJobFunc(f.eventListeners.onAfterJobExecution)
|
||||
f.decrementRunState()
|
||||
}
|
||||
|
||||
switch f.runConfig.mode {
|
||||
case defaultMode:
|
||||
runJob()
|
||||
case singletonMode:
|
||||
_, _, _ = f.limiter.Do("main", func() (interface{}, error) {
|
||||
select {
|
||||
case <-stopCtx.Done():
|
||||
return nil, nil
|
||||
case <-f.ctx.Done():
|
||||
return nil, nil
|
||||
default:
|
||||
}
|
||||
runJob()
|
||||
return nil, nil
|
||||
})
|
||||
}
|
||||
}()
|
||||
case <-e.stopCh:
|
||||
cancel()
|
||||
runningJobsWg.Wait()
|
||||
e.stopCh <- struct{}{}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *executor) stop() {
|
||||
e.stopCh <- struct{}{}
|
||||
<-e.stopCh
|
||||
}
|
130
vendor/github.com/go-co-op/gocron/gocron.go
generated
vendored
Normal file
130
vendor/github.com/go-co-op/gocron/gocron.go
generated
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
// Package gocron : A Golang Job Scheduling Package.
|
||||
//
|
||||
// An in-process scheduler for periodic jobs that uses the builder pattern
|
||||
// for configuration. gocron lets you run Golang functions periodically
|
||||
// at pre-determined intervals using a simple, human-friendly syntax.
|
||||
//
|
||||
package gocron
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PanicHandlerFunc represents a type that can be set to handle panics occurring
|
||||
// during job execution.
|
||||
type PanicHandlerFunc func(jobName string, recoverData interface{})
|
||||
|
||||
// The global panic handler
|
||||
var (
|
||||
panicHandler PanicHandlerFunc
|
||||
panicHandlerMutex = sync.RWMutex{}
|
||||
)
|
||||
|
||||
// SetPanicHandler sets the global panicHandler to the given function.
|
||||
// Leaving it nil or setting it to nil disables automatic panic handling.
|
||||
// If the panicHandler is not nil, any panic that occurs during executing a job will be recovered
|
||||
// and the panicHandlerFunc will be called with the job's name and the recover data.
|
||||
func SetPanicHandler(handler PanicHandlerFunc) {
|
||||
panicHandlerMutex.Lock()
|
||||
defer panicHandlerMutex.Unlock()
|
||||
panicHandler = handler
|
||||
}
|
||||
|
||||
// Error declarations for gocron related errors
|
||||
var (
|
||||
ErrNotAFunction = errors.New("only functions can be scheduled into the job queue")
|
||||
ErrNotScheduledWeekday = errors.New("job not scheduled weekly on a weekday")
|
||||
ErrJobNotFoundWithTag = errors.New("no jobs found with given tag")
|
||||
ErrUnsupportedTimeFormat = errors.New("the given time format is not supported")
|
||||
ErrInvalidInterval = errors.New(".Every() interval must be greater than 0")
|
||||
ErrInvalidIntervalType = errors.New(".Every() interval must be int, time.Duration, or string")
|
||||
ErrInvalidIntervalUnitsSelection = errors.New(".Every(time.Duration) and .Cron() cannot be used with units (e.g. .Seconds())")
|
||||
ErrInvalidFunctionParameters = errors.New("length of function parameters must match job function parameters")
|
||||
|
||||
ErrAtTimeNotSupported = errors.New("the At() method is not supported for this time unit")
|
||||
ErrWeekdayNotSupported = errors.New("weekday is not supported for time unit")
|
||||
ErrInvalidDayOfMonthEntry = errors.New("only days 1 through 28 are allowed for monthly schedules")
|
||||
ErrTagsUnique = func(tag string) error { return fmt.Errorf("a non-unique tag was set on the job: %s", tag) }
|
||||
ErrWrongParams = errors.New("wrong list of params")
|
||||
ErrDoWithJobDetails = errors.New("DoWithJobDetails expects a function whose last parameter is a gocron.Job")
|
||||
ErrUpdateCalledWithoutJob = errors.New("a call to Scheduler.Update() requires a call to Scheduler.Job() first")
|
||||
ErrCronParseFailure = errors.New("cron expression failed to be parsed")
|
||||
ErrInvalidDaysOfMonthDuplicateValue = errors.New("duplicate days of month is not allowed in Month() and Months() methods")
|
||||
)
|
||||
|
||||
func wrapOrError(toWrap error, err error) error {
|
||||
var returnErr error
|
||||
if toWrap != nil && !errors.Is(err, toWrap) {
|
||||
returnErr = fmt.Errorf("%s: %w", err, toWrap)
|
||||
} else {
|
||||
returnErr = err
|
||||
}
|
||||
return returnErr
|
||||
}
|
||||
|
||||
// regex patterns for supported time formats
|
||||
var (
|
||||
timeWithSeconds = regexp.MustCompile(`(?m)^\d{1,2}:\d\d:\d\d$`)
|
||||
timeWithoutSeconds = regexp.MustCompile(`(?m)^\d{1,2}:\d\d$`)
|
||||
)
|
||||
|
||||
type schedulingUnit int
|
||||
|
||||
const (
|
||||
// default unit is seconds
|
||||
milliseconds schedulingUnit = iota
|
||||
seconds
|
||||
minutes
|
||||
hours
|
||||
days
|
||||
weeks
|
||||
months
|
||||
duration
|
||||
crontab
|
||||
)
|
||||
|
||||
func callJobFunc(jobFunc interface{}) {
|
||||
if jobFunc != nil {
|
||||
reflect.ValueOf(jobFunc).Call([]reflect.Value{})
|
||||
}
|
||||
}
|
||||
|
||||
func callJobFuncWithParams(jobFunc interface{}, params []interface{}) {
|
||||
f := reflect.ValueOf(jobFunc)
|
||||
if len(params) != f.Type().NumIn() {
|
||||
return
|
||||
}
|
||||
in := make([]reflect.Value, len(params))
|
||||
for k, param := range params {
|
||||
in[k] = reflect.ValueOf(param)
|
||||
}
|
||||
f.Call(in)
|
||||
}
|
||||
|
||||
func getFunctionName(fn interface{}) string {
|
||||
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
|
||||
}
|
||||
|
||||
func parseTime(t string) (hour, min, sec int, err error) {
|
||||
var timeLayout string
|
||||
switch {
|
||||
case timeWithSeconds.Match([]byte(t)):
|
||||
timeLayout = "15:04:05"
|
||||
case timeWithoutSeconds.Match([]byte(t)):
|
||||
timeLayout = "15:04"
|
||||
default:
|
||||
return 0, 0, 0, ErrUnsupportedTimeFormat
|
||||
}
|
||||
|
||||
parsedTime, err := time.Parse(timeLayout, t)
|
||||
if err != nil {
|
||||
return 0, 0, 0, ErrUnsupportedTimeFormat
|
||||
}
|
||||
return parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(), nil
|
||||
}
|
478
vendor/github.com/go-co-op/gocron/job.go
generated
vendored
Normal file
478
vendor/github.com/go-co-op/gocron/job.go
generated
vendored
Normal file
@ -0,0 +1,478 @@
|
||||
package gocron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
|
||||
// Job struct stores the information necessary to run a Job
|
||||
type Job struct {
|
||||
mu *jobMutex
|
||||
jobFunction
|
||||
interval int // interval * unit between runs
|
||||
random // details for randomness
|
||||
duration time.Duration // time duration between runs
|
||||
unit schedulingUnit // time units, e.g. 'minutes', 'hours'...
|
||||
startsImmediately bool // if the Job should run upon scheduler start
|
||||
atTimes []time.Duration // optional time(s) at which this Job runs when interval is day
|
||||
startAtTime time.Time // optional time at which the Job starts
|
||||
error error // error related to Job
|
||||
lastRun time.Time // datetime of last run
|
||||
nextRun time.Time // datetime of next run
|
||||
scheduledWeekdays []time.Weekday // Specific days of the week to start on
|
||||
daysOfTheMonth []int // Specific days of the month to run the job
|
||||
tags []string // allow the user to tag Jobs with certain labels
|
||||
runCount int // number of times the job ran
|
||||
timer *time.Timer // handles running tasks at specific time
|
||||
cronSchedule cron.Schedule // stores the schedule when a task uses cron
|
||||
runWithDetails bool // when true the job is passed as the last arg of the jobFunc
|
||||
}
|
||||
|
||||
type random struct {
|
||||
rand *rand.Rand
|
||||
randomizeInterval bool // whether the interval is random
|
||||
randomIntervalRange [2]int // random interval range
|
||||
}
|
||||
|
||||
type jobFunction struct {
|
||||
eventListeners // additional functions to allow run 'em during job performing
|
||||
function interface{} // task's function
|
||||
parameters []interface{} // task's function parameters
|
||||
parametersLen int // length of the passed parameters
|
||||
name string //nolint the function name to run
|
||||
runConfig runConfig // configuration for how many times to run the job
|
||||
limiter *singleflight.Group // limits inflight runs of job to one
|
||||
ctx context.Context // for cancellation
|
||||
cancel context.CancelFunc // for cancellation
|
||||
runState *int64 // will be non-zero when jobs are running
|
||||
}
|
||||
|
||||
type eventListeners struct {
|
||||
onBeforeJobExecution interface{} // performs before job executing
|
||||
onAfterJobExecution interface{} // performs after job executing
|
||||
}
|
||||
|
||||
type jobMutex struct {
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (jf *jobFunction) incrementRunState() {
|
||||
if jf.runState != nil {
|
||||
atomic.AddInt64(jf.runState, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func (jf *jobFunction) decrementRunState() {
|
||||
if jf.runState != nil {
|
||||
atomic.AddInt64(jf.runState, -1)
|
||||
}
|
||||
}
|
||||
|
||||
func (jf *jobFunction) copy() jobFunction {
|
||||
cp := jobFunction{
|
||||
eventListeners: jf.eventListeners,
|
||||
function: jf.function,
|
||||
parameters: nil,
|
||||
parametersLen: jf.parametersLen,
|
||||
name: jf.name,
|
||||
runConfig: jf.runConfig,
|
||||
limiter: jf.limiter,
|
||||
ctx: jf.ctx,
|
||||
cancel: jf.cancel,
|
||||
runState: jf.runState,
|
||||
}
|
||||
cp.parameters = append(cp.parameters, jf.parameters...)
|
||||
return cp
|
||||
}
|
||||
|
||||
type runConfig struct {
|
||||
finiteRuns bool
|
||||
maxRuns int
|
||||
mode mode
|
||||
}
|
||||
|
||||
// mode is the Job's running mode
|
||||
type mode int8
|
||||
|
||||
const (
|
||||
// defaultMode disable any mode
|
||||
defaultMode mode = iota
|
||||
|
||||
// singletonMode switch to single job mode
|
||||
singletonMode
|
||||
)
|
||||
|
||||
// newJob creates a new Job with the provided interval
|
||||
func newJob(interval int, startImmediately bool, singletonMode bool) *Job {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var zero int64
|
||||
job := &Job{
|
||||
mu: &jobMutex{},
|
||||
interval: interval,
|
||||
unit: seconds,
|
||||
lastRun: time.Time{},
|
||||
nextRun: time.Time{},
|
||||
jobFunction: jobFunction{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
runState: &zero,
|
||||
},
|
||||
tags: []string{},
|
||||
startsImmediately: startImmediately,
|
||||
}
|
||||
if singletonMode {
|
||||
job.SingletonMode()
|
||||
}
|
||||
return job
|
||||
}
|
||||
|
||||
func (j *Job) setRandomInterval(a, b int) {
|
||||
j.random.rand = rand.New(rand.NewSource(time.Now().UnixNano())) // nolint
|
||||
|
||||
j.random.randomizeInterval = true
|
||||
if a < b {
|
||||
j.random.randomIntervalRange[0] = a
|
||||
j.random.randomIntervalRange[1] = b + 1
|
||||
} else {
|
||||
j.random.randomIntervalRange[0] = b
|
||||
j.random.randomIntervalRange[1] = a + 1
|
||||
}
|
||||
}
|
||||
|
||||
func (j *Job) getRandomInterval() int {
|
||||
randNum := j.rand.Intn(j.randomIntervalRange[1] - j.randomIntervalRange[0])
|
||||
return j.randomIntervalRange[0] + randNum
|
||||
}
|
||||
|
||||
func (j *Job) getInterval() int {
|
||||
if j.randomizeInterval {
|
||||
return j.getRandomInterval()
|
||||
}
|
||||
return j.interval
|
||||
}
|
||||
|
||||
func (j *Job) neverRan() bool {
|
||||
jobLastRun := j.LastRun()
|
||||
return jobLastRun.IsZero()
|
||||
}
|
||||
|
||||
func (j *Job) getStartsImmediately() bool {
|
||||
return j.startsImmediately
|
||||
}
|
||||
|
||||
func (j *Job) setStartsImmediately(b bool) {
|
||||
j.startsImmediately = b
|
||||
}
|
||||
|
||||
func (j *Job) setTimer(t *time.Timer) {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
j.timer = t
|
||||
}
|
||||
|
||||
func (j *Job) getFirstAtTime() time.Duration {
|
||||
var t time.Duration
|
||||
if len(j.atTimes) > 0 {
|
||||
t = j.atTimes[0]
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (j *Job) getAtTime(lastRun time.Time) time.Duration {
|
||||
var r time.Duration
|
||||
if len(j.atTimes) == 0 {
|
||||
return r
|
||||
}
|
||||
|
||||
if len(j.atTimes) == 1 {
|
||||
return j.atTimes[0]
|
||||
}
|
||||
|
||||
if lastRun.IsZero() {
|
||||
r = j.atTimes[0]
|
||||
} else {
|
||||
for _, d := range j.atTimes {
|
||||
nt := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day(), 0, 0, 0, 0, lastRun.Location()).Add(d)
|
||||
if nt.After(lastRun) {
|
||||
r = d
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (j *Job) addAtTime(t time.Duration) {
|
||||
if len(j.atTimes) == 0 {
|
||||
j.atTimes = append(j.atTimes, t)
|
||||
return
|
||||
}
|
||||
exist := false
|
||||
index := sort.Search(len(j.atTimes), func(i int) bool {
|
||||
atTime := j.atTimes[i]
|
||||
b := atTime >= t
|
||||
if b {
|
||||
exist = atTime == t
|
||||
}
|
||||
return b
|
||||
})
|
||||
|
||||
// ignore if present
|
||||
if exist {
|
||||
return
|
||||
}
|
||||
|
||||
j.atTimes = append(j.atTimes, time.Duration(0))
|
||||
copy(j.atTimes[index+1:], j.atTimes[index:])
|
||||
j.atTimes[index] = t
|
||||
}
|
||||
|
||||
func (j *Job) getStartAtTime() time.Time {
|
||||
return j.startAtTime
|
||||
}
|
||||
|
||||
func (j *Job) setStartAtTime(t time.Time) {
|
||||
j.startAtTime = t
|
||||
}
|
||||
|
||||
func (j *Job) getUnit() schedulingUnit {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.unit
|
||||
}
|
||||
|
||||
func (j *Job) setUnit(t schedulingUnit) {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
j.unit = t
|
||||
}
|
||||
|
||||
func (j *Job) getDuration() time.Duration {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.duration
|
||||
}
|
||||
|
||||
func (j *Job) setDuration(t time.Duration) {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
j.duration = t
|
||||
}
|
||||
|
||||
// hasTags returns true if all tags are matched on this Job
|
||||
func (j *Job) hasTags(tags ...string) bool {
|
||||
// Build map of all Job tags for easy comparison
|
||||
jobTags := map[string]int{}
|
||||
for _, tag := range j.tags {
|
||||
jobTags[tag] = 0
|
||||
}
|
||||
|
||||
// Loop through required tags and if one doesn't exist, return false
|
||||
for _, tag := range tags {
|
||||
_, ok := jobTags[tag]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Error returns an error if one occurred while creating the Job.
|
||||
// If multiple errors occurred, they will be wrapped and can be
|
||||
// checked using the standard unwrap options.
|
||||
func (j *Job) Error() error {
|
||||
return j.error
|
||||
}
|
||||
|
||||
// Tag allows you to add arbitrary labels to a Job that do not
|
||||
// impact the functionality of the Job
|
||||
func (j *Job) Tag(tags ...string) {
|
||||
j.tags = append(j.tags, tags...)
|
||||
}
|
||||
|
||||
// Untag removes a tag from a Job
|
||||
func (j *Job) Untag(t string) {
|
||||
var newTags []string
|
||||
for _, tag := range j.tags {
|
||||
if t != tag {
|
||||
newTags = append(newTags, tag)
|
||||
}
|
||||
}
|
||||
|
||||
j.tags = newTags
|
||||
}
|
||||
|
||||
// Tags returns the tags attached to the Job
|
||||
func (j *Job) Tags() []string {
|
||||
return j.tags
|
||||
}
|
||||
|
||||
// SetEventListeners accepts two functions that will be called, one before and one after the job is run
|
||||
func (j *Job) SetEventListeners(onBeforeJobExecution interface{}, onAfterJobExecution interface{}) {
|
||||
j.eventListeners = eventListeners{
|
||||
onBeforeJobExecution: onBeforeJobExecution,
|
||||
onAfterJobExecution: onAfterJobExecution,
|
||||
}
|
||||
}
|
||||
|
||||
// ScheduledTime returns the time of the Job's next scheduled run
|
||||
func (j *Job) ScheduledTime() time.Time {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.nextRun
|
||||
}
|
||||
|
||||
// ScheduledAtTime returns the specific time of day the Job will run at.
|
||||
// If multiple times are set, the earliest time will be returned.
|
||||
func (j *Job) ScheduledAtTime() string {
|
||||
if len(j.atTimes) == 0 {
|
||||
return "0:0"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%d:%d", j.getFirstAtTime()/time.Hour, (j.getFirstAtTime()%time.Hour)/time.Minute)
|
||||
}
|
||||
|
||||
// ScheduledAtTimes returns the specific times of day the Job will run at
|
||||
func (j *Job) ScheduledAtTimes() []string {
|
||||
r := make([]string, len(j.atTimes))
|
||||
for i, t := range j.atTimes {
|
||||
r[i] = fmt.Sprintf("%d:%d", t/time.Hour, (t%time.Hour)/time.Minute)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// Weekday returns which day of the week the Job will run on and
|
||||
// will return an error if the Job is not scheduled weekly
|
||||
func (j *Job) Weekday() (time.Weekday, error) {
|
||||
if len(j.scheduledWeekdays) == 0 {
|
||||
return time.Sunday, ErrNotScheduledWeekday
|
||||
}
|
||||
return j.scheduledWeekdays[0], nil
|
||||
}
|
||||
|
||||
// Weekdays returns a slice of time.Weekday that the Job will run in a week and
|
||||
// will return an error if the Job is not scheduled weekly
|
||||
func (j *Job) Weekdays() []time.Weekday {
|
||||
// appending on j.scheduledWeekdays may cause a side effect
|
||||
if len(j.scheduledWeekdays) == 0 {
|
||||
return []time.Weekday{time.Sunday}
|
||||
}
|
||||
|
||||
return j.scheduledWeekdays
|
||||
}
|
||||
|
||||
// LimitRunsTo limits the number of executions of this job to n.
|
||||
// Upon reaching the limit, the job is removed from the scheduler.
|
||||
//
|
||||
// Note: If a job is added to a running scheduler and this method is then used
|
||||
// you may see the job run more than the set limit as job is scheduled immediately
|
||||
// by default upon being added to the scheduler. It is recommended to use the
|
||||
// LimitRunsTo() func on the scheduler chain when scheduling the job.
|
||||
// For example: scheduler.LimitRunsTo(1).Do()
|
||||
func (j *Job) LimitRunsTo(n int) {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
j.runConfig.finiteRuns = true
|
||||
j.runConfig.maxRuns = n
|
||||
}
|
||||
|
||||
// SingletonMode prevents a new job from starting if the prior job has not yet
|
||||
// completed it's run
|
||||
// Note: If a job is added to a running scheduler and this method is then used
|
||||
// you may see the job run overrun itself as job is scheduled immediately
|
||||
// by default upon being added to the scheduler. It is recommended to use the
|
||||
// SingletonMode() func on the scheduler chain when scheduling the job.
|
||||
func (j *Job) SingletonMode() {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
j.runConfig.mode = singletonMode
|
||||
j.jobFunction.limiter = &singleflight.Group{}
|
||||
}
|
||||
|
||||
// shouldRun evaluates if this job should run again
|
||||
// based on the runConfig
|
||||
func (j *Job) shouldRun() bool {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return !j.runConfig.finiteRuns || j.runCount < j.runConfig.maxRuns
|
||||
}
|
||||
|
||||
// LastRun returns the time the job was run last
|
||||
func (j *Job) LastRun() time.Time {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.lastRun
|
||||
}
|
||||
|
||||
func (j *Job) setLastRun(t time.Time) {
|
||||
j.lastRun = t
|
||||
}
|
||||
|
||||
// NextRun returns the time the job will run next
|
||||
func (j *Job) NextRun() time.Time {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.nextRun
|
||||
}
|
||||
|
||||
func (j *Job) setNextRun(t time.Time) {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
j.nextRun = t
|
||||
}
|
||||
|
||||
// RunCount returns the number of time the job ran so far
|
||||
func (j *Job) RunCount() int {
|
||||
return j.runCount
|
||||
}
|
||||
|
||||
func (j *Job) stop() {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
if j.timer != nil {
|
||||
j.timer.Stop()
|
||||
}
|
||||
if j.cancel != nil {
|
||||
j.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// IsRunning reports whether any instances of the job function are currently running
|
||||
func (j *Job) IsRunning() bool {
|
||||
return atomic.LoadInt64(j.runState) != 0
|
||||
}
|
||||
|
||||
// you must lock the job before calling copy
|
||||
func (j *Job) copy() Job {
|
||||
return Job{
|
||||
mu: &jobMutex{},
|
||||
jobFunction: j.jobFunction,
|
||||
interval: j.interval,
|
||||
duration: j.duration,
|
||||
unit: j.unit,
|
||||
startsImmediately: j.startsImmediately,
|
||||
atTimes: j.atTimes,
|
||||
startAtTime: j.startAtTime,
|
||||
error: j.error,
|
||||
lastRun: j.lastRun,
|
||||
nextRun: j.nextRun,
|
||||
scheduledWeekdays: j.scheduledWeekdays,
|
||||
daysOfTheMonth: j.daysOfTheMonth,
|
||||
tags: j.tags,
|
||||
runCount: j.runCount,
|
||||
timer: j.timer,
|
||||
cronSchedule: j.cronSchedule,
|
||||
runWithDetails: j.runWithDetails,
|
||||
}
|
||||
}
|
1312
vendor/github.com/go-co-op/gocron/scheduler.go
generated
vendored
Normal file
1312
vendor/github.com/go-co-op/gocron/scheduler.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
27
vendor/github.com/go-co-op/gocron/timeHelper.go
generated
vendored
Normal file
27
vendor/github.com/go-co-op/gocron/timeHelper.go
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
package gocron
|
||||
|
||||
import "time"
|
||||
|
||||
var _ TimeWrapper = (*trueTime)(nil)
|
||||
|
||||
// TimeWrapper is an interface that wraps the Now, Sleep, and Unix methods of the time package.
|
||||
// This allows the library and users to mock the time package for testing.
|
||||
type TimeWrapper interface {
|
||||
Now(*time.Location) time.Time
|
||||
Unix(int64, int64) time.Time
|
||||
Sleep(time.Duration)
|
||||
}
|
||||
|
||||
type trueTime struct{}
|
||||
|
||||
func (t *trueTime) Now(location *time.Location) time.Time {
|
||||
return time.Now().In(location)
|
||||
}
|
||||
|
||||
func (t *trueTime) Unix(sec int64, nsec int64) time.Time {
|
||||
return time.Unix(sec, nsec)
|
||||
}
|
||||
|
||||
func (t *trueTime) Sleep(d time.Duration) {
|
||||
time.Sleep(d)
|
||||
}
|
22
vendor/github.com/robfig/cron/v3/.gitignore
generated
vendored
Normal file
22
vendor/github.com/robfig/cron/v3/.gitignore
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
1
vendor/github.com/robfig/cron/v3/.travis.yml
generated
vendored
Normal file
1
vendor/github.com/robfig/cron/v3/.travis.yml
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
language: go
|
21
vendor/github.com/robfig/cron/v3/LICENSE
generated
vendored
Normal file
21
vendor/github.com/robfig/cron/v3/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
Copyright (C) 2012 Rob Figueiredo
|
||||
All Rights Reserved.
|
||||
|
||||
MIT LICENSE
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
125
vendor/github.com/robfig/cron/v3/README.md
generated
vendored
Normal file
125
vendor/github.com/robfig/cron/v3/README.md
generated
vendored
Normal file
@ -0,0 +1,125 @@
|
||||
[![GoDoc](http://godoc.org/github.com/robfig/cron?status.png)](http://godoc.org/github.com/robfig/cron)
|
||||
[![Build Status](https://travis-ci.org/robfig/cron.svg?branch=master)](https://travis-ci.org/robfig/cron)
|
||||
|
||||
# cron
|
||||
|
||||
Cron V3 has been released!
|
||||
|
||||
To download the specific tagged release, run:
|
||||
|
||||
go get github.com/robfig/cron/v3@v3.0.0
|
||||
|
||||
Import it in your program as:
|
||||
|
||||
import "github.com/robfig/cron/v3"
|
||||
|
||||
It requires Go 1.11 or later due to usage of Go Modules.
|
||||
|
||||
Refer to the documentation here:
|
||||
http://godoc.org/github.com/robfig/cron
|
||||
|
||||
The rest of this document describes the the advances in v3 and a list of
|
||||
breaking changes for users that wish to upgrade from an earlier version.
|
||||
|
||||
## Upgrading to v3 (June 2019)
|
||||
|
||||
cron v3 is a major upgrade to the library that addresses all outstanding bugs,
|
||||
feature requests, and rough edges. It is based on a merge of master which
|
||||
contains various fixes to issues found over the years and the v2 branch which
|
||||
contains some backwards-incompatible features like the ability to remove cron
|
||||
jobs. In addition, v3 adds support for Go Modules, cleans up rough edges like
|
||||
the timezone support, and fixes a number of bugs.
|
||||
|
||||
New features:
|
||||
|
||||
- Support for Go modules. Callers must now import this library as
|
||||
`github.com/robfig/cron/v3`, instead of `gopkg.in/...`
|
||||
|
||||
- Fixed bugs:
|
||||
- 0f01e6b parser: fix combining of Dow and Dom (#70)
|
||||
- dbf3220 adjust times when rolling the clock forward to handle non-existent midnight (#157)
|
||||
- eeecf15 spec_test.go: ensure an error is returned on 0 increment (#144)
|
||||
- 70971dc cron.Entries(): update request for snapshot to include a reply channel (#97)
|
||||
- 1cba5e6 cron: fix: removing a job causes the next scheduled job to run too late (#206)
|
||||
|
||||
- Standard cron spec parsing by default (first field is "minute"), with an easy
|
||||
way to opt into the seconds field (quartz-compatible). Although, note that the
|
||||
year field (optional in Quartz) is not supported.
|
||||
|
||||
- Extensible, key/value logging via an interface that complies with
|
||||
the https://github.com/go-logr/logr project.
|
||||
|
||||
- The new Chain & JobWrapper types allow you to install "interceptors" to add
|
||||
cross-cutting behavior like the following:
|
||||
- Recover any panics from jobs
|
||||
- Delay a job's execution if the previous run hasn't completed yet
|
||||
- Skip a job's execution if the previous run hasn't completed yet
|
||||
- Log each job's invocations
|
||||
- Notification when jobs are completed
|
||||
|
||||
It is backwards incompatible with both v1 and v2. These updates are required:
|
||||
|
||||
- The v1 branch accepted an optional seconds field at the beginning of the cron
|
||||
spec. This is non-standard and has led to a lot of confusion. The new default
|
||||
parser conforms to the standard as described by [the Cron wikipedia page].
|
||||
|
||||
UPDATING: To retain the old behavior, construct your Cron with a custom
|
||||
parser:
|
||||
|
||||
// Seconds field, required
|
||||
cron.New(cron.WithSeconds())
|
||||
|
||||
// Seconds field, optional
|
||||
cron.New(
|
||||
cron.WithParser(
|
||||
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor))
|
||||
|
||||
- The Cron type now accepts functional options on construction rather than the
|
||||
previous ad-hoc behavior modification mechanisms (setting a field, calling a setter).
|
||||
|
||||
UPDATING: Code that sets Cron.ErrorLogger or calls Cron.SetLocation must be
|
||||
updated to provide those values on construction.
|
||||
|
||||
- CRON_TZ is now the recommended way to specify the timezone of a single
|
||||
schedule, which is sanctioned by the specification. The legacy "TZ=" prefix
|
||||
will continue to be supported since it is unambiguous and easy to do so.
|
||||
|
||||
UPDATING: No update is required.
|
||||
|
||||
- By default, cron will no longer recover panics in jobs that it runs.
|
||||
Recovering can be surprising (see issue #192) and seems to be at odds with
|
||||
typical behavior of libraries. Relatedly, the `cron.WithPanicLogger` option
|
||||
has been removed to accommodate the more general JobWrapper type.
|
||||
|
||||
UPDATING: To opt into panic recovery and configure the panic logger:
|
||||
|
||||
cron.New(cron.WithChain(
|
||||
cron.Recover(logger), // or use cron.DefaultLogger
|
||||
))
|
||||
|
||||
- In adding support for https://github.com/go-logr/logr, `cron.WithVerboseLogger` was
|
||||
removed, since it is duplicative with the leveled logging.
|
||||
|
||||
UPDATING: Callers should use `WithLogger` and specify a logger that does not
|
||||
discard `Info` logs. For convenience, one is provided that wraps `*log.Logger`:
|
||||
|
||||
cron.New(
|
||||
cron.WithLogger(cron.VerbosePrintfLogger(logger)))
|
||||
|
||||
|
||||
### Background - Cron spec format
|
||||
|
||||
There are two cron spec formats in common usage:
|
||||
|
||||
- The "standard" cron format, described on [the Cron wikipedia page] and used by
|
||||
the cron Linux system utility.
|
||||
|
||||
- The cron format used by [the Quartz Scheduler], commonly used for scheduled
|
||||
jobs in Java software
|
||||
|
||||
[the Cron wikipedia page]: https://en.wikipedia.org/wiki/Cron
|
||||
[the Quartz Scheduler]: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-06.html
|
||||
|
||||
The original version of this package included an optional "seconds" field, which
|
||||
made it incompatible with both of these formats. Now, the "standard" format is
|
||||
the default format accepted, and the Quartz format is opt-in.
|
92
vendor/github.com/robfig/cron/v3/chain.go
generated
vendored
Normal file
92
vendor/github.com/robfig/cron/v3/chain.go
generated
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// JobWrapper decorates the given Job with some behavior.
|
||||
type JobWrapper func(Job) Job
|
||||
|
||||
// Chain is a sequence of JobWrappers that decorates submitted jobs with
|
||||
// cross-cutting behaviors like logging or synchronization.
|
||||
type Chain struct {
|
||||
wrappers []JobWrapper
|
||||
}
|
||||
|
||||
// NewChain returns a Chain consisting of the given JobWrappers.
|
||||
func NewChain(c ...JobWrapper) Chain {
|
||||
return Chain{c}
|
||||
}
|
||||
|
||||
// Then decorates the given job with all JobWrappers in the chain.
|
||||
//
|
||||
// This:
|
||||
// NewChain(m1, m2, m3).Then(job)
|
||||
// is equivalent to:
|
||||
// m1(m2(m3(job)))
|
||||
func (c Chain) Then(j Job) Job {
|
||||
for i := range c.wrappers {
|
||||
j = c.wrappers[len(c.wrappers)-i-1](j)
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
// Recover panics in wrapped jobs and log them with the provided logger.
|
||||
func Recover(logger Logger) JobWrapper {
|
||||
return func(j Job) Job {
|
||||
return FuncJob(func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
const size = 64 << 10
|
||||
buf := make([]byte, size)
|
||||
buf = buf[:runtime.Stack(buf, false)]
|
||||
err, ok := r.(error)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%v", r)
|
||||
}
|
||||
logger.Error(err, "panic", "stack", "...\n"+string(buf))
|
||||
}
|
||||
}()
|
||||
j.Run()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// DelayIfStillRunning serializes jobs, delaying subsequent runs until the
|
||||
// previous one is complete. Jobs running after a delay of more than a minute
|
||||
// have the delay logged at Info.
|
||||
func DelayIfStillRunning(logger Logger) JobWrapper {
|
||||
return func(j Job) Job {
|
||||
var mu sync.Mutex
|
||||
return FuncJob(func() {
|
||||
start := time.Now()
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if dur := time.Since(start); dur > time.Minute {
|
||||
logger.Info("delay", "duration", dur)
|
||||
}
|
||||
j.Run()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// SkipIfStillRunning skips an invocation of the Job if a previous invocation is
|
||||
// still running. It logs skips to the given logger at Info level.
|
||||
func SkipIfStillRunning(logger Logger) JobWrapper {
|
||||
return func(j Job) Job {
|
||||
var ch = make(chan struct{}, 1)
|
||||
ch <- struct{}{}
|
||||
return FuncJob(func() {
|
||||
select {
|
||||
case v := <-ch:
|
||||
j.Run()
|
||||
ch <- v
|
||||
default:
|
||||
logger.Info("skip")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
27
vendor/github.com/robfig/cron/v3/constantdelay.go
generated
vendored
Normal file
27
vendor/github.com/robfig/cron/v3/constantdelay.go
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
package cron
|
||||
|
||||
import "time"
|
||||
|
||||
// ConstantDelaySchedule represents a simple recurring duty cycle, e.g. "Every 5 minutes".
|
||||
// It does not support jobs more frequent than once a second.
|
||||
type ConstantDelaySchedule struct {
|
||||
Delay time.Duration
|
||||
}
|
||||
|
||||
// Every returns a crontab Schedule that activates once every duration.
|
||||
// Delays of less than a second are not supported (will round up to 1 second).
|
||||
// Any fields less than a Second are truncated.
|
||||
func Every(duration time.Duration) ConstantDelaySchedule {
|
||||
if duration < time.Second {
|
||||
duration = time.Second
|
||||
}
|
||||
return ConstantDelaySchedule{
|
||||
Delay: duration - time.Duration(duration.Nanoseconds())%time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// Next returns the next time this should be run.
|
||||
// This rounds so that the next activation time will be on the second.
|
||||
func (schedule ConstantDelaySchedule) Next(t time.Time) time.Time {
|
||||
return t.Add(schedule.Delay - time.Duration(t.Nanosecond())*time.Nanosecond)
|
||||
}
|
355
vendor/github.com/robfig/cron/v3/cron.go
generated
vendored
Normal file
355
vendor/github.com/robfig/cron/v3/cron.go
generated
vendored
Normal file
@ -0,0 +1,355 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cron keeps track of any number of entries, invoking the associated func as
|
||||
// specified by the schedule. It may be started, stopped, and the entries may
|
||||
// be inspected while running.
|
||||
type Cron struct {
|
||||
entries []*Entry
|
||||
chain Chain
|
||||
stop chan struct{}
|
||||
add chan *Entry
|
||||
remove chan EntryID
|
||||
snapshot chan chan []Entry
|
||||
running bool
|
||||
logger Logger
|
||||
runningMu sync.Mutex
|
||||
location *time.Location
|
||||
parser ScheduleParser
|
||||
nextID EntryID
|
||||
jobWaiter sync.WaitGroup
|
||||
}
|
||||
|
||||
// ScheduleParser is an interface for schedule spec parsers that return a Schedule
|
||||
type ScheduleParser interface {
|
||||
Parse(spec string) (Schedule, error)
|
||||
}
|
||||
|
||||
// Job is an interface for submitted cron jobs.
|
||||
type Job interface {
|
||||
Run()
|
||||
}
|
||||
|
||||
// Schedule describes a job's duty cycle.
|
||||
type Schedule interface {
|
||||
// Next returns the next activation time, later than the given time.
|
||||
// Next is invoked initially, and then each time the job is run.
|
||||
Next(time.Time) time.Time
|
||||
}
|
||||
|
||||
// EntryID identifies an entry within a Cron instance
|
||||
type EntryID int
|
||||
|
||||
// Entry consists of a schedule and the func to execute on that schedule.
|
||||
type Entry struct {
|
||||
// ID is the cron-assigned ID of this entry, which may be used to look up a
|
||||
// snapshot or remove it.
|
||||
ID EntryID
|
||||
|
||||
// Schedule on which this job should be run.
|
||||
Schedule Schedule
|
||||
|
||||
// Next time the job will run, or the zero time if Cron has not been
|
||||
// started or this entry's schedule is unsatisfiable
|
||||
Next time.Time
|
||||
|
||||
// Prev is the last time this job was run, or the zero time if never.
|
||||
Prev time.Time
|
||||
|
||||
// WrappedJob is the thing to run when the Schedule is activated.
|
||||
WrappedJob Job
|
||||
|
||||
// Job is the thing that was submitted to cron.
|
||||
// It is kept around so that user code that needs to get at the job later,
|
||||
// e.g. via Entries() can do so.
|
||||
Job Job
|
||||
}
|
||||
|
||||
// Valid returns true if this is not the zero entry.
|
||||
func (e Entry) Valid() bool { return e.ID != 0 }
|
||||
|
||||
// byTime is a wrapper for sorting the entry array by time
|
||||
// (with zero time at the end).
|
||||
type byTime []*Entry
|
||||
|
||||
func (s byTime) Len() int { return len(s) }
|
||||
func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s byTime) Less(i, j int) bool {
|
||||
// Two zero times should return false.
|
||||
// Otherwise, zero is "greater" than any other time.
|
||||
// (To sort it at the end of the list.)
|
||||
if s[i].Next.IsZero() {
|
||||
return false
|
||||
}
|
||||
if s[j].Next.IsZero() {
|
||||
return true
|
||||
}
|
||||
return s[i].Next.Before(s[j].Next)
|
||||
}
|
||||
|
||||
// New returns a new Cron job runner, modified by the given options.
|
||||
//
|
||||
// Available Settings
|
||||
//
|
||||
// Time Zone
|
||||
// Description: The time zone in which schedules are interpreted
|
||||
// Default: time.Local
|
||||
//
|
||||
// Parser
|
||||
// Description: Parser converts cron spec strings into cron.Schedules.
|
||||
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
|
||||
//
|
||||
// Chain
|
||||
// Description: Wrap submitted jobs to customize behavior.
|
||||
// Default: A chain that recovers panics and logs them to stderr.
|
||||
//
|
||||
// See "cron.With*" to modify the default behavior.
|
||||
func New(opts ...Option) *Cron {
|
||||
c := &Cron{
|
||||
entries: nil,
|
||||
chain: NewChain(),
|
||||
add: make(chan *Entry),
|
||||
stop: make(chan struct{}),
|
||||
snapshot: make(chan chan []Entry),
|
||||
remove: make(chan EntryID),
|
||||
running: false,
|
||||
runningMu: sync.Mutex{},
|
||||
logger: DefaultLogger,
|
||||
location: time.Local,
|
||||
parser: standardParser,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// FuncJob is a wrapper that turns a func() into a cron.Job
|
||||
type FuncJob func()
|
||||
|
||||
func (f FuncJob) Run() { f() }
|
||||
|
||||
// AddFunc adds a func to the Cron to be run on the given schedule.
|
||||
// The spec is parsed using the time zone of this Cron instance as the default.
|
||||
// An opaque ID is returned that can be used to later remove it.
|
||||
func (c *Cron) AddFunc(spec string, cmd func()) (EntryID, error) {
|
||||
return c.AddJob(spec, FuncJob(cmd))
|
||||
}
|
||||
|
||||
// AddJob adds a Job to the Cron to be run on the given schedule.
|
||||
// The spec is parsed using the time zone of this Cron instance as the default.
|
||||
// An opaque ID is returned that can be used to later remove it.
|
||||
func (c *Cron) AddJob(spec string, cmd Job) (EntryID, error) {
|
||||
schedule, err := c.parser.Parse(spec)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return c.Schedule(schedule, cmd), nil
|
||||
}
|
||||
|
||||
// Schedule adds a Job to the Cron to be run on the given schedule.
|
||||
// The job is wrapped with the configured Chain.
|
||||
func (c *Cron) Schedule(schedule Schedule, cmd Job) EntryID {
|
||||
c.runningMu.Lock()
|
||||
defer c.runningMu.Unlock()
|
||||
c.nextID++
|
||||
entry := &Entry{
|
||||
ID: c.nextID,
|
||||
Schedule: schedule,
|
||||
WrappedJob: c.chain.Then(cmd),
|
||||
Job: cmd,
|
||||
}
|
||||
if !c.running {
|
||||
c.entries = append(c.entries, entry)
|
||||
} else {
|
||||
c.add <- entry
|
||||
}
|
||||
return entry.ID
|
||||
}
|
||||
|
||||
// Entries returns a snapshot of the cron entries.
|
||||
func (c *Cron) Entries() []Entry {
|
||||
c.runningMu.Lock()
|
||||
defer c.runningMu.Unlock()
|
||||
if c.running {
|
||||
replyChan := make(chan []Entry, 1)
|
||||
c.snapshot <- replyChan
|
||||
return <-replyChan
|
||||
}
|
||||
return c.entrySnapshot()
|
||||
}
|
||||
|
||||
// Location gets the time zone location
|
||||
func (c *Cron) Location() *time.Location {
|
||||
return c.location
|
||||
}
|
||||
|
||||
// Entry returns a snapshot of the given entry, or nil if it couldn't be found.
|
||||
func (c *Cron) Entry(id EntryID) Entry {
|
||||
for _, entry := range c.Entries() {
|
||||
if id == entry.ID {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
return Entry{}
|
||||
}
|
||||
|
||||
// Remove an entry from being run in the future.
|
||||
func (c *Cron) Remove(id EntryID) {
|
||||
c.runningMu.Lock()
|
||||
defer c.runningMu.Unlock()
|
||||
if c.running {
|
||||
c.remove <- id
|
||||
} else {
|
||||
c.removeEntry(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Start the cron scheduler in its own goroutine, or no-op if already started.
|
||||
func (c *Cron) Start() {
|
||||
c.runningMu.Lock()
|
||||
defer c.runningMu.Unlock()
|
||||
if c.running {
|
||||
return
|
||||
}
|
||||
c.running = true
|
||||
go c.run()
|
||||
}
|
||||
|
||||
// Run the cron scheduler, or no-op if already running.
|
||||
func (c *Cron) Run() {
|
||||
c.runningMu.Lock()
|
||||
if c.running {
|
||||
c.runningMu.Unlock()
|
||||
return
|
||||
}
|
||||
c.running = true
|
||||
c.runningMu.Unlock()
|
||||
c.run()
|
||||
}
|
||||
|
||||
// run the scheduler.. this is private just due to the need to synchronize
|
||||
// access to the 'running' state variable.
|
||||
func (c *Cron) run() {
|
||||
c.logger.Info("start")
|
||||
|
||||
// Figure out the next activation times for each entry.
|
||||
now := c.now()
|
||||
for _, entry := range c.entries {
|
||||
entry.Next = entry.Schedule.Next(now)
|
||||
c.logger.Info("schedule", "now", now, "entry", entry.ID, "next", entry.Next)
|
||||
}
|
||||
|
||||
for {
|
||||
// Determine the next entry to run.
|
||||
sort.Sort(byTime(c.entries))
|
||||
|
||||
var timer *time.Timer
|
||||
if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
|
||||
// If there are no entries yet, just sleep - it still handles new entries
|
||||
// and stop requests.
|
||||
timer = time.NewTimer(100000 * time.Hour)
|
||||
} else {
|
||||
timer = time.NewTimer(c.entries[0].Next.Sub(now))
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case now = <-timer.C:
|
||||
now = now.In(c.location)
|
||||
c.logger.Info("wake", "now", now)
|
||||
|
||||
// Run every entry whose next time was less than now
|
||||
for _, e := range c.entries {
|
||||
if e.Next.After(now) || e.Next.IsZero() {
|
||||
break
|
||||
}
|
||||
c.startJob(e.WrappedJob)
|
||||
e.Prev = e.Next
|
||||
e.Next = e.Schedule.Next(now)
|
||||
c.logger.Info("run", "now", now, "entry", e.ID, "next", e.Next)
|
||||
}
|
||||
|
||||
case newEntry := <-c.add:
|
||||
timer.Stop()
|
||||
now = c.now()
|
||||
newEntry.Next = newEntry.Schedule.Next(now)
|
||||
c.entries = append(c.entries, newEntry)
|
||||
c.logger.Info("added", "now", now, "entry", newEntry.ID, "next", newEntry.Next)
|
||||
|
||||
case replyChan := <-c.snapshot:
|
||||
replyChan <- c.entrySnapshot()
|
||||
continue
|
||||
|
||||
case <-c.stop:
|
||||
timer.Stop()
|
||||
c.logger.Info("stop")
|
||||
return
|
||||
|
||||
case id := <-c.remove:
|
||||
timer.Stop()
|
||||
now = c.now()
|
||||
c.removeEntry(id)
|
||||
c.logger.Info("removed", "entry", id)
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// startJob runs the given job in a new goroutine.
|
||||
func (c *Cron) startJob(j Job) {
|
||||
c.jobWaiter.Add(1)
|
||||
go func() {
|
||||
defer c.jobWaiter.Done()
|
||||
j.Run()
|
||||
}()
|
||||
}
|
||||
|
||||
// now returns current time in c location
|
||||
func (c *Cron) now() time.Time {
|
||||
return time.Now().In(c.location)
|
||||
}
|
||||
|
||||
// Stop stops the cron scheduler if it is running; otherwise it does nothing.
|
||||
// A context is returned so the caller can wait for running jobs to complete.
|
||||
func (c *Cron) Stop() context.Context {
|
||||
c.runningMu.Lock()
|
||||
defer c.runningMu.Unlock()
|
||||
if c.running {
|
||||
c.stop <- struct{}{}
|
||||
c.running = false
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
c.jobWaiter.Wait()
|
||||
cancel()
|
||||
}()
|
||||
return ctx
|
||||
}
|
||||
|
||||
// entrySnapshot returns a copy of the current cron entry list.
|
||||
func (c *Cron) entrySnapshot() []Entry {
|
||||
var entries = make([]Entry, len(c.entries))
|
||||
for i, e := range c.entries {
|
||||
entries[i] = *e
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
func (c *Cron) removeEntry(id EntryID) {
|
||||
var entries []*Entry
|
||||
for _, e := range c.entries {
|
||||
if e.ID != id {
|
||||
entries = append(entries, e)
|
||||
}
|
||||
}
|
||||
c.entries = entries
|
||||
}
|
231
vendor/github.com/robfig/cron/v3/doc.go
generated
vendored
Normal file
231
vendor/github.com/robfig/cron/v3/doc.go
generated
vendored
Normal file
@ -0,0 +1,231 @@
|
||||
/*
|
||||
Package cron implements a cron spec parser and job runner.
|
||||
|
||||
Installation
|
||||
|
||||
To download the specific tagged release, run:
|
||||
|
||||
go get github.com/robfig/cron/v3@v3.0.0
|
||||
|
||||
Import it in your program as:
|
||||
|
||||
import "github.com/robfig/cron/v3"
|
||||
|
||||
It requires Go 1.11 or later due to usage of Go Modules.
|
||||
|
||||
Usage
|
||||
|
||||
Callers may register Funcs to be invoked on a given schedule. Cron will run
|
||||
them in their own goroutines.
|
||||
|
||||
c := cron.New()
|
||||
c.AddFunc("30 * * * *", func() { fmt.Println("Every hour on the half hour") })
|
||||
c.AddFunc("30 3-6,20-23 * * *", func() { fmt.Println(".. in the range 3-6am, 8-11pm") })
|
||||
c.AddFunc("CRON_TZ=Asia/Tokyo 30 04 * * *", func() { fmt.Println("Runs at 04:30 Tokyo time every day") })
|
||||
c.AddFunc("@hourly", func() { fmt.Println("Every hour, starting an hour from now") })
|
||||
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty, starting an hour thirty from now") })
|
||||
c.Start()
|
||||
..
|
||||
// Funcs are invoked in their own goroutine, asynchronously.
|
||||
...
|
||||
// Funcs may also be added to a running Cron
|
||||
c.AddFunc("@daily", func() { fmt.Println("Every day") })
|
||||
..
|
||||
// Inspect the cron job entries' next and previous run times.
|
||||
inspect(c.Entries())
|
||||
..
|
||||
c.Stop() // Stop the scheduler (does not stop any jobs already running).
|
||||
|
||||
CRON Expression Format
|
||||
|
||||
A cron expression represents a set of times, using 5 space-separated fields.
|
||||
|
||||
Field name | Mandatory? | Allowed values | Allowed special characters
|
||||
---------- | ---------- | -------------- | --------------------------
|
||||
Minutes | Yes | 0-59 | * / , -
|
||||
Hours | Yes | 0-23 | * / , -
|
||||
Day of month | Yes | 1-31 | * / , - ?
|
||||
Month | Yes | 1-12 or JAN-DEC | * / , -
|
||||
Day of week | Yes | 0-6 or SUN-SAT | * / , - ?
|
||||
|
||||
Month and Day-of-week field values are case insensitive. "SUN", "Sun", and
|
||||
"sun" are equally accepted.
|
||||
|
||||
The specific interpretation of the format is based on the Cron Wikipedia page:
|
||||
https://en.wikipedia.org/wiki/Cron
|
||||
|
||||
Alternative Formats
|
||||
|
||||
Alternative Cron expression formats support other fields like seconds. You can
|
||||
implement that by creating a custom Parser as follows.
|
||||
|
||||
cron.New(
|
||||
cron.WithParser(
|
||||
cron.NewParser(
|
||||
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)))
|
||||
|
||||
Since adding Seconds is the most common modification to the standard cron spec,
|
||||
cron provides a builtin function to do that, which is equivalent to the custom
|
||||
parser you saw earlier, except that its seconds field is REQUIRED:
|
||||
|
||||
cron.New(cron.WithSeconds())
|
||||
|
||||
That emulates Quartz, the most popular alternative Cron schedule format:
|
||||
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
|
||||
|
||||
Special Characters
|
||||
|
||||
Asterisk ( * )
|
||||
|
||||
The asterisk indicates that the cron expression will match for all values of the
|
||||
field; e.g., using an asterisk in the 5th field (month) would indicate every
|
||||
month.
|
||||
|
||||
Slash ( / )
|
||||
|
||||
Slashes are used to describe increments of ranges. For example 3-59/15 in the
|
||||
1st field (minutes) would indicate the 3rd minute of the hour and every 15
|
||||
minutes thereafter. The form "*\/..." is equivalent to the form "first-last/...",
|
||||
that is, an increment over the largest possible range of the field. The form
|
||||
"N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the
|
||||
increment until the end of that specific range. It does not wrap around.
|
||||
|
||||
Comma ( , )
|
||||
|
||||
Commas are used to separate items of a list. For example, using "MON,WED,FRI" in
|
||||
the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
|
||||
|
||||
Hyphen ( - )
|
||||
|
||||
Hyphens are used to define ranges. For example, 9-17 would indicate every
|
||||
hour between 9am and 5pm inclusive.
|
||||
|
||||
Question mark ( ? )
|
||||
|
||||
Question mark may be used instead of '*' for leaving either day-of-month or
|
||||
day-of-week blank.
|
||||
|
||||
Predefined schedules
|
||||
|
||||
You may use one of several pre-defined schedules in place of a cron expression.
|
||||
|
||||
Entry | Description | Equivalent To
|
||||
----- | ----------- | -------------
|
||||
@yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
|
||||
@monthly | Run once a month, midnight, first of month | 0 0 1 * *
|
||||
@weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
|
||||
@daily (or @midnight) | Run once a day, midnight | 0 0 * * *
|
||||
@hourly | Run once an hour, beginning of hour | 0 * * * *
|
||||
|
||||
Intervals
|
||||
|
||||
You may also schedule a job to execute at fixed intervals, starting at the time it's added
|
||||
or cron is run. This is supported by formatting the cron spec like this:
|
||||
|
||||
@every <duration>
|
||||
|
||||
where "duration" is a string accepted by time.ParseDuration
|
||||
(http://golang.org/pkg/time/#ParseDuration).
|
||||
|
||||
For example, "@every 1h30m10s" would indicate a schedule that activates after
|
||||
1 hour, 30 minutes, 10 seconds, and then every interval after that.
|
||||
|
||||
Note: The interval does not take the job runtime into account. For example,
|
||||
if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes,
|
||||
it will have only 2 minutes of idle time between each run.
|
||||
|
||||
Time zones
|
||||
|
||||
By default, all interpretation and scheduling is done in the machine's local
|
||||
time zone (time.Local). You can specify a different time zone on construction:
|
||||
|
||||
cron.New(
|
||||
cron.WithLocation(time.UTC))
|
||||
|
||||
Individual cron schedules may also override the time zone they are to be
|
||||
interpreted in by providing an additional space-separated field at the beginning
|
||||
of the cron spec, of the form "CRON_TZ=Asia/Tokyo".
|
||||
|
||||
For example:
|
||||
|
||||
# Runs at 6am in time.Local
|
||||
cron.New().AddFunc("0 6 * * ?", ...)
|
||||
|
||||
# Runs at 6am in America/New_York
|
||||
nyc, _ := time.LoadLocation("America/New_York")
|
||||
c := cron.New(cron.WithLocation(nyc))
|
||||
c.AddFunc("0 6 * * ?", ...)
|
||||
|
||||
# Runs at 6am in Asia/Tokyo
|
||||
cron.New().AddFunc("CRON_TZ=Asia/Tokyo 0 6 * * ?", ...)
|
||||
|
||||
# Runs at 6am in Asia/Tokyo
|
||||
c := cron.New(cron.WithLocation(nyc))
|
||||
c.SetLocation("America/New_York")
|
||||
c.AddFunc("CRON_TZ=Asia/Tokyo 0 6 * * ?", ...)
|
||||
|
||||
The prefix "TZ=(TIME ZONE)" is also supported for legacy compatibility.
|
||||
|
||||
Be aware that jobs scheduled during daylight-savings leap-ahead transitions will
|
||||
not be run!
|
||||
|
||||
Job Wrappers
|
||||
|
||||
A Cron runner may be configured with a chain of job wrappers to add
|
||||
cross-cutting functionality to all submitted jobs. For example, they may be used
|
||||
to achieve the following effects:
|
||||
|
||||
- Recover any panics from jobs (activated by default)
|
||||
- Delay a job's execution if the previous run hasn't completed yet
|
||||
- Skip a job's execution if the previous run hasn't completed yet
|
||||
- Log each job's invocations
|
||||
|
||||
Install wrappers for all jobs added to a cron using the `cron.WithChain` option:
|
||||
|
||||
cron.New(cron.WithChain(
|
||||
cron.SkipIfStillRunning(logger),
|
||||
))
|
||||
|
||||
Install wrappers for individual jobs by explicitly wrapping them:
|
||||
|
||||
job = cron.NewChain(
|
||||
cron.SkipIfStillRunning(logger),
|
||||
).Then(job)
|
||||
|
||||
Thread safety
|
||||
|
||||
Since the Cron service runs concurrently with the calling code, some amount of
|
||||
care must be taken to ensure proper synchronization.
|
||||
|
||||
All cron methods are designed to be correctly synchronized as long as the caller
|
||||
ensures that invocations have a clear happens-before ordering between them.
|
||||
|
||||
Logging
|
||||
|
||||
Cron defines a Logger interface that is a subset of the one defined in
|
||||
github.com/go-logr/logr. It has two logging levels (Info and Error), and
|
||||
parameters are key/value pairs. This makes it possible for cron logging to plug
|
||||
into structured logging systems. An adapter, [Verbose]PrintfLogger, is provided
|
||||
to wrap the standard library *log.Logger.
|
||||
|
||||
For additional insight into Cron operations, verbose logging may be activated
|
||||
which will record job runs, scheduling decisions, and added or removed jobs.
|
||||
Activate it with a one-off logger as follows:
|
||||
|
||||
cron.New(
|
||||
cron.WithLogger(
|
||||
cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))))
|
||||
|
||||
|
||||
Implementation
|
||||
|
||||
Cron entries are stored in an array, sorted by their next activation time. Cron
|
||||
sleeps until the next job is due to be run.
|
||||
|
||||
Upon waking:
|
||||
- it runs each entry that is active on that second
|
||||
- it calculates the next run times for the jobs that were run
|
||||
- it re-sorts the array of entries by next activation time.
|
||||
- it goes to sleep until the soonest job.
|
||||
*/
|
||||
package cron
|
86
vendor/github.com/robfig/cron/v3/logger.go
generated
vendored
Normal file
86
vendor/github.com/robfig/cron/v3/logger.go
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DefaultLogger is used by Cron if none is specified.
|
||||
var DefaultLogger Logger = PrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))
|
||||
|
||||
// DiscardLogger can be used by callers to discard all log messages.
|
||||
var DiscardLogger Logger = PrintfLogger(log.New(ioutil.Discard, "", 0))
|
||||
|
||||
// Logger is the interface used in this package for logging, so that any backend
|
||||
// can be plugged in. It is a subset of the github.com/go-logr/logr interface.
|
||||
type Logger interface {
|
||||
// Info logs routine messages about cron's operation.
|
||||
Info(msg string, keysAndValues ...interface{})
|
||||
// Error logs an error condition.
|
||||
Error(err error, msg string, keysAndValues ...interface{})
|
||||
}
|
||||
|
||||
// PrintfLogger wraps a Printf-based logger (such as the standard library "log")
|
||||
// into an implementation of the Logger interface which logs errors only.
|
||||
func PrintfLogger(l interface{ Printf(string, ...interface{}) }) Logger {
|
||||
return printfLogger{l, false}
|
||||
}
|
||||
|
||||
// VerbosePrintfLogger wraps a Printf-based logger (such as the standard library
|
||||
// "log") into an implementation of the Logger interface which logs everything.
|
||||
func VerbosePrintfLogger(l interface{ Printf(string, ...interface{}) }) Logger {
|
||||
return printfLogger{l, true}
|
||||
}
|
||||
|
||||
type printfLogger struct {
|
||||
logger interface{ Printf(string, ...interface{}) }
|
||||
logInfo bool
|
||||
}
|
||||
|
||||
func (pl printfLogger) Info(msg string, keysAndValues ...interface{}) {
|
||||
if pl.logInfo {
|
||||
keysAndValues = formatTimes(keysAndValues)
|
||||
pl.logger.Printf(
|
||||
formatString(len(keysAndValues)),
|
||||
append([]interface{}{msg}, keysAndValues...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func (pl printfLogger) Error(err error, msg string, keysAndValues ...interface{}) {
|
||||
keysAndValues = formatTimes(keysAndValues)
|
||||
pl.logger.Printf(
|
||||
formatString(len(keysAndValues)+2),
|
||||
append([]interface{}{msg, "error", err}, keysAndValues...)...)
|
||||
}
|
||||
|
||||
// formatString returns a logfmt-like format string for the number of
|
||||
// key/values.
|
||||
func formatString(numKeysAndValues int) string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString("%s")
|
||||
if numKeysAndValues > 0 {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
for i := 0; i < numKeysAndValues/2; i++ {
|
||||
if i > 0 {
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
sb.WriteString("%v=%v")
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// formatTimes formats any time.Time values as RFC3339.
|
||||
func formatTimes(keysAndValues []interface{}) []interface{} {
|
||||
var formattedArgs []interface{}
|
||||
for _, arg := range keysAndValues {
|
||||
if t, ok := arg.(time.Time); ok {
|
||||
arg = t.Format(time.RFC3339)
|
||||
}
|
||||
formattedArgs = append(formattedArgs, arg)
|
||||
}
|
||||
return formattedArgs
|
||||
}
|
45
vendor/github.com/robfig/cron/v3/option.go
generated
vendored
Normal file
45
vendor/github.com/robfig/cron/v3/option.go
generated
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Option represents a modification to the default behavior of a Cron.
|
||||
type Option func(*Cron)
|
||||
|
||||
// WithLocation overrides the timezone of the cron instance.
|
||||
func WithLocation(loc *time.Location) Option {
|
||||
return func(c *Cron) {
|
||||
c.location = loc
|
||||
}
|
||||
}
|
||||
|
||||
// WithSeconds overrides the parser used for interpreting job schedules to
|
||||
// include a seconds field as the first one.
|
||||
func WithSeconds() Option {
|
||||
return WithParser(NewParser(
|
||||
Second | Minute | Hour | Dom | Month | Dow | Descriptor,
|
||||
))
|
||||
}
|
||||
|
||||
// WithParser overrides the parser used for interpreting job schedules.
|
||||
func WithParser(p ScheduleParser) Option {
|
||||
return func(c *Cron) {
|
||||
c.parser = p
|
||||
}
|
||||
}
|
||||
|
||||
// WithChain specifies Job wrappers to apply to all jobs added to this cron.
|
||||
// Refer to the Chain* functions in this package for provided wrappers.
|
||||
func WithChain(wrappers ...JobWrapper) Option {
|
||||
return func(c *Cron) {
|
||||
c.chain = NewChain(wrappers...)
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger uses the provided logger.
|
||||
func WithLogger(logger Logger) Option {
|
||||
return func(c *Cron) {
|
||||
c.logger = logger
|
||||
}
|
||||
}
|
434
vendor/github.com/robfig/cron/v3/parser.go
generated
vendored
Normal file
434
vendor/github.com/robfig/cron/v3/parser.go
generated
vendored
Normal file
@ -0,0 +1,434 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Configuration options for creating a parser. Most options specify which
|
||||
// fields should be included, while others enable features. If a field is not
|
||||
// included the parser will assume a default value. These options do not change
|
||||
// the order fields are parse in.
|
||||
type ParseOption int
|
||||
|
||||
const (
|
||||
Second ParseOption = 1 << iota // Seconds field, default 0
|
||||
SecondOptional // Optional seconds field, default 0
|
||||
Minute // Minutes field, default 0
|
||||
Hour // Hours field, default 0
|
||||
Dom // Day of month field, default *
|
||||
Month // Month field, default *
|
||||
Dow // Day of week field, default *
|
||||
DowOptional // Optional day of week field, default *
|
||||
Descriptor // Allow descriptors such as @monthly, @weekly, etc.
|
||||
)
|
||||
|
||||
var places = []ParseOption{
|
||||
Second,
|
||||
Minute,
|
||||
Hour,
|
||||
Dom,
|
||||
Month,
|
||||
Dow,
|
||||
}
|
||||
|
||||
var defaults = []string{
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"*",
|
||||
"*",
|
||||
"*",
|
||||
}
|
||||
|
||||
// A custom Parser that can be configured.
|
||||
type Parser struct {
|
||||
options ParseOption
|
||||
}
|
||||
|
||||
// NewParser creates a Parser with custom options.
|
||||
//
|
||||
// It panics if more than one Optional is given, since it would be impossible to
|
||||
// correctly infer which optional is provided or missing in general.
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// // Standard parser without descriptors
|
||||
// specParser := NewParser(Minute | Hour | Dom | Month | Dow)
|
||||
// sched, err := specParser.Parse("0 0 15 */3 *")
|
||||
//
|
||||
// // Same as above, just excludes time fields
|
||||
// subsParser := NewParser(Dom | Month | Dow)
|
||||
// sched, err := specParser.Parse("15 */3 *")
|
||||
//
|
||||
// // Same as above, just makes Dow optional
|
||||
// subsParser := NewParser(Dom | Month | DowOptional)
|
||||
// sched, err := specParser.Parse("15 */3")
|
||||
//
|
||||
func NewParser(options ParseOption) Parser {
|
||||
optionals := 0
|
||||
if options&DowOptional > 0 {
|
||||
optionals++
|
||||
}
|
||||
if options&SecondOptional > 0 {
|
||||
optionals++
|
||||
}
|
||||
if optionals > 1 {
|
||||
panic("multiple optionals may not be configured")
|
||||
}
|
||||
return Parser{options}
|
||||
}
|
||||
|
||||
// Parse returns a new crontab schedule representing the given spec.
|
||||
// It returns a descriptive error if the spec is not valid.
|
||||
// It accepts crontab specs and features configured by NewParser.
|
||||
func (p Parser) Parse(spec string) (Schedule, error) {
|
||||
if len(spec) == 0 {
|
||||
return nil, fmt.Errorf("empty spec string")
|
||||
}
|
||||
|
||||
// Extract timezone if present
|
||||
var loc = time.Local
|
||||
if strings.HasPrefix(spec, "TZ=") || strings.HasPrefix(spec, "CRON_TZ=") {
|
||||
var err error
|
||||
i := strings.Index(spec, " ")
|
||||
eq := strings.Index(spec, "=")
|
||||
if loc, err = time.LoadLocation(spec[eq+1 : i]); err != nil {
|
||||
return nil, fmt.Errorf("provided bad location %s: %v", spec[eq+1:i], err)
|
||||
}
|
||||
spec = strings.TrimSpace(spec[i:])
|
||||
}
|
||||
|
||||
// Handle named schedules (descriptors), if configured
|
||||
if strings.HasPrefix(spec, "@") {
|
||||
if p.options&Descriptor == 0 {
|
||||
return nil, fmt.Errorf("parser does not accept descriptors: %v", spec)
|
||||
}
|
||||
return parseDescriptor(spec, loc)
|
||||
}
|
||||
|
||||
// Split on whitespace.
|
||||
fields := strings.Fields(spec)
|
||||
|
||||
// Validate & fill in any omitted or optional fields
|
||||
var err error
|
||||
fields, err = normalizeFields(fields, p.options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
field := func(field string, r bounds) uint64 {
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
var bits uint64
|
||||
bits, err = getField(field, r)
|
||||
return bits
|
||||
}
|
||||
|
||||
var (
|
||||
second = field(fields[0], seconds)
|
||||
minute = field(fields[1], minutes)
|
||||
hour = field(fields[2], hours)
|
||||
dayofmonth = field(fields[3], dom)
|
||||
month = field(fields[4], months)
|
||||
dayofweek = field(fields[5], dow)
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &SpecSchedule{
|
||||
Second: second,
|
||||
Minute: minute,
|
||||
Hour: hour,
|
||||
Dom: dayofmonth,
|
||||
Month: month,
|
||||
Dow: dayofweek,
|
||||
Location: loc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// normalizeFields takes a subset set of the time fields and returns the full set
|
||||
// with defaults (zeroes) populated for unset fields.
|
||||
//
|
||||
// As part of performing this function, it also validates that the provided
|
||||
// fields are compatible with the configured options.
|
||||
func normalizeFields(fields []string, options ParseOption) ([]string, error) {
|
||||
// Validate optionals & add their field to options
|
||||
optionals := 0
|
||||
if options&SecondOptional > 0 {
|
||||
options |= Second
|
||||
optionals++
|
||||
}
|
||||
if options&DowOptional > 0 {
|
||||
options |= Dow
|
||||
optionals++
|
||||
}
|
||||
if optionals > 1 {
|
||||
return nil, fmt.Errorf("multiple optionals may not be configured")
|
||||
}
|
||||
|
||||
// Figure out how many fields we need
|
||||
max := 0
|
||||
for _, place := range places {
|
||||
if options&place > 0 {
|
||||
max++
|
||||
}
|
||||
}
|
||||
min := max - optionals
|
||||
|
||||
// Validate number of fields
|
||||
if count := len(fields); count < min || count > max {
|
||||
if min == max {
|
||||
return nil, fmt.Errorf("expected exactly %d fields, found %d: %s", min, count, fields)
|
||||
}
|
||||
return nil, fmt.Errorf("expected %d to %d fields, found %d: %s", min, max, count, fields)
|
||||
}
|
||||
|
||||
// Populate the optional field if not provided
|
||||
if min < max && len(fields) == min {
|
||||
switch {
|
||||
case options&DowOptional > 0:
|
||||
fields = append(fields, defaults[5]) // TODO: improve access to default
|
||||
case options&SecondOptional > 0:
|
||||
fields = append([]string{defaults[0]}, fields...)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown optional field")
|
||||
}
|
||||
}
|
||||
|
||||
// Populate all fields not part of options with their defaults
|
||||
n := 0
|
||||
expandedFields := make([]string, len(places))
|
||||
copy(expandedFields, defaults)
|
||||
for i, place := range places {
|
||||
if options&place > 0 {
|
||||
expandedFields[i] = fields[n]
|
||||
n++
|
||||
}
|
||||
}
|
||||
return expandedFields, nil
|
||||
}
|
||||
|
||||
var standardParser = NewParser(
|
||||
Minute | Hour | Dom | Month | Dow | Descriptor,
|
||||
)
|
||||
|
||||
// ParseStandard returns a new crontab schedule representing the given
|
||||
// standardSpec (https://en.wikipedia.org/wiki/Cron). It requires 5 entries
|
||||
// representing: minute, hour, day of month, month and day of week, in that
|
||||
// order. It returns a descriptive error if the spec is not valid.
|
||||
//
|
||||
// It accepts
|
||||
// - Standard crontab specs, e.g. "* * * * ?"
|
||||
// - Descriptors, e.g. "@midnight", "@every 1h30m"
|
||||
func ParseStandard(standardSpec string) (Schedule, error) {
|
||||
return standardParser.Parse(standardSpec)
|
||||
}
|
||||
|
||||
// getField returns an Int with the bits set representing all of the times that
|
||||
// the field represents or error parsing field value. A "field" is a comma-separated
|
||||
// list of "ranges".
|
||||
func getField(field string, r bounds) (uint64, error) {
|
||||
var bits uint64
|
||||
ranges := strings.FieldsFunc(field, func(r rune) bool { return r == ',' })
|
||||
for _, expr := range ranges {
|
||||
bit, err := getRange(expr, r)
|
||||
if err != nil {
|
||||
return bits, err
|
||||
}
|
||||
bits |= bit
|
||||
}
|
||||
return bits, nil
|
||||
}
|
||||
|
||||
// getRange returns the bits indicated by the given expression:
|
||||
// number | number "-" number [ "/" number ]
|
||||
// or error parsing range.
|
||||
func getRange(expr string, r bounds) (uint64, error) {
|
||||
var (
|
||||
start, end, step uint
|
||||
rangeAndStep = strings.Split(expr, "/")
|
||||
lowAndHigh = strings.Split(rangeAndStep[0], "-")
|
||||
singleDigit = len(lowAndHigh) == 1
|
||||
err error
|
||||
)
|
||||
|
||||
var extra uint64
|
||||
if lowAndHigh[0] == "*" || lowAndHigh[0] == "?" {
|
||||
start = r.min
|
||||
end = r.max
|
||||
extra = starBit
|
||||
} else {
|
||||
start, err = parseIntOrName(lowAndHigh[0], r.names)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
switch len(lowAndHigh) {
|
||||
case 1:
|
||||
end = start
|
||||
case 2:
|
||||
end, err = parseIntOrName(lowAndHigh[1], r.names)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
default:
|
||||
return 0, fmt.Errorf("too many hyphens: %s", expr)
|
||||
}
|
||||
}
|
||||
|
||||
switch len(rangeAndStep) {
|
||||
case 1:
|
||||
step = 1
|
||||
case 2:
|
||||
step, err = mustParseInt(rangeAndStep[1])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Special handling: "N/step" means "N-max/step".
|
||||
if singleDigit {
|
||||
end = r.max
|
||||
}
|
||||
if step > 1 {
|
||||
extra = 0
|
||||
}
|
||||
default:
|
||||
return 0, fmt.Errorf("too many slashes: %s", expr)
|
||||
}
|
||||
|
||||
if start < r.min {
|
||||
return 0, fmt.Errorf("beginning of range (%d) below minimum (%d): %s", start, r.min, expr)
|
||||
}
|
||||
if end > r.max {
|
||||
return 0, fmt.Errorf("end of range (%d) above maximum (%d): %s", end, r.max, expr)
|
||||
}
|
||||
if start > end {
|
||||
return 0, fmt.Errorf("beginning of range (%d) beyond end of range (%d): %s", start, end, expr)
|
||||
}
|
||||
if step == 0 {
|
||||
return 0, fmt.Errorf("step of range should be a positive number: %s", expr)
|
||||
}
|
||||
|
||||
return getBits(start, end, step) | extra, nil
|
||||
}
|
||||
|
||||
// parseIntOrName returns the (possibly-named) integer contained in expr.
|
||||
func parseIntOrName(expr string, names map[string]uint) (uint, error) {
|
||||
if names != nil {
|
||||
if namedInt, ok := names[strings.ToLower(expr)]; ok {
|
||||
return namedInt, nil
|
||||
}
|
||||
}
|
||||
return mustParseInt(expr)
|
||||
}
|
||||
|
||||
// mustParseInt parses the given expression as an int or returns an error.
|
||||
func mustParseInt(expr string) (uint, error) {
|
||||
num, err := strconv.Atoi(expr)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to parse int from %s: %s", expr, err)
|
||||
}
|
||||
if num < 0 {
|
||||
return 0, fmt.Errorf("negative number (%d) not allowed: %s", num, expr)
|
||||
}
|
||||
|
||||
return uint(num), nil
|
||||
}
|
||||
|
||||
// getBits sets all bits in the range [min, max], modulo the given step size.
|
||||
func getBits(min, max, step uint) uint64 {
|
||||
var bits uint64
|
||||
|
||||
// If step is 1, use shifts.
|
||||
if step == 1 {
|
||||
return ^(math.MaxUint64 << (max + 1)) & (math.MaxUint64 << min)
|
||||
}
|
||||
|
||||
// Else, use a simple loop.
|
||||
for i := min; i <= max; i += step {
|
||||
bits |= 1 << i
|
||||
}
|
||||
return bits
|
||||
}
|
||||
|
||||
// all returns all bits within the given bounds. (plus the star bit)
|
||||
func all(r bounds) uint64 {
|
||||
return getBits(r.min, r.max, 1) | starBit
|
||||
}
|
||||
|
||||
// parseDescriptor returns a predefined schedule for the expression, or error if none matches.
|
||||
func parseDescriptor(descriptor string, loc *time.Location) (Schedule, error) {
|
||||
switch descriptor {
|
||||
case "@yearly", "@annually":
|
||||
return &SpecSchedule{
|
||||
Second: 1 << seconds.min,
|
||||
Minute: 1 << minutes.min,
|
||||
Hour: 1 << hours.min,
|
||||
Dom: 1 << dom.min,
|
||||
Month: 1 << months.min,
|
||||
Dow: all(dow),
|
||||
Location: loc,
|
||||
}, nil
|
||||
|
||||
case "@monthly":
|
||||
return &SpecSchedule{
|
||||
Second: 1 << seconds.min,
|
||||
Minute: 1 << minutes.min,
|
||||
Hour: 1 << hours.min,
|
||||
Dom: 1 << dom.min,
|
||||
Month: all(months),
|
||||
Dow: all(dow),
|
||||
Location: loc,
|
||||
}, nil
|
||||
|
||||
case "@weekly":
|
||||
return &SpecSchedule{
|
||||
Second: 1 << seconds.min,
|
||||
Minute: 1 << minutes.min,
|
||||
Hour: 1 << hours.min,
|
||||
Dom: all(dom),
|
||||
Month: all(months),
|
||||
Dow: 1 << dow.min,
|
||||
Location: loc,
|
||||
}, nil
|
||||
|
||||
case "@daily", "@midnight":
|
||||
return &SpecSchedule{
|
||||
Second: 1 << seconds.min,
|
||||
Minute: 1 << minutes.min,
|
||||
Hour: 1 << hours.min,
|
||||
Dom: all(dom),
|
||||
Month: all(months),
|
||||
Dow: all(dow),
|
||||
Location: loc,
|
||||
}, nil
|
||||
|
||||
case "@hourly":
|
||||
return &SpecSchedule{
|
||||
Second: 1 << seconds.min,
|
||||
Minute: 1 << minutes.min,
|
||||
Hour: all(hours),
|
||||
Dom: all(dom),
|
||||
Month: all(months),
|
||||
Dow: all(dow),
|
||||
Location: loc,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
const every = "@every "
|
||||
if strings.HasPrefix(descriptor, every) {
|
||||
duration, err := time.ParseDuration(descriptor[len(every):])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse duration %s: %s", descriptor, err)
|
||||
}
|
||||
return Every(duration), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unrecognized descriptor: %s", descriptor)
|
||||
}
|
188
vendor/github.com/robfig/cron/v3/spec.go
generated
vendored
Normal file
188
vendor/github.com/robfig/cron/v3/spec.go
generated
vendored
Normal file
@ -0,0 +1,188 @@
|
||||
package cron
|
||||
|
||||
import "time"
|
||||
|
||||
// SpecSchedule specifies a duty cycle (to the second granularity), based on a
|
||||
// traditional crontab specification. It is computed initially and stored as bit sets.
|
||||
type SpecSchedule struct {
|
||||
Second, Minute, Hour, Dom, Month, Dow uint64
|
||||
|
||||
// Override location for this schedule.
|
||||
Location *time.Location
|
||||
}
|
||||
|
||||
// bounds provides a range of acceptable values (plus a map of name to value).
|
||||
type bounds struct {
|
||||
min, max uint
|
||||
names map[string]uint
|
||||
}
|
||||
|
||||
// The bounds for each field.
|
||||
var (
|
||||
seconds = bounds{0, 59, nil}
|
||||
minutes = bounds{0, 59, nil}
|
||||
hours = bounds{0, 23, nil}
|
||||
dom = bounds{1, 31, nil}
|
||||
months = bounds{1, 12, map[string]uint{
|
||||
"jan": 1,
|
||||
"feb": 2,
|
||||
"mar": 3,
|
||||
"apr": 4,
|
||||
"may": 5,
|
||||
"jun": 6,
|
||||
"jul": 7,
|
||||
"aug": 8,
|
||||
"sep": 9,
|
||||
"oct": 10,
|
||||
"nov": 11,
|
||||
"dec": 12,
|
||||
}}
|
||||
dow = bounds{0, 6, map[string]uint{
|
||||
"sun": 0,
|
||||
"mon": 1,
|
||||
"tue": 2,
|
||||
"wed": 3,
|
||||
"thu": 4,
|
||||
"fri": 5,
|
||||
"sat": 6,
|
||||
}}
|
||||
)
|
||||
|
||||
const (
|
||||
// Set the top bit if a star was included in the expression.
|
||||
starBit = 1 << 63
|
||||
)
|
||||
|
||||
// Next returns the next time this schedule is activated, greater than the given
|
||||
// time. If no time can be found to satisfy the schedule, return the zero time.
|
||||
func (s *SpecSchedule) Next(t time.Time) time.Time {
|
||||
// General approach
|
||||
//
|
||||
// For Month, Day, Hour, Minute, Second:
|
||||
// Check if the time value matches. If yes, continue to the next field.
|
||||
// If the field doesn't match the schedule, then increment the field until it matches.
|
||||
// While incrementing the field, a wrap-around brings it back to the beginning
|
||||
// of the field list (since it is necessary to re-verify previous field
|
||||
// values)
|
||||
|
||||
// Convert the given time into the schedule's timezone, if one is specified.
|
||||
// Save the original timezone so we can convert back after we find a time.
|
||||
// Note that schedules without a time zone specified (time.Local) are treated
|
||||
// as local to the time provided.
|
||||
origLocation := t.Location()
|
||||
loc := s.Location
|
||||
if loc == time.Local {
|
||||
loc = t.Location()
|
||||
}
|
||||
if s.Location != time.Local {
|
||||
t = t.In(s.Location)
|
||||
}
|
||||
|
||||
// Start at the earliest possible time (the upcoming second).
|
||||
t = t.Add(1*time.Second - time.Duration(t.Nanosecond())*time.Nanosecond)
|
||||
|
||||
// This flag indicates whether a field has been incremented.
|
||||
added := false
|
||||
|
||||
// If no time is found within five years, return zero.
|
||||
yearLimit := t.Year() + 5
|
||||
|
||||
WRAP:
|
||||
if t.Year() > yearLimit {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// Find the first applicable month.
|
||||
// If it's this month, then do nothing.
|
||||
for 1<<uint(t.Month())&s.Month == 0 {
|
||||
// If we have to add a month, reset the other parts to 0.
|
||||
if !added {
|
||||
added = true
|
||||
// Otherwise, set the date at the beginning (since the current time is irrelevant).
|
||||
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, loc)
|
||||
}
|
||||
t = t.AddDate(0, 1, 0)
|
||||
|
||||
// Wrapped around.
|
||||
if t.Month() == time.January {
|
||||
goto WRAP
|
||||
}
|
||||
}
|
||||
|
||||
// Now get a day in that month.
|
||||
//
|
||||
// NOTE: This causes issues for daylight savings regimes where midnight does
|
||||
// not exist. For example: Sao Paulo has DST that transforms midnight on
|
||||
// 11/3 into 1am. Handle that by noticing when the Hour ends up != 0.
|
||||
for !dayMatches(s, t) {
|
||||
if !added {
|
||||
added = true
|
||||
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
||||
}
|
||||
t = t.AddDate(0, 0, 1)
|
||||
// Notice if the hour is no longer midnight due to DST.
|
||||
// Add an hour if it's 23, subtract an hour if it's 1.
|
||||
if t.Hour() != 0 {
|
||||
if t.Hour() > 12 {
|
||||
t = t.Add(time.Duration(24-t.Hour()) * time.Hour)
|
||||
} else {
|
||||
t = t.Add(time.Duration(-t.Hour()) * time.Hour)
|
||||
}
|
||||
}
|
||||
|
||||
if t.Day() == 1 {
|
||||
goto WRAP
|
||||
}
|
||||
}
|
||||
|
||||
for 1<<uint(t.Hour())&s.Hour == 0 {
|
||||
if !added {
|
||||
added = true
|
||||
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, loc)
|
||||
}
|
||||
t = t.Add(1 * time.Hour)
|
||||
|
||||
if t.Hour() == 0 {
|
||||
goto WRAP
|
||||
}
|
||||
}
|
||||
|
||||
for 1<<uint(t.Minute())&s.Minute == 0 {
|
||||
if !added {
|
||||
added = true
|
||||
t = t.Truncate(time.Minute)
|
||||
}
|
||||
t = t.Add(1 * time.Minute)
|
||||
|
||||
if t.Minute() == 0 {
|
||||
goto WRAP
|
||||
}
|
||||
}
|
||||
|
||||
for 1<<uint(t.Second())&s.Second == 0 {
|
||||
if !added {
|
||||
added = true
|
||||
t = t.Truncate(time.Second)
|
||||
}
|
||||
t = t.Add(1 * time.Second)
|
||||
|
||||
if t.Second() == 0 {
|
||||
goto WRAP
|
||||
}
|
||||
}
|
||||
|
||||
return t.In(origLocation)
|
||||
}
|
||||
|
||||
// dayMatches returns true if the schedule's day-of-week and day-of-month
|
||||
// restrictions are satisfied by the given time.
|
||||
func dayMatches(s *SpecSchedule, t time.Time) bool {
|
||||
var (
|
||||
domMatch bool = 1<<uint(t.Day())&s.Dom > 0
|
||||
dowMatch bool = 1<<uint(t.Weekday())&s.Dow > 0
|
||||
)
|
||||
if s.Dom&starBit > 0 || s.Dow&starBit > 0 {
|
||||
return domMatch && dowMatch
|
||||
}
|
||||
return domMatch || dowMatch
|
||||
}
|
136
vendor/golang.org/x/sync/semaphore/semaphore.go
generated
vendored
Normal file
136
vendor/golang.org/x/sync/semaphore/semaphore.go
generated
vendored
Normal file
@ -0,0 +1,136 @@
|
||||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package semaphore provides a weighted semaphore implementation.
|
||||
package semaphore // import "golang.org/x/sync/semaphore"
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type waiter struct {
|
||||
n int64
|
||||
ready chan<- struct{} // Closed when semaphore acquired.
|
||||
}
|
||||
|
||||
// NewWeighted creates a new weighted semaphore with the given
|
||||
// maximum combined weight for concurrent access.
|
||||
func NewWeighted(n int64) *Weighted {
|
||||
w := &Weighted{size: n}
|
||||
return w
|
||||
}
|
||||
|
||||
// Weighted provides a way to bound concurrent access to a resource.
|
||||
// The callers can request access with a given weight.
|
||||
type Weighted struct {
|
||||
size int64
|
||||
cur int64
|
||||
mu sync.Mutex
|
||||
waiters list.List
|
||||
}
|
||||
|
||||
// Acquire acquires the semaphore with a weight of n, blocking until resources
|
||||
// are available or ctx is done. On success, returns nil. On failure, returns
|
||||
// ctx.Err() and leaves the semaphore unchanged.
|
||||
//
|
||||
// If ctx is already done, Acquire may still succeed without blocking.
|
||||
func (s *Weighted) Acquire(ctx context.Context, n int64) error {
|
||||
s.mu.Lock()
|
||||
if s.size-s.cur >= n && s.waiters.Len() == 0 {
|
||||
s.cur += n
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
if n > s.size {
|
||||
// Don't make other Acquire calls block on one that's doomed to fail.
|
||||
s.mu.Unlock()
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
ready := make(chan struct{})
|
||||
w := waiter{n: n, ready: ready}
|
||||
elem := s.waiters.PushBack(w)
|
||||
s.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
err := ctx.Err()
|
||||
s.mu.Lock()
|
||||
select {
|
||||
case <-ready:
|
||||
// Acquired the semaphore after we were canceled. Rather than trying to
|
||||
// fix up the queue, just pretend we didn't notice the cancelation.
|
||||
err = nil
|
||||
default:
|
||||
isFront := s.waiters.Front() == elem
|
||||
s.waiters.Remove(elem)
|
||||
// If we're at the front and there're extra tokens left, notify other waiters.
|
||||
if isFront && s.size > s.cur {
|
||||
s.notifyWaiters()
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return err
|
||||
|
||||
case <-ready:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// TryAcquire acquires the semaphore with a weight of n without blocking.
|
||||
// On success, returns true. On failure, returns false and leaves the semaphore unchanged.
|
||||
func (s *Weighted) TryAcquire(n int64) bool {
|
||||
s.mu.Lock()
|
||||
success := s.size-s.cur >= n && s.waiters.Len() == 0
|
||||
if success {
|
||||
s.cur += n
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return success
|
||||
}
|
||||
|
||||
// Release releases the semaphore with a weight of n.
|
||||
func (s *Weighted) Release(n int64) {
|
||||
s.mu.Lock()
|
||||
s.cur -= n
|
||||
if s.cur < 0 {
|
||||
s.mu.Unlock()
|
||||
panic("semaphore: released more than held")
|
||||
}
|
||||
s.notifyWaiters()
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Weighted) notifyWaiters() {
|
||||
for {
|
||||
next := s.waiters.Front()
|
||||
if next == nil {
|
||||
break // No more waiters blocked.
|
||||
}
|
||||
|
||||
w := next.Value.(waiter)
|
||||
if s.size-s.cur < w.n {
|
||||
// Not enough tokens for the next waiter. We could keep going (to try to
|
||||
// find a waiter with a smaller request), but under load that could cause
|
||||
// starvation for large requests; instead, we leave all remaining waiters
|
||||
// blocked.
|
||||
//
|
||||
// Consider a semaphore used as a read-write lock, with N tokens, N
|
||||
// readers, and one writer. Each reader can Acquire(1) to obtain a read
|
||||
// lock. The writer can Acquire(N) to obtain a write lock, excluding all
|
||||
// of the readers. If we allow the readers to jump ahead in the queue,
|
||||
// the writer will starve — there is always one token available for every
|
||||
// reader.
|
||||
break
|
||||
}
|
||||
|
||||
s.cur += w.n
|
||||
s.waiters.Remove(next)
|
||||
close(w.ready)
|
||||
}
|
||||
}
|
7
vendor/modules.txt
vendored
7
vendor/modules.txt
vendored
@ -40,6 +40,9 @@ github.com/felixge/httpsnoop
|
||||
# github.com/fsnotify/fsnotify v1.5.4
|
||||
## explicit; go 1.16
|
||||
github.com/fsnotify/fsnotify
|
||||
# github.com/go-co-op/gocron v1.16.2
|
||||
## explicit; go 1.17
|
||||
github.com/go-co-op/gocron
|
||||
# github.com/go-fed/activity v1.0.0
|
||||
## explicit; go 1.12
|
||||
github.com/go-fed/activity/pub
|
||||
@ -398,6 +401,9 @@ github.com/rbcervilla/redisstore/v8
|
||||
# github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0
|
||||
## explicit; go 1.12
|
||||
github.com/remyoudompheng/bigfft
|
||||
# github.com/robfig/cron/v3 v3.0.1
|
||||
## explicit; go 1.12
|
||||
github.com/robfig/cron/v3
|
||||
# github.com/sirupsen/logrus v1.9.0
|
||||
## explicit; go 1.13
|
||||
github.com/sirupsen/logrus
|
||||
@ -618,6 +624,7 @@ golang.org/x/net/internal/timeseries
|
||||
golang.org/x/net/trace
|
||||
# golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
|
||||
## explicit
|
||||
golang.org/x/sync/semaphore
|
||||
golang.org/x/sync/singleflight
|
||||
# golang.org/x/sys v0.0.0-20220731174439-a90be440212d
|
||||
## explicit; go 1.17
|
||||
|
@ -51,6 +51,7 @@ en:
|
||||
Notifications: Notifications
|
||||
OAuthConfigured: OAuth Configured
|
||||
ObfuscatedDomain: Obfuscated Domain
|
||||
Paused: Paused
|
||||
ReceivedActivities: Received Activities
|
||||
Relay: Relay
|
||||
Repo: Repo
|
||||
|
@ -2,9 +2,10 @@
|
||||
{{- $textBlocked := .Localizer.TextBlocked -}}
|
||||
{{- $textClose := .Localizer.TextClose -}}
|
||||
{{- $textDomain := .Localizer.TextDomain -}}
|
||||
{{- $textImport := .Localizer.TextImport -}}
|
||||
{{- $textFollowing := .Localizer.TextFollowing -}}
|
||||
{{- $textImport := .Localizer.TextImport -}}
|
||||
{{- $textInstances := .Localizer.TextInstances -}}
|
||||
{{- $textPaused := .Localizer.TextPaused -}}
|
||||
{{- template "header" . }}
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
@ -28,7 +29,7 @@
|
||||
{{- range $instance := .Instances }}
|
||||
{{- $token := token $instance }}
|
||||
<tr>
|
||||
<th scope="row" class="align-middle"><a href="{{ pathAppAdminInstanceView $token }}">{{ $instance.Domain }}{{ if $instance.IsPunycode }} ({{ $instance.UnicodeDomain }}){{ end }}</a></th>
|
||||
<th scope="row" class="align-middle"><a href="{{ pathAppAdminInstanceView $token }}">{{ $instance.Domain }}{{ if $instance.IsPunycode }} ({{ $instance.UnicodeDomain }}){{ end }}</a>{{ if $instance.IsPaused }} <span class="badge text-bg-secondary" lang="{{ $textPaused.Language }}">{{ $textPaused.Lower }}</span>{{end}}</th>
|
||||
<td class="align-middle">{{ if $instance.IsFollowing }}<i class="fa-solid fa-check text-success"></i>{{ else }}<i class="fa-solid fa-xmark text-danger"></i>{{ end }}</td>
|
||||
<td class="align-middle">{{ if ne $instance.BlockID 0 }}<i class="fa-solid fa-check text-success"></i>{{ else }}<i class="fa-solid fa-xmark text-danger"></i>{{ end }}</td>
|
||||
<td class="align-middle text-end">
|
||||
|
@ -10,6 +10,7 @@
|
||||
{{- $textInstance := .Localizer.TextInstance -}}
|
||||
{{- $textMetrics := .Localizer.TextMetrics -}}
|
||||
{{- $textOAuthConfigured := .Localizer.TextOAuthConfigured -}}
|
||||
{{- $textPaused := .Localizer.TextPaused -}}
|
||||
{{- $textReceivedActivities := .Localizer.TextReceivedActivities -}}
|
||||
{{- $textServerHostname := .Localizer.TextServerHostname -}}
|
||||
{{- $textSoftware := .Localizer.TextSoftware -}}
|
||||
@ -17,7 +18,7 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 lang="{{ $textInstance.Language }}"><i class="fa-solid fa-gear"></i> {{ $textInstance }} {{ .Instance.UnicodeDomain }}</h1>
|
||||
<h1 lang="{{ $textInstance.Language }}"><i class="fa-solid fa-gear"></i> {{ $textInstance }} {{ .Instance.UnicodeDomain }}{{ if .Instance.IsPaused }} <span class="badge text-bg-secondary" lang="{{ $textPaused.Language }}">{{ $textPaused.Lower }}</span>{{end}}</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
74
web/template/admin_jobs.gohtml
Normal file
74
web/template/admin_jobs.gohtml
Normal file
@ -0,0 +1,74 @@
|
||||
{{ define "admin_jobs" -}}
|
||||
{{- $textClose := .Localizer.TextClose -}}
|
||||
{{- $textJob := .Localizer.TextJob -}}
|
||||
{{- $textJobs := .Localizer.TextJobs -}}
|
||||
{{- $textLastRun := .Localizer.TextLastRun -}}
|
||||
{{- $textNextRun := .Localizer.TextNextRun -}}
|
||||
{{- $textRunJob := .Localizer.TextRunJob -}}
|
||||
{{- $textRunning := .Localizer.TextRunning -}}
|
||||
{{- template "header" . }}
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 lang="{{ $textJobs.Language }}"><i class="fa-solid fa-chart-line"></i> {{ $textJobs }}</h1>
|
||||
<!--div class="btn-toolbar mb-2 mb-md-0">
|
||||
<div class="btn-group">
|
||||
<button type="button" class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#runJobModal">
|
||||
{{ $textRunJob }}
|
||||
</button>
|
||||
</div>
|
||||
</div-->
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col" lang="{{ $textJob.Language }}">{{ $textJob }}</th>
|
||||
<th scope="col" lang="{{ $textRunning.Language }}">{{ $textRunning }}</th>
|
||||
<th scope="col" lang="{{ $textLastRun.Language }}">{{ $textLastRun }}</th>
|
||||
<th scope="col" lang="{{ $textNextRun.Language }}">{{ $textNextRun }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{- range $job := .Jobs }}
|
||||
<tr>
|
||||
<th scope="row" class="align-middle">{{ $job.Name }}</th>
|
||||
<td class="align-middle">{{ template "yesno" $job.Running }}</td>
|
||||
<td class="align-middle">{{ $job.LastRun }}</td>
|
||||
<td class="align-middle">{{ $job.NextRun }}</td>
|
||||
</tr>
|
||||
{{- end }}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div><!-- /.container -->
|
||||
<div class="modal fade" id="runJobModal" tabindex="-1" aria-labelledby="runJobModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<form method="post">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="runJobModalLabel" lang="{{ $textRunJob.Language }}">{{ $textRunJob }}</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ $textClose }}"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
{{- if .FormRunJobError }}
|
||||
{{ template "alert" .FormRunJobError}}
|
||||
{{- end }}
|
||||
{{ template "form_input" .FormRunJobAction }}
|
||||
<div class="mb-3">
|
||||
{{ template "form_select" .FormRunJobJob }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" lang="{{ $textClose.Language }}">{{ $textClose }}</button>
|
||||
<button type="submit" class="btn btn-primary" lang="{{ $textRunJob.Language }}">{{ $textRunJob }}</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div><!-- /#runJobModal -->
|
||||
{{ template "footer" . }}
|
||||
{{ end }}
|
17
web/template/form_select.gohtml
Normal file
17
web/template/form_select.gohtml
Normal file
@ -0,0 +1,17 @@
|
||||
{{ define "form_select" -}}
|
||||
{{- if .Label }}
|
||||
{{ template "form_label" . }}
|
||||
{{- end }}
|
||||
<select class="form-select{{ if .Validation }}{{ if .Validation.Valid }} is-valid{{ else }} is-invalid{{ end }}{{ end }}" id="{{ .ID }}"{{ if .Name }} name="{{ .Name }}"{{ end }}{{ if .Label }} aria-label="{{.Label.Text }}"{{ end }}{{ if .Disabled }} disabled{{ end }}{{ if .Required }} required{{ end }}>
|
||||
{{- range .Options }}
|
||||
<option{{ if .Value}} value="{{ .Value }}"{{ end }}{{ if .Selected}} selected{{ end }}>{{ .Text }}</option>
|
||||
{{- end }}
|
||||
</select>
|
||||
{{- if .Validation}}
|
||||
{{- if .Validation.Response }}
|
||||
<div class="{{ if .Validation.Valid}}valid-feedback{{ else }}invalid-feedback{{ end }}">
|
||||
{{ .Validation.Response }}
|
||||
</div>
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
1
web/template/yesno.gohtml
Normal file
1
web/template/yesno.gohtml
Normal file
@ -0,0 +1 @@
|
||||
{{ define "yesno" }}{{ if . }}<i class="fa-solid fa-check text-success"></i>{{ else }}<i class="fa-solid fa-xmark text-danger"></i>{{ end }}{{ end }}
|
Loading…
Reference in New Issue
Block a user