167 lines
5.2 KiB
PHP
167 lines
5.2 KiB
PHP
<?php
|
|
|
|
class stTaskScheluderProgressBar extends stProgressBarTask
|
|
{
|
|
/**
|
|
*
|
|
* @var \stTaskInterface
|
|
*/
|
|
protected $currentTask = null;
|
|
|
|
protected $taskChanged = false;
|
|
|
|
public function initialize($offset = 0)
|
|
{
|
|
$this->currentTask = $this->getCurrentTask($offset);
|
|
}
|
|
|
|
public function count()
|
|
{
|
|
return $this->getParameter('count');
|
|
}
|
|
|
|
public function execute($offset = 0)
|
|
{
|
|
if ($this->taskChanged && null === $this->currentTask)
|
|
{
|
|
return $this->count();
|
|
}
|
|
|
|
if ($this->taskChanged && !$this->currentTask->isReadyToExecute(false))
|
|
{
|
|
return $offset + $this->currentTask->doCount();
|
|
}
|
|
|
|
$this->currentTask->getTask()->doStart();
|
|
|
|
$taskOffset = $offset - $this->getParameter('current_task_start_offset');
|
|
|
|
if ($this->currentTask->getTask()->getTaskConfigurationParameter('frontend'))
|
|
{
|
|
sfLoader::loadHelpers(['Helper', 'stUrl']);
|
|
$url = st_url_for('@stTaskScheluderFrontend?task_id='.$this->currentTask->getId().'&offset='.$taskOffset.'&token='.$this->getToken(), true, 'frontend');
|
|
$postData = http_build_query(['parameters' => $this->getParameter('current_task_parameters', [])], '', '&');
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
|
|
|
$result = curl_exec($ch);
|
|
$error = curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
if (empty($result) || !empty($error))
|
|
{
|
|
$this->currentTask->getTask()->doFinish();
|
|
return $offset + $this->currentTask->doCount() - $taskOffset;
|
|
}
|
|
|
|
$response = json_decode($result, true);
|
|
|
|
if (null === $response)
|
|
{
|
|
$this->currentTask->getTask()->doFinish();
|
|
return $offset + $this->currentTask->doCount() - $taskOffset;
|
|
}
|
|
|
|
$currentTaskOffset = $response['offset'];
|
|
|
|
$this->currentTask->setParameters($response['parameters']);
|
|
}
|
|
else
|
|
{
|
|
if (false === $this->currentTask->doInitialize())
|
|
{
|
|
$this->currentTask->getTask()->doFinish();
|
|
return $offset + $this->currentTask->doCount();
|
|
}
|
|
|
|
$currentTaskOffset = $this->currentTask->doExecute($taskOffset);
|
|
}
|
|
|
|
$this->setMessage($this->currentTask->getName());
|
|
$this->setParameter('current_task_parameters', $this->currentTask->getParameters());
|
|
|
|
usleep(250000);
|
|
|
|
return $offset + $currentTaskOffset - $taskOffset;
|
|
}
|
|
|
|
protected function getCurrentTask($offset): ?stTaskInterface
|
|
{
|
|
$task = $this->getParameter('current_task_id') ? stTaskFactory::createTaskById($this->getParameter('current_task_id')) : null;
|
|
|
|
if (null !== $task)
|
|
{
|
|
$task->setParameters($this->getParameter('current_task_parameters', []));
|
|
|
|
if (false === $task->doInitialize())
|
|
{
|
|
return $task;
|
|
}
|
|
}
|
|
|
|
$this->taskChanged = false;
|
|
|
|
if (null === $this->getParameter('current_task_id') || 0 === $task->doCount() || $offset - $this->getParameter('current_task_start_offset') >= $task->doCount())
|
|
{
|
|
$currentTask = $this->getNextTask();
|
|
$this->taskChanged = true;
|
|
$this->setParameter('current_task_id', $currentTask ? $currentTask['id'] : null);
|
|
$this->setParameter('current_task_start_offset', $currentTask ? $currentTask['offset'] : null);
|
|
$this->setParameter('current_task_parameters', isset($currentTask['parameters']) ? $currentTask['parameters'] : []);
|
|
|
|
if ($this->getParameter('current_task_id'))
|
|
{
|
|
$task = stTaskFactory::createTaskById($this->getParameter('current_task_id'));
|
|
$task->setParameters($this->getParameter('current_task_parameters', []));
|
|
}
|
|
else
|
|
{
|
|
$task = null;
|
|
}
|
|
|
|
sleep(1);
|
|
}
|
|
|
|
return $task;
|
|
}
|
|
|
|
public function finished()
|
|
{
|
|
stSecureToken::invalidateDBToken($this->getToken());
|
|
|
|
sfLoader::loadHelpers(array('Helper', 'stPartial', 'stAdminGenerator', 'stTaskScheluder'));
|
|
|
|
$this->setMessage(st_task_scheluder_progress_finished_message());
|
|
}
|
|
|
|
protected function getNextTask(): ?array
|
|
{
|
|
$taskPool = $this->getParameter('task_pool');
|
|
|
|
$task = array_pop($taskPool);
|
|
|
|
$this->setParameter('task_pool', $taskPool);
|
|
|
|
return $task;
|
|
}
|
|
|
|
protected function getToken(): string
|
|
{
|
|
$token = $this->getParameter('token');
|
|
|
|
if (null === $token)
|
|
{
|
|
$token = stSecureToken::createDBToken(3600 * 48);
|
|
$this->setParameter('token', $token);
|
|
}
|
|
|
|
return $token;
|
|
}
|
|
} |