summaryrefslogtreecommitdiff
path: root/app/Core
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-06-05 14:19:07 -0400
committerFrederic Guillot <fred@kanboard.net>2016-06-05 14:19:07 -0400
commita08339059b06bcfb673b13a380b6f199abfb5983 (patch)
tree81638ec619684f04d9b5676bea1f73e1a2197cd1 /app/Core
parentf48e5456312ddb366f9b623cd97b0e740fe99d9e (diff)
Improve background workers
Diffstat (limited to 'app/Core')
-rw-r--r--app/Core/Http/Client.php43
-rw-r--r--app/Core/Queue/JobHandler.php21
2 files changed, 61 insertions, 3 deletions
diff --git a/app/Core/Http/Client.php b/app/Core/Http/Client.php
index 12b0a1cb..16e423dc 100644
--- a/app/Core/Http/Client.php
+++ b/app/Core/Http/Client.php
@@ -3,6 +3,7 @@
namespace Kanboard\Core\Http;
use Kanboard\Core\Base;
+use Kanboard\Job\HttpAsyncJob;
/**
* HTTP client
@@ -80,6 +81,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
*
* @access public
@@ -99,21 +118,40 @@ 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;
diff --git a/app/Core/Queue/JobHandler.php b/app/Core/Queue/JobHandler.php
index a2c4a2c7..f8736cce 100644
--- a/app/Core/Queue/JobHandler.php
+++ b/app/Core/Queue/JobHandler.php
@@ -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);
+ }
+ }
}