* Refactor queryDownloadImageStatus function.

This commit is contained in:
daitingting
2023-12-26 11:08:56 +00:00
parent 0fd6aa40c3
commit c77cdf049b
2 changed files with 48 additions and 39 deletions
+10 -36
View File
@@ -250,50 +250,27 @@ class zahostModel extends model
}
/**
* 查询镜像的状态。
* Query image download progress.
*
* @param object $image
* @access public
* @return object Return Status code.
* @return object
*/
public function queryDownloadImageStatus($image)
public function queryDownloadImageStatus(object $image)
{
if(!empty($this->imageStatusList)) $result = $this->imageStatusList;
if(empty($this->imageStatusList))
{
$host = $this->getById($image->host);
$host = $this->getByID($image->host);
$apiUrl = 'http://' . $host->extranet . ':' . $host->zap . '/api/v1/task/getStatus';
$result = $this->imageStatusList = json_decode(commonModel::http($apiUrl, array(), array(CURLOPT_CUSTOMREQUEST => 'POST'), array("Authorization:$host->tokenSN"), 'json'));
$this->imageStatusList = $result = json_decode(commonModel::http($apiUrl, array(), array(CURLOPT_CUSTOMREQUEST => 'POST'), array("Authorization:$host->tokenSN"), 'json'));
if(!$result or $result->code != 'success') return $image->status;
}else{
$result = $this->imageStatusList;
}
$currentTask = null;
$is_finish = false;
foreach($result->data as $status => $group)
{
if($is_finish) break;
foreach($group as $task)
{
if($is_finish) break;
if($task->task == $image->id)
{
$task->endDate = substr($task->endDate, 0, 19);
if(empty($currentTask) || strtotime($task->endDate) > strtotime($currentTask->endDate))
{
$currentTask = $task;
}
if($task->status == 'inprogress')
{
$currentTask = $task;
$is_finish = true;
break;
}
}
}
if(!$result || $result->code != 'success') return $image;
}
$currentTask = $this->zahostTao->getCurrentTask($image->id, $result->data);
if($currentTask)
{
$image->rate = $currentTask->rate;
@@ -301,10 +278,7 @@ class zahostModel extends model
if(!empty($result->data->inprogress) && $currentTask->task != $result->data->inprogress[0]->task && $currentTask->status == 'created') $image->status = 'pending';
$this->dao->update(TABLE_IMAGE)
->set('status')->eq($image->status)
->set('path')->eq(!empty($currentTask->path) ? $currentTask->path : '')
->where('id')->eq($image->id)->exec();
$this->dao->update(TABLE_IMAGE)->set('status')->eq($image->status)->set('path')->eq(zget($currentTask, 'path', ''))->where('id')->eq($image->id)->exec();
$image->taskID = $currentTask->id;
}
+38 -3
View File
@@ -6,9 +6,9 @@ class zahostTao extends zahostModel
* 将没有插入到 image 表的镜像数据插入到 image 表中。
* Insert image list.
*
* @param array $imageList
* @param int $hostID
* @param array $downloadedImageList
* @param array $imageList
* @param int $hostID
* @param array $downloadedImageList
* @access protected
* @return bool
*/
@@ -33,4 +33,39 @@ class zahostTao extends zahostModel
return $refreshPageData;
}
/**
* 获取当前的下载任务。
* Get current download task.
*
* @param int $imageID
* @param array $statusGroupTasks
* @access protected
* @return null|object
*/
protected function getCurrentTask(int $imageID, object $statusGroupTasks): null|object
{
$currentTask = null;
$finished = false;
foreach($statusGroupTasks as $groupTasks)
{
if($finished) break;
foreach($groupTasks as $task)
{
if($finished) break;
if($task->task != $imageID) continue;
$task->endDate = substr($task->endDate, 0, 19);
if(empty($currentTask) || strtotime($task->endDate) > strtotime($currentTask->endDate)) $currentTask = $task;
if($task->status == 'inprogress')
{
$currentTask = $task;
$finished = true;
break;
}
}
}
return $currentTask;
}
}