Kri*_*ofe 10 php controller real-time redis laravel
我有一个服务器长时间运行应用程序,存储过程如下:
routes.php文件
Route::post('cloud/slice/{modelfileid}', 'CloudController@slice');
Route::get('cloud/slice/sliceProgress', 'CloudController@sliceProgress');
Run Code Online (Sandbox Code Playgroud)
slice()函数的一部分:
while($s = fgets($pipes[2]))
{
if(strpos($s, "Progress:") !== FALSE)
{
Log::info(100 * substr($s, -10, 4) . '%');
$this->redis->SET('sliceProgress' . $uid, 100 * substr($s, -10, 4) . '%');
}
}
Run Code Online (Sandbox Code Playgroud)
和我的客户每隔0.5秒请求redis的进度:
public function sliceProgress()
{
if (!isset($_SESSION["uid"]))
return Redirect::to('cloud');
$uid = $_SESSION["uid"];
return Response::json(array('status' => 'success', 'data' => array('progress' => $this->redis->GET('sliceProgress' . $uid))));
}
Run Code Online (Sandbox Code Playgroud)
和
$interval(function()
{
$scope.refreshProgress();
}, 500);
Run Code Online (Sandbox Code Playgroud)
但客户获得进度请求将持续到100%结束,没有低于100%的进度.也就是说,当服务器切片应用程序完成时,我得到进度响应.我认为laravel在处理切片时不会处理第二个请求.
编辑:我使用下面的代码在redis-> SET工作正常后得到slice()中的redis,这输出sliceProgress实时.
Log::info($this->redis->GET('sliceProgress' . $uid));
尝试在异步队列或 crons 等后台进程中运行此代码
while($s = fgets($pipes[2]))
{
if(strpos($s, "Progress:") !== FALSE)
{
Log::info(100 * substr($s, -10, 4) . '%');
$this->redis->SET('sliceProgress' . $uid, 100 * substr($s, -10, 4) . '%');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
393 次 |
| 最近记录: |