Sometmes entities are updated in shutdown functions, an example would be workbench_moderation module. This can happen after _search_api_index_queued_items execution which may cause incorrect data beeing indexed and entities set for indexation on the next cron run instead of beeing indexed immediately (if the setting is present).

Comments

Graber created an issue. See original summary.

graber’s picture

Status: Active » Needs review
StatusFileSize
new1.95 KB

Solution: made the indexation registration happen as late as possible.

graber’s picture

Title: _search_api_index_queued_items called to early » _search_api_index_queued_items called too early
drunken monkey’s picture

Thanks for creating this issue report!
While I appreciate that this might cause a problem in rare cases, I'm unsure whether this really warrants that additional code and complexity. It does seem like a rather "edgy" edge case – and with such code, there's always the possibility of now running into a different edge case.
So, thanks, but unless more people come out here in support of fixing this, I don't think I'll be committing this.

graber’s picture

Title: _search_api_index_queued_items called too early » Items not indexed immediately with workbench moderation on

I updated the issue title so people will find it easier. This caused many issues for a very long time on one large project I'm currently working on and was damn hard to debug, because of many possible causes and code beeing executed in shutdown functions. Hopefully it'll save someone a bit of struggle :)

As to edge cases - Yes, there may be a case where something would need to run after sending items to a search server and that patch would make it impossible. I fixed the issue by adding an extra late call of _search_api_index_queued_items on every admin page request in one of custom modules, quite similar as in the attached patch.

donquixote’s picture

So, thanks, but unless more people come out here in support of fixing this, I don't think I'll be committing this.

I think the problem applies only in specific scenarios.
We had this with search_api_et + workbench_moderation, and this site still has an older version of the latter. Not sure if the same would happen in a newer version.

Normally, without search_api_et, it would go like this:
If we look in node_save():

  • hook_node_update() runs first, here workbench_moderation registers its shutdown function.
  • hook_entity_update() runs after that, here search_api registers its shutdown function.

So the shutdown function from search_api would run after workbench_moderation, which is good.

But with search_api_et and entity_translation, we get this when a translation is saved:
Again, in hook_node_save():

  • hook_field_attach_update() runs first, here the translations are saved. From here, hook_entity_translation_update() is called, and from there search_api_et calls search_api to register the shutdown function.
  • hook_node_update() runs, here workbench_moderation registers its shutdown function.
  • hook_entity_update() runs after that, here search_api would register its shutdown function - but it is already registered.

There could also be other scenarios where the order of shutdown functions would be different, e.g. if another entity gets saved earlier in the request.

I'm unsure whether this really warrants that additional code and complexity.

The same can be achieved in a simpler and more transparent way.

donquixote’s picture

Status: Needs review » Needs work

The patch in #6 has a problem, if search_api_index_specific_items_delayed() is called within another shutdown function. In that case, the prepended function is never called.

Instead we need this:

    // Register the shutdown function relative to other shutdown functions from
    // known contrib modules, so that the main code will run after other
    // shutdown functions, especially after workbench_moderation_store().
    // To achieve this, append a shutdown function which appends another
    // shutdown function.
    drupal_register_shutdown_function(
      'drupal_register_shutdown_function',
      '_search_api_index_queued_items');
mibfire’s picture

Here is the patch that fixes the issues what @donquixote mentioned in https://www.drupal.org/project/search_api/issues/2930706#comment-13832461 comment.

drunken monkey’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB
new760 bytes

Thanks a lot for your work on this. I just updated the comment and code style a bit (making the interdiff pretty useless, I guess …).
Could someone else please confirm that this works for them? Then I can commit.

drunken monkey’s picture

Could someone please test this?

joel_osc’s picture

Hi @drunkenmonkey, I just tested this patch and it works great! Thank-you to everyone for working on this. +1 for RBTC.

drunken monkey’s picture

Status: Needs review » Fixed

Great to hear, thanks for testing and reporting!
Committed. Thanks again, everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.