From 124ae952808b0f56f80cd00cfe2638b3dc54d342 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Wed, 14 Jul 2021 13:02:35 +0100 Subject: [PATCH] Remove debug line for invocations This showed the URL which is no longer required Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- main.go | 17 +++++++++++++++-- types/cron_function.go | 25 +++++++++++++------------ types/scheduler.go | 18 +++++++++--------- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index 2ae4bce..49eb5b9 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,8 @@ const topic = "cron-function" func main() { config, err := getControllerConfig() if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) + os.Exit(1) } sha, ver := version.GetReleaseInfo() @@ -38,6 +39,17 @@ func main() { config.ContentType, config.PrintResponse) + go func() { + for { + r := <-invoker.Responses + if r.Error != nil { + log.Printf("Error with: %s, %s", r.Function, err.Error()) + } else { + log.Printf("Response: %s [%d]", r.Function, r.Status) + } + } + }() + cronScheduler := cfunction.NewScheduler() interval := time.Second * 10 @@ -45,7 +57,8 @@ func main() { err = startFunctionProbe(interval, topic, config, cronScheduler, invoker) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) + os.Exit(1) } } diff --git a/types/cron_function.go b/types/cron_function.go index 7c3ec44..5392b37 100644 --- a/types/cron_function.go +++ b/types/cron_function.go @@ -50,32 +50,32 @@ func (c *CronFunctions) Contains(cf *CronFunction) bool { // and returns error if it is not possible func ToCronFunction(f ptypes.FunctionStatus, namespace string, topic string) (CronFunction, error) { if f.Annotations == nil { - return CronFunction{}, errors.New(fmt.Sprint(f.Name, " has no annotations.")) + return CronFunction{}, fmt.Errorf("%s has no annotations", f.Name) } + fTopic := (*f.Annotations)["topic"] fSchedule := (*f.Annotations)["schedule"] if fTopic != topic { - return CronFunction{}, errors.New(fmt.Sprint(f.Name, " has wrong topic: ", fTopic)) + return CronFunction{}, fmt.Errorf("%s has wrong topic: %s", fTopic, f.Name) } if !CheckSchedule(fSchedule) { - return CronFunction{}, errors.New(fmt.Sprint(f.Name, " has wrong cron schedule: ", fSchedule)) + return CronFunction{}, fmt.Errorf("%s has wrong cron schedule: %s", f.Name, fSchedule) } - var c CronFunction - c.FuncData = f - c.Name = f.Name - c.Namespace = namespace - c.Schedule = fSchedule - return c, nil + return CronFunction{ + FuncData: f, + Name: f.Name, + Namespace: namespace, + Schedule: fSchedule, + }, nil } // InvokeFunction Invokes the cron function func (c CronFunction) InvokeFunction(i *types.Invoker) (*[]byte, error) { gwURL := fmt.Sprintf("%s/%s", i.GatewayURL, c.String()) - log.Printf("HTTP POST: %s", gwURL) req, err := http.NewRequest(http.MethodPost, gwURL, nil) if err != nil { @@ -91,7 +91,7 @@ func (c CronFunction) InvokeFunction(i *types.Invoker) (*[]byte, error) { if err != nil { i.Responses <- types.InvokerResponse{ - Error: errors.Wrap(err, fmt.Sprint("unable to invoke ", c.Name, " in ", c.Namespace)), + Error: errors.Wrap(err, fmt.Sprintf("unable to invoke %s", c.String())), } return nil, err } @@ -103,8 +103,9 @@ func (c CronFunction) InvokeFunction(i *types.Invoker) (*[]byte, error) { if err != nil { log.Printf("Error reading body") i.Responses <- types.InvokerResponse{ - Error: errors.Wrap(err, fmt.Sprint("unable to invoke ", c.Name, " in ", c.Namespace)), + Error: errors.Wrap(err, fmt.Sprintf("unable to invoke %s", c.String())), } + return nil, err } diff --git a/types/scheduler.go b/types/scheduler.go index dac6c83..f47df8c 100644 --- a/types/scheduler.go +++ b/types/scheduler.go @@ -35,15 +35,6 @@ type ScheduledFunction struct { // ScheduledFunctions is an array of ScheduledFunction type ScheduledFunctions []ScheduledFunction -// AddCronFunction adds a function to cron -func (s *Scheduler) AddCronFunction(c CronFunction, invoker *types.Invoker) (ScheduledFunction, error) { - eID, err := s.main.AddFunc(c.Schedule, func() { - log.Printf("Executing function: %s", c.String()) - c.InvokeFunction(invoker) - }) - return ScheduledFunction{c, EntryID(eID)}, err -} - // NewScheduler returns a scheduler func NewScheduler() *Scheduler { return &Scheduler{ @@ -56,6 +47,15 @@ func (s *Scheduler) Start() { s.main.Start() } +// AddCronFunction adds a function to cron +func (s *Scheduler) AddCronFunction(c CronFunction, invoker *types.Invoker) (ScheduledFunction, error) { + eID, err := s.main.AddFunc(c.Schedule, func() { + log.Printf("Executing function: %s", c.String()) + c.InvokeFunction(invoker) + }) + return ScheduledFunction{c, EntryID(eID)}, err +} + // Remove removes the function from scheduler func (s *Scheduler) Remove(function ScheduledFunction) { s.main.Remove(cron.EntryID(function.ID))