@@ -72,6 +72,18 @@ func (j *Job) Run() {
7272 if j .log == nil {
7373 j .log = log .NewNopLogger ()
7474 }
75+
76+ // if the interval is not set > 0, create needed channels
77+ if j .Interval <= 0 {
78+ if j .Trigger == nil {
79+ j .Trigger = make (chan bool )
80+ }
81+
82+ if j .Done == nil {
83+ j .Done = make (chan bool )
84+ }
85+ }
86+
7587 // if there are no connection URLs for this job it can't be run
7688 if j .Connections == nil {
7789 level .Error (j .log ).Log ("msg" , "No conenctions for job" , "job" , j .Name )
@@ -110,13 +122,29 @@ func (j *Job) Run() {
110122 // enter the run loop
111123 // tries to run each query on each connection at approx the interval
112124 for {
113- bo := backoff .NewExponentialBackOff ()
114- bo .MaxElapsedTime = j .Interval
115- if err := backoff .Retry (j .runOnce , bo ); err != nil {
116- level .Error (j .log ).Log ("msg" , "Failed to run" , "err" , err )
125+ // if the interval is 0 or lower, wait to be triggered
126+ if j .Interval <= 0 {
127+ j .log .Log ("level" , "debug" , "msg" , "Wait for trigger" )
128+ // wait for trigger
129+ <- j .Trigger
130+
131+ if err := j .runOnce (); err != nil {
132+ j .log .Log ("level" , "error" , "msg" , "Failed to run" , "err" , err )
133+ }
134+
135+ // send to done chanel
136+ j .Done <- true
137+ j .log .Log ("level" , "debug" , "msg" , "Job finished" )
138+ } else {
139+ // interval is grater than 0 so procide with async operation
140+ bo := backoff .NewExponentialBackOff ()
141+ bo .MaxElapsedTime = j .Interval
142+ if err := backoff .Retry (j .runOnce , bo ); err != nil {
143+ level .Error (j .log ).Log ("msg" , "Failed to run" , "err" , err )
144+ }
145+ level .Debug (j .log ).Log ("msg" , "Sleeping until next run" , "sleep" , j .Interval .String ())
146+ time .Sleep (j .Interval )
117147 }
118- level .Debug (j .log ).Log ("msg" , "Sleeping until next run" , "sleep" , j .Interval .String ())
119- time .Sleep (j .Interval )
120148 }
121149}
122150
0 commit comments