Method for running large PHP script from WP admin

9 views
Skip to first unread message

petervan...@gmail.com

unread,
Aug 10, 2021, 6:20:51 PM8/10/21
to Minneapolis St. Paul WordPress User Group
Hi all,

I have a large PHP script that I'm triggering from a WP admin POST function.  It probably takes about four or five minutes to complete, which is longer than any script I've worked with previously, so I need to take additional steps.

Right now, the POST request fails with a 504 error after about 30 seconds because it hasn't gotten any response.  The triggered script continues running for a while, but is also not being completed (it worked fine locally, but is not completing on my deployment server).  I probably just need to adjust my server's PHP settings or streamline my import script better.

I'm wondering if anyone has a recommendation for a general method to use to send repeated updates to the browser making the POST request and create something like a progress bar.  I can imagine how that might work, but want to make sure I don't miss a useful tool and build too much from scratch.

Thanks,
- Peter

Nick Ciske

unread,
Aug 10, 2021, 6:36:06 PM8/10/21
to mpls-stpau...@googlegroups.com
Options here are:
  1. Increase the overall timeout for that script (if PHP/Apache/Nginx allow scripts to do that) or all PHP scripts (but be warned that can mean any script can run super long and eat up your PHP workers, so avoid this if you can).
  2. If you can, go asynchronous with something like Action Scheduler and let it chew away in the background. You can hack something together using QP cron and posting to itself if the timeout is near, but that’s a solved problem with AS… so try not to reinvent the wheel ;-)
  3. Go CLI - the PHP CLI (or WP CLI) doesn’t have timeouts because it’s not running in a web worker. This can be an option for large batch jobs, provided you can rework your script to run in the CLI vs web server (as many assumptions are not true in the CLI).
Bumping PHP version, RAM, or server size may also help… a bit, but 4-5m means you’re hitting a serious bottleneck somewhere (likely in the WPDB layer).


Nick Ciske 
CTO/CISO 
Nick Ciske Photo Luminfire.com
nick....@luminfire.com   |   612-564-1626 x702
Custom technology solutions that make businesses more effective, productive, and profitable.

Peter VanKoughnett

unread,
Aug 11, 2021, 12:37:49 PM8/11/21
to mpls-stpau...@googlegroups.com
Thanks Nick, Action Scheduler didn't take long to get up and running and is working well for me 🙂

--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpls-stpaul-word...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpls-stpaul-wordpress/2e7e4f89-5d4a-4ea2-9df5-5d2caabe8fae%40Spark.
Reply all
Reply to author
Forward
0 new messages