@@ -30,6 +30,7 @@ import (
3030 "github.com/v3io/v3io-go/pkg/controlplane"
3131 "github.com/v3io/v3io-go/pkg/errors"
3232
33+ "github.com/nuclio/errors"
3334 "github.com/nuclio/logger"
3435 "github.com/valyala/fasthttp"
3536)
@@ -245,6 +246,115 @@ func (s *session) GetRunningUserAttributesSync(getRunningUserAttributesInput *v3
245246 return & userNameOutput , err
246247}
247248
249+ // ReloadAppServicesConfigAndWaitForCompletion reloads the app service config in the backend and waits for job completion (blocking)
250+ func (s * session ) ReloadAppServicesConfigAndWaitForCompletion (ctx context.Context , retryInterval , timeout time.Duration ) error {
251+ jobId , err := s .ReloadAppServicesConfig (ctx )
252+ if err != nil {
253+ return errors .Wrap (err , "Failed reloading app service config" )
254+ }
255+
256+ return s .WaitForJobCompletion (ctx , jobId , retryInterval , timeout )
257+ }
258+
259+ // ReloadAppServicesConfig reloads the app service config in the backend (blocking)
260+ func (s * session ) ReloadAppServicesConfig (ctx context.Context ) (string , error ) {
261+ reloadAppServicesConfigInput := v3ioc.ReloadAppServicesConfigInput {
262+ ControlPlaneInput : v3ioc.ControlPlaneInput {
263+ Ctx : ctx ,
264+ },
265+ }
266+
267+ reloadAppServicesConfigJobOutput := v3ioc.ReloadAppServicesConfigJobOutput {}
268+
269+ err := s .createResource (ctx ,
270+ "configurations/app_services/reloads" ,
271+ "cluster_configuration_reload" ,
272+ & reloadAppServicesConfigInput .ControlPlaneInput ,
273+ map [string ]string {},
274+ & reloadAppServicesConfigJobOutput .ControlPlaneOutput ,
275+ & reloadAppServicesConfigJobOutput .JobAttributes )
276+
277+ if err != nil {
278+ return "" , err
279+ }
280+
281+ return reloadAppServicesConfigJobOutput .ID , nil
282+ }
283+
284+ // WaitForJobCompletion waits for completion of job with given id (blocking)
285+ func (s * session ) WaitForJobCompletion (ctx context.Context , jobId string , retryInterval , timeout time.Duration ) error {
286+ getJobsInput := v3ioc.GetJobsInput {
287+ ControlPlaneInput : v3ioc.ControlPlaneInput {
288+ ID : jobId ,
289+ Ctx : ctx ,
290+ },
291+ }
292+
293+ deadline := time .Now ().Add (timeout )
294+
295+ for time .Now ().Before (deadline ) {
296+ getJobsOutput , err := s .GetJobs (& getJobsInput )
297+ if err != nil {
298+ return errors .Wrap (err , "Failed getting job" )
299+ }
300+
301+ switch getJobsOutput .State {
302+ case v3ioc .JobStateCompleted :
303+ s .logger .DebugWithCtx (ctx ,
304+ "Job Completed" ,
305+ "jobId" , jobId ,
306+ "jobResult" , getJobsOutput .Result )
307+ return nil
308+ case v3ioc .JobStateFailed :
309+ s .logger .WarnWithCtx (ctx ,
310+ "Job has failed" ,
311+ "jobId" , jobId ,
312+ "jobResult" , getJobsOutput .Result )
313+ return errors .New (
314+ "Job has failed" )
315+ case v3ioc .JobStateCanceled :
316+ s .logger .WarnWithCtx (ctx ,
317+ "Job was canceled" ,
318+ "jobId" , jobId ,
319+ "jobResult" , getJobsOutput .Result )
320+ return errors .New ("Job was canceled" )
321+ default :
322+ s .logger .DebugWithCtx (ctx ,
323+ "Job in progress" ,
324+ "jobId" , jobId ,
325+ "retryInterval" , retryInterval ,
326+ "timeout" , timeout )
327+
328+ // TODO: create and use backoff
329+ time .Sleep (retryInterval )
330+ }
331+ }
332+
333+ return errors .New ("Timed out waiting for job completion" )
334+ }
335+
336+ // GetJobs gets jobs (blocking)
337+ func (s * session ) GetJobs (getJobsInput * v3ioc.GetJobsInput ) (* v3ioc.GetJobsOutput , error ) {
338+
339+ // prepare job response resource
340+ getJobsOutput := v3ioc.GetJobsOutput {}
341+
342+ // specific path for job detail endpoint
343+ detailPath := fmt .Sprintf ("jobs/%s" , getJobsInput .ID )
344+
345+ err := s .getResource (getJobsInput .Ctx ,
346+ detailPath ,
347+ & getJobsInput .ControlPlaneInput ,
348+ & getJobsOutput .ControlPlaneOutput ,
349+ & getJobsOutput .JobAttributes )
350+
351+ if err != nil {
352+ return nil , err
353+ }
354+
355+ return & getJobsOutput , err
356+ }
357+
248358func (s * session ) createResource (ctx context.Context ,
249359 path string ,
250360 kind string ,
0 commit comments