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

AttributeError when running database-changing Job from nautobot-server runjob --commit #944

Closed
glennmatthews opened this issue Sep 24, 2021 · 0 comments · Fixed by #945
Closed
Assignees
Labels
type: bug Something isn't working as expected

Comments

@glennmatthews
Copy link
Contributor

Environment

  • Python version:
  • Nautobot version: 1.1.3

Steps to Reproduce

  1. Use the nautobot-server runjob --commit <job> command to enqueue a Job that will make any change to the database when run
  2. When the Job calls any model's save() method, an AttributeError is thrown:
File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 754, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 803, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "/usr/local/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 179, in send
    for receiver in self._live_receivers(sender)
  File "/usr/local/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 179, in 
    for receiver in self._live_receivers(sender)
  File "/usr/local/lib/python3.7/site-packages/nautobot/utilities/utils.py", line 311, in _curried
    return _curried_func(*args, *moreargs, **{**kwargs, **morekwargs})
  File "/usr/local/lib/python3.7/site-packages/nautobot/extras/signals.py", line 62, in _handle_changed_object
    objectchange.user = _get_user_if_authenticated(request, objectchange)
  File "/usr/local/lib/python3.7/site-packages/nautobot/extras/signals.py", line 38, in _get_user_if_authenticated
    if request.user.is_authenticated:
AttributeError: 'NoneType' object has no attribute 'user'

Expected Behavior

Job to run successfully to completion just as it would if invoked via the UI or REST API

Observed Behavior

Job aborts and is cleaned up.

Initial Analysis

The runjob command calls:

        job_result = JobResult.enqueue_job(
            run_job,
            job_class.class_path,
            job_content_type,
            None,
            data={},
            request=None,  #  <-------<< important
            commit=options["commit"],
        )

JobResult.enqueue_job passes the provided request (in this case None) as a parameter to the enqueued run_job function.

When run_job runs with commit=True, it calls with change_logging(request): which enables change-logging for all DB changes triggered by the job via the _handle_changed_object signal.

_handle_changed_object expects a valid request object that it can retrieve the calling user and request-id from, but since it's None, an AttributeError is thrown.

Possible solutions:

  1. In runjob, create a mock Request object via web_request_context and pass that instead of None
  2. Enhance _handle_changed_object to gracefully handle a null Request.

IMO option 1 is probably better.

@glennmatthews glennmatthews added type: bug Something isn't working as expected status: accepted labels Sep 24, 2021
@glennmatthews glennmatthews self-assigned this Sep 24, 2021
glennmatthews added a commit that referenced this issue Sep 24, 2021
glennmatthews added a commit that referenced this issue Sep 30, 2021
…#945)

* Fix #944: require a username when running a database-affecting Job from the command line.

* Apply Veit's suggestions from code review

Co-authored-by: Veit Heller <veit@veitheller.de>

* Revise jobs.md

Co-authored-by: Veit Heller <veit@veitheller.de>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant