Skip to content

Commit 75a38aa

Browse files
committed
fix(cli): error during suga dev if a cron expression is blank
1 parent 154c4b3 commit 75a38aa

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

cli/internal/simulation/service/service.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,39 +127,41 @@ func (s *ServiceSimulation) startSchedules(stdoutWriter, stderrorWriter io.Write
127127
for _, schedule := range s.intent.Schedules {
128128
cronExpression := strings.TrimSpace(schedule.Cron)
129129

130-
if cronExpression != "" {
131-
url := url.URL{
132-
Scheme: "http",
133-
Host: fmt.Sprintf("localhost:%d", s.port),
134-
Path: schedule.Path,
135-
}
130+
if cronExpression == "" {
131+
return nil, fmt.Errorf("service %s has a schedule with an empty cron expression", style.Teal(fmt.Sprintf("[%s]", s.name)))
132+
}
133+
134+
url := url.URL{
135+
Scheme: "http",
136+
Host: fmt.Sprintf("localhost:%d", s.port),
137+
Path: schedule.Path,
138+
}
136139

137-
_, err := cron.AddFunc(cronExpression, func() {
138-
fmt.Printf("%s -> %s POST %s\n", style.Purple(fmt.Sprintf("[schedule.%s] (%s)", s.name, cronExpression)), style.Teal(fmt.Sprintf("[%s]", s.name)), schedule.Path)
139-
req, err := http.NewRequest(http.MethodPost, url.String(), nil)
140-
if err != nil {
141-
// log the error
142-
fmt.Fprint(stderrorWriter, "error creating request for schedule", err)
143-
return
144-
}
145-
146-
resp, err := http.DefaultClient.Do(req)
147-
if err != nil {
148-
fmt.Fprint(stderrorWriter, "error sending request for schedule", err)
149-
return
150-
}
151-
152-
defer resp.Body.Close()
153-
154-
if resp.StatusCode != http.StatusOK {
155-
fmt.Fprint(stderrorWriter, "error sending request for schedule", resp.StatusCode)
156-
return
157-
}
158-
})
140+
_, err := cron.AddFunc(cronExpression, func() {
141+
fmt.Printf("%s -> %s POST %s\n", style.Purple(fmt.Sprintf("[schedule.%s] (%s)", s.name, cronExpression)), style.Teal(fmt.Sprintf("[%s]", s.name)), schedule.Path)
142+
req, err := http.NewRequest(http.MethodPost, url.String(), nil)
143+
if err != nil {
144+
// log the error
145+
fmt.Fprint(stderrorWriter, "error creating request for schedule", err)
146+
return
147+
}
159148

149+
resp, err := http.DefaultClient.Do(req)
160150
if err != nil {
161-
return nil, err
151+
fmt.Fprint(stderrorWriter, "error sending request for schedule", err)
152+
return
162153
}
154+
155+
defer resp.Body.Close()
156+
157+
if resp.StatusCode != http.StatusOK {
158+
fmt.Fprint(stderrorWriter, "error sending request for schedule", resp.StatusCode)
159+
return
160+
}
161+
})
162+
163+
if err != nil {
164+
return nil, err
163165
}
164166
}
165167

0 commit comments

Comments
 (0)