【问题标题】:PHP gearman - calling a gearman worker within a gearman workerPHP gearman - 在 gearman worker 中调用 gearman worker
【发布时间】:2019-03-25 19:08:34
【问题描述】:

我对齿轮人很陌生。我正在尝试编写一个 PHP 脚本来从 URL 下载脚本并将其上传到用户的谷歌驱动器。一种备份脚本..

我要做的是在进程中调用启动一个 gearman 工作进程,首先将图像从源下载到临时目录,然后将其上传到谷歌驱动器。这是脚本:

    <?php
require_once "../classes/drive.class.php";
    $worker = new GearmanWorker();
    $worker-> addServer('localhost');
    $worker->addFunction('init', 'downloader');
    $worker->addFunction('upload', 'uploader');
    function downloader($job){
        // downloads the images from facebook
        $data = unserialize($job->workload()); // receives serialized data
        $url = $data->url;
        $file = rand().'.jpg';
        $saveto  = __DIR__.'/tmp/'.$file;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $raw=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($saveto)){
            unlink($saveto);
        }
        $fp = fopen($saveto,'x');
        fwrite($fp, $raw);
        fclose($fp);
        // create a gearman client to upload image to google drive
        $client = new GearmanClient();
        $client->addServer();
        $data['file'] = $saveto;
        return $client->doNormal('upload', serialize($data)); // ensure synchronous dispatch
        // can implement a post request return call, to denote a loading point on a loading bar.
    }
    function uploader($job){
        $data = unserialize($job->workload());
        $file = $data->file;
        $google = $data->google; 
        $drive = new Drive($google);
        return $drive->init($file); // returns boolean
    }
?>

问题是当我使用php worker.php &amp; 启动工作程序时,该进程启动,但当我开始在控制台中执行其他操作并在控制台上打印消息“DONE”时自行终止。

如何执行我的流程?并保持此脚本运行?

这是一个模糊的解释,但请尝试调查并提供帮助。我对齿轮人真的很陌生。

谢谢

【问题讨论】:

    标签: php gearman


    【解决方案1】:

    你错过了工作循环。

    // Create the worker and configure it's capabilities
    $worker = new GearmanWorker();
    $worker->addServer('localhost');
    $worker->addFunction('init', 'downloader');
    $worker->addFunction('upload', 'uploader');
    
    // Start the worker
    while($worker->work());
    
    // Your function definition
    function downloader($job) {
        // do stuff with $job
    }
    
    function uploader($job) {
        // do stuff with $job
    }
    

    【讨论】:

    • 谢谢,我真是太傻了!理论上应该内部调用工作..我可以只调用上传器方法而不创建后台进程吗?
    • 是的,有一个创造新工作的工人完全没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多