chrisboulton / php-resque

PHP port of resque (Workers and Queueing)
MIT License
3.43k stars 759 forks source link

ZF2 autoload ? #220

Closed EMCP closed 10 years ago

EMCP commented 10 years ago

I'm having class not found issues with PHP-Resque, trying to enqueue a job class that resides in the ZF2 namespace.

** [17:45:10 2014-10-27] Checking default
** [17:45:10 2014-10-27] Found job on default
** [17:45:10 2014-10-27] got (Job{default} | ID: 6b071b66db60bf7d4bc75cbfd37db6d4 | \phpresque\V1\Model\ApigilityJob | [{"name":"EMCP"}])
** [17:45:10 2014-10-27] Forked 8501 at 2014-10-27 17:45:10
** [17:45:10 2014-10-27] Processing default since 2014-10-27 17:45:10
** [17:45:10 2014-10-27] (Job{default} | ID: 6b071b66db60bf7d4bc75cbfd37db6d4 | \phpresque\V1\Model\ApigilityJob | [{"name":"EMCP"}]) failed: Could not find job class \phpresque\V1\Model\ApigilityJob.
** [17:45:10 2014-10-27] Checking default

I've tried to roll in the ZF2 ClassMapAutoloader like so with no success

sudo -u www-data APP_INCLUDE=/path/to/vendor/zendframework/zendframework/library/Zend/Loader/ClassMapAutoloader.php QUEUE=* VVERBOSE=1 php resque.php

Controller Class that Enqueues the Job

//Enqueue a worker
$args = array(
            'name' => 'EMCP'
);
Resque::enqueue('default', '\\phpresque\\V1\Model\\ApigilityJob', $args);

The Job Class

<?php

namespace phpresque\V1\Model;

class ApigilityJob
{

    public function __construct(){

    }

    public function perform()
    {
        // Work work work
        echo "helloWorld";
    }
}
danhunsaker commented 10 years ago

The only place the problem could exist, at this point, is with the autoloader itself. I'm not familiar with ZF2's loader, but I imagine there's a configuration file that tells it where to look for the namespace path you're using, or at the very least that you have your job class file in the default location it would expect such a class to reside (such as /path/to/vendor/phpresque/V1/Model/Apigility.php). That's where I'd have to start, followed by making sure the Zend autoloader is actually active. The file will be included, but some projects (Zend especially) like to not execute anything until you manually call it in your own code. Not saying that's the case here - as I said, ZF2 and I are not acquainted - but something similar is the most likely culprit.

If all of that is fine, try writing a wrapper script that will include_once() or require_once() the ZF2 autoloader, and APPINCLUDE that instead. Or better yet, have the wrapper script set the environment variables to configure your queues and such, then require_once('/path/to/bin/resque') - that's been the most stable and reliable configuration in my experience. Never had much luck with APPINCLUDE.

EMCP commented 10 years ago

After a LOT of research I think I need to write an autoloader.php of my own. It would load the Zend Library and any modules I've written (and needed by the worker)

I've yet to see it done, and time is pushing me to instead go with a ZF2 project already in existence.

I'm going to close, but thank you for shedding some light on what PHP-Resque needs in order to get going. Hard coding a single PHP file worked, but the worker cannot leverage any of my pre-existing logic.. so it's quite a useless worker :)

If I try your 2nd paragraph's advice I'll come back here and update.