【问题标题】:Gearman and inotify for tasks executionGearman 和 inotify 用于任务执行
【发布时间】:2016-05-09 13:33:14
【问题描述】:

我对如何在我的 php 应用程序中有效地使用 gearman 有一些疑问。

我正在使用 inotify 监控一个文件夹,其中将存储和处理大量文件(每次超过 1000 个)。 用于解析它们中的每一个并将其内容保存在数据库中,我正在尝试使用 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();                
                }
            }
    }

我创建了一个这样的worker.php文件:

<?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());

事情正在发生。 worker 函数运行,第一个文件的数据存入数据库但出现错误:

这是我的 nohup.out 文件的输出。

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

“他”想要什么? :)

【问题讨论】:

    标签: php gearman inotify php-5.5


    【解决方案1】:

    我设法解决了问题的最后一部分。

    错误:

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

    是因为我在这里返回了一个对象:

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

    在我的工作函数中未返回任何内容碰巧修复了错误。需要学习更好的 Gearman 以及如何创建更多的工作人员来运行我的代码。

    仅作记录:如果您尝试将工作人员连接到远程 gearman 作业服务器,您可能会遇到麻烦。 要允许远程连接,您必须更改位于 /etc/default/gearman-job-server 的 gearman-server 配置中的侦听端口:

    # Parameters to pass to gearmand.
    PARAMS=""
    

    请注意,如果您将服务器放在公共网络中,则服务器对来自任何地方的远程连接完全开放。

    【讨论】:

      猜你喜欢
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2013-04-28
      • 1970-01-01
      • 2011-01-25
      • 2018-12-28
      • 1970-01-01
      相关资源
      最近更新 更多