Skip to content

Commit

Permalink
Merge pull request #22 from uafrica/cake36
Browse files Browse the repository at this point in the history
Cake 3.6 upgrade
  • Loading branch information
dakota committed Aug 16, 2018
2 parents 4d71028 + b6b9a78 commit 9836577
Show file tree
Hide file tree
Showing 23 changed files with 451 additions and 100 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
"type": "cakephp-plugin",
"require": {
"php": "^7.0",
"cakephp/cakephp": "^3.4",
"cakephp/cakephp": "^3.6",
"cakephp/migrations": "^1.5",
"php-amqplib/php-amqplib": "^2.5"
},
"require-dev": {
"cakephp/app": "^3.4",
"cakephp/app": "^3.6",
"phpunit/phpunit": "^5.7",
"sizuhiko/cake_fabricate": "^0.2.1",
"squizlabs/php_codesniffer": "^3.0",
"cakephp/cakephp-codesniffer": "^3.0.0"
},
"suggest": {
"dereuromark/cakephp-ide-helper": "For maximum IDE support, especially around enqueue() usage."
},
"scripts": {
"cs-check": "phpcs --colors -p -s --extensions=php,ctp --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/",
"cs-fix": "phpcbf --colors -p -s --extensions=php,ctp --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/"
Expand Down
37 changes: 0 additions & 37 deletions config/bootstrap.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Broker/RabbitMqBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function publishJob(Job $job)
{
$delay = $job->getRunAt()->isFuture() ? Time::now()->diffInSeconds($job->getRunAt(), false) * 1000 : 0;

$jobPriority = $this->_manager->config('maximum.priority') - $job->getPriority();
$jobPriority = $this->_manager->getConfig('maximum.priority') - $job->getPriority();
if ($jobPriority < 0) {
$jobPriority = 0;
} elseif ($jobPriority > 255) {
Expand All @@ -86,6 +86,7 @@ public function publishJob(Job $job)
];

$this->getDriver()->publishJob($jobData);
$job->setPushedToBroker(true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Datasource/TableDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TableDatasource extends BaseDatasource
*/
protected function _table(): DatastoreInterface
{
return $this->tableLocator()
return $this->getTableLocator()
->get($this->getConfig('tableName'));
}

Expand Down
72 changes: 72 additions & 0 deletions src/DelayedJob/DebugKitJobManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace DelayedJobs\DelayedJob;

use DelayedJobs\Broker\BrokerInterface;
use DelayedJobs\Datasource\DatasourceInterface;

/**
* Class DebugKitJobManager
*/
class DebugKitJobManager extends JobManager
{
/**
* A reference to the object were jobs will be pushed too for logging
*
* @var \ArrayObject
*/
protected $jobLog;

public function __construct(
array $config = [],
\DelayedJobs\Datasource\DatasourceInterface $datastore = null,
\DelayedJobs\Broker\BrokerInterface $messageBroker = null
) {
$this->jobLog = $config['debugKitLog'];
unset($config['debugKitLog']);

parent::__construct($config, $datastore, $messageBroker);
}

/**
* @param \DelayedJobs\DelayedJob\Job $job The job instance
* @return void
*/
protected function pushToLog(Job $job)
{
$jobData = [
'id' => $job->getId(),
'worker' => $job->getWorker(),
'sequence' => $job->getSequence(),
'payload' => $job->getPayload(),
'priority' => $job->getPriority(),
'pushedToBroker' => $job->isPushedToBroker(),
];
$this->jobLog[] = $jobData;
}

/**
* @param \DelayedJobs\DelayedJob\Job $job Job that needs to be enqueued
* @param bool $skipPersist Skip the persistance step (e.g. it's already been persisted
* @return void
*/
public function enqueue(Job $job, bool $skipPersist = false)
{
parent::enqueue($job, $skipPersist);

$this->pushToLog($job);
}

/**
* @param array $jobs
* @return void
*/
public function enqueueBatch(array $jobs)
{
parent::enqueueBatch($jobs);

foreach ($jobs as $job) {
$this->pushToLog($job);
}
}
}
4 changes: 2 additions & 2 deletions src/DelayedJob/EnqueueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function enqueue($worker, $payload = null, array $options = []): Job
->setData($options);
}

JobManager::instance()
JobManager::getInstance()
->enqueue($job);

return $job;
Expand Down Expand Up @@ -62,7 +62,7 @@ public function enqueueBatch($worker, array $jobsToEnqueue, array $options = [])
$jobs[] = $job;
}

JobManager::instance()
JobManager::getInstance()
->enqueueBatch($jobs);

return $jobs;
Expand Down
25 changes: 25 additions & 0 deletions src/DelayedJob/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class Job
* @var bool
*/
protected $_manualRun = false;
/**
* Indicates that this job instance was pushed to the job broker
*
* @var bool
*/
protected $_pushedToBroker = false;

/**
* Job constructor.
Expand Down Expand Up @@ -754,4 +760,23 @@ public function setBrokerMessageBody($brokerMessageBody): Job

return $this;
}

/**
* @return bool
*/
public function isPushedToBroker(): bool
{
return $this->_pushedToBroker;
}

/**
* @param bool $pushedToBroker
* @return $this
*/
public function setPushedToBroker(bool $pushedToBroker): Job
{
$this->_pushedToBroker = $pushedToBroker;

return $this;
}
}
23 changes: 14 additions & 9 deletions src/DelayedJob/JobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,23 @@ public function __construct(
}

/**
* Returns the globally available instance of a \DelayedJobs\DelayedJobs\DelayedJobsManager
* Set as the globally available instance
*
* If called with the first parameter, it will be set as the globally available instance
* @param \DelayedJobs\DelayedJob\ManagerInterface|null $manager The manager interface to inject
* @return void
*/
public static function setInstance(?ManagerInterface $manager)
{
static::$_instance = $manager;
}

/**
* Returns the globally available instance of a \DelayedJobs\DelayedJobs\JobsManager
*
* @param \DelayedJobs\DelayedJob\ManagerInterface $manager Delayed jobs instance.
* @return \DelayedJobs\DelayedJob\ManagerInterface the global delayed jobs manager
*/
public static function instance(ManagerInterface $manager = null): ManagerInterface
public static function getInstance(): ManagerInterface
{
if ($manager instanceof ManagerInterface) {
static::$_instance = $manager;
}
if (empty(static::$_instance)) {
static::$_instance = new self(Configure::read('DelayedJobs'));
}
Expand Down Expand Up @@ -376,10 +381,10 @@ protected function _handleResult(ResultInterface $result, $duration)
protected function _dispatchWorkerEvent(JobWorkerInterface $jobWorker, $name, $data = null, $subject = null): Event
{
$event = new Event($name, $subject ?? $this, $data);
$this->eventManager()
$this->getEventManager()
->dispatch($event);
if ($jobWorker instanceof EventDispatcherInterface) {
$jobWorker->eventManager()
$jobWorker->getEventManager()
->dispatch($event);
}

Expand Down
46 changes: 46 additions & 0 deletions src/Generator/Task/WorkerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace DelayedJobs\Generator\Task;

use Cake\Core\App;
use DelayedJobs\DelayedJob\Job;
use DelayedJobs\WorkerFinder;
use IdeHelper\Generator\Task\TaskInterface;

/**
* Class WorkerTask
*/
class WorkerTask implements TaskInterface
{
const CLASS_JOB = Job::class;

/**
* @return array
*/
public function collect()
{
$map = [];
$workers = $this->collectWorkers();
$map = [];
foreach ($workers as $worker) {
$map[$worker] = '\\' . static::CLASS_JOB . '::class';
}

$result['\\' . static::CLASS_JOB . '::enqueue(0)'] = $map;

return $result;
}

/**
* @return string[]
*/
protected function collectWorkers()
{
$result = [];
$workerFinder = new WorkerFinder();
$workers = $workerFinder->allAppAndPluginWorkers();
sort($workers);

return $workers;
}
}
Loading

0 comments on commit 9836577

Please sign in to comment.