Skip to content

Commit

Permalink
Return errors when creating cron jobs for a new deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Apr 14, 2024
1 parent 58f86af commit 92d1f9e
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions backend/controller/cronjobs/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cronjobs
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"time"
Expand Down Expand Up @@ -110,39 +111,43 @@ func NewForTesting(ctx context.Context, key model.ControllerKey, originURL *url.
return svc
}

func (s *Service) NewCronJobsForModule(ctx context.Context, module *schemapb.Module) (jobs []dal.CronJob, err error) {
logger := log.FromContext(ctx)

func (s *Service) NewCronJobsForModule(ctx context.Context, module *schemapb.Module) ([]dal.CronJob, error) {
start := s.clock.Now().UTC()
newJobs := []dal.CronJob{}
merr := []error{}
for _, decl := range module.Decls {
if verb, ok := decl.Value.(*schemapb.Decl_Verb); ok {
for _, metadata := range verb.Verb.Metadata {
if cronMetadata, ok := metadata.Value.(*schemapb.Metadata_CronJob); ok {
cronStr := cronMetadata.CronJob.Cron
schedule, err := cron.Parse(cronStr)
if err != nil {
logger.Errorf(err, "failed to parse cron schedule %q", cronStr)
continue
}
next, err := cron.NextAfter(schedule, start, false)
if err != nil {
logger.Errorf(err, "failed to calculate next execution for cron job %v:%v with schedule %q", module.Name, verb.Verb.Name, schedule)
continue
}
newJobs = append(newJobs, dal.CronJob{
Key: model.NewCronJobKey(module.Name, verb.Verb.Name),
Ref: schema.Ref{Module: module.Name, Name: verb.Verb.Name},
Schedule: cronStr,
StartTime: start,
NextExecution: next,
State: dal.JobStateIdle,
// DeploymentKey: Filled in by DAL
})
verb, ok := decl.Value.(*schemapb.Decl_Verb)
if !ok {
continue
}
for _, metadata := range verb.Verb.Metadata {
if cronMetadata, ok := metadata.Value.(*schemapb.Metadata_CronJob); ok {
cronStr := cronMetadata.CronJob.Cron
schedule, err := cron.Parse(cronStr)
if err != nil {
merr = append(merr, fmt.Errorf("failed to parse cron schedule %q: %w", cronStr, err))
continue
}
next, err := cron.NextAfter(schedule, start, false)
if err != nil {
merr = append(merr, fmt.Errorf("failed to calculate next execution for cron job %v:%v with schedule %q: %w", module.Name, verb.Verb.Name, schedule, err))
continue
}
newJobs = append(newJobs, dal.CronJob{
Key: model.NewCronJobKey(module.Name, verb.Verb.Name),
Ref: schema.Ref{Module: module.Name, Name: verb.Verb.Name},
Schedule: cronStr,
StartTime: start,
NextExecution: next,
State: dal.JobStateIdle,
// DeploymentKey: Filled in by DAL
})
}
}
}
if len(merr) > 0 {
return nil, errors.Join(merr...)
}
return newJobs, nil
}

Expand Down

0 comments on commit 92d1f9e

Please sign in to comment.