feature: Cron support (#102)

This commit is contained in:
James Read 2023-02-03 08:53:10 +00:00 committed by GitHub
parent f05de1c726
commit 2d173266df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/OliveTin/OliveTin/internal/executor" "github.com/OliveTin/OliveTin/internal/executor"
grpcapi "github.com/OliveTin/OliveTin/internal/grpcapi" grpcapi "github.com/OliveTin/OliveTin/internal/grpcapi"
"github.com/OliveTin/OliveTin/internal/oncron"
"github.com/OliveTin/OliveTin/internal/onstartup" "github.com/OliveTin/OliveTin/internal/onstartup"
updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck" updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck"
@ -105,6 +106,7 @@ func main() {
executor := executor.DefaultExecutor() executor := executor.DefaultExecutor()
go onstartup.Execute(cfg, executor) go onstartup.Execute(cfg, executor)
go oncron.Schedule(cfg, executor)
go updatecheck.StartUpdateChecker(version, commit, cfg, configDir) go updatecheck.StartUpdateChecker(version, commit, cfg, configDir)

View File

@ -11,6 +11,7 @@ type Action struct {
Timeout int Timeout int
Acls []string Acls []string
ExecOnStartup bool ExecOnStartup bool
ExecOnCron []string
Arguments []ActionArgument Arguments []ActionArgument
} }

50
internal/oncron/cron.go Normal file
View File

@ -0,0 +1,50 @@
package oncron
import (
"github.com/OliveTin/OliveTin/internal/acl"
"github.com/OliveTin/OliveTin/internal/config"
"github.com/OliveTin/OliveTin/internal/executor"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
func Schedule(cfg *config.Config, ex *executor.Executor) {
scheduler := cron.New(cron.WithSeconds())
for _, action := range cfg.Actions {
for _, cronline := range action.ExecOnCron {
scheduleAction(cfg, scheduler, cronline, ex, action)
}
}
scheduler.Start()
}
func scheduleAction(cfg *config.Config, scheduler *cron.Cron, cronline string, ex *executor.Executor, action config.Action) {
log.WithFields(log.Fields{
"action": action.Title,
"cronline": cronline,
}).Infof("Scheduling Action for cron")
_, err := scheduler.AddFunc(cronline, func() {
req := &executor.ExecutionRequest{
ActionName: action.Title,
Cfg: cfg,
Tags: []string{"cron"},
AuthenticatedUser: &acl.AuthenticatedUser{
Username: "cron",
},
}
ex.ExecRequest(req)
})
if err != nil {
log.WithFields(log.Fields{
"action": action.Title,
"cronError": err,
}).Errorf("CRON schedule error")
return
}
}