Search code examples
phpgearmaninotifyphp-5.5

Gearman and inotify for tasks execution


I have some doubts in how i can efficiently use gearman in my php application.

Im using inotify for monitoring a folder where a lot of files will be stored and processed (more than 1000 per time). For parsing each of them and save it's content in a database im trying to use gearman.

    while(true){
        sleep(5);   # spare some CPU Cycles
        set_time_limit(0); # unlimited timeout request 
        // read events 
        $events = inotify_read($this->instance);

        // if the event is happening within our 'Files directory'
            if ($events[0]['wd'] === $this->watch_id){              
                foreach ($events as $key=>$value)
                {
                    if($events[$key]['mask'] === IN_CREATE){
                        # A new file was created in folder                          
                        $client = new \GearmanClient();
                        $client->addServer();
                        $client->addTask("parse_file", $events[$key]['name']);  # add task to parse that file
                        printf("Created file: %s in Files directory\n", $events[$key]['name']);
                    }
                    else if ($events[$key]['mask'] === IN_DELETE){
                        printf("Deleted file: %s in Files directory\n", $events[$key]['name']);
                    }                           
                }
                if(!is_null($client)){  # once everything is done, run the tasks.   
                    $client->runTasks();                
                }
            }
    }

I created a worker.php file like this:

<?php
namespace controllers;
use app\file\File;
require_once 'vendor/autoload.php';

$worker = new \GearmanWorker();
$worker->addServer();
$worker->addFunction('parse_file', function($job){
    echo "entrou no add function!<br>";
    print_r ($job->workload());
    sleep(2);
    return new File($job->workload()); # this class parses the files content in database
});                     
while ($worker->work());

Things are happening now. The worker function runs and the data of the first file is stored in the the database but a error occurs:

This is the output of my nohup.out file.

Catchable fatal error: Object of class app\file\File could not be converted to string in /var/www/html/worker.php on line 18 

What does "he" want? :)


Solution

  • I manage to figure it out the final piece of the problem.

    The error:

    Catchable fatal error: Object of class app\file\File could not be converted to string in /var/www/html/worker.php on line 18 
    

    Was because i was returning a object here:

    sleep(2);
        return new File($job->workload()); # this class parses the files content in database
    

    Not returning anything in my worker function happened to fix the error. Need to study better Gearman and how to create more workers to run my code.

    Just for the record: If you try to connect a worker to a remote gearman job server, you will probably have a bad time. To allow remote connections you must change the listen port in gearman-server configuration located at /etc/default/gearman-job-server:

    # Parameters to pass to gearmand.
    PARAMS=""
    

    Beware that this way the server is completely open to remote connections from anywhere if you have it in a public network.