@@ -12,7 +12,7 @@ import (
1212 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313 "sigs.k8s.io/yaml"
1414
15- batchv1alpha1 "github.com/flanksource/batch-runner/pkg/apis/batch/v1"
15+ v1 "github.com/flanksource/batch-runner/pkg/apis/batch/v1"
1616 "github.com/flanksource/commons/logger"
1717 "github.com/flanksource/duty/context"
1818 "github.com/flanksource/duty/shell"
@@ -31,6 +31,8 @@ import (
3131 _ "gocloud.dev/pubsub/rabbitpubsub"
3232)
3333
34+ var retry = NewRetryCache ()
35+
3436func pretty (o any ) string {
3537 s , err := json .MarshalIndent (o , "" , " " )
3638 if err != nil {
@@ -42,25 +44,23 @@ func pretty(o any) string {
4244 return err .Error ()
4345 }
4446 return string (b )
45-
4647}
47- func RunConsumer (rootCtx context.Context , config * batchv1alpha1 .Config ) error {
48+ func RunConsumer (rootCtx context.Context , config * v1 .Config ) error {
4849 if config .LogLevel != "" {
4950 logger .StandardLogger ().SetLogLevel (config .LogLevel )
5051 rootCtx .Infof ("Set log level to %s => %v" , config .LogLevel , rootCtx .Logger .GetLevel ())
5152 }
5253
53- rootCtx .Infof ("Config: \n %+v" , pretty (config ))
54+ rootCtx .Tracef ("Config: \n %+v" , pretty (config ))
5455
5556 sub , err := Subscribe (rootCtx , config )
5657 if err != nil {
5758 return oops .Wrapf (err , "Error building URL" )
5859 }
5960
60- rootCtx .Infof ("Receiving messages from %+v " , sub )
61+ rootCtx .Infof ("Consuming from %s " , config . String () )
6162 ctx2 , cancel := gocontext .WithCancel (gocontext .Background ())
6263 shutdown .AddHook (func () {
63- rootCtx .Infof ("Shutting down consumer" )
6464 cancel ()
6565 })
6666
@@ -160,15 +160,27 @@ func RunConsumer(rootCtx context.Context, config *batchv1alpha1.Config) error {
160160 continue
161161 }
162162
163- _delay := time .Second * 5
164- if msg .Nackable () {
165- msg .Nack ()
163+ if exec .Retry == nil {
164+ exec .Retry = & v1.Retry {
165+ Attempts : 3 ,
166+ Delay : 30 ,
167+ }
166168 }
169+
167170 if err != nil {
168- ctx .Errorf ("Error running, (retrying in %s %v \n %s" , _delay , err , exec . Script )
171+ ctx .Errorf ("Error running %s: %s \n %s" , exec . Script , err , details )
169172 } else {
170173 ctx .Errorf ("Script returned non-zero exit code: %s" , details )
171- time .Sleep (_delay )
174+ }
175+
176+ delay := retry .GetBackoff (ctx , msg .LoggableID , exec .Retry )
177+ if delay != nil {
178+ if msg .Nackable () {
179+ msg .Nack ()
180+ }
181+ time .Sleep (* delay )
182+ } else {
183+ msg .Ack ()
172184 }
173185
174186 } else {
0 commit comments