From: Ondrej Lichtner <olichtne(a)redhat.com>
The prepare_job method will create and return a lnst.Controller.Job
object the same as the Namespace.run method but won't send the command
to start it to the Slave. Instead the tester can call the Job.start
method himself to send the start command later.
This could be used to achieve better grouping of time related job
starts, currently it would only be useful if you intend to do resource
intensive work between Namespace.run calls, but I can imagine extending
this functionality to actually provide a more intelligent synchronized
start of multiple jobs.
Consider this just an idea, might be removed later.
v2:
* the Namespace.run method is now just a shortcut for prepare_job +
job.start. The exception handling isn't necessary and is removed
in this version
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst/Controller/Job.py | 9 +++++++++
lnst/Controller/Namespace.py | 32 +++++++++-----------------------
2 files changed, 18 insertions(+), 23 deletions(-)
diff --git a/lnst/Controller/Job.py b/lnst/Controller/Job.py
index 89b8451..0e17934 100644
--- a/lnst/Controller/Job.py
+++ b/lnst/Controller/Job.py
@@ -146,6 +146,15 @@ class Job(object):
else:
return False
+ def start(self, bg=False, timeout=DEFAULT_TIMEOUT):
+ self._netns._machine.run_job(self)
+
+ if not bg:
+ if not self.wait(timeout):
+ logging.debug("Killing timed-out job")
+ self.kill()
+ return self
+
def wait(self, timeout=DEFAULT_TIMEOUT):
"""waits for the Job to finish for the specified amount of time
diff --git a/lnst/Controller/Namespace.py b/lnst/Controller/Namespace.py
index af5bda1..c50152d 100644
--- a/lnst/Controller/Namespace.py
+++ b/lnst/Controller/Namespace.py
@@ -80,8 +80,13 @@ class Namespace(object):
returns a string name for any other namespace"""
return self._name
- def run(self, what, bg=False, fail=False, timeout=DEFAULT_TIMEOUT,
- json=False, desc=None, job_level=ResultLevel.DEBUG):
+ def prepare_job(self, what, fail=False, json=False, desc=None,
+ job_level=ResultLevel.DEBUG):
+ return Job(self, what, expect=not fail, json=json, desc=desc,
+ level=job_level)
+
+ def run(self, what, fail=False, json=False, desc=None,
+ job_level=ResultLevel.DEBUG, bg=False, timeout=DEFAULT_TIMEOUT):
"""
Args:
what (mandatory) -- what should be run on the host. Can be either a
@@ -105,27 +110,8 @@ class Namespace(object):
running Job remotely and when the result data arrives from the Slave
the Job object will be automatically updated.
"""
-
- job = Job(self, what, expect=not fail, json=json, desc=desc,
- level=job_level)
-
- try:
- self._machine.run_job(job)
-
- if not bg:
- if not job.wait(timeout):
- logging.debug("Killing timed-out job")
- job.kill()
- except:
- raise
- finally:
- pass
- #TODO check expect result here
- # if bg=True:
- # add "job started" result
- # else:
- # add job result
-
+ job = self.prepare_job(what, fail, json, desc, job_level)
+ job.start(bg, timeout)
return job
def __getattr__(self, name):
--
2.19.1