-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to run job and wait for result #229
base: develop
Are you sure you want to change the base?
Add method to run job and wait for result #229
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Renrut5 it looks like you have some conflicts now due to some merged PRs, but otherwise this LGTM. Thank you.
@joewesch rebased and updated. |
pynautobot/core/endpoint.py
Outdated
>>> nb.extras.jobs.run_and_wait( | ||
class_path="local/data_quality/VerifyHostnames", | ||
data={"hostname_regex": ".*"}, | ||
commit=True, | ||
interval=5, | ||
max_rechecks=10, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an example of setting the response to a variable and maybe how they can use the response in a meaningful way (e.g., printing something)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joewesch added some changes, is that what you're looking for?
@Renrut5 do you plan on finishing this PR or should we close it? |
…dd-run-job-and-wait-method
pynautobot/core/endpoint.py
Outdated
>>> job_response = nb.extras.jobs.run_and_wait( | ||
class_path="local/data_quality/VerifyHostnames", | ||
data={"hostname_regex": ".*"}, | ||
commit=True, | ||
interval=5, | ||
max_rechecks=10, | ||
) | ||
>>> print(f"Job completed, Job Result ID: {job_response.job_result.id}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method doesn't seem to return the same object as .run()
In [1]: from pynautobot import api
In [2]: nb = api(url="https://demo.nautobot.com/", token=40*"a")
In [3]: response1 = nb.extras.jobs.run(job_id="d15877c4-dc10-45db-9d70-9405727cfb55")
In [4]: response1.job_result.id
Out[4]: '6875ec6b-39b3-4d22-a174-41f09d00dc3b'
In [5]: response2 = nb.extras.jobs.run_and_wait(job_id="d15877c4-dc10-45db-9d70-9405727cfb55")
In [6]: response2.job_result.id
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[6], line 1
----> 1 response2.job_result.id
File ~/pynautobot/pynautobot/core/response.py:187, in Record.__getattr__(self, k)
184 if ret or hasattr(self, k):
185 return ret
--> 187 raise AttributeError('object has no attribute "{}"'.format(k))
AttributeError: object has no attribute "job_result"
In [7]: response2.id
Out[7]: '8179b813-6819-4b3c-8caf-e064d7a7c676'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, I think it should return the same object type, so please fix it to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joewesch fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's utilize the existing methods to do this instead:
job_result_id = job_obj.job_result.id | ||
job_result_url = f"{self.base_url}/extras/job-results/{job_result_id}/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
job_result_id = job_obj.job_result.id | |
job_result_url = f"{self.base_url}/extras/job-results/{job_result_id}/" | |
job_result = job_obj.job_result |
req = Request( | ||
base=job_result_url, | ||
token=self.token, | ||
http_session=self.api.http_session, | ||
api_version=api_version, | ||
).get() | ||
|
||
result = req.get("job_result", {}) | ||
status = result.get("status", {}).get("value") | ||
|
||
if status not in active_job_statuses: | ||
return job_obj |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req = Request( | |
base=job_result_url, | |
token=self.token, | |
http_session=self.api.http_session, | |
api_version=api_version, | |
).get() | |
result = req.get("job_result", {}) | |
status = result.get("status", {}).get("value") | |
if status not in active_job_statuses: | |
return job_obj | |
# Refresh the job result | |
job_result.full_details() | |
if job_result.status not in active_job_statuses: | |
return job_obj |
closes #32