我对Gearman很陌生。我正在尝试编写PHP脚本以从URL下载脚本并将其上传到用户的Google驱动器。某种备份脚本。
我想做的是在该过程中调用启动Gearman Worker进程,以首先将图像从源下载到temp目录,然后将其上传到Google驱动器。这是脚本:
<?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 &
启动worker时,该过程开始,但是一旦我开始在控制台上执行其他操作并在控制台上打印消息“ DONE”,就会杀死自己。
如何执行我的流程?并保持该脚本运行?
这是一个模糊的解释,但请尝试研究并提供帮助。我真的是齿轮工新手。
谢谢
答案 0 :(得分:2)
您缺少工作循环。
// 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
}