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

Refs #37104 - fix sync_management #10873

Merged
merged 1 commit into from
Feb 1, 2024

Conversation

MariaAga
Copy link
Contributor

  • in app/assets/javascripts/katello/sync_management/sync_management.js
    the real change is to move KT.content = and KT.content_actions = to be at the top, and remove $(document).ready(function() { as it will only run the script when the document is ready because of the content_for
  • moving katello.common.js from sync_management/index.js to sync_management/index.html.erb to make sure its fully loaded in time
  • Wrapping all the scripts in content_for so they wont run too early

@MariaAga MariaAga mentioned this pull request Jan 31, 2024
@evgeni
Copy link
Member

evgeni commented Jan 31, 2024

Hmm, trying the packit build, it now fails to load common/index.js:

GET https://centos8-stream-katello-nightly.tanso.example.com/assets/katello/common/index.js [HTTP/2 404  1ms]

Edit: https://centos8-stream-katello-nightly.tanso.example.com/assets/katello/common/index-41006bb366e1429ef7397de66336e76035be453bc9bac600cd845b9571266232.js would exist. Something is missing to add the hash?

Comment on lines 2 to 3
<script src="/assets/katello/common/index.js"></script>
<script src="/assets/katello/sync_management.js"></script>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<script src="/assets/katello/common/index.js"></script>
<script src="/assets/katello/sync_management.js"></script>
<%= javascript 'katello/common/index' %>
<%= javascript 'katello/sync_management' %>

this makes things load the right files but in the wrong order:

<script type="text/javascript">
      const html = "  \n  \n  <script>\n    localize({\n      \"cancel\": \"Cancel\",\n      \"cancelled\": \"Sync Canceled\",\n      \"error\": \"Error\",\n      \"complete\": \"Sync complete.\",\n      \"no_start_time\": \"No start time currently available.\"\n    });\n\n      KT.repo_status = \$.parseJSON(\'{}\');\n      KT.permissions = { \"syncable\" : false };\n  <\/script>\n  <script src=\"/assets/katello/common/vendor-9f3c77c20de190d174986979ad7d0d5cee9362a9a280adabf643e57ee4259155.js\"><\/script>\n  <script src=\"/assets/katello/common-41006bb366e1429ef7397de66336e76035be453bc9bac600cd845b9571266232.js\"><\/script>\n  <script src=\"/assets/katello/common/index-41006bb366e1429ef7397de66336e76035be453bc9bac600cd845b9571266232.js\"><\/script><script src=\"/assets/katello/sync_management-db168612191a6dc850c8d5cd28375ee6af25d8fd011e62638f11f7fd903f5025.js\"><\/script>\n"
      load_dynamic_javascripts(html);
    </script>

and well, this gives the localize error again

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<script src="/assets/katello/common/index.js"></script>
<script src="/assets/katello/sync_management.js"></script>
<%= javascript_include_tag 'katello/common/index' %>
<%= javascript_include_tag 'katello/sync_management' %>

This works.
Now, wth is the difference between javascript and javascript_include_tag?!

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Thinking more, this makes sense!

  • Your original intent was "have the script load tags and the following embedded code in one block, so that it gets executed in the right order", but had the bug that it ignored asset pipeline paths.
  • Using javascript creates a content_for(:javascripts…) block, which gets yielded into the :javascripts section individually (so the = of <%= didn't really do anything) and thus the order is fucked up
  • Using javascript_include_tag creates script tags that honor the asset pipeline AND prints them directly where the code is, thus resulting in the correct order as one block, which then gets yielded into :javascripts.

Copy link
Member

@evgeni evgeni Feb 1, 2024

Choose a reason for hiding this comment

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

Mhh. Clicking the "active only" checkbox still gives me

Uncaught TypeError: KT.content is undefined
    <anonymous> sync_management-db168612191a6dc850c8d5cd28375ee6af25d8fd011e62638f11f7fd903f5025.js:1
    jQuery 2
        dispatch
        handle

and spinner.gif is 404 (but that's already a problem in 4.9, and probably older: https://projects.theforeman.org/issues/37133)

Copy link
Member

Choose a reason for hiding this comment

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

>>> KT
<<< Object { utils: $(n), common: {…}, widget: {}, getData: getData(n), orgswitcher: {…}, menu: {…}, object: {…} }

>>> load_dynamic_javascripts("<script src=\"/assets/katello/sync_management-db168612191a6dc850c8d5cd28375ee6af25d8fd011e62638f11f7fd903f5025.js\"><\/script>")
<<< undefined

>>> KT
<<< Object { utils: $(n), common: {…}, widget: {}, getData: getData(n), orgswitcher: {…}, menu: {…}, object: {…}, content: {…}, content_actions: {…} }

So reloading that sync management script works. WTH.

Copy link
Member

Choose a reason for hiding this comment

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

Could it be something re-initializes KT, thus overwriting what this code did?

Comment on lines 2 to 4
<%= javascript "katello/hosts/host_and_hostgroup_edit" %>
<% content_for(:javascripts) do -%>
<%= javascript "katello/hosts/host_and_hostgroup_edit" %>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines 2 to 3
<%= javascript "katello/hosts/activation_key_edit" %>
<% content_for(:javascripts) do -%>
<%= javascript "katello/hosts/activation_key_edit" %>
<% end %>

Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines 2 to 4

<%= javascript_include_tag 'katello/organizations/download_certificate' %>

<% content_for(:javascripts) do -%>
<%= javascript_include_tag 'katello/organizations/download_certificate' %>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

should we standardize on either

<%= javascript … %>

OR

  <% content_for(:javascripts) do -%>
    <%= javascript_include_tag … %>
  <% end %>

using both seems odd?

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 rather use

  <% content_for(:javascripts) do -%>
    <%= javascript_include_tag … %>
  <% end %>

just because when I see <%= javascript … %> I assumed it was something default from ruby and not a custom thing

Copy link
Member

Choose a reason for hiding this comment

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

I do agree that ths makes it more explicit.

@@ -1,17 +1,19 @@
<%= javascript 'katello/sync_management' %>
<% content_for(:javascripts) do -%>
Copy link
Member

@evgeni evgeni Feb 1, 2024

Choose a reason for hiding this comment

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

Suggested change
<% content_for(:javascripts) do -%>
<% content_for(:katello_javascripts) do -%>

THIS fixes it.

the reset happens here:

<% content_for(:javascripts) do %>
<%= javascript_include_tag "katello/common/vendor" %>
<%= javascript_include_tag "katello/common" %>
<%= yield(:katello_javascripts) %>
<% end %>

and once done like this, we don't have to reference katello/common/index ourself at all, as that is now loaded in the right order

Copy link
Member

Choose a reason for hiding this comment

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

<% content_for(:katello_javascripts) do -%>
  <%= javascript_include_tag 'katello/sync_management' %>
  <script>
    localize({
      "cancel": "<%= escape_javascript(_('Cancel')) %>",
      "cancelled": "<%= escape_javascript(_('Sync Canceled')) %>",
      "error": "<%= escape_javascript(_('Error')) %>",
      "complete": "<%= escape_javascript(_('Sync complete.')) %>",
      "no_start_time": "<%= escape_javascript(_('No start time currently available.')) %>"
    });

      KT.repo_status = $.parseJSON('<%= escape_javascript(@repo_status.to_json.html_safe) %>');
      KT.permissions = { "syncable" : <%= any_syncable? %> };
  </script>
<% end -%>

this is how it looks in full on my box now

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 still get "Uncaught TypeError: Cannot read properties of undefined (reading 'select_repo')" on select none with this?

Copy link
Member

Choose a reason for hiding this comment

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

Works for me (on a prod install, don't have a devel katello right now)

Copy link
Member

Choose a reason for hiding this comment

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

This is the code generated for loading (I added line breaks for readability):

<script type="text/javascript">
      const html = "
        <script src=\"/assets/katello/common/vendor-9f3c77c20de190d174986979ad7d0d5cee9362a9a280adabf643e57ee4259155.js\"><\/script>\n
        <script src=\"/assets/katello/common-41006bb366e1429ef7397de66336e76035be453bc9bac600cd845b9571266232.js\"><\/script>\n
        <script src=\"/assets/katello/sync_management-db168612191a6dc850c8d5cd28375ee6af25d8fd011e62638f11f7fd903f5025.js\"><\/script>\n
        <script>\n    localize({\n      \"cancel\": \"Cancel\",\n      \"cancelled\": \"Sync Canceled\",\n      \"error\": \"Error\",\n      \"complete\": \"Sync complete.\",\n      \"no_start_time\": \"No start time currently available.\"\n    });\n\n      KT.repo_status = \$.parseJSON(\'{\\\"1\\\":{\\\"id\\\":1,\\\"product_id\\\":1,\\\"progress\\\":{},\\\"state\\\":\\\"Never Synced\\\",\\\"raw_state\\\":\\\"never_synced\\\"}}\');\n      KT.permissions = { \"syncable\" : true };\n  <\/script>\n\n"
      load_dynamic_javascripts(html);
    </script>

Copy link
Member

Choose a reason for hiding this comment

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

Yeah… but that still means that the code gets executed twice, which seems wrong :)

Copy link
Member

Choose a reason for hiding this comment

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

I mean… It's probably still a good idea to have such a safety guard, but it should not be the "real" fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which code gets executed twice?

Copy link
Member

Choose a reason for hiding this comment

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

katello/common gets executed twice if we use content_for(:javascripts) (and not :katello_javascripts), as that generates:

  • katello/common (from sync_management/index.html)
  • katello/sync_management
  • katello/common (from layouts/katello)

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 mean… It's probably still a good idea to have such a safety guard, but it should not be the "real" fix

Yeah, I thought you meant adding it will cause the double execution.
Added it to the pr and used your solution, thanks!

@evgeni
Copy link
Member

evgeni commented Feb 1, 2024

FWIW, I'd omit the "Refs #37104 - wrap scripts in content_for(:javascripts)" commit from this PR and only have the "fix sync_management" changes

(but I am neither author, nor reviewer, so just stating my thoughts ;) )

@jeremylenz
Copy link
Member

merging based on @evgeni's review

Thanks @MariaAga !

@jeremylenz jeremylenz merged commit e8229c3 into Katello:master Feb 1, 2024
6 checks passed
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.

3 participants