Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job class not found if using namespace #121

Closed
eddiejaoude opened this issue Aug 14, 2013 · 22 comments
Closed

Job class not found if using namespace #121

eddiejaoude opened this issue Aug 14, 2013 · 22 comments

Comments

@eddiejaoude
Copy link

When using namespaces, the job class is not found.

I have tried Resque::enqueue('default', 'My\Job', $args); but still no luck.

Any ideas?

@rajibahmed
Copy link
Contributor

I think 'My\Job' will fix the issue.

@danhunsaker
Copy link
Contributor

Tends to do the trick in my code, too. Either way, this is a PHP issue with namespace specifications within strings (they opted to use the string escape character for namespace hierarchy separations, probably rather by accident), rather than an issue with Resque itself (Resque isn't namespace-aware).

@eddiejaoude
Copy link
Author

Ok thanks. I will give this a go.

@eddiejaoude
Copy link
Author

Unfortunately it still does not find the class...from redis monitor...

1376640721.282846 "SET" "resque:worker:ubuntu:3903:*" "{"queue":"default","run_at":"Fri Aug 16 09:12:01 BST 2013","payload":{"class":"Application\Worker\Example","args":[{"data":"abcd"}],"id":"21775b50a7d3425e22eee88dc4fa6b68"}}"

1376640721.291225 "EXISTS" "resque:job:21775b50a7d3425e22eee88dc4fa6b68:status"
1376640721.295362 "RPUSH" "resque:failed" "{"failed_at":"Fri Aug 16 09:12:01 BST 2013","payload":{"class":"Application\Worker\Example","args":[{"data":"abcd"}],"id":"21775b50a7d3425e22eee88dc4fa6b68"},"exception":"Resque_Exception","error":"Could not find job class Application\Worker\Example.","backtrace":["#0 /media/psf/Home/vhosts/gh.ncc-group-api/git/vendor/chrisboulton/php-resque/lib/Resque/Job.php(189): Resque_Job->getInstance()","#1 /media/psf/Home/vhosts/gh.ncc-group-api/git/vendor/chrisboulton/php-resque/lib/Resque/Worker.php(241): Resque_Job->perform()","#2 /media/psf/Home/vhosts/gh.ncc-group-api/git/vendor/chrisboulton/php-resque/lib/Resque/Worker.php(203): Resque_Worker->perform(Object(Resque_Job))","#3 /media/psf/Home/vhosts/gh.ncc-group-api/git/vendor/chrisboulton/php-resque/bin/resque(119): Resque_Worker->work(5, false)","#4 {main}"],"worker":"ubuntu:3903:*","queue":"default"}"

@rajibahmed
Copy link
Contributor

You have backslash in the end of className I think Application\Worker\Example while writing the name.
I use the double back slash it works fine. And try to do a class_exists with out the double backslashes. Then add the doubles.

//Best

@eddiejaoude
Copy link
Author

Thanks for your quick response.

The backslash at the end I think is the for closing quote ", as I have not put one at the end.

class_exists returns true for with single & double backslash prior to enqueue call.

@rajibahmed
Copy link
Contributor

:) then I have no clue. And I think this problem is specific to your application/implementation. Please close this issue because this is not a bug/feature.

@eddiejaoude
Copy link
Author

No problem. Thanks anyway :)

@danhunsaker
Copy link
Contributor

What's probably happening, then, is that the class you need is loaded on the enqueuing side of your application, but not on the worker side. Keep in mind that a completely separate process (or group of processes, if you use COUNT > 1) is used to run the jobs than the process(es) used to enqueue them. This is the purpose of the APP_INCLUDE environment variable (although I tend to have my application include bin/resque rather than use APP_INCLUDE, but either approach is equally valid): to load in the various code dependencies of your jobs.

So, check that the class is available to the workers. You can do this by adding something like echo json_encode(class_exists('Application\\Worker\\Example')); immediately before the $worker->work($interval) lines. If that outputs true, the problem lies elsewhere, but if it outputs false instead, you just need to make sure your class is loaded before you queue things up. It looks like you might be using PSR-0 compliant classes for your jobs, which means you should be able to use a PSR-0 autoloader (such as the one used/generated by Composer) to do this - you just need to make sure the autoloader knows where to start looking. Of course, you could also just include() or require() the class files for all the jobs you expect to run - that's really up to you.

@danhunsaker
Copy link
Contributor

@eddiejaoude Did you ever figure this out? I really hope so, but if so, this issue really needs to be closed, and only you or Chris can do that.

Cheers!

@eddiejaoude
Copy link
Author

@danhunsaker I don't think I did, I can't remember, in the end I might have worked around it at the time - it was not a massive issue as it was only a POC project.

I can close the issue.

@SteveEdson
Copy link

I'm having the same issue. I autoload my classes with composer like so:

{
    "require": {
        "chrisboulton/php-resque": "dev-master"
    },
    "autoload": {
        "psr-0": {
            "SteveEdson\\" : "class"
        }
    }
}

I can autoload these fine usually, when autoload.php is included, and appropriate use statements are used, however I'm having issues when trying to use with php-resque. Any help would be appreciated.

@danhunsaker
Copy link
Contributor

Just need to make sure that you use the same autoloader in your workers. And then make sure your jobs are defined with the full class name, including namespaces. A use in your qeueuing logic won't carry over to the worker and job.

@SteveEdson
Copy link

My worker.php file looks like this

<?php

require(__DIR__ . "/vendor/autoload.php");

putenv("VVERBOSE=1");
putenv("LOGGING=1");
putenv("QUEUE=default");
putenv("COUNT=2");
require_once ('vendor/bin/resque');

And I set a job like so:

<?php

require("vendor/autoload.php");

Resque::setBackend('localhost:6379');

Resque::enqueue('default', "SteveEdson\\Jobs\\My_Job", $args);

Is there anything wrong with either of these setups?

Thanks

@danhunsaker
Copy link
Contributor

Doesn't look like it from here, though I've always been paranoid about PHP name spacing and included the leading backslash. So \\SteveEdson\\Jobs\\My_Job...

@SteveEdson
Copy link

Unfortunately, that does not work either :(

[notice] Starting work on (Job{default} | ID: 739244f12f227e3266a9bd90da2f7a7a | \SteveEdson\Jobs\My_Job | [{"foo":"bar"}])
[critical] (Job{default} | ID: 739244f12f227e3266a9bd90da2f7a7a | \SteveEdson\Jobs\My_Job | [{"foo":"bar"}]) has failed Could not find job class \SteveEdson\Jobs\My_Job.

@SteveEdson
Copy link

Aha! I've figured it out. Its not a PHP Resque issue. The test job I was using was called My_Job. I think because of the PSR-0 standard, it was treating the underscores as directory separators. Renaming the class to Job works fine now.

Sorry about that, thanks you for your time!

@danhunsaker
Copy link
Contributor

D'oh. Completely missed that. Yes, PSR-0 is set up that way. PSR-4 removes support for underscores as pseudo-namespace separators so they can be used in class names, but we haven't adopted it yet. Glad you got it working!

@rabnawazPikesSoft
Copy link

my My_queue.php in libraries folder in code ignitor and include it on autoupload.php as well and still getting the issue the

i have tried this
Resque::enqueue('default', 'My\queue', $timeslot);
Resque::enqueue('default', 'My_queue', $timeslot);
Resque::enqueue('default', 'application\libraries\My_queue', $timeslot);
Resque::enqueue('default', 'application\libraries\My_queue', $timeslot);

Resque::enqueue('default', 'application/libraries/My_queue', $timeslot);

i have use all thhis while enqueing in queue but still not find my_queue job class

please guide me thanks

@danhunsaker
Copy link
Contributor

You have to either tell Composer to load the file using PSR-4 instead of PSR-0, or rename the class so it doesn't have an underscore (_) in it. The second approach will probably work better.

Don't mix \\ and _ in your class names.

@rabnawazPikesSoft
Copy link

@danhunsaker I HAVE renamed the class and it class name now class name is without underscore and still getting the same error no luck
and i am using PSR-4

@danhunsaker
Copy link
Contributor

You've restarted the worker(s) after renaming? Does the class autoload correctly in other parts of your application? Sometimes Composer needs to regenerate the autoload.php in order to find everything, and you have to restart all your code for the changes to be picked up - have you tried doing that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants