Skip to content
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

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from

Conversation

Renrut5
Copy link

@Renrut5 Renrut5 commented Aug 13, 2024

closes #32

@Renrut5 Renrut5 changed the title add run_and_wait method Add method to run job and wait for result Aug 13, 2024
@Renrut5 Renrut5 marked this pull request as ready for review August 15, 2024 17:48
@Renrut5 Renrut5 requested a review from a team August 15, 2024 17:48
joewesch
joewesch previously approved these changes Oct 10, 2024
Copy link
Contributor

@joewesch joewesch left a 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.

@Renrut5
Copy link
Author

Renrut5 commented Oct 10, 2024

@joewesch rebased and updated.

Comment on lines 700 to 706
>>> nb.extras.jobs.run_and_wait(
class_path="local/data_quality/VerifyHostnames",
data={"hostname_regex": ".*"},
commit=True,
interval=5,
max_rechecks=10,
)
Copy link
Contributor

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)?

Copy link
Author

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?

@joewesch
Copy link
Contributor

@Renrut5 do you plan on finishing this PR or should we close it?

Comment on lines 700 to 707
>>> 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}")
Copy link
Contributor

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'

Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joewesch fixed

Copy link
Contributor

@joewesch joewesch left a 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:

Comment on lines +714 to +715
job_result_id = job_obj.job_result.id
job_result_url = f"{self.base_url}/extras/job-results/{job_result_id}/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +733 to +744
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add method to easily run a Job from pynautobot
2 participants