added cron support
This commit is contained in:
@@ -74,6 +74,7 @@ require (
|
|||||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||||
github.com/frikky/schemaless v0.0.17 // indirect
|
github.com/frikky/schemaless v0.0.17 // indirect
|
||||||
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
|
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
|
||||||
|
github.com/go-co-op/gocron v1.37.0 // indirect
|
||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||||
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
|
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
|
||||||
github.com/go-logr/logr v1.4.2 // indirect
|
github.com/go-logr/logr v1.4.2 // indirect
|
||||||
@@ -118,6 +119,7 @@ require (
|
|||||||
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
|
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
|
||||||
|
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||||
github.com/sashabaranov/go-openai v1.40.1 // indirect
|
github.com/sashabaranov/go-openai v1.40.1 // indirect
|
||||||
github.com/sendgrid/rest v2.6.9+incompatible // indirect
|
github.com/sendgrid/rest v2.6.9+incompatible // indirect
|
||||||
github.com/sendgrid/sendgrid-go v3.16.1+incompatible // indirect
|
github.com/sendgrid/sendgrid-go v3.16.1+incompatible // indirect
|
||||||
@@ -141,6 +143,7 @@ require (
|
|||||||
go.opentelemetry.io/otel/sdk/metric v1.36.0 // indirect
|
go.opentelemetry.io/otel/sdk/metric v1.36.0 // indirect
|
||||||
go.opentelemetry.io/otel/trace v1.36.0 // indirect
|
go.opentelemetry.io/otel/trace v1.36.0 // indirect
|
||||||
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
|
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
|
||||||
|
go.uber.org/atomic v1.9.0 // indirect
|
||||||
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
|
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
|
||||||
golang.org/x/net v0.40.0 // indirect
|
golang.org/x/net v0.40.0 // indirect
|
||||||
golang.org/x/oauth2 v0.30.0 // indirect
|
golang.org/x/oauth2 v0.30.0 // indirect
|
||||||
|
|||||||
+17
-6
@@ -4034,6 +4034,7 @@ func runInitEs(ctx context.Context) {
|
|||||||
// FIXME: This should ONLY run on one backend instance
|
// FIXME: This should ONLY run on one backend instance
|
||||||
|
|
||||||
schedules, err := shuffle.GetAllSchedules(ctx, "ALL")
|
schedules, err := shuffle.GetAllSchedules(ctx, "ALL")
|
||||||
|
log.Printf("Schedules %s", schedules)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[WARNING] Failed getting schedules during service init: %s", err)
|
log.Printf("[WARNING] Failed getting schedules during service init: %s", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -4078,14 +4079,23 @@ func runInitEs(ctx context.Context) {
|
|||||||
|
|
||||||
//log.Printf("Schedule: %#v", schedule)
|
//log.Printf("Schedule: %#v", schedule)
|
||||||
//log.Printf("Schedule time: every %d seconds", schedule.Seconds)
|
//log.Printf("Schedule time: every %d seconds", schedule.Seconds)
|
||||||
jobret, err := newscheduler.Every(schedule.Seconds).Seconds().NotImmediately().Run(job(schedule))
|
if schedule.Seconds == 0 && len(schedule.Frequency) > 0 {
|
||||||
if err != nil {
|
cronJob, err := CronScheduler.Cron(schedule.Frequency).Do(job(schedule))
|
||||||
log.Printf("[ERROR] Failed to start schedule for workflow %s: %s", schedule.WorkflowId, err)
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] Failed to start schedule for workflow %s: %s", schedule.WorkflowId, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("[DEBUG] Successfully started schedule for workflow %s", schedule.WorkflowId)
|
||||||
|
}
|
||||||
|
cronJobs[schedule.Id] = cronJob
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[DEBUG] Successfully started schedule for workflow %s", schedule.WorkflowId)
|
jobret, err := newscheduler.Every(schedule.Seconds).Seconds().NotImmediately().Run(job(schedule))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] Failed to start schedule for workflow %s: %s", schedule.WorkflowId, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("[DEBUG] Successfully started schedule for workflow %s", schedule.WorkflowId)
|
||||||
|
}
|
||||||
|
scheduledJobs[schedule.Id] = jobret
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduledJobs[schedule.Id] = jobret
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5090,6 +5100,7 @@ func handleAppZipUpload(resp http.ResponseWriter, request *http.Request) {
|
|||||||
func initHandlers() {
|
func initHandlers() {
|
||||||
var err error
|
var err error
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
CronScheduler.StartAsync()
|
||||||
|
|
||||||
log.Printf("[DEBUG] Starting Shuffle backend - initializing database connection")
|
log.Printf("[DEBUG] Starting Shuffle backend - initializing database connection")
|
||||||
//requestCache = cache.New(5*time.Minute, 10*time.Minute)
|
//requestCache = cache.New(5*time.Minute, 10*time.Minute)
|
||||||
|
|||||||
+34
-14
@@ -26,6 +26,7 @@ import (
|
|||||||
uuid "github.com/satori/go.uuid"
|
uuid "github.com/satori/go.uuid"
|
||||||
|
|
||||||
newscheduler "github.com/carlescere/scheduler"
|
newscheduler "github.com/carlescere/scheduler"
|
||||||
|
"github.com/go-co-op/gocron"
|
||||||
"github.com/frikky/kin-openapi/openapi3"
|
"github.com/frikky/kin-openapi/openapi3"
|
||||||
"github.com/go-git/go-billy/v5"
|
"github.com/go-git/go-billy/v5"
|
||||||
"github.com/go-git/go-billy/v5/memfs"
|
"github.com/go-git/go-billy/v5/memfs"
|
||||||
@@ -42,17 +43,22 @@ var baseEnvironment = "onprem"
|
|||||||
var cloudname = "cloud"
|
var cloudname = "cloud"
|
||||||
|
|
||||||
var scheduledJobs = map[string]*newscheduler.Job{}
|
var scheduledJobs = map[string]*newscheduler.Job{}
|
||||||
|
var cronJobs = map[string]*gocron.Job{}
|
||||||
var scheduledOrgs = map[string]*newscheduler.Job{}
|
var scheduledOrgs = map[string]*newscheduler.Job{}
|
||||||
|
|
||||||
|
var CronScheduler = gocron.NewScheduler(time.UTC)
|
||||||
|
|
||||||
// Frequency = cronjob OR minutes between execution
|
// Frequency = cronjob OR minutes between execution
|
||||||
func createSchedule(ctx context.Context, scheduleId, workflowId, name, startNode, frequency, orgId string, body []byte) error {
|
func createSchedule(ctx context.Context, scheduleId, workflowId, name, startNode, frequency, orgId string, body []byte) error {
|
||||||
var err error
|
var err error
|
||||||
testSplit := strings.Split(frequency, "*")
|
testSplit := strings.Split(frequency, "*")
|
||||||
cronJob := ""
|
cronJob := ""
|
||||||
|
isCron := false
|
||||||
newfrequency := 0
|
newfrequency := 0
|
||||||
|
|
||||||
if len(testSplit) > 5 {
|
if len(testSplit) > 5 {
|
||||||
cronJob = frequency
|
cronJob = frequency
|
||||||
|
isCron = true
|
||||||
} else {
|
} else {
|
||||||
newfrequency, err = strconv.Atoi(frequency)
|
newfrequency, err = strconv.Atoi(frequency)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,12 +71,7 @@ func createSchedule(ctx context.Context, scheduleId, workflowId, name, startNode
|
|||||||
//} else if int(newfrequency) <
|
//} else if int(newfrequency) <
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse. Can't handle CRON, only numbers
|
if newfrequency < 1 && !isCron {
|
||||||
if len(cronJob) > 0 {
|
|
||||||
return errors.New("cronJob isn't formatted correctly")
|
|
||||||
}
|
|
||||||
|
|
||||||
if newfrequency < 1 {
|
|
||||||
return errors.New("Frequency has to be more than 0")
|
return errors.New("Frequency has to be more than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,17 +97,27 @@ func createSchedule(ctx context.Context, scheduleId, workflowId, name, startNode
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[INFO] Starting frequency for execution: %d", newfrequency)
|
log.Printf("[INFO] Starting frequency for execution: %s", frequency)
|
||||||
|
|
||||||
//jobret, err := newscheduler.Every(newfrequency).Seconds().NotImmediately().Run(job)
|
if isCron {
|
||||||
jobret, err := newscheduler.Every(newfrequency).Seconds().Run(job)
|
cronJob, err := CronScheduler.Cron(cronJob).Do(job)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to schedule workflow: %s", err)
|
log.Printf("[ERROR] Failed to start schedule with cron(%s): %s", cronJob, err)
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
cronJobs[scheduleId] = cronJob
|
||||||
|
} else {
|
||||||
|
//jobret, err := newscheduler.Every(newfrequency).Seconds().NotImmediately().Run(job)
|
||||||
|
jobret, err := newscheduler.Every(newfrequency).Seconds().Run(job)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to schedule workflow: %s", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduledJobs[scheduleId] = jobret
|
||||||
}
|
}
|
||||||
|
|
||||||
//scheduledJobs = append(scheduledJobs, jobret)
|
//scheduledJobs = append(scheduledJobs, jobret)
|
||||||
scheduledJobs[scheduleId] = jobret
|
|
||||||
|
|
||||||
// Doesn't need running/not running. If stopped, we just delete it.
|
// Doesn't need running/not running. If stopped, we just delete it.
|
||||||
timeNow := int64(time.Now().Unix())
|
timeNow := int64(time.Now().Unix())
|
||||||
@@ -117,6 +128,7 @@ func createSchedule(ctx context.Context, scheduleId, workflowId, name, startNode
|
|||||||
Argument: string(body),
|
Argument: string(body),
|
||||||
WrappedArgument: bodyWrapper,
|
WrappedArgument: bodyWrapper,
|
||||||
Seconds: newfrequency,
|
Seconds: newfrequency,
|
||||||
|
Frequency: frequency,
|
||||||
CreationTime: timeNow,
|
CreationTime: timeNow,
|
||||||
LastModificationtime: timeNow,
|
LastModificationtime: timeNow,
|
||||||
LastRuntime: timeNow,
|
LastRuntime: timeNow,
|
||||||
@@ -1541,7 +1553,15 @@ func deleteSchedule(ctx context.Context, id string) error {
|
|||||||
value.Lock()
|
value.Lock()
|
||||||
} else {
|
} else {
|
||||||
// FIXME - allow it to kind of stop anyway?
|
// FIXME - allow it to kind of stop anyway?
|
||||||
return errors.New("Can't find the schedule.")
|
if j, ok := cronJobs[id]; ok {
|
||||||
|
err := CronScheduler.RemoveByID(j)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] Failed to remove the scheduler %s", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New("Can't find the schedule.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user