Organising rules for efficient execution

Events happening in the community are now at Drupal community events on www.drupal.org.
pragnatek's picture

I'm getting the hang of writing custom actions but I need some help in how to execute a number of rules together into a workflow that doesn't slow my site down too much.

I have a number of custom actions executed on the event 'After saving new content' with a condition of 'Content has type'.

The actions include:

  • Load 2 referenced nodes
  • Calculate & write new cck field values into these referenced nodes
  • Send emails to multiple users

All actions work as expected but I do have a slight performance problem with my 'Send emails' action. I'm not using contrib messaging modules to send email but just using the drupal_mail() function. This is a lightweight & simple to use function for my purposes (I am adding to the weight a bit by using htmlmail).

I'm building the list of users to mail to by running a db_query based on values from my loaded referenced nodes. I'm also customising the email body using values returned in this query. I'm then looping through the list and executing drupal_mail() against each user. As you can imagine this takes a bit of time but in itself the couple of seconds it takes is not the problem.

However because the email action is triggered from the same event that also writes values and saves to my content nodes, the user is not returned to a node view page until the drupal_mail functions have executed.

What I really want is that my custom node updates complete quickly and the user is returned to a view of their node page. Meantime (in the background) drupal_mail gets to work sending the emails.

What would be a good way to achieve this using Rules?
(I'm looking at sets and schedules - but some reason I cannot get my head round it this afternoon!)

Thanks for any advice.

Comments

Be grateful if anyone has any

pragnatek's picture

Be grateful if anyone has any thoughts on this. Thanks

A few ideas

itangalo's picture

A few comments/ideas:

  • Running db_queries to load users seem wrong to me – can't this be done by using user_load() combined with information you already got in the node? And, as always, don't use the "Execute custom PHP" action – unless it's used for prototyping.
  • The Rules Bonus Pack module has an action for sending e-mail to all users in a user reference field. Maybe this can take some load off?
  • If you want to execute the e-mail rule at another time than the actual node save, I would suggest putting it all in a rule set and then use Rules Scheduler to send it (say) 5 minutes later.

Makes sense?
//Johan Falk
**
Learn Drupal with Nodeone! Drupal 7 introduction | The Views module | Learn Page manager! | Drupal configuration learning curve | Rules for Drupal 6 | Theming Drupal 6 | 49 Drupal 6 modules you should know

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: