Shells and Tasks

Published on May 16, 2007 and tagged with cakephp  shell  task

Last week I wrote about the new command line features of CakePHP 1.2. And this week it is already outdated what I wrote then ;-) The command line scripts are now called “Shells” instead of “CakeScripts”, so I adapted the previous article to those changes.

With the recent changes a new concept has been introduced: Tasks. Well, tasks are not entirely new as tasks were already used by bake2 (which no longer exists). But now tasks are for shells what components are for controllers.

Tasks are placed in app/vendors/shells/tasks if they are application-specific resp. in vendors/shells/tasks if they are general-purpose tasks. Like shell scripts, task classes have to extend the “Shell” class, but the class name has to end with “Task”. Here a simple example of a task:

// vendors/shells/tasks/test.php
class TestTask extends Shell {

    function execute() {
        // do something
    }
}

In the core tasks (and in this test task) the main function is called “execute”, but you are free to use any name you want.

The test task can now be used in the shell script in the following way:

// vendors/shells/demo.php
class DemoShell extends Shell {
    var $tasks = array('Test');

    function initialize() {
        // empty
    }

    function main() {
        $this->Test->execute();
    }
}

Happy baking :)

18 comments baked

  • Joel Moss May 16, 2007 at 10:09

    I noticed all these changes, but am a little unsure as to why there are still tasks when it seems that shells do the same thing. I am rewriting my migrations and fixtures tasks to work with the new console/shell system, and was simply going to add them as shell scripts in /vendors/shells.

    Do you why there are still tasks when shell scripts can do the same thing?

  • kabturek May 16, 2007 at 18:55

    >Joel Moss
    and the notes task ! ;)

    thx for the heads up dho
    i cant keep up with the changes myself ;)

  • Nate May 16, 2007 at 18:59

    Shells are designed to manage full CLI-based applications, whereas the purpose of Tasks are to break out shell logic into discrete, reusable commands.

  • Joel Moss May 16, 2007 at 21:44

    @kabturek: on its way, although since I switched to Mac and textmate, I no longer have a need for the notes task.

    @Nate: hhmmm, seems like a bit of unneeded duplication to me.

  • Nate May 16, 2007 at 22:09

    Joel: I guess if you think components are unnecessary duplication of controllers, then yeah.

  • Joel Moss May 16, 2007 at 22:22

    @nate: valid point ;)

  • gwoo May 17, 2007 at 00:59

    Joel:
    Bake is a good example of why tasks are useful.
    example:
    $ cake bake

    the above command will run through bake in interactive mode, if it does not find the database.php it will run the DbConfigTask.

    whereas:
    $ cake bake db_config
    will just run the DbConfigTask

    For something like Acl there are no tasks because everything can be done through methods in the Shell.
    $ cake acl create aro null John
    will run the “create” method in the AclShell

    Complete docs coming soon.

  • cakebaker May 19, 2007 at 09:35

    @all: Thanks for your comments :)

  • Joel Moss May 20, 2007 at 00:16

    I suppose tasks are just a way of breaking up a console script into smaller tasks. Which is more manageable, but the same can still be achieved with one console script and a method for each task.

    But each to their own ;)

  • cakebaker May 21, 2007 at 09:43

    @Joel: Well, the advantage of using tasks over using multiple methods is that tasks can be reused in other shell scripts.

  • Diego July 12, 2007 at 02:30

    Hey this is very cool, indeed, but i have been trying to find out how to use; if it is possible?, my models in app/models
    Has anyone tried to??

  • cakebaker July 12, 2007 at 08:47

    @Diego: Hm, I don’t understand what you try to accomplish…

  • Diego July 12, 2007 at 18:30

    @cakebaker: Hi there! thanx for your help, there aren’t much examples out there, what i am trying to do is the following, from a shell script, i would like to access some data stored in the DB, Lets say the POSTS model for example, trying to get innactive posts or something like that. I’ve tried looking in the ACL shell without much success.

  • cakebaker July 13, 2007 at 09:08

    @Diego: You can use something like:

    loadModel('Post');
    $this->Post = new Post();
    $this->Post->findAll();
    

    HTH

  • Debbie September 05, 2007 at 11:04

    hi,

    i want to write a console app which will do from cron stuff my web interface can do interactively. SO i don’t want my code in shells or tasks. i want my shells and my controllers to use the same stuff, the models and the components. unless my controllers can also run tasks, i would only write stuff in tasks which is only useful from the command line.

    so do you think controllers can also run tasks or the code should go in a component or a vendor class?

  • cakebaker September 06, 2007 at 11:00

    @Debbie: I think it should be no problem to use a task inside a controller as long as you don’t use command line specific things in your task like reading user input from the command line. And you probably have to include the required files manually as it was not planned to use tasks in that way ;-)

    Good luck!

  • CakePHP : effectuer une “task” via une interface web - Damien Mathieu, développeur web July 12, 2008 at 02:28

    [...] ce genre de scripts. Il en existe déjà un résumant assez bien la chose sur cakebaker [...]

  • CakePHP : exécuter une “task” via l’interface web - Damien Mathieu, développeur web July 27, 2008 at 16:55

    [...] de développer ce genre de scripts. Il en existe déjà un résumant assez bien la chose sur cakebaker [...]

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License