PHP 德语 - 在德国工人中称呼德国工人
PHP gearman - calling a gearman worker within a gearman worker
我对 gearman 很陌生。我正在尝试编写 PHP 脚本以从 URL 下载脚本并将其上传到用户的 google 驱动器。某种备份脚本..
我想做的是在进程中调用启动一个 gearman worker 进程,首先将图像从源下载到临时目录,然后将其上传到 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"。
我如何执行流程?并保留此脚本 运行?
这是一个模糊的解释,但请尝试查看并提供帮助。我对gearman真的很陌生。
谢谢
您缺少工作循环。
// 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
}
我对 gearman 很陌生。我正在尝试编写 PHP 脚本以从 URL 下载脚本并将其上传到用户的 google 驱动器。某种备份脚本..
我想做的是在进程中调用启动一个 gearman worker 进程,首先将图像从源下载到临时目录,然后将其上传到 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"。
我如何执行流程?并保留此脚本 运行?
这是一个模糊的解释,但请尝试查看并提供帮助。我对gearman真的很陌生。
谢谢
您缺少工作循环。
// 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
}