Skip to content

Commit

Permalink
add top-level notification timeout
Browse files Browse the repository at this point in the history
It used to make or extend the context of service.notify
  • Loading branch information
umputun committed Oct 27, 2023
1 parent a1b5c1d commit 60181eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ notify:
--notify.telegram-token= API token for the Telegram bot [$CRONN_NOTIFY_TELEGRAM_TOKEN]
--notify.telegram-destinations= List of Telegram chat IDs the bot will post messages to [$CRONN_NOTIFY_TELEGRAM_DESTINATIONS]
--notify.webhook-urls= List of webhook URLs the bot will post messages to [$CRONN_NOTIFY_WEBHOOK_URLS]
--noify.timeout= timeout for notifications (default: 10s) [$TIMEOUT]
log:
--log.enabled enable logging [$CRONN_LOG_ENABLED]
--log.debug debug mode [$CRONN_LOG_DEBUG]
Expand Down
2 changes: 2 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var opts struct {
TelegramToken string `long:"telegram-token" env:"TELEGRAM_TOKEN" description:"API token for the Telegram bot"`
TelegramDestinations []string `long:"telegram-destinations" env:"TELEGRAM_DESTINATIONS" description:"List of Telegram chat IDs the bot will post messages to" env-delim:","`
WebhookURLs []string `long:"webhook-urls" env:"WEBHOOK_URLS" description:"List of webhook URLs the bot will post messages to" env-delim:","`
TimeOut time.Duration `long:"timeout" env:"TIMEOUT" default:"10s" description:"timeout for notification"`
} `group:"notify" namespace:"notify" env-namespace:"CRONN_NOTIFY"`

Log struct {
Expand Down Expand Up @@ -141,6 +142,7 @@ func main() {
EnableLogPrefix: opts.Log.EnablePrefix,
Stdout: stdout,
DeDup: service.NewDeDup(opts.DeDup),
NotifyTimeout: opts.Notify.TimeOut,
}

cronService.Do(ctx)
Expand Down
13 changes: 9 additions & 4 deletions app/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
//go:generate moq -out mocks/repeater.go -pkg mocks -skip-ensure -fmt goimports . Repeater
//go:generate moq -out mocks/schedule.go -pkg mocks -skip-ensure -fmt goimports . Schedule

// Scheduler is a top-level service wiring cron, resumer ans parser and provifing the main entry point (blocking) to start the process
// Scheduler is a top-level service wiring cron, resumer ans parser and provifing the main entry point (blocking)
// to start the process
type Scheduler struct {
Cron
Resumer Resumer
Expand All @@ -42,6 +43,7 @@ type Scheduler struct {
EnableLogPrefix bool
Repeater Repeater
Stdout io.Writer
NotifyTimeout time.Duration
}

// Resumer defines interface for resumer.Resumer providing auto-restart for failed jobs
Expand Down Expand Up @@ -151,7 +153,9 @@ func (s *Scheduler) jobFunc(r crontab.JobSpec, sched Schedule) cron.FuncJob {
}

if err = s.executeCommand(cmd, s.Stdout); err != nil {
if e := s.notify(context.TODO(), r, err.Error()); e != nil {
ctxTimeout, cancel := context.WithTimeout(context.Background(), s.NotifyTimeout)
defer cancel()
if e := s.notify(ctxTimeout, r, err.Error()); e != nil {
return fmt.Errorf("failed to notify: %w", err)
}
return err
Expand Down Expand Up @@ -202,7 +206,6 @@ func (s *Scheduler) executeCommand(command string, logWriter io.Writer) error {
}

func (s *Scheduler) notify(ctx context.Context, r crontab.JobSpec, errMsg string) error {

if s.Notifier == nil || reflect.ValueOf(s.Notifier).IsNil() {
return nil
}
Expand Down Expand Up @@ -282,7 +285,9 @@ func (s *Scheduler) resumeInterrupted(concur int) {
gr.Go(func(ctx context.Context) {
if err := s.executeCommand(cmd.Command, s.Stdout); err != nil {
r := crontab.JobSpec{Spec: "auto-resume", Command: cmd.Command}
if e := s.notify(ctx, r, err.Error()); e != nil {
ctxTimeout, cancel := context.WithTimeout(ctx, s.NotifyTimeout)
defer cancel()
if e := s.notify(ctxTimeout, r, err.Error()); e != nil {
log.Printf("[WARN] failed to notify, %v", e)
return
}
Expand Down

0 comments on commit 60181eb

Please sign in to comment.