Get a Kohana Minion Task running in CloudControl Pinky Stack

  • Februar 13, 2014

I ran into a few difficulties getting a worker in Kohana correct running. The Kohana Framework introduced in its V3 the new module calledMinions . A helper library to create continuous running processes which is nice for jobs with high load. It is useful for calculation or communication with third party services like sending email.

The documentation on CloudControl is still missing some good examples.

Add the Worker addon

You have to add the „Worker-Addon “ for free with the following command.

cctrlapp app_name/deployment_name addon.add worker.single  

It tells you that the „Worker-Addon“ is for free with one process. I got charged 720h of running for one unit which is about 7€/month.


Create a Worker

As Minion Task

You can use the Kohana documentation to create a simple Minion Task. Using Minion as worker gives you full access to your classes and vendors. You can easily start database queries or work like a cron call.

Here is a sample Minion-Task for CloudControl. You can see I added the background information for NewRelic and made the LogWriter class write its logs.

 * This is a Sample task.  
class Task_Sample extends Minion_Task  
     * This is a demo task  
     * @return null  
    protected function _execute(array $params)  
        if (function_exists("newrelic_background_job"))   

        while(true) {  

            $staff_to_do = Your_Library::get_me_stuff_which_is_undone();  

            if (count($staff_to_do) == 0)  
                sleep(10); // or less depending how often you want to call your database  

            foreach ($staff_to_do as $staff_doing)  

                Log::instance()->add(Log::INFO,"I did staff");  

            // Logs need to be writen manually   

You should the actual code in your library class and think of the Minion Task like a normal Controller.


As PHP-File

You can also create a single PHP-File and use it as worker. It has a very good memory footprint but doesn’t give you access to any Kohana classes. It is still good to create something like a socket server.

#!/usr/bin/env php  




foreach ($argv as $arg) {  

    // do some calculation without any Kohana classes  


Create the Procfile

Now comes the interesting part. You have to add file called „Procfile “ to your root directory. The content of this file should look like this.

web: bash boot.sh --port $PORT  
minionworker: php code/application/webroot/index.php --task=sample  
fileworker: php code/applications/myphpfileworker.php --foo=bar  

Instead of the names „mineworker“ you can just write „worker“. But as soon as you have more than one worker you should start giving them names anyway. So why not start with correct names. Important is that CloudControl puts our repository code in a subfolder called „/code/“. You have to adjust the paths to your „webroot/index.php“ to fit your structure.

Start/stop and logs

Now you can start your worker on the console with the cctrlapp.

See what is running right now. At your first run there should be no worker running.

$ cctrlapp app_name/deployment_name worker  
nr. wrk_id      command  

Start the worker

$ cctrlapp app_name/deployment_name worker.add minonworker  

Check the log file

$ cctrlapp app_name/deployment_name log worker  

[Thu Feb 13 09:12:50 2014] wrkphfpkmsu Starting worker (command: 'minionworker', parameter: '') ...  
[Thu Feb 13 09:12:57 2014] wrkphfpkmsu Started worker (command: 'php code/application/webroot/index.php --task=minionworker ', parameter: '')  

Check what is running. Now we find our worker in the list.

$ ctrlapp app_name/deployment_name worker  
 nr. wrk_id      command  
   1 wrkphfpkmsu emailworker  

Stop the worker

$ cctrlapp app_name/deployment_name worker.remove wrkphfpkmsu  

Check the log file

$ cctrlapp app_name/deployment_name log worker  

[Thu Feb 13 09:14:47 2014] wrkphfpkmsu Stopping worker by API request  
[Thu Feb 13 09:14:49 2014] wrkphfpkmsu Worker removed by API Request