On Fri, Mar 18, 2011 at 10:53:40AM +0100, Rene Nussbaumer wrote:
> On Fri, Mar 18, 2011 at 10:41 AM, Iustin Pop <[email protected]> wrote:
> > From: Theo Van Dinter <[email protected]>
> >
> > This adds a new method WaitForJobCompletion that can be used for
> > client who are not interested in the entire job log, just in its
> > completion status.
> > ---
> >  lib/rapi/client.py |   33 ++++++++++++++++++++++++++++++++-
> >  1 files changed, 32 insertions(+), 1 deletions(-)
> >
> > diff --git a/lib/rapi/client.py b/lib/rapi/client.py
> > index b49db8a..d9e4fa2 100644
> > --- a/lib/rapi/client.py
> > +++ b/lib/rapi/client.py
> > @@ -1,7 +1,7 @@
> >  #
> >  #
> >
> > -# Copyright (C) 2010 Google Inc.
> > +# Copyright (C) 2010, 2011 Google Inc.
> >  #
> >  # This program is free software; you can redistribute it and/or modify
> >  # it under the terms of the GNU General Public License as published by
> > @@ -39,6 +39,7 @@ import socket
> >  import urllib
> >  import threading
> >  import pycurl
> > +import time
> >
> >  try:
> >   from cStringIO import StringIO
> > @@ -1161,6 +1162,36 @@ class GanetiRapiClient(object): # pylint: 
> > disable-msg=R0904
> >                              "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
> >                              None, None)
> >
> > +  def WaitForJobCompletion(self, job_id, period=5, retries=-1):
> > +    """Polls cluster for job status until completion.
> > +
> > +    Completion is defined as any of the following states:
> > +      "error", "canceled", or "success"
> > +
> > +    @type job_id: int
> > +    @param job_id: job id to watch
> > +
> > +    @type period: int
> > +    @param period: how often to poll for status (optional, default 5s)
> > +
> > +    @type retries: int
> > +    @param retries: how many time to poll before giving up
> > +                    (optional, default -1 means unlimited)
> > +
> > +    @rtype: bool
> > +    @return: True if job succeeded or False if failed/status timeout
> > +    """
> > +    while retries != 0:
> > +      job_result = self.GetJobStatus(job_id)
> > +      if not job_result or job_result["status"] in ("error", "canceled"):
> > +        return False
> > +      if job_result["status"] == "success":
> > +        return True
> > +      time.sleep(period)
> > +      if retries > 0:
> > +        retries -= 1
> > +    return False
> > +
> 
> It would probably make sense to use utils.Retry here, what do you think?

Except for the fact that the RAPI client is designed to be standalone,
and can't use any Ganeti libraries, yes :)

thanks,
iustin

Reply via email to