fixing cron dependancies

This commit is contained in:
mr 2024-08-08 11:14:24 +02:00
parent f45ad91687
commit 580b492fd3
2 changed files with 13 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package oclib
import (
"errors"
"strings"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
@ -10,7 +11,7 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/models/workspace"
"github.com/vk496/cron"
cron "github.com/robfig/cron/v3"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@ -40,7 +41,7 @@ func (wfa *workflowMongoAccessor) CheckBooking(start *time.Time) bool {
func (wfa *workflowMongoAccessor) getExecutions(id string, data *Workflow) ([]*workflow_execution.WorkflowExecution, error) {
workflows_execution := []*workflow_execution.WorkflowExecution{}
if data.Schedule != nil {
if data.Schedule.Start == nil || data.Schedule.Start.IsZero() {
if data.Schedule.Start == nil {
return workflows_execution, errors.New("should get a start date on the scheduler.")
}
if data.Schedule.End != nil && data.Schedule.End.IsZero() {
@ -50,11 +51,18 @@ func (wfa *workflowMongoAccessor) getExecutions(id string, data *Workflow) ([]*w
if data.Schedule.End == nil {
return workflows_execution, errors.New("a cron task should got a end date.")
}
c, err := cron.Parse(data.Schedule.Cron)
cronStr := strings.Split(" ", data.Schedule.Cron)
if len(cronStr) < 6 {
return nil, errors.New("Bad cron message: " + data.Schedule.Cron + ". Should be at least ss mm hh dd MM dw")
}
subCron := strings.Join(cronStr[:6], " ")
// cron should be parsed as ss mm hh dd MM dw t (min 6 fields)
specParser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
sched, err := specParser.Parse(subCron)
if err != nil {
return workflows_execution, errors.New("Bad cron message: " + err.Error())
}
for s := c.Next(*data.Schedule.Start); !s.IsZero() && s.Before(*data.Schedule.End); s = c.Next(s) {
for s := sched.Next(*data.Schedule.Start); !s.IsZero() && s.Before(*data.Schedule.End); s = sched.Next(s) {
obj := &workflow_execution.WorkflowExecution{
AbstractObject: utils.AbstractObject{
Name: data.Schedule.Name,

View File

@ -7,7 +7,7 @@ type WorkflowSchedule struct {
Name string `json:"name" bson:"name" validate:"required"`
Start *time.Time `json:"start" bson:"start" validate:"required"`
End *time.Time `json:"end,omitempty" bson:"end,omitempty"`
Cron string `json:"cron,omitempty" bson:"cron,omitempty"`
Cron string `json:"cron,omitempty" bson:"cron,omitempty"` // ss mm hh dd MM dw task
}
func (ws *WorkflowSchedule) GetAllDates() (timetable []time.Time) {