【发布时间】: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 & 启动工作程序时,该进程启动,但当我开始在控制台中执行其他操作并在控制台上打印消息“DONE”时自行终止。
如何执行我的流程?并保持此脚本运行?
这是一个模糊的解释,但请尝试调查并提供帮助。我对齿轮人真的很陌生。
谢谢
【问题讨论】: