awesomemotive / WP-Mail-SMTP

The most popular WordPress SMTP and PHP Mailer plugin. Trusted by over 3 million sites.
https://wordpress.org/plugins/wp-mail-smtp/
GNU General Public License v3.0
55 stars 34 forks source link

3.2.1: Usage tracking queries should not be made in frontend requests #111

Closed lkraav closed 2 years ago

lkraav commented 2 years ago

Expected Behavior

No irrelevant queries on frontend requests, ever.

Current Behavior

On front page queries (and probably elsewhere), I see in Query Monitor:

SELECT a.action_id FROM wp_actionscheduler_actions a WHERE a.hook='wp_mail_smtp_send_usage_data' AND a.status='in-progress' ORDER BY scheduled_date_gmt DESC LIMIT 1

Traceback:

    ActionScheduler_DBStore->find_action()
    wp-content/plugins/wp-mail-smtp/vendor/woocommerce/action-scheduler/classes/data-stores/ActionScheduler_DBStore.php:262
    as_next_scheduled_action()
    wp-content/plugins/wp-mail-smtp/vendor/woocommerce/action-scheduler/functions.php:177
    WPMailSMTP\U\SendUsageTask->init()
    wp-content/plugins/wp-mail-smtp/src/UsageTracking/SendUsageTask.php:60
    WPMailSMTP\T\Tasks->init()
    wp-content/plugins/wp-mail-smtp/src/Tasks/Tasks.php:46
    WPMailSMTP\Core->get_tasks()
    wp-content/plugins/wp-mail-smtp/src/Core.php:237
    do_action('init')
    wp-includes/plugin.php:474

Possible Solution

Use is_admin() && ! wp_is_ajax() to work with usage tracking.

Steps to Reproduce

  1. Install Query Monitor
  2. Make any frontend request to your site

Screenshots

image

capuderg commented 2 years ago

Hi @lkraav,

as you can see we are using the Action Scheduler library in our plugin. This library uses the default WPCron to fire its background tasks. WPCron is run on page requests (frontend or backend), that's just how it works. If you want to resolve this issue, the best option is to disable WPCron and configure a real Cron job on your server which will processes these background tasks. Here is an article on how to do that.

Have a nice day!

lkraav commented 2 years ago

I am running a server cron, hence my interest into why frontend requests are being spammed with AS action processing.

Traceback shows it starts from WPMailSMTP\Core->get_tasks() so I don't see how this is an AS problem.

capuderg commented 2 years ago

If you have a server Cron job, that triggers WP's wp-cron.php file, then you should just disable the "WP cron" (constant to disable it is mentioned in the article above). This will prevent the AS actions to be triggered on frontend and backend requests (they will only be triggered by your server cron job).

"WP cron" is not a good solution, but not all web hostings allow cron job creation... That's why WP created this "cron" which runs on all page visits. AS hooks into that WP cron and we are using AS in the plugin...

lkraav commented 2 years ago

Not using wp-cron.php, I've been running WP CLI-based cron for a long time now.

OK it's becoming clear I will need to dig into code analysis here.