看板初始化提交
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Queue;
|
||||
|
||||
use Exception;
|
||||
use Kanboard\Core\Base;
|
||||
use Kanboard\Job\BaseJob;
|
||||
use SimpleQueue\Job;
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Class JobHandler
|
||||
*
|
||||
* @package Kanboard\Core\Queue
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class JobHandler extends Base
|
||||
{
|
||||
/**
|
||||
* Serialize a job
|
||||
*
|
||||
* @access public
|
||||
* @param BaseJob $job
|
||||
* @return Job
|
||||
*/
|
||||
public function serializeJob(BaseJob $job)
|
||||
{
|
||||
return new Job(array(
|
||||
'class' => get_class($job),
|
||||
'params' => $job->getJobParams(),
|
||||
'user_id' => $this->userSession->getId(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a job
|
||||
*
|
||||
* @access public
|
||||
* @param Job $job
|
||||
*/
|
||||
public function executeJob(Job $job)
|
||||
{
|
||||
$payload = $job->getBody();
|
||||
|
||||
try {
|
||||
$className = $payload['class'];
|
||||
$this->prepareJobSession($payload['user_id']);
|
||||
$this->prepareJobEnvironment();
|
||||
|
||||
if (DEBUG) {
|
||||
$this->logger->debug(__METHOD__.' Received job => '.$className.' ('.getmypid().')');
|
||||
$this->logger->debug(__METHOD__.' => '.json_encode($payload));
|
||||
}
|
||||
|
||||
$worker = new $className($this->container);
|
||||
call_user_func_array(array($worker, 'execute'), $payload['params']);
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error(__METHOD__.': Error during job execution: '.$e->getMessage());
|
||||
$this->logger->error(__METHOD__ .' => '.json_encode($payload));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the session for the job
|
||||
*
|
||||
* @access protected
|
||||
* @param integer $user_id
|
||||
*/
|
||||
protected function prepareJobSession($user_id)
|
||||
{
|
||||
session_flush();
|
||||
|
||||
if ($user_id > 0) {
|
||||
$user = $this->userModel->getById($user_id);
|
||||
$this->userSession->initialize($user);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush in-memory caching and specific events
|
||||
*
|
||||
* @access protected
|
||||
*/
|
||||
protected function prepareJobEnvironment()
|
||||
{
|
||||
$this->memoryCache->flush();
|
||||
$this->actionManager->removeEvents();
|
||||
$this->dispatcher->dispatch(new Event(), 'app.bootstrap');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Core\Queue;
|
||||
|
||||
use Kanboard\Core\Base;
|
||||
use Kanboard\Job\BaseJob;
|
||||
use LogicException;
|
||||
use SimpleQueue\Queue;
|
||||
|
||||
/**
|
||||
* Class QueueManager
|
||||
*
|
||||
* @package Kanboard\Core\Queue
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class QueueManager extends Base
|
||||
{
|
||||
/**
|
||||
* @var Queue
|
||||
*/
|
||||
protected $queue = null;
|
||||
|
||||
/**
|
||||
* Set queue driver
|
||||
*
|
||||
* @access public
|
||||
* @param Queue $queue
|
||||
* @return $this
|
||||
*/
|
||||
public function setQueue(Queue $queue)
|
||||
{
|
||||
$this->queue = $queue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a new job to the queue
|
||||
*
|
||||
* @access public
|
||||
* @param BaseJob $job
|
||||
* @return $this
|
||||
*/
|
||||
public function push(BaseJob $job)
|
||||
{
|
||||
$jobClassName = get_class($job);
|
||||
|
||||
if ($this->queue !== null) {
|
||||
$this->logger->debug(__METHOD__.': Job pushed in queue: '.$jobClassName);
|
||||
$this->queue->push(JobHandler::getInstance($this->container)->serializeJob($job));
|
||||
} else {
|
||||
$this->logger->debug(__METHOD__.': Job executed synchronously: '.$jobClassName);
|
||||
call_user_func_array(array($job, 'execute'), $job->getJobParams());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for new jobs
|
||||
*
|
||||
* @access public
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function listen()
|
||||
{
|
||||
if ($this->queue === null) {
|
||||
throw new LogicException('No queue driver defined or unable to connect to broker!');
|
||||
}
|
||||
|
||||
while ($job = $this->queue->pull()) {
|
||||
JobHandler::getInstance($this->container)->executeJob($job);
|
||||
$this->queue->completed($job);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user