How to debug Batch API more efficiently?
-
29-09-2020 - |
Question
I've some issues with Feeds importing (I don't want to focus on particular case) and I'd like to find the way of able to debug Batch API processing more efficiently. Given the fact of complexity of Feeds module (a plethora of classes), I can't easily implement one-time processing in a short time.
Here are the methods which I'm aware so far:
- using
dd()
from Devel andtail -f drupal_debug.txt
(minus: not time efficient) - using
watchdog()
likewatchdog('foo', '<pre>'. print_r($foo, TRUE). '</pre>');
thendrush watchdog-show --tail --full
to monitor in real-time - using XDebug and its
xdebug.auto_trace
(minus: non-interactive excess of trace logs)
but they're not helpful in particular case and at the same time I'd like to learn something new.
Ideally I'm looking for some easy way to convert Batch API into standard non-AJAX processing, so I can do old-school var_dump($var); exit;
and refreshing the page by sending the same POST requests over and over again, unless there are some more efficient ways. I'm also considering temporary core hacks to achieve that.
Any ideas?
Solution
Here are some workarounds which I've found so far which makes life easier.
Using curl
You need: drush
and curl
.
- Add
var_export($name); exit;
into the batch code place of interest, so batch won't be set to finish state. - In the web browser open Network console (e.g. DevTools in Chrome).
- Start batch process as usual (in Feeds it's at:
/import
). Stop the batch processing just before the redirect by pressing Escape. Then note the URI.
Alternative way is to fetch
id
directly from thebatch
table:$ drush sqlq "SELECT bid FROM batch"
Click on the previous 302 request and take a note of your session cookie from Request Headers, you need only
SESS0X=Y
cookie, e.g.Cookie: SESS0F00=B4R
Run
curl
as below (you need to replaceid
, session cookie and your host):$ curl -X POST -d "id=124&op=do" --cookie "SESS0F00=B4R" "http://myhost.local/batch" your_debug_data_here
Previous step is repeatable after each code change which makes it easy to debug variables in no time.
For ever more efficiently, you can map it (e.g. in vim
), so the output can be executed each time when you need it.
Using repeatable XHR requests
- Add
var_export($name); exit;
into the batch code place of interest, so batch won't be set to finish state. - In the web browser open Network console (e.g. DevTools in Chrome).
- Start batch process as usual (in Feeds it's at:
/import
) and it should fail, because ofexit;
added in step 1. - Note the
batch?id=x&op=do
request and do Replay XHR (at least in Chrome). Now on each code change you should be able to repeat the last step over and over again.
There are probably more ways of doing similar thing (including use of some plugins such as Postman, etc).
OTHER TIPS
Using xdebug will be able to do the debug during the batch process. Here is a tutorial I just found in Youtube for people who don't know how to use xdebug: PhpStorm & Xdebug: Installation and Configuration, I didn't look at that video but hope it will be helpful.