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

Tasks are not executed as tasks! #18

Open
half2me opened this issue Dec 8, 2015 · 5 comments
Open

Tasks are not executed as tasks! #18

half2me opened this issue Dec 8, 2015 · 5 comments

Comments

@half2me
Copy link

half2me commented Dec 8, 2015

Tasks are executed with call_user_func_array this means that the main() method of the task will be executed without any support for the CakePHP framework, just plain PHP.

Here is my example:

namespace App\Shell\Task;
use Cake\Console\Shell;

class MyTask extends Shell
{
    public function initialize()
    {
        parent::initialize();
        $this->loadModel('Example');
    }
    public function main()
    {
        $portals = $this->Example->find();
    }
}

The initialize function is never called, so the Model data cannot be accessed.
When running the task, we get the following error:

Notice Error: Undefined property: App\Shell\Task\MyTask::$Example in [/mypath/vendor/cakephp/cakephp/src/Console/Shell.php, line 525]

Instead we should use load tasks with TaskRegsitry
(http://book.cakephp.org/3.0/en/console-and-shells.html#loading-tasks-on-the-fly-with-taskregistry)
Arguements can be passed as well since 3.1
(http://book.cakephp.org/3.0/en/console-and-shells.html#passing-extra-parameters-to-the-dispatched-shell)

@trentrichardson
Copy link
Owner

Sorry for the delay. I agree, this looks like it would be an improvement over how tasks are currently loaded and executed. When I get an opportunity I will try to update this (next two holiday weeks will fill up my schedule). If anyone has experience with the the taskregistry with 3.0 and can send a pull request that would be great too.

@trentrichardson
Copy link
Owner

I looked back over this and currently doing deeper thinking on how to re-work this to be a little more future-proof. Currently the plugin does use the TaskRegistry to load the task.

https://github.com/trentrichardson/cakephp-scheduler/blob/master/src/Shell/SchedulerShell.php#L211

However as you mention there could be some issue with call_user_func_array losing context of it's task instance. Or maybe the task registry is returning the class and not the instance on the line linked to above.

The dispatchShell method appears to be geared for calling Shells, not Tasks as SchedulerShell does. Technically Tasks extend Shell, so they are shells, but I don't know that this method was intended to call Tasks directly, and could cause issue down the road?

@half2me
Copy link
Author

half2me commented Jan 19, 2016

might be useful to discuss with the CakePHP community?
I'll also look over some options as soon as I've finished with my exams this week...

@trentrichardson
Copy link
Owner

@half2me I added some tests in the cakephp-v3.0 branch and was able to replicate this issue. I will look deeper into using dispatchShell over the next couple nights. If you get an opportunity as well look at the cakephp-v3.0 branch as that is where the latest code is.

@half2me
Copy link
Author

half2me commented Jan 20, 2016

@trentrichardson alrighty

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

2 participants