53426e44591a6_screen_shot_20140407_at_112122-800x524

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.

https://www.cloudcontrol.com/add-ons/worker

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.

<?php  
/**  
 * 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"))   
        {  
            newrelic_background_job(TRUE);  
        }  

        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  
                continue;  
            }  

            foreach ($staff_to_do as $staff_doing)  
            {  
                Your_Library::realy_do_staff($staff_doing);  

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

            // Logs need to be writen manually   
            Log::instance()->write();  
        }  
    }  
}  

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

http://kohanaframework.org/3.3/guide/minion/tasks

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  
<?php  

error_reporting(E_ALL);  

set_time_limit(0);  

ob_implicit_flush();  

foreach ($argv as $arg) {  
    $e=explode("=",$arg);  
    if(count($e)==2)  
        $_GET[$e[0]]=$e[1];  
    else      
        $_GET[$e[0]]=0;  
}  

while(TRUE)   
{  
    // 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  
Workers  
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  
Workers  
 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