zend-framework2laminas-api-toolsphp-resque

Resque could not find job class ZF2 namespaces


Attempting to use the Redis backed PHP-Resque project https://github.com/chrisboulton/php-resque from within a ZF2 project. (Apigility to be specific)

What I'm having trouble with, is combining the ZF2 namespaces and the classes

for instance :

Controller

    //Enqueue a worker

    $args = array(
        'name' => 'EMCP'
    );
    Resque::enqueue('default', 'phpresque\\V1\\Model\\MyResqueJob', $args);

MyResqueJob Class

namespace phpresque\V1\Model;

class MyResqueJob
{


    public function perform()
    {
        // Work work work
        echo "helloWorld";
    }
}

Error message given by resque :

 ubuntu@/zf2projectname/vendor/chrisboulton/php-resque$ QUEUE=* APP_INCLUDE=/zf2projectname/vendor/autoload.php VVERBOSE=1 php resque.php 

** [03:01:37 2014-10-26] Sleeping for 5
** [03:01:42 2014-10-26] Checking default
** [03:01:42 2014-10-26] Found job on default
** [03:01:42 2014-10-26] got (Job{default} | ID: 38fa104b11de81731c15ba9c2f1853ab | phpresque\V1\Model\MyResqueJob | [{"name":"EMCP"}])
** [03:01:42 2014-10-26] Forked 30642 at 2014-10-26 03:01:42
** [03:01:42 2014-10-26] Processing default since 2014-10-26 03:01:42
** [03:01:42 2014-10-26] (Job{default} | ID: 38fa104b11de81731c15ba9c2f1853ab | phpresque\V1\Model\MyResqueJob | [{"name":"EMCP"}]) failed: Could not find job class phpresque\V1\Model\MyResqueJob.
** [03:01:42 2014-10-26] Checking default

Solution

  • Edit : I dropped using Resque but here's the suggestion below on how it could work.. I ended up using SlmQueue instead https://github.com/juriansluiman/SlmQueue

    According to the ZF2 github community, the following should be done :

    Shouldn't your APP_INCLUDE environment var contain the path to composer's init_autoloader.php? Just including the autoloader class will not suffice, as you not configuring and registering the autoloader anywhere.

    and

    YMMV, but what I've done to integrate my ZF2 app into Resque is create a separate entry point script that initializes the ZF2 app within the context of the Resque worker:

    <?php
    // resque_context.php
    
    require_once 'vendor/autoload.php';
    
    $application = \Zend\Mvc\Application::init(include 'config/application.config.php');
    $config = $application->getServiceManager()->get('config');
    

    And provide it's path to the Resque run script via the APP_INCLUDE environment variable.