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

Job Log Model #1030

Merged
merged 30 commits into from Dec 13, 2021
Merged

Job Log Model #1030

merged 30 commits into from Dec 13, 2021

Conversation

smk4664
Copy link
Contributor

@smk4664 smk4664 commented Oct 25, 2021

Fixes #992, fixes #864, fixes #650, fixes #453

This adds a new model called JobLogEntry to store log messages from jobs. For #650 it removes the conditional rendering, but does not change the JS. I might be able to fix that before the review is complete though.

Here are some screenshots of the changes:
Screen Shot 2021-10-23 at 10 41 49 PM
Screen Shot 2021-10-23 at 10 42 16 PM
Screen Shot 2021-10-23 at 10 42 56 PM
Screen Shot 2021-10-24 at 9 38 39 PM
Screen Shot 2021-10-24 at 9 39 03 PM

This adds the JobLogEntry and migrations to convert existing
logs from JobResult to the new Model.
This also removed duplicate views. Ensured that existing
functionality was kept between the views.
Since the deletion of git repositories happens outside of
the worker, the job_logs db was not able to find the job_resutl.
Since this does not run in transaction.atomic, we don't need the
job_logs db.
Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

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

Very nice work! Thanks for taking this on.

nautobot/docs/additional-features/jobs.md Outdated Show resolved Hide resolved
nautobot/docs/additional-features/jobs.md Outdated Show resolved Hide resolved
nautobot/docs/models/extras/jobresult.md Outdated Show resolved Hide resolved
nautobot/extras/datasources/git.py Outdated Show resolved Hide resolved
nautobot/extras/datasources/registry.py Outdated Show resolved Hide resolved
nautobot/extras/tables.py Outdated Show resolved Hide resolved
nautobot/extras/templates/extras/inc/jobresult.html Outdated Show resolved Hide resolved
nautobot/extras/tests/test_jobs.py Show resolved Hide resolved
nautobot/extras/tests/test_jobs.py Outdated Show resolved Hide resolved
smk4664 and others added 8 commits October 25, 2021 12:22
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
nautobot/docs/models/extras/jobresult.md Outdated Show resolved Hide resolved
nautobot/extras/management/commands/runjob.py Outdated Show resolved Hide resolved
nautobot/extras/models/models.py Outdated Show resolved Hide resolved
nautobot/extras/models/models.py Outdated Show resolved Hide resolved
nautobot/extras/models/models.py Outdated Show resolved Hide resolved
nautobot/extras/models/models.py Outdated Show resolved Hide resolved
nautobot/extras/signals.py Show resolved Hide resolved
nautobot/extras/tables.py Show resolved Hide resolved
nautobot/extras/tests/test_jobs.py Outdated Show resolved Hide resolved
nautobot/extras/tests/test_jobs.py Show resolved Hide resolved
@glennmatthews
Copy link
Contributor

Can you please do some testing with this changeset and the nautobot-ssot plugin just to verify that the plugin continues to operate as expected?

@glennmatthews glennmatthews added this to the v1.2.0 milestone Nov 18, 2021
@smk4664
Copy link
Contributor Author

smk4664 commented Nov 18, 2021

Of course!

nautobot/docs/models/extras/jobresult.md Outdated Show resolved Hide resolved
nautobot/extras/models/models.py Outdated Show resolved Hide resolved
nautobot/extras/datasources/registry.py Outdated Show resolved Hide resolved
nautobot/extras/models/models.py Outdated Show resolved Hide resolved
nautobot/extras/tables.py Show resolved Hide resolved
nautobot/extras/tests/test_jobs.py Outdated Show resolved Hide resolved
smk4664 and others added 4 commits November 22, 2021 14:26
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
This will only happen when including the extras/inc/jobresult.html
from within a plugin and not providing the table. This will help
plugins such as the Nautobot SSOT with the transition.
Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

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

This is looking great!

nautobot/extras/management/commands/runjob.py Show resolved Hide resolved
nautobot/extras/templates/extras/gitrepository_result.html Outdated Show resolved Hide resolved
nautobot/extras/templates/extras/jobresult.html Outdated Show resolved Hide resolved
nautobot/extras/tests/test_models.py Outdated Show resolved Hide resolved
smk4664 and others added 3 commits December 6, 2021 15:35
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

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

Almost there! This is a complicated set of page templates, appreciate you putting in the effort here! I think there's room for some refinement in how the extras/jobresult.html and extras/inc/jobresult.html pages intersect, happy to spend some time pairing on this tomorrow if you'd like.

@smk4664
Copy link
Contributor Author

smk4664 commented Dec 6, 2021

Almost there! This is a complicated set of page templates, appreciate you putting in the effort here! I think there's room for some refinement in how the extras/jobresult.html and extras/inc/jobresult.html pages intersect, happy to spend some time pairing on this tomorrow if you'd like.

I would appreciate the time. It is a big change.

Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks for seeing this through.

Copy link
Contributor

@jathanism jathanism left a comment

Choose a reason for hiding this comment

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

Sorry to be a thorn in the 11th hour, but there are some concerns about query performance I would like to be discussed before we merge this.

@@ -132,6 +132,10 @@ def _configure_settings(config):
if settings.METRICS_ENABLED and "postgres" in settings.DATABASES["default"]["ENGINE"]:
settings.DATABASES["default"]["ENGINE"] = "django_prometheus.db.backends.postgresql"

# Create fake db for job logs. This uses the default db, but allows us to save logs within
# transaction.atomic().
settings.DATABASES["job_logs"] = settings.DATABASES["default"]
Copy link
Contributor

Choose a reason for hiding this comment

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

This worries me in that it is a magic setting that we're trying to move away from.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, if we move away from running jobs in Atomic Transactions then this can go away as well. This is not meant to be modified, so that is why we stuck it here.

nautobot/docs/models/extras/joblogentry.md Outdated Show resolved Hide resolved
nautobot/docs/models/extras/joblogentry.md Outdated Show resolved Hide resolved
nautobot/docs/models/extras/jobresult.md Outdated Show resolved Hide resolved
nautobot/extras/constants.py Outdated Show resolved Hide resolved
Comment on lines +77 to +79
groups = set(JobLogEntry.objects.filter(job_result=job_result).values_list("grouping", flat=True))
for group in sorted(groups):
logs = JobLogEntry.objects.filter(job_result__pk=job_result.pk, grouping=group)
Copy link
Contributor

Choose a reason for hiding this comment

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

This smells like possible bad query performance to me. Have you considered the following?

  • Ordering the query by grouping to eliminate the need to use sorted(groups)?
  • Using prefetch_related on job_result to get all of these objects in a single query, instead of round trips to the database for each group?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was asked to add sorting to as a set does not have an order. I could change the filter, but it would still be in an unordered set. @glennmatthews was concerned about the sorting.

I will look at the prefetch_related.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also to note, that this is only for running the jobs from the CLI, which I would think is rare.

)
)

for log_entry in attrs["log"]:
status = log_entry[1]
for log_entry in logs:
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using logs.iterator() here since these are single-use instances that you're not mutating.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will look into iterator, but this is only used when running jobs through management commands, which has limited use as they can't take arguments.

smk4664 and others added 5 commits December 13, 2021 12:57
Co-authored-by: Jathan McCollum <jathan@gmail.com>
Co-authored-by: Jathan McCollum <jathan@gmail.com>
Co-authored-by: Jathan McCollum <jathan@gmail.com>
Copy link
Contributor

@jathanism jathanism left a comment

Choose a reason for hiding this comment

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

:shipit:

@glennmatthews
Copy link
Contributor

Bother, this should have been retargeted into develop for 1.2.0 rather than next for 1.3.0; I missed that detail. Looking into sorting that out...

glennmatthews added a commit that referenced this pull request Dec 13, 2021
* Create JobLogEntry and migrations.

This adds the JobLogEntry and migrations to convert existing
logs from JobResult to the new Model.

* Remove functions and documentation that is no longer relevent.

* Fix tests and allow JobLogEntries to be created outside transaction.atomic

* Convert views to use generic.objectview and django-tables2.

This also removed duplicate views. Ensured that existing
functionality was kept between the views.

* Correct git repository deletion bug.

Since the deletion of git repositories happens outside of
the worker, the job_logs db was not able to find the job_resutl.
Since this does not run in transaction.atomic, we don't need the
job_logs db.

* Update nautobot/docs/additional-features/jobs.md

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Update nautobot/docs/models/extras/jobresult.md

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Update nautobot/docs/additional-features/jobs.md

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Update nautobot/extras/datasources/git.py

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Address page span issue with Additonal Data.

* Add truncating of object strings to the JobLogEntry.

* Address the rest of the feedback.

* Remove unused imports.

* Updating Migration order after upstream changes.

* Address feedback from PR.

* Black recent changes.

* Update nautobot/docs/models/extras/jobresult.md

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Update nautobot/extras/models/models.py

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Update nautobot/extras/models/models.py

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Make additional changes recommended by peer review.

* Display error message if the job log table is not available.

This will only happen when including the extras/inc/jobresult.html
from within a plugin and not providing the table. This will help
plugins such as the Nautobot SSOT with the transition.

* Update nautobot/extras/templates/extras/jobresult.html

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Update nautobot/extras/tests/test_models.py

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>

* Address hanging div's and add back active_tab.

* Update nautobot/docs/models/extras/joblogentry.md

Co-authored-by: Jathan McCollum <jathan@gmail.com>

* Update nautobot/docs/models/extras/jobresult.md

Co-authored-by: Jathan McCollum <jathan@gmail.com>

* Update nautobot/docs/models/extras/joblogentry.md

Co-authored-by: Jathan McCollum <jathan@gmail.com>

* Prepend constants with JOB_LOG_

* Black

Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
Co-authored-by: Jathan McCollum <jathan@gmail.com>
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.

None yet

4 participants