When importing content using Feeds using an importer associated with a node type, the mapping can be configured to assign taxonomy terms from the Feed Node to the imported Feed Items. On a project, we need to extend this feature by also updating all the already imported Feed Items when a Feed Node is updated.
This can be done using hook_node_update($node)
. But since a single Feed Node can have a lot of Feed Items, they cannot be all updated in a single HTTP request otherwise we may encounter a timeout. The obvious way to avoid timeouts, is to use the Batch operations API.
Most of the time, hook_node_update()
implementations will be executed because node_save
has been called from a form submit handler, therefore, most of the time, there is no need to call batch_process from a hook_node_update()
since it will be called automatically at the end of the form processing. Calling batch_progress
from hook_node_update()
may also produce unexpected result since it will prevent other implementation of the same hook or others hooks to be executed.
But what about the times when the hook_node_update()
implementations are not executed from a form submit handler. For instance, when node_save
is called from an implementation of hook_cron
? Is there a way to detect these cases to either run all the update at once or delay them to a dedicated queue?