wp-async-task don’t fire run_action method

I have to work with the techcrunch wp-async-task to run a synchronization task in background in my wordpress plugin.

So to test, at the bottom of the main file I have :

Read More
//top of the php file
require_once(dirname(__FILE__) . '/lib/WP_Async_Task.php');
require_once(dirname(__FILE__) . '/class/my_api_status.class.php');
define('API_URL', '...');

/* ... */

// At the bottom of the file
function my_api_status($api_url)
{
  sleep(5);
  $r = wp_safe_remote_get($api_url);
  if (!is_wp_error($r)) {
    $body = json_decode(wp_remote_retrieve_body($r));
    if (isset($body->success)) {
      return;
    }
  }
}
add_action('wp_async_api_status', 'my_api_status');

function my_init_api_status()
{
  new ApiStatusTask();
  do_action('api_status', constant('API_URL'));
}
add_action('plugins_loaded', 'my_init_api_status');

And api status task class

class ApiStatusTask extends WP_Async_Task {

  protected $action = 'api_status';

  /**
   * Prepare data for the asynchronous request
   * @throws Exception If for any reason the request should not happen
   * @param array $data An array of data sent to the hook
   * @return array
   */
  protected function prepare_data( $data ) {
    return array(
      'api_url' => $data[0]
    );
  }

  /**
   * Run the async task action
   */
  protected function run_action() {
    if(isset($_POST['api_url'])){
      do_action("wp_async_$this->action", $_POST['api_url']);
    }
  }

}

The function prepare_data is correctly called by launchand after that launch_on_shutdown is also correctly called and finally wp_remote_post is called at the end of launch_on_shutdown with admin-post.php.
But the function run_action is never called … and so the my_api_status in the main file.

What it possibly go wrong ?

Related posts

1 comment

  1. I will put a complete example of a plugin here soon. But for now, I found my problem :

    // In the `launch_on_shutdown` method of `WP_Async_Task` class
    
    public function launch_on_shutdown() {
      GcLogger::getLogger()->debug('WP_Async_Task::launch_on_shutdown');
      if ( ! empty( $this->_body_data ) ) {
        $cookies = array();
        foreach ( $_COOKIE as $name => $value ) {
          $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value );
        }
    
        $request_args = array(
            'timeout'   => 0.01,
            'blocking'  => false,
            'sslverify' => false, //apply_filters( 'https_local_ssl_verify', true ),
            'body'      => $this->_body_data,
            'headers'   => array(
                'cookie' => implode( '; ', $cookies ),
            ),
        );
    
        $url = admin_url( 'admin-post.php' );
        GcLogger::getLogger()->debug('WP_Async_Task::launch_on_shutdown wp_remote_post');
        wp_remote_post( $url, $request_args );
      }
    }
    

    The sslverify option failed in my local environment. I just had to put it on false if we are not in production.

    With this option set, the run_action is correctly trigger.

Comments are closed.