Improve background workers
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Kanboard\Core\Http;
|
||||
|
||||
use Kanboard\Core\Base;
|
||||
use Kanboard\Job\HttpAsyncJob;
|
||||
|
||||
/**
|
||||
* HTTP client
|
||||
@@ -79,6 +80,24 @@ class Client extends Base
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a POST HTTP request encoded in JSON (Fire and forget)
|
||||
*
|
||||
* @access public
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param string[] $headers
|
||||
*/
|
||||
public function postJsonAsync($url, array $data, array $headers = array())
|
||||
{
|
||||
$this->queueManager->push(HttpAsyncJob::getInstance($this->container)->withParams(
|
||||
'POST',
|
||||
$url,
|
||||
json_encode($data),
|
||||
array_merge(array('Content-type: application/json'), $headers)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a POST HTTP request encoded in www-form-urlencoded
|
||||
*
|
||||
@@ -98,22 +117,41 @@ class Client extends Base
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a POST HTTP request encoded in www-form-urlencoded (fire and forget)
|
||||
*
|
||||
* @access public
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param string[] $headers
|
||||
*/
|
||||
public function postFormAsync($url, array $data, array $headers = array())
|
||||
{
|
||||
$this->queueManager->push(HttpAsyncJob::getInstance($this->container)->withParams(
|
||||
'POST',
|
||||
$url,
|
||||
http_build_query($data),
|
||||
array_merge(array('Content-type: application/x-www-form-urlencoded'), $headers)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the HTTP request
|
||||
*
|
||||
* @access private
|
||||
* @access public
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param string $content
|
||||
* @param string[] $headers
|
||||
* @return string
|
||||
*/
|
||||
private function doRequest($method, $url, $content, array $headers)
|
||||
public function doRequest($method, $url, $content, array $headers)
|
||||
{
|
||||
if (empty($url)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$startTime = microtime(true);
|
||||
$stream = @fopen(trim($url), 'r', false, stream_context_create($this->getContext($method, $content, $headers)));
|
||||
$response = '';
|
||||
|
||||
@@ -128,6 +166,7 @@ class Client extends Base
|
||||
$this->logger->debug('HttpClient: payload='.$content);
|
||||
$this->logger->debug('HttpClient: metadata='.var_export(@stream_get_meta_data($stream), true));
|
||||
$this->logger->debug('HttpClient: response='.$response);
|
||||
$this->logger->debug('HttpClient: executionTime='.(microtime(true) - $startTime));
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
@@ -26,6 +26,7 @@ class JobHandler extends Base
|
||||
return new Job(array(
|
||||
'class' => get_class($job),
|
||||
'params' => $job->getJobParams(),
|
||||
'user_id' => $this->userSession->getId(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -39,12 +40,30 @@ class JobHandler extends Base
|
||||
{
|
||||
$payload = $job->getBody();
|
||||
$className = $payload['class'];
|
||||
$this->prepareJobSession($payload['user_id']);
|
||||
|
||||
if (DEBUG) {
|
||||
$this->logger->debug(__METHOD__.' Received job => '.$className);
|
||||
$this->logger->debug(__METHOD__.' Received job => '.$className.' ('.getmypid().')');
|
||||
}
|
||||
|
||||
$worker = new $className($this->container);
|
||||
call_user_func_array(array($worker, 'execute'), $payload['params']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the session for the job
|
||||
*
|
||||
* @access protected
|
||||
* @param integer $user_id
|
||||
*/
|
||||
protected function prepareJobSession($user_id)
|
||||
{
|
||||
$session = array();
|
||||
$this->sessionStorage->setStorage($session);
|
||||
|
||||
if ($user_id > 0) {
|
||||
$user = $this->userModel->getById($user_id);
|
||||
$this->userSession->initialize($user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
43
app/Job/HttpAsyncJob.php
Normal file
43
app/Job/HttpAsyncJob.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Job;
|
||||
|
||||
/**
|
||||
* Async HTTP Client (fire and forget)
|
||||
*
|
||||
* @package Kanboard\Job
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class HttpAsyncJob extends BaseJob
|
||||
{
|
||||
/**
|
||||
* Set job parameters
|
||||
*
|
||||
* @access public
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param string $content
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
*/
|
||||
public function withParams($method, $url, $content, array $headers)
|
||||
{
|
||||
$this->jobParams = array($method, $url, $content, $headers);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set job parameters
|
||||
*
|
||||
* @access public
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param string $content
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
*/
|
||||
public function execute($method, $url, $content, array $headers)
|
||||
{
|
||||
$this->httpClient->doRequest($method, $url, $content, $headers);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user