Skip to content

Commit

Permalink
fix: cron was not being parsed at all! (#1750)
Browse files Browse the repository at this point in the history
Added integration tests to ensure these are being parsed correctly.
  • Loading branch information
gak authored Jun 12, 2024
1 parent 6e49fd6 commit bcd9772
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 6 deletions.
10 changes: 10 additions & 0 deletions backend/controller/cronjobs/testdata/go/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ type EchoResponse struct {
func Cron(ctx context.Context) error {
return os.WriteFile(os.Getenv("DEST_FILE"), []byte("Hello, world!"), 0644)
}

//ftl:cron 5m
func FiveMinutes(ctx context.Context) error {
return nil
}

//ftl:cron Sat
func Saturday(ctx context.Context) error {
return nil
}
2 changes: 1 addition & 1 deletion backend/schema/metadatacronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type MetadataCronJob struct {
Pos Position `parser:"" protobuf:"1,optional"`

Cron string `parser:"'+' 'cron' Whitespace @((' ' | Number | '-' | '/' | '*' | ',')+)" protobuf:"2"`
Cron string `parser:"'+' 'cron' Whitespace @( (Number ('s' | 'm' | 'h')) | ('Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun') | (' ' | Number | '-' | '/' | '*' | ',')+ )" protobuf:"2"`
}

var _ Metadata = (*MetadataCronJob)(nil)
Expand Down
36 changes: 36 additions & 0 deletions backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ module todo {
export verb destroy(builtin.HttpRequest<todo.DestroyRequest>) builtin.HttpResponse<todo.DestroyResponse, String>
+ingress http GET /todo/destroy/{name}
verb mondays(Unit) Unit
+cron Mon
verb scheduled(Unit) Unit
+cron */10 * * 1-10,11-31 * * *
verb twiceADay(Unit) Unit
+cron 12h
}
module foo {
Expand Down Expand Up @@ -192,6 +198,14 @@ Module
Unit
Unit
MetadataCronJob
Verb
Unit
Unit
MetadataCronJob
Verb
Unit
Unit
MetadataCronJob
`
actual := &strings.Builder{}
i := 0
Expand Down Expand Up @@ -719,6 +733,10 @@ module todo {
+ingress http GET /todo/destroy/{name}
verb scheduled(Unit) Unit
+cron */10 * * 1-10,11-31 * * *
verb twiceADay(Unit) Unit
+cron 12h
verb mondays(Unit) Unit
+cron Mon
}
`
actual, err := ParseModuleString("", input)
Expand Down Expand Up @@ -845,6 +863,24 @@ var testSchema = MustValidate(&Schema{
},
},
},
&Verb{Name: "twiceADay",
Request: &Unit{Unit: true},
Response: &Unit{Unit: true},
Metadata: []Metadata{
&MetadataCronJob{
Cron: "12h",
},
},
},
&Verb{Name: "mondays",
Request: &Unit{Unit: true},
Response: &Unit{Unit: true},
Metadata: []Metadata{
&MetadataCronJob{
Cron: "Mon",
},
},
},
},
},
{
Expand Down
3 changes: 2 additions & 1 deletion go-runtime/compile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/alecthomas/participle/v2"

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/cron"
)

// This file contains a parser for Go FTL directives.
Expand Down Expand Up @@ -152,7 +153,7 @@ func (d *directiveIngress) String() string {
type directiveCronJob struct {
Pos schema.Position

Cron string `parser:"'cron' Whitespace @((' ' | Number | '-' | '/' | '*' | ',')+)"`
Cron cron.Pattern `parser:"'cron' @@"`
}

func (*directiveCronJob) directive() {}
Expand Down
2 changes: 1 addition & 1 deletion go-runtime/compile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ func visitFuncDecl(pctx *parseContext, node *ast.FuncDecl) (verb *schema.Verb) {
isExported = false
metadata = append(metadata, &schema.MetadataCronJob{
Pos: dir.Pos,
Cron: dir.Cron,
Cron: dir.Cron.String(),
})
case *directiveRetry:
metadata = append(metadata, &schema.MetadataRetry{
Expand Down
5 changes: 3 additions & 2 deletions go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,9 @@ func TestErrorReporting(t *testing.T) {
`145:1-35: type can not be a variant of more than 1 type enums (TypeEnum1, TypeEnum2)`,
`151:27-27: enum discriminator "TypeEnum3" cannot contain exported methods`,
`154:1-35: enum discriminator "NoMethodsTypeEnum" must define at least one method`,
`168:2-62: can not publish directly to topics in other modules`,
`169:9-26: can not call verbs in other modules directly: use ftl.Call(…) instead`,
`166:3-3: unexpected token "d"`,
`173:2-62: can not publish directly to topics in other modules`,
`174:9-26: can not call verbs in other modules directly: use ftl.Call(…) instead`,
}
assert.Equal(t, expected, actual)
}
Expand Down
5 changes: 5 additions & 0 deletions go-runtime/compile/testdata/failing/failing.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func BadCron(ctx context.Context) error {
return nil
}

//ftl:cron 4d
func DaysNotPossible(ctx context.Context) error {
return nil
}

//ftl:verb
func BadPublish(ctx context.Context) error {
ps.PublicBroadcast.Publish(ctx, ps.PayinEvent{Name: "Test"})
Expand Down
8 changes: 7 additions & 1 deletion internal/cron/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ var (
)

type Pattern struct {
Duration *string `parser:"@(Number (?! Whitespace) Ident)+"`
Duration *string `parser:"@(Number ('s' | 'm' | 'h'))"`
DayOfWeek *DayOfWeek `parser:"| @('Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun')"`
Components []Component `parser:"| @@*"`
}

func (p Pattern) String() string {
if p.Duration != nil {
return *p.Duration
}
if p.DayOfWeek != nil {
return string(*p.DayOfWeek)
}
return strings.Join(slices.Map(p.Components, func(component Component) string {
return component.String()
}), " ")
Expand Down

0 comments on commit bcd9772

Please sign in to comment.