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 and tail -f drupal_debug.txt (minus: not time efficient)
  • using watchdog() like watchdog('foo', '<pre>'. print_r($foo, TRUE). '</pre>'); then drush 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?

Was it helpful?

Solution

Here are some workarounds which I've found so far which makes life easier.

Using curl

You need: drush and curl.

  1. Add var_export($name); exit; into the batch code place of interest, so batch won't be set to finish state.
  2. In the web browser open Network console (e.g. DevTools in Chrome).
  3. Start batch process as usual (in Feeds it's at: /import).
  4. Stop the batch processing just before the redirect by pressing Escape. Then note the URI.

    Import canceled

    Alternative way is to fetch id directly from the batch table:

    $ drush sqlq "SELECT bid FROM batch"
    
  5. 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
    
  6. Run curl as below (you need to replace id, session cookie and your host):

    $ curl -X POST -d "id=124&op=do" --cookie "SESS0F00=B4R" "http://myhost.local/batch"
    your_debug_data_here
    
  7. 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

  1. Add var_export($name); exit; into the batch code place of interest, so batch won't be set to finish state.
  2. In the web browser open Network console (e.g. DevTools in Chrome).
  3. Start batch process as usual (in Feeds it's at: /import) and it should fail, because of exit; added in step 1.
  4. Note the batch?id=x&op=do request and do Replay XHR (at least in Chrome).
  5. Now on each code change you should be able to repeat the last step over and over again.

    batch?id=x&op=do

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top