@@ -11,187 +11,187 @@ |
||
| 11 | 11 | |
| 12 | 12 | class QueueShellTest extends TestCase |
| 13 | 13 | { |
| 14 | - use ToolsTestTrait; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * @var \Queue\Shell\QueueShell|\PHPUnit_Framework_MockObject_MockObject |
|
| 19 | - */ |
|
| 20 | - public $QueueShell; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * |
|
| 24 | - * @var \Tools\TestSuite\ConsoleOutput |
|
| 25 | - */ |
|
| 26 | - public $out; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * @var \Tools\TestSuite\ConsoleOutput |
|
| 31 | - */ |
|
| 32 | - public $err; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Fixtures to load |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - public $fixtures = [ |
|
| 40 | - 'plugin.Queue.QueuedTasks' |
|
| 41 | - ]; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Setup Defaults |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function setUp() |
|
| 49 | - { |
|
| 50 | - parent::setUp(); |
|
| 51 | - |
|
| 52 | - $this->out = new ConsoleOutput(); |
|
| 53 | - $this->err = new ConsoleOutput(); |
|
| 54 | - $io = new ConsoleIo($this->out, $this->err); |
|
| 55 | - |
|
| 56 | - $this->QueueShell = $this->getMockBuilder(QueueShell::class) |
|
| 57 | - ->setMethods([ |
|
| 58 | - 'in', |
|
| 59 | - 'err', |
|
| 60 | - '_stop' |
|
| 61 | - ]) |
|
| 62 | - ->setConstructorArgs([ |
|
| 63 | - $io |
|
| 64 | - ]) |
|
| 65 | - ->getMock(); |
|
| 66 | - $this->QueueShell->initialize(); |
|
| 67 | - |
|
| 68 | - Configure::write('Queue', [ |
|
| 69 | - 'sleepTime' => 2, |
|
| 70 | - 'defaultWorkerTimeout' => 3, |
|
| 71 | - 'workerMaxRuntime' => 5, |
|
| 72 | - 'cleanupTimeout' => 10, |
|
| 73 | - 'exitWhenNothingToDo' => false, |
|
| 74 | - 'log' => false |
|
| 75 | - ]); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * |
|
| 80 | - * @return void |
|
| 81 | - */ |
|
| 82 | - public function testObject() |
|
| 83 | - { |
|
| 84 | - $this->assertTrue(is_object($this->QueueShell)); |
|
| 85 | - $this->assertInstanceOf(QueueShell::class, $this->QueueShell); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * |
|
| 90 | - * @return void |
|
| 91 | - */ |
|
| 92 | - public function testStats() |
|
| 93 | - { |
|
| 94 | - $this->_needsConnection(); |
|
| 95 | - |
|
| 96 | - $this->QueueShell->stats(); |
|
| 97 | - $this->assertContains('Total unfinished jobs: 0', $this->out->output()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * |
|
| 102 | - * @return void |
|
| 103 | - */ |
|
| 104 | - public function testSettings() |
|
| 105 | - { |
|
| 106 | - $this->QueueShell->settings(); |
|
| 107 | - $this->assertContains('* cleanupTimeout: 10', $this->out->output()); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - public function testAddInexistent() |
|
| 115 | - { |
|
| 116 | - $this->QueueShell->args[] = 'FooBar'; |
|
| 117 | - $this->QueueShell->add(); |
|
| 118 | - $this->assertContains('Error: Task not found: FooBar', $this->out->output()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function testAdd() |
|
| 126 | - { |
|
| 127 | - $this->QueueShell->args[] = 'Example'; |
|
| 128 | - $this->QueueShell->add(); |
|
| 129 | - |
|
| 130 | - $this->assertContains('OK, job created, now run the worker', $this->out->output(), print_r($this->out->output(), true)); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * |
|
| 135 | - * @return void |
|
| 136 | - */ |
|
| 137 | - public function testTimeNeeded() |
|
| 138 | - { |
|
| 139 | - $this->QueueShell = $this->getMockBuilder(QueueShell::class) |
|
| 140 | - ->setMethods([ |
|
| 141 | - '_time' |
|
| 142 | - ]) |
|
| 143 | - ->getMock(); |
|
| 144 | - |
|
| 145 | - $first = time(); |
|
| 146 | - $second = $first - HOUR + MINUTE; |
|
| 147 | - $this->QueueShell->expects($this->at(0)) |
|
| 148 | - ->method('_time') |
|
| 149 | - ->will($this->returnValue($first)); |
|
| 150 | - $this->QueueShell->expects($this->at(1)) |
|
| 151 | - ->method('_time') |
|
| 152 | - ->will($this->returnValue($second)); |
|
| 153 | - $this->QueueShell->expects($this->exactly(2)) |
|
| 154 | - ->method('_time') |
|
| 155 | - ->withAnyParameters(); |
|
| 156 | - |
|
| 157 | - $result = $this->invokeMethod($this->QueueShell, '_timeNeeded'); |
|
| 158 | - $this->assertSame('3540s', $result); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * |
|
| 163 | - * @return void |
|
| 164 | - */ |
|
| 165 | - public function testMemoryUsage() |
|
| 166 | - { |
|
| 167 | - $result = $this->invokeMethod($this->QueueShell, '_memoryUsage'); |
|
| 168 | - $this->assertRegExp('/^\d+MB/', $result, 'Should be e.g. `17MB` or `17MB/1GB` etc.'); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * |
|
| 173 | - * @return void |
|
| 174 | - */ |
|
| 175 | - public function testStringToArray() |
|
| 176 | - { |
|
| 177 | - $string = 'Foo,Bar,'; |
|
| 178 | - $result = $this->invokeMethod($this->QueueShell, '_stringToArray', [$string]); |
|
| 179 | - |
|
| 180 | - $expected = [ |
|
| 181 | - 'Foo', |
|
| 182 | - 'Bar' |
|
| 183 | - ]; |
|
| 184 | - $this->assertSame($expected, $result); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Helper method for skipping tests that need a real connection. |
|
| 189 | - * |
|
| 190 | - * @return void |
|
| 191 | - */ |
|
| 192 | - protected function _needsConnection() |
|
| 193 | - { |
|
| 194 | - $config = ConnectionManager::getConfig('test'); |
|
| 195 | - $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
|
| 196 | - } |
|
| 14 | + use ToolsTestTrait; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * @var \Queue\Shell\QueueShell|\PHPUnit_Framework_MockObject_MockObject |
|
| 19 | + */ |
|
| 20 | + public $QueueShell; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * |
|
| 24 | + * @var \Tools\TestSuite\ConsoleOutput |
|
| 25 | + */ |
|
| 26 | + public $out; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * @var \Tools\TestSuite\ConsoleOutput |
|
| 31 | + */ |
|
| 32 | + public $err; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Fixtures to load |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + public $fixtures = [ |
|
| 40 | + 'plugin.Queue.QueuedTasks' |
|
| 41 | + ]; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Setup Defaults |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function setUp() |
|
| 49 | + { |
|
| 50 | + parent::setUp(); |
|
| 51 | + |
|
| 52 | + $this->out = new ConsoleOutput(); |
|
| 53 | + $this->err = new ConsoleOutput(); |
|
| 54 | + $io = new ConsoleIo($this->out, $this->err); |
|
| 55 | + |
|
| 56 | + $this->QueueShell = $this->getMockBuilder(QueueShell::class) |
|
| 57 | + ->setMethods([ |
|
| 58 | + 'in', |
|
| 59 | + 'err', |
|
| 60 | + '_stop' |
|
| 61 | + ]) |
|
| 62 | + ->setConstructorArgs([ |
|
| 63 | + $io |
|
| 64 | + ]) |
|
| 65 | + ->getMock(); |
|
| 66 | + $this->QueueShell->initialize(); |
|
| 67 | + |
|
| 68 | + Configure::write('Queue', [ |
|
| 69 | + 'sleepTime' => 2, |
|
| 70 | + 'defaultWorkerTimeout' => 3, |
|
| 71 | + 'workerMaxRuntime' => 5, |
|
| 72 | + 'cleanupTimeout' => 10, |
|
| 73 | + 'exitWhenNothingToDo' => false, |
|
| 74 | + 'log' => false |
|
| 75 | + ]); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * |
|
| 80 | + * @return void |
|
| 81 | + */ |
|
| 82 | + public function testObject() |
|
| 83 | + { |
|
| 84 | + $this->assertTrue(is_object($this->QueueShell)); |
|
| 85 | + $this->assertInstanceOf(QueueShell::class, $this->QueueShell); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * |
|
| 90 | + * @return void |
|
| 91 | + */ |
|
| 92 | + public function testStats() |
|
| 93 | + { |
|
| 94 | + $this->_needsConnection(); |
|
| 95 | + |
|
| 96 | + $this->QueueShell->stats(); |
|
| 97 | + $this->assertContains('Total unfinished jobs: 0', $this->out->output()); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * |
|
| 102 | + * @return void |
|
| 103 | + */ |
|
| 104 | + public function testSettings() |
|
| 105 | + { |
|
| 106 | + $this->QueueShell->settings(); |
|
| 107 | + $this->assertContains('* cleanupTimeout: 10', $this->out->output()); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + public function testAddInexistent() |
|
| 115 | + { |
|
| 116 | + $this->QueueShell->args[] = 'FooBar'; |
|
| 117 | + $this->QueueShell->add(); |
|
| 118 | + $this->assertContains('Error: Task not found: FooBar', $this->out->output()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function testAdd() |
|
| 126 | + { |
|
| 127 | + $this->QueueShell->args[] = 'Example'; |
|
| 128 | + $this->QueueShell->add(); |
|
| 129 | + |
|
| 130 | + $this->assertContains('OK, job created, now run the worker', $this->out->output(), print_r($this->out->output(), true)); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * |
|
| 135 | + * @return void |
|
| 136 | + */ |
|
| 137 | + public function testTimeNeeded() |
|
| 138 | + { |
|
| 139 | + $this->QueueShell = $this->getMockBuilder(QueueShell::class) |
|
| 140 | + ->setMethods([ |
|
| 141 | + '_time' |
|
| 142 | + ]) |
|
| 143 | + ->getMock(); |
|
| 144 | + |
|
| 145 | + $first = time(); |
|
| 146 | + $second = $first - HOUR + MINUTE; |
|
| 147 | + $this->QueueShell->expects($this->at(0)) |
|
| 148 | + ->method('_time') |
|
| 149 | + ->will($this->returnValue($first)); |
|
| 150 | + $this->QueueShell->expects($this->at(1)) |
|
| 151 | + ->method('_time') |
|
| 152 | + ->will($this->returnValue($second)); |
|
| 153 | + $this->QueueShell->expects($this->exactly(2)) |
|
| 154 | + ->method('_time') |
|
| 155 | + ->withAnyParameters(); |
|
| 156 | + |
|
| 157 | + $result = $this->invokeMethod($this->QueueShell, '_timeNeeded'); |
|
| 158 | + $this->assertSame('3540s', $result); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * |
|
| 163 | + * @return void |
|
| 164 | + */ |
|
| 165 | + public function testMemoryUsage() |
|
| 166 | + { |
|
| 167 | + $result = $this->invokeMethod($this->QueueShell, '_memoryUsage'); |
|
| 168 | + $this->assertRegExp('/^\d+MB/', $result, 'Should be e.g. `17MB` or `17MB/1GB` etc.'); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * |
|
| 173 | + * @return void |
|
| 174 | + */ |
|
| 175 | + public function testStringToArray() |
|
| 176 | + { |
|
| 177 | + $string = 'Foo,Bar,'; |
|
| 178 | + $result = $this->invokeMethod($this->QueueShell, '_stringToArray', [$string]); |
|
| 179 | + |
|
| 180 | + $expected = [ |
|
| 181 | + 'Foo', |
|
| 182 | + 'Bar' |
|
| 183 | + ]; |
|
| 184 | + $this->assertSame($expected, $result); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Helper method for skipping tests that need a real connection. |
|
| 189 | + * |
|
| 190 | + * @return void |
|
| 191 | + */ |
|
| 192 | + protected function _needsConnection() |
|
| 193 | + { |
|
| 194 | + $config = ConnectionManager::getConfig('test'); |
|
| 195 | + $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
|
| 196 | + } |
|
| 197 | 197 | } |
@@ -28,536 +28,536 @@ |
||
| 28 | 28 | class QueuedTasksTable extends Table |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - const DRIVER_MYSQL = 'Mysql'; |
|
| 32 | - |
|
| 33 | - const DRIVER_POSTGRES = 'Postgres'; |
|
| 34 | - |
|
| 35 | - const DRIVER_SQLSERVER = 'Sqlserver'; |
|
| 36 | - |
|
| 37 | - const STATS_LIMIT = 100000; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Initialize method |
|
| 41 | - * |
|
| 42 | - * @param array $config The configuration for the Table. |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function initialize(array $config) |
|
| 46 | - { |
|
| 47 | - parent::initialize($config); |
|
| 48 | - |
|
| 49 | - $this->setTable('queued_tasks'); |
|
| 50 | - $this->setDisplayField('id'); |
|
| 51 | - $this->setPrimaryKey('id'); |
|
| 52 | - |
|
| 53 | - $this->addBehavior('Timestamp'); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * set connection name |
|
| 58 | - * |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - public static function defaultConnectionName() |
|
| 62 | - { |
|
| 63 | - $connection = Configure::read('Queue.connection'); |
|
| 64 | - if (!empty($connection)) { |
|
| 65 | - return $connection; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return parent::defaultConnectionName(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * |
|
| 73 | - * @param \Cake\Event\Event $event Model event |
|
| 74 | - * @param \ArrayObject $data The data |
|
| 75 | - * @param \ArrayObject $options The options |
|
| 76 | - * @return void |
|
| 77 | - */ |
|
| 78 | - public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
|
| 79 | - { |
|
| 80 | - if (isset($data['data']) && $data['data'] === '') { |
|
| 81 | - $data['data'] = null; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Adds a new job to the queue. |
|
| 87 | - * |
|
| 88 | - * @param string $taskName Task name |
|
| 89 | - * @param array|null $data Array of data |
|
| 90 | - * @param string $notBefore A datetime which indicates when the job may be executed |
|
| 91 | - * @return \Queue\Model\Entity\QueuedTask Saved job entity |
|
| 92 | - */ |
|
| 93 | - public function createJob($taskName, array $data = null, string $notBefore = null) |
|
| 94 | - { |
|
| 95 | - $task = [ |
|
| 96 | - 'task' => $taskName, |
|
| 97 | - 'data' => serialize($data), |
|
| 98 | - 'not_before' => $this->getDateTime() |
|
| 99 | - ]; |
|
| 100 | - |
|
| 101 | - if (!empty($notBefore)) { |
|
| 102 | - $task['not_before'] = $this->getDateTime(strtotime($notBefore)); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $queuedTask = $this->newEntity($task); |
|
| 106 | - |
|
| 107 | - return $this->saveOrFail($queuedTask); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Returns the number of items in the queue. |
|
| 112 | - * Either returns the number of ALL pending jobs, or the number of pending jobs of the passed type. |
|
| 113 | - * |
|
| 114 | - * @param string|null $taskName Task type to Count |
|
| 115 | - * @return int |
|
| 116 | - */ |
|
| 117 | - public function getLength($taskName = null) |
|
| 118 | - { |
|
| 119 | - $findConf = [ |
|
| 120 | - 'conditions' => [ |
|
| 121 | - 'completed IS' => null |
|
| 122 | - ] |
|
| 123 | - ]; |
|
| 124 | - if ($taskName !== null) { |
|
| 125 | - $findConf['conditions']['task'] = $taskName; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - return $this->find('all', $findConf)->count(); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Return a list of all task types in the Queue. |
|
| 133 | - * |
|
| 134 | - * @return \Cake\ORM\Query |
|
| 135 | - */ |
|
| 136 | - public function getTypes() |
|
| 137 | - { |
|
| 138 | - $findCond = [ |
|
| 139 | - 'fields' => [ |
|
| 140 | - 'task' |
|
| 141 | - ], |
|
| 142 | - 'group' => [ |
|
| 143 | - 'task' |
|
| 144 | - ], |
|
| 145 | - 'keyField' => 'task', |
|
| 146 | - 'valueField' => 'task' |
|
| 147 | - ]; |
|
| 148 | - |
|
| 149 | - return $this->find('list', $findCond); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Return some statistics about finished jobs still in the Database. |
|
| 154 | - * TO-DO: rewrite as virtual field |
|
| 155 | - * |
|
| 156 | - * @return \Cake\ORM\Query |
|
| 157 | - */ |
|
| 158 | - public function getStats() |
|
| 159 | - { |
|
| 160 | - $driverName = $this->_getDriverName(); |
|
| 161 | - $options = [ |
|
| 162 | - 'fields' => function (Query $query) use ($driverName) { |
|
| 163 | - $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
|
| 164 | - $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
| 165 | - $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
|
| 166 | - switch ($driverName) { |
|
| 167 | - case static::DRIVER_SQLSERVER: |
|
| 168 | - $alltime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', created)"); |
|
| 169 | - $runtime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
| 170 | - $fetchdelay = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', fetched) - (CASE WHEN not_before IS NULL THEN DATEDIFF(s, '1970-01-01 00:00:00', created) ELSE DATEDIFF(s, '1970-01-01 00:00:00', not_before) END)"); |
|
| 171 | - break; |
|
| 172 | - } |
|
| 173 | - /** |
|
| 174 | - * |
|
| 175 | - * @var \Cake\ORM\Query |
|
| 176 | - */ |
|
| 177 | - return [ |
|
| 178 | - 'task', |
|
| 179 | - 'num' => $query->func()->count('*'), |
|
| 180 | - 'alltime' => $alltime, |
|
| 181 | - 'runtime' => $runtime, |
|
| 182 | - 'fetchdelay' => $fetchdelay |
|
| 183 | - ]; |
|
| 184 | - }, |
|
| 185 | - 'conditions' => [ |
|
| 186 | - 'completed IS NOT' => null |
|
| 187 | - ], |
|
| 188 | - 'group' => [ |
|
| 189 | - 'task' |
|
| 190 | - ] |
|
| 191 | - ]; |
|
| 192 | - |
|
| 193 | - return $this->find('all', $options); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Returns [ |
|
| 198 | - * 'Task' => [ |
|
| 199 | - * 'YYYY-MM-DD' => INT, |
|
| 200 | - * ... |
|
| 201 | - * ] |
|
| 202 | - * ] |
|
| 203 | - * |
|
| 204 | - * @param string|null $taskName The task name |
|
| 205 | - * @return array |
|
| 206 | - */ |
|
| 207 | - public function getFullStats($taskName = null) |
|
| 208 | - { |
|
| 209 | - $driverName = $this->_getDriverName(); |
|
| 210 | - $fields = function (Query $query) use ($driverName) { |
|
| 211 | - $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
| 212 | - switch ($driverName) { |
|
| 213 | - case static::DRIVER_SQLSERVER: |
|
| 214 | - $runtime = $query->newExpr("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
| 215 | - break; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return [ |
|
| 219 | - 'task', |
|
| 220 | - 'created', |
|
| 221 | - 'duration' => $runtime |
|
| 222 | - ]; |
|
| 223 | - }; |
|
| 224 | - |
|
| 225 | - $conditions = [ |
|
| 226 | - 'completed IS NOT' => null |
|
| 227 | - ]; |
|
| 228 | - if ($taskName) { |
|
| 229 | - $conditions['task'] = $taskName; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - $tasks = $this->find() |
|
| 233 | - ->select($fields) |
|
| 234 | - ->where($conditions) |
|
| 235 | - ->enableHydration(false) |
|
| 236 | - ->orderDesc('id') |
|
| 237 | - ->limit(static::STATS_LIMIT) |
|
| 238 | - ->all() |
|
| 239 | - ->toArray(); |
|
| 240 | - |
|
| 241 | - $result = []; |
|
| 242 | - |
|
| 243 | - $days = []; |
|
| 244 | - |
|
| 245 | - foreach ($tasks as $task) { |
|
| 246 | - /** @var \DateTime $created */ |
|
| 247 | - $created = $task['created']; |
|
| 248 | - $day = $created->format('Y-m-d'); |
|
| 249 | - if (!isset($days[$day])) { |
|
| 250 | - $days[$day] = $day; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $result[$task['task']][$day][] = $task['duration']; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - foreach ($result as $type => $tasks) { |
|
| 257 | - foreach ($tasks as $day => $durations) { |
|
| 258 | - $average = array_sum($durations) / count($durations); |
|
| 259 | - $result[$type][$day] = (int)$average; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - foreach ($days as $day) { |
|
| 263 | - if (isset($result[$type][$day])) { |
|
| 264 | - continue; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $result[$type][$day] = 0; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - ksort($result[$type]); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return $result; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Look for a new job that can be processed with the current abilities and |
|
| 278 | - * from the specified group (or any if null). |
|
| 279 | - * |
|
| 280 | - * @param array $capabilities Available QueueWorkerTasks. |
|
| 281 | - * @param array $types Request a job from these types (or exclude certain types), or any otherwise. |
|
| 282 | - * @return \Queue\Model\Entity\QueuedTask|null |
|
| 283 | - */ |
|
| 284 | - public function requestJob(array $capabilities, array $types = []) |
|
| 285 | - { |
|
| 286 | - $now = $this->getDateTime(); |
|
| 287 | - $nowStr = $now->toDateTimeString(); |
|
| 288 | - $driverName = $this->_getDriverName(); |
|
| 289 | - |
|
| 290 | - $query = $this->find(); |
|
| 291 | - $age = $query->newExpr()->add('IFNULL(TIMESTAMPDIFF(SECOND, "' . $nowStr . '", not_before), 0)'); |
|
| 292 | - switch ($driverName) { |
|
| 293 | - case static::DRIVER_SQLSERVER: |
|
| 294 | - $age = $query->newExpr()->add('ISNULL(DATEDIFF(SECOND, GETDATE(), not_before), 0)'); |
|
| 295 | - break; |
|
| 296 | - case static::DRIVER_POSTGRES: |
|
| 297 | - $age = $query->newExpr()->add('COALESCE((EXTRACT(EPOCH FROM now()) - EXTRACT(EPOCH FROM not_before)), 0)'); |
|
| 298 | - break; |
|
| 299 | - } |
|
| 300 | - $options = [ |
|
| 301 | - 'conditions' => [ |
|
| 302 | - 'completed IS' => null, |
|
| 303 | - 'OR' => [] |
|
| 304 | - ], |
|
| 305 | - 'fields' => [ |
|
| 306 | - 'age' => $age |
|
| 307 | - ], |
|
| 308 | - 'order' => [ |
|
| 309 | - 'age' => 'ASC', |
|
| 310 | - 'id' => 'ASC' |
|
| 311 | - ] |
|
| 312 | - ]; |
|
| 313 | - |
|
| 314 | - if ($types) { |
|
| 315 | - $options['conditions'] = $this->addFilter($options['conditions'], 'task', $types); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - // Generate the task specific conditions. |
|
| 319 | - foreach ($capabilities as $task) { |
|
| 320 | - list ($plugin, $name) = pluginSplit($task['name']); |
|
| 321 | - $timeoutAt = $now->copy(); |
|
| 322 | - $tmp = [ |
|
| 323 | - 'task' => $name, |
|
| 324 | - 'AND' => [ |
|
| 325 | - [ |
|
| 326 | - 'OR' => [ |
|
| 327 | - 'not_before <=' => $nowStr, |
|
| 328 | - 'not_before IS' => null |
|
| 329 | - ] |
|
| 330 | - ], |
|
| 331 | - [ |
|
| 332 | - 'OR' => [ |
|
| 333 | - 'fetched <' => $timeoutAt->subSeconds($task['timeout']), |
|
| 334 | - 'fetched IS' => null |
|
| 335 | - ] |
|
| 336 | - ] |
|
| 337 | - ], |
|
| 338 | - 'failed_count <' => ($task['retries'] + 1) |
|
| 339 | - ]; |
|
| 340 | - $options['conditions']['OR'][] = $tmp; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
|
| 344 | - $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
| 345 | - $task = $query->find('all', $options) |
|
| 346 | - ->enableAutoFields(true) |
|
| 347 | - ->epilog('FOR UPDATE') |
|
| 348 | - ->first(); |
|
| 349 | - |
|
| 350 | - if (!$task) { |
|
| 351 | - return null; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $key = sha1(microtime()); |
|
| 355 | - $task = $this->patchEntity($task, [ |
|
| 356 | - 'worker_key' => $key, |
|
| 357 | - 'fetched' => $now |
|
| 358 | - ]); |
|
| 359 | - |
|
| 360 | - return $this->saveOrFail($task); |
|
| 361 | - }); |
|
| 362 | - |
|
| 363 | - if (!$task) { |
|
| 364 | - return null; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - return $task; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Mark a task as Completed, removing it from the queue. |
|
| 372 | - * |
|
| 373 | - * @param \Queue\Model\Entity\QueuedTask $task Task |
|
| 374 | - * @return bool Success |
|
| 375 | - */ |
|
| 376 | - public function markJobDone(QueuedTask $task) |
|
| 377 | - { |
|
| 378 | - $fields = [ |
|
| 379 | - 'completed' => $this->getDateTime() |
|
| 380 | - ]; |
|
| 381 | - $task = $this->patchEntity($task, $fields); |
|
| 382 | - |
|
| 383 | - return (bool)$this->save($task); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Mark a job as Failed, incrementing the failed-counter and Requeueing it. |
|
| 388 | - * |
|
| 389 | - * @param \Queue\Model\Entity\QueuedTask $task Task |
|
| 390 | - * @param string|null $failureMessage Optional message to append to the failure_message field. |
|
| 391 | - * @return bool Success |
|
| 392 | - */ |
|
| 393 | - public function markJobFailed(QueuedTask $task, $failureMessage = null) |
|
| 394 | - { |
|
| 395 | - $fields = [ |
|
| 396 | - 'failed_count' => $task->failed_count + 1, |
|
| 397 | - 'failure_message' => $failureMessage |
|
| 398 | - ]; |
|
| 399 | - $task = $this->patchEntity($task, $fields); |
|
| 400 | - |
|
| 401 | - return (bool)$this->save($task); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Reset current jobs |
|
| 406 | - * |
|
| 407 | - * @param int|null $id ID |
|
| 408 | - * |
|
| 409 | - * @return int Success |
|
| 410 | - */ |
|
| 411 | - public function reset($id = null) |
|
| 412 | - { |
|
| 413 | - $fields = [ |
|
| 414 | - 'completed' => null, |
|
| 415 | - 'fetched' => null, |
|
| 416 | - 'failed_count' => 0, |
|
| 417 | - 'worker_key' => null, |
|
| 418 | - 'failure_message' => null |
|
| 419 | - ]; |
|
| 420 | - $conditions = [ |
|
| 421 | - 'completed IS' => null |
|
| 422 | - ]; |
|
| 423 | - if ($id) { |
|
| 424 | - $conditions['id'] = $id; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $this->updateAll($fields, $conditions); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * |
|
| 432 | - * @param string $taskName Task name |
|
| 433 | - * |
|
| 434 | - * @return int |
|
| 435 | - */ |
|
| 436 | - public function rerun($taskName) |
|
| 437 | - { |
|
| 438 | - $fields = [ |
|
| 439 | - 'completed' => null, |
|
| 440 | - 'fetched' => null, |
|
| 441 | - 'failed_count' => 0, |
|
| 442 | - 'worker_key' => null, |
|
| 443 | - 'failure_message' => null |
|
| 444 | - ]; |
|
| 445 | - $conditions = [ |
|
| 446 | - 'completed IS NOT' => null, |
|
| 447 | - 'task' => $taskName |
|
| 448 | - ]; |
|
| 449 | - |
|
| 450 | - return $this->updateAll($fields, $conditions); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Cleanup/Delete Completed Tasks. |
|
| 455 | - * |
|
| 456 | - * @return void |
|
| 457 | - */ |
|
| 458 | - public function cleanOldJobs() |
|
| 459 | - { |
|
| 460 | - if (!Configure::read('Queue.cleanuptimeout')) { |
|
| 461 | - return; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - $this->deleteAll([ |
|
| 465 | - 'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout') |
|
| 466 | - ]); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * |
|
| 471 | - * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
| 472 | - * @param array $taskConfiguration Task configuration |
|
| 473 | - * @return string |
|
| 474 | - */ |
|
| 475 | - public function getFailedStatus($queuedTask, array $taskConfiguration) |
|
| 476 | - { |
|
| 477 | - $failureMessageRequeued = 'requeued'; |
|
| 478 | - |
|
| 479 | - $queuedTaskName = 'Queue' . $queuedTask->task; |
|
| 480 | - if (empty($taskConfiguration[$queuedTaskName])) { |
|
| 481 | - return $failureMessageRequeued; |
|
| 482 | - } |
|
| 483 | - $retries = $taskConfiguration[$queuedTaskName]['retries']; |
|
| 484 | - if ($queuedTask->failed_count <= $retries) { |
|
| 485 | - return $failureMessageRequeued; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - return 'aborted'; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * truncate() |
|
| 493 | - * |
|
| 494 | - * @return void |
|
| 495 | - */ |
|
| 496 | - public function truncate() |
|
| 497 | - { |
|
| 498 | - $sql = $this->getSchema()->truncateSql($this->_connection); |
|
| 499 | - foreach ($sql as $snippet) { |
|
| 500 | - $this->_connection->execute($snippet); |
|
| 501 | - } |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * get the name of the driver |
|
| 506 | - * |
|
| 507 | - * @return string |
|
| 508 | - */ |
|
| 509 | - protected function _getDriverName() |
|
| 510 | - { |
|
| 511 | - $className = explode('\\', $this->getConnection()->config()['driver']); |
|
| 512 | - $name = end($className); |
|
| 513 | - |
|
| 514 | - return $name; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * |
|
| 519 | - * @param array $conditions Conditions |
|
| 520 | - * @param string $key Key |
|
| 521 | - * @param array $values Values |
|
| 522 | - * @return array |
|
| 523 | - */ |
|
| 524 | - protected function addFilter(array $conditions, $key, array $values) |
|
| 525 | - { |
|
| 526 | - $include = []; |
|
| 527 | - $exclude = []; |
|
| 528 | - foreach ($values as $value) { |
|
| 529 | - if (substr($value, 0, 1) === '-') { |
|
| 530 | - $exclude[] = substr($value, 1); |
|
| 531 | - } else { |
|
| 532 | - $include[] = $value; |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - if ($include) { |
|
| 537 | - $conditions[$key . ' IN'] = $include; |
|
| 538 | - } |
|
| 539 | - if ($exclude) { |
|
| 540 | - $conditions[$key . ' NOT IN'] = $exclude; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - return $conditions; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Returns a DateTime object from different input. |
|
| 548 | - * |
|
| 549 | - * Without argument this will be "now". |
|
| 550 | - * |
|
| 551 | - * @param int|string|\Cake\I18n\FrozenTime|\Cake\I18n\Time|null $notBefore Not before time |
|
| 552 | - * |
|
| 553 | - * @return \Cake\I18n\FrozenTime|\Cake\I18n\Time |
|
| 554 | - */ |
|
| 555 | - protected function getDateTime($notBefore = null) |
|
| 556 | - { |
|
| 557 | - if (is_object($notBefore)) { |
|
| 558 | - return $notBefore; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - return new FrozenTime($notBefore); |
|
| 562 | - } |
|
| 31 | + const DRIVER_MYSQL = 'Mysql'; |
|
| 32 | + |
|
| 33 | + const DRIVER_POSTGRES = 'Postgres'; |
|
| 34 | + |
|
| 35 | + const DRIVER_SQLSERVER = 'Sqlserver'; |
|
| 36 | + |
|
| 37 | + const STATS_LIMIT = 100000; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Initialize method |
|
| 41 | + * |
|
| 42 | + * @param array $config The configuration for the Table. |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function initialize(array $config) |
|
| 46 | + { |
|
| 47 | + parent::initialize($config); |
|
| 48 | + |
|
| 49 | + $this->setTable('queued_tasks'); |
|
| 50 | + $this->setDisplayField('id'); |
|
| 51 | + $this->setPrimaryKey('id'); |
|
| 52 | + |
|
| 53 | + $this->addBehavior('Timestamp'); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * set connection name |
|
| 58 | + * |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + public static function defaultConnectionName() |
|
| 62 | + { |
|
| 63 | + $connection = Configure::read('Queue.connection'); |
|
| 64 | + if (!empty($connection)) { |
|
| 65 | + return $connection; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return parent::defaultConnectionName(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * |
|
| 73 | + * @param \Cake\Event\Event $event Model event |
|
| 74 | + * @param \ArrayObject $data The data |
|
| 75 | + * @param \ArrayObject $options The options |
|
| 76 | + * @return void |
|
| 77 | + */ |
|
| 78 | + public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
|
| 79 | + { |
|
| 80 | + if (isset($data['data']) && $data['data'] === '') { |
|
| 81 | + $data['data'] = null; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Adds a new job to the queue. |
|
| 87 | + * |
|
| 88 | + * @param string $taskName Task name |
|
| 89 | + * @param array|null $data Array of data |
|
| 90 | + * @param string $notBefore A datetime which indicates when the job may be executed |
|
| 91 | + * @return \Queue\Model\Entity\QueuedTask Saved job entity |
|
| 92 | + */ |
|
| 93 | + public function createJob($taskName, array $data = null, string $notBefore = null) |
|
| 94 | + { |
|
| 95 | + $task = [ |
|
| 96 | + 'task' => $taskName, |
|
| 97 | + 'data' => serialize($data), |
|
| 98 | + 'not_before' => $this->getDateTime() |
|
| 99 | + ]; |
|
| 100 | + |
|
| 101 | + if (!empty($notBefore)) { |
|
| 102 | + $task['not_before'] = $this->getDateTime(strtotime($notBefore)); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $queuedTask = $this->newEntity($task); |
|
| 106 | + |
|
| 107 | + return $this->saveOrFail($queuedTask); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Returns the number of items in the queue. |
|
| 112 | + * Either returns the number of ALL pending jobs, or the number of pending jobs of the passed type. |
|
| 113 | + * |
|
| 114 | + * @param string|null $taskName Task type to Count |
|
| 115 | + * @return int |
|
| 116 | + */ |
|
| 117 | + public function getLength($taskName = null) |
|
| 118 | + { |
|
| 119 | + $findConf = [ |
|
| 120 | + 'conditions' => [ |
|
| 121 | + 'completed IS' => null |
|
| 122 | + ] |
|
| 123 | + ]; |
|
| 124 | + if ($taskName !== null) { |
|
| 125 | + $findConf['conditions']['task'] = $taskName; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + return $this->find('all', $findConf)->count(); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Return a list of all task types in the Queue. |
|
| 133 | + * |
|
| 134 | + * @return \Cake\ORM\Query |
|
| 135 | + */ |
|
| 136 | + public function getTypes() |
|
| 137 | + { |
|
| 138 | + $findCond = [ |
|
| 139 | + 'fields' => [ |
|
| 140 | + 'task' |
|
| 141 | + ], |
|
| 142 | + 'group' => [ |
|
| 143 | + 'task' |
|
| 144 | + ], |
|
| 145 | + 'keyField' => 'task', |
|
| 146 | + 'valueField' => 'task' |
|
| 147 | + ]; |
|
| 148 | + |
|
| 149 | + return $this->find('list', $findCond); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Return some statistics about finished jobs still in the Database. |
|
| 154 | + * TO-DO: rewrite as virtual field |
|
| 155 | + * |
|
| 156 | + * @return \Cake\ORM\Query |
|
| 157 | + */ |
|
| 158 | + public function getStats() |
|
| 159 | + { |
|
| 160 | + $driverName = $this->_getDriverName(); |
|
| 161 | + $options = [ |
|
| 162 | + 'fields' => function (Query $query) use ($driverName) { |
|
| 163 | + $alltime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(created)'); |
|
| 164 | + $runtime = $query->func()->avg('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
| 165 | + $fetchdelay = $query->func()->avg('UNIX_TIMESTAMP(fetched) - IF(not_before is NULL, UNIX_TIMESTAMP(created), UNIX_TIMESTAMP(not_before))'); |
|
| 166 | + switch ($driverName) { |
|
| 167 | + case static::DRIVER_SQLSERVER: |
|
| 168 | + $alltime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', created)"); |
|
| 169 | + $runtime = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
| 170 | + $fetchdelay = $query->func()->avg("DATEDIFF(s, '1970-01-01 00:00:00', fetched) - (CASE WHEN not_before IS NULL THEN DATEDIFF(s, '1970-01-01 00:00:00', created) ELSE DATEDIFF(s, '1970-01-01 00:00:00', not_before) END)"); |
|
| 171 | + break; |
|
| 172 | + } |
|
| 173 | + /** |
|
| 174 | + * |
|
| 175 | + * @var \Cake\ORM\Query |
|
| 176 | + */ |
|
| 177 | + return [ |
|
| 178 | + 'task', |
|
| 179 | + 'num' => $query->func()->count('*'), |
|
| 180 | + 'alltime' => $alltime, |
|
| 181 | + 'runtime' => $runtime, |
|
| 182 | + 'fetchdelay' => $fetchdelay |
|
| 183 | + ]; |
|
| 184 | + }, |
|
| 185 | + 'conditions' => [ |
|
| 186 | + 'completed IS NOT' => null |
|
| 187 | + ], |
|
| 188 | + 'group' => [ |
|
| 189 | + 'task' |
|
| 190 | + ] |
|
| 191 | + ]; |
|
| 192 | + |
|
| 193 | + return $this->find('all', $options); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Returns [ |
|
| 198 | + * 'Task' => [ |
|
| 199 | + * 'YYYY-MM-DD' => INT, |
|
| 200 | + * ... |
|
| 201 | + * ] |
|
| 202 | + * ] |
|
| 203 | + * |
|
| 204 | + * @param string|null $taskName The task name |
|
| 205 | + * @return array |
|
| 206 | + */ |
|
| 207 | + public function getFullStats($taskName = null) |
|
| 208 | + { |
|
| 209 | + $driverName = $this->_getDriverName(); |
|
| 210 | + $fields = function (Query $query) use ($driverName) { |
|
| 211 | + $runtime = $query->newExpr('UNIX_TIMESTAMP(completed) - UNIX_TIMESTAMP(fetched)'); |
|
| 212 | + switch ($driverName) { |
|
| 213 | + case static::DRIVER_SQLSERVER: |
|
| 214 | + $runtime = $query->newExpr("DATEDIFF(s, '1970-01-01 00:00:00', completed) - DATEDIFF(s, '1970-01-01 00:00:00', fetched)"); |
|
| 215 | + break; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return [ |
|
| 219 | + 'task', |
|
| 220 | + 'created', |
|
| 221 | + 'duration' => $runtime |
|
| 222 | + ]; |
|
| 223 | + }; |
|
| 224 | + |
|
| 225 | + $conditions = [ |
|
| 226 | + 'completed IS NOT' => null |
|
| 227 | + ]; |
|
| 228 | + if ($taskName) { |
|
| 229 | + $conditions['task'] = $taskName; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + $tasks = $this->find() |
|
| 233 | + ->select($fields) |
|
| 234 | + ->where($conditions) |
|
| 235 | + ->enableHydration(false) |
|
| 236 | + ->orderDesc('id') |
|
| 237 | + ->limit(static::STATS_LIMIT) |
|
| 238 | + ->all() |
|
| 239 | + ->toArray(); |
|
| 240 | + |
|
| 241 | + $result = []; |
|
| 242 | + |
|
| 243 | + $days = []; |
|
| 244 | + |
|
| 245 | + foreach ($tasks as $task) { |
|
| 246 | + /** @var \DateTime $created */ |
|
| 247 | + $created = $task['created']; |
|
| 248 | + $day = $created->format('Y-m-d'); |
|
| 249 | + if (!isset($days[$day])) { |
|
| 250 | + $days[$day] = $day; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $result[$task['task']][$day][] = $task['duration']; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + foreach ($result as $type => $tasks) { |
|
| 257 | + foreach ($tasks as $day => $durations) { |
|
| 258 | + $average = array_sum($durations) / count($durations); |
|
| 259 | + $result[$type][$day] = (int)$average; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + foreach ($days as $day) { |
|
| 263 | + if (isset($result[$type][$day])) { |
|
| 264 | + continue; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $result[$type][$day] = 0; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + ksort($result[$type]); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return $result; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Look for a new job that can be processed with the current abilities and |
|
| 278 | + * from the specified group (or any if null). |
|
| 279 | + * |
|
| 280 | + * @param array $capabilities Available QueueWorkerTasks. |
|
| 281 | + * @param array $types Request a job from these types (or exclude certain types), or any otherwise. |
|
| 282 | + * @return \Queue\Model\Entity\QueuedTask|null |
|
| 283 | + */ |
|
| 284 | + public function requestJob(array $capabilities, array $types = []) |
|
| 285 | + { |
|
| 286 | + $now = $this->getDateTime(); |
|
| 287 | + $nowStr = $now->toDateTimeString(); |
|
| 288 | + $driverName = $this->_getDriverName(); |
|
| 289 | + |
|
| 290 | + $query = $this->find(); |
|
| 291 | + $age = $query->newExpr()->add('IFNULL(TIMESTAMPDIFF(SECOND, "' . $nowStr . '", not_before), 0)'); |
|
| 292 | + switch ($driverName) { |
|
| 293 | + case static::DRIVER_SQLSERVER: |
|
| 294 | + $age = $query->newExpr()->add('ISNULL(DATEDIFF(SECOND, GETDATE(), not_before), 0)'); |
|
| 295 | + break; |
|
| 296 | + case static::DRIVER_POSTGRES: |
|
| 297 | + $age = $query->newExpr()->add('COALESCE((EXTRACT(EPOCH FROM now()) - EXTRACT(EPOCH FROM not_before)), 0)'); |
|
| 298 | + break; |
|
| 299 | + } |
|
| 300 | + $options = [ |
|
| 301 | + 'conditions' => [ |
|
| 302 | + 'completed IS' => null, |
|
| 303 | + 'OR' => [] |
|
| 304 | + ], |
|
| 305 | + 'fields' => [ |
|
| 306 | + 'age' => $age |
|
| 307 | + ], |
|
| 308 | + 'order' => [ |
|
| 309 | + 'age' => 'ASC', |
|
| 310 | + 'id' => 'ASC' |
|
| 311 | + ] |
|
| 312 | + ]; |
|
| 313 | + |
|
| 314 | + if ($types) { |
|
| 315 | + $options['conditions'] = $this->addFilter($options['conditions'], 'task', $types); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + // Generate the task specific conditions. |
|
| 319 | + foreach ($capabilities as $task) { |
|
| 320 | + list ($plugin, $name) = pluginSplit($task['name']); |
|
| 321 | + $timeoutAt = $now->copy(); |
|
| 322 | + $tmp = [ |
|
| 323 | + 'task' => $name, |
|
| 324 | + 'AND' => [ |
|
| 325 | + [ |
|
| 326 | + 'OR' => [ |
|
| 327 | + 'not_before <=' => $nowStr, |
|
| 328 | + 'not_before IS' => null |
|
| 329 | + ] |
|
| 330 | + ], |
|
| 331 | + [ |
|
| 332 | + 'OR' => [ |
|
| 333 | + 'fetched <' => $timeoutAt->subSeconds($task['timeout']), |
|
| 334 | + 'fetched IS' => null |
|
| 335 | + ] |
|
| 336 | + ] |
|
| 337 | + ], |
|
| 338 | + 'failed_count <' => ($task['retries'] + 1) |
|
| 339 | + ]; |
|
| 340 | + $options['conditions']['OR'][] = $tmp; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** @var \Queue\Model\Entity\QueuedTask|null $task */ |
|
| 344 | + $task = $this->getConnection()->transactional(function () use ($query, $options, $now) { |
|
| 345 | + $task = $query->find('all', $options) |
|
| 346 | + ->enableAutoFields(true) |
|
| 347 | + ->epilog('FOR UPDATE') |
|
| 348 | + ->first(); |
|
| 349 | + |
|
| 350 | + if (!$task) { |
|
| 351 | + return null; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $key = sha1(microtime()); |
|
| 355 | + $task = $this->patchEntity($task, [ |
|
| 356 | + 'worker_key' => $key, |
|
| 357 | + 'fetched' => $now |
|
| 358 | + ]); |
|
| 359 | + |
|
| 360 | + return $this->saveOrFail($task); |
|
| 361 | + }); |
|
| 362 | + |
|
| 363 | + if (!$task) { |
|
| 364 | + return null; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + return $task; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Mark a task as Completed, removing it from the queue. |
|
| 372 | + * |
|
| 373 | + * @param \Queue\Model\Entity\QueuedTask $task Task |
|
| 374 | + * @return bool Success |
|
| 375 | + */ |
|
| 376 | + public function markJobDone(QueuedTask $task) |
|
| 377 | + { |
|
| 378 | + $fields = [ |
|
| 379 | + 'completed' => $this->getDateTime() |
|
| 380 | + ]; |
|
| 381 | + $task = $this->patchEntity($task, $fields); |
|
| 382 | + |
|
| 383 | + return (bool)$this->save($task); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Mark a job as Failed, incrementing the failed-counter and Requeueing it. |
|
| 388 | + * |
|
| 389 | + * @param \Queue\Model\Entity\QueuedTask $task Task |
|
| 390 | + * @param string|null $failureMessage Optional message to append to the failure_message field. |
|
| 391 | + * @return bool Success |
|
| 392 | + */ |
|
| 393 | + public function markJobFailed(QueuedTask $task, $failureMessage = null) |
|
| 394 | + { |
|
| 395 | + $fields = [ |
|
| 396 | + 'failed_count' => $task->failed_count + 1, |
|
| 397 | + 'failure_message' => $failureMessage |
|
| 398 | + ]; |
|
| 399 | + $task = $this->patchEntity($task, $fields); |
|
| 400 | + |
|
| 401 | + return (bool)$this->save($task); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Reset current jobs |
|
| 406 | + * |
|
| 407 | + * @param int|null $id ID |
|
| 408 | + * |
|
| 409 | + * @return int Success |
|
| 410 | + */ |
|
| 411 | + public function reset($id = null) |
|
| 412 | + { |
|
| 413 | + $fields = [ |
|
| 414 | + 'completed' => null, |
|
| 415 | + 'fetched' => null, |
|
| 416 | + 'failed_count' => 0, |
|
| 417 | + 'worker_key' => null, |
|
| 418 | + 'failure_message' => null |
|
| 419 | + ]; |
|
| 420 | + $conditions = [ |
|
| 421 | + 'completed IS' => null |
|
| 422 | + ]; |
|
| 423 | + if ($id) { |
|
| 424 | + $conditions['id'] = $id; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $this->updateAll($fields, $conditions); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * |
|
| 432 | + * @param string $taskName Task name |
|
| 433 | + * |
|
| 434 | + * @return int |
|
| 435 | + */ |
|
| 436 | + public function rerun($taskName) |
|
| 437 | + { |
|
| 438 | + $fields = [ |
|
| 439 | + 'completed' => null, |
|
| 440 | + 'fetched' => null, |
|
| 441 | + 'failed_count' => 0, |
|
| 442 | + 'worker_key' => null, |
|
| 443 | + 'failure_message' => null |
|
| 444 | + ]; |
|
| 445 | + $conditions = [ |
|
| 446 | + 'completed IS NOT' => null, |
|
| 447 | + 'task' => $taskName |
|
| 448 | + ]; |
|
| 449 | + |
|
| 450 | + return $this->updateAll($fields, $conditions); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Cleanup/Delete Completed Tasks. |
|
| 455 | + * |
|
| 456 | + * @return void |
|
| 457 | + */ |
|
| 458 | + public function cleanOldJobs() |
|
| 459 | + { |
|
| 460 | + if (!Configure::read('Queue.cleanuptimeout')) { |
|
| 461 | + return; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + $this->deleteAll([ |
|
| 465 | + 'completed <' => time() - (int)Configure::read('Queue.cleanuptimeout') |
|
| 466 | + ]); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * |
|
| 471 | + * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
| 472 | + * @param array $taskConfiguration Task configuration |
|
| 473 | + * @return string |
|
| 474 | + */ |
|
| 475 | + public function getFailedStatus($queuedTask, array $taskConfiguration) |
|
| 476 | + { |
|
| 477 | + $failureMessageRequeued = 'requeued'; |
|
| 478 | + |
|
| 479 | + $queuedTaskName = 'Queue' . $queuedTask->task; |
|
| 480 | + if (empty($taskConfiguration[$queuedTaskName])) { |
|
| 481 | + return $failureMessageRequeued; |
|
| 482 | + } |
|
| 483 | + $retries = $taskConfiguration[$queuedTaskName]['retries']; |
|
| 484 | + if ($queuedTask->failed_count <= $retries) { |
|
| 485 | + return $failureMessageRequeued; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + return 'aborted'; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * truncate() |
|
| 493 | + * |
|
| 494 | + * @return void |
|
| 495 | + */ |
|
| 496 | + public function truncate() |
|
| 497 | + { |
|
| 498 | + $sql = $this->getSchema()->truncateSql($this->_connection); |
|
| 499 | + foreach ($sql as $snippet) { |
|
| 500 | + $this->_connection->execute($snippet); |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * get the name of the driver |
|
| 506 | + * |
|
| 507 | + * @return string |
|
| 508 | + */ |
|
| 509 | + protected function _getDriverName() |
|
| 510 | + { |
|
| 511 | + $className = explode('\\', $this->getConnection()->config()['driver']); |
|
| 512 | + $name = end($className); |
|
| 513 | + |
|
| 514 | + return $name; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * |
|
| 519 | + * @param array $conditions Conditions |
|
| 520 | + * @param string $key Key |
|
| 521 | + * @param array $values Values |
|
| 522 | + * @return array |
|
| 523 | + */ |
|
| 524 | + protected function addFilter(array $conditions, $key, array $values) |
|
| 525 | + { |
|
| 526 | + $include = []; |
|
| 527 | + $exclude = []; |
|
| 528 | + foreach ($values as $value) { |
|
| 529 | + if (substr($value, 0, 1) === '-') { |
|
| 530 | + $exclude[] = substr($value, 1); |
|
| 531 | + } else { |
|
| 532 | + $include[] = $value; |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + if ($include) { |
|
| 537 | + $conditions[$key . ' IN'] = $include; |
|
| 538 | + } |
|
| 539 | + if ($exclude) { |
|
| 540 | + $conditions[$key . ' NOT IN'] = $exclude; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + return $conditions; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Returns a DateTime object from different input. |
|
| 548 | + * |
|
| 549 | + * Without argument this will be "now". |
|
| 550 | + * |
|
| 551 | + * @param int|string|\Cake\I18n\FrozenTime|\Cake\I18n\Time|null $notBefore Not before time |
|
| 552 | + * |
|
| 553 | + * @return \Cake\I18n\FrozenTime|\Cake\I18n\Time |
|
| 554 | + */ |
|
| 555 | + protected function getDateTime($notBefore = null) |
|
| 556 | + { |
|
| 557 | + if (is_object($notBefore)) { |
|
| 558 | + return $notBefore; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + return new FrozenTime($notBefore); |
|
| 562 | + } |
|
| 563 | 563 | } |
@@ -6,57 +6,57 @@ |
||
| 6 | 6 | class Config |
| 7 | 7 | { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * |
|
| 11 | - * @return int |
|
| 12 | - */ |
|
| 13 | - public static function defaultWorkerTimeout() |
|
| 14 | - { |
|
| 15 | - return Configure::read('Queue.defaultWorkerTimeout', 600); // 10min |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * |
|
| 20 | - * @return int |
|
| 21 | - */ |
|
| 22 | - public static function workerMaxRuntime() |
|
| 23 | - { |
|
| 24 | - return Configure::read('Queue.workerMaxRuntime', 120); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - * @return int |
|
| 30 | - */ |
|
| 31 | - public static function cleanupTimeout() |
|
| 32 | - { |
|
| 33 | - return Configure::read('Queue.cleanupTimeout', 2592000); // 30 days |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * @return int |
|
| 39 | - */ |
|
| 40 | - public static function sleepTime() |
|
| 41 | - { |
|
| 42 | - return Configure::read('Queue.sleepTime', 10); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * |
|
| 47 | - * @return int |
|
| 48 | - */ |
|
| 49 | - public static function gcprob() |
|
| 50 | - { |
|
| 51 | - return Configure::read('Queue.gcprob', 10); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public static function defaultWorkerRetries() |
|
| 59 | - { |
|
| 60 | - return Configure::read('Queue.defaultWorkerRetries', 1); |
|
| 61 | - } |
|
| 9 | + /** |
|
| 10 | + * |
|
| 11 | + * @return int |
|
| 12 | + */ |
|
| 13 | + public static function defaultWorkerTimeout() |
|
| 14 | + { |
|
| 15 | + return Configure::read('Queue.defaultWorkerTimeout', 600); // 10min |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * |
|
| 20 | + * @return int |
|
| 21 | + */ |
|
| 22 | + public static function workerMaxRuntime() |
|
| 23 | + { |
|
| 24 | + return Configure::read('Queue.workerMaxRuntime', 120); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * @return int |
|
| 30 | + */ |
|
| 31 | + public static function cleanupTimeout() |
|
| 32 | + { |
|
| 33 | + return Configure::read('Queue.cleanupTimeout', 2592000); // 30 days |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + * @return int |
|
| 39 | + */ |
|
| 40 | + public static function sleepTime() |
|
| 41 | + { |
|
| 42 | + return Configure::read('Queue.sleepTime', 10); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * |
|
| 47 | + * @return int |
|
| 48 | + */ |
|
| 49 | + public static function gcprob() |
|
| 50 | + { |
|
| 51 | + return Configure::read('Queue.gcprob', 10); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public static function defaultWorkerRetries() |
|
| 59 | + { |
|
| 60 | + return Configure::read('Queue.defaultWorkerRetries', 1); |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -15,115 +15,115 @@ |
||
| 15 | 15 | class SimpleQueueTransport extends AbstractTransport |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Send mail |
|
| 20 | - * |
|
| 21 | - * @param \Cake\Mailer\Email $email Email |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function send(Email $email) |
|
| 25 | - { |
|
| 26 | - if (!empty($this->_config['queue'])) { |
|
| 27 | - $this->_config = $this->_config['queue'] + $this->_config; |
|
| 28 | - $email->setConfig((array)$this->_config['queue'] + [ |
|
| 29 | - 'queue' => [] |
|
| 30 | - ]); |
|
| 31 | - unset($this->_config['queue']); |
|
| 32 | - } |
|
| 18 | + /** |
|
| 19 | + * Send mail |
|
| 20 | + * |
|
| 21 | + * @param \Cake\Mailer\Email $email Email |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function send(Email $email) |
|
| 25 | + { |
|
| 26 | + if (!empty($this->_config['queue'])) { |
|
| 27 | + $this->_config = $this->_config['queue'] + $this->_config; |
|
| 28 | + $email->setConfig((array)$this->_config['queue'] + [ |
|
| 29 | + 'queue' => [] |
|
| 30 | + ]); |
|
| 31 | + unset($this->_config['queue']); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - $settings = [ |
|
| 35 | - 'from' => [ |
|
| 36 | - $email->getFrom() |
|
| 37 | - ], |
|
| 38 | - 'to' => [ |
|
| 39 | - $email->getTo() |
|
| 40 | - ], |
|
| 41 | - 'cc' => [ |
|
| 42 | - $email->getCc() |
|
| 43 | - ], |
|
| 44 | - 'bcc' => [ |
|
| 45 | - $email->getBcc() |
|
| 46 | - ], |
|
| 47 | - 'charset' => [ |
|
| 48 | - $email->getCharset() |
|
| 49 | - ], |
|
| 50 | - 'replyTo' => [ |
|
| 51 | - $email->getReplyTo() |
|
| 52 | - ], |
|
| 53 | - 'readReceipt' => [ |
|
| 54 | - $email->getReadReceipt() |
|
| 55 | - ], |
|
| 56 | - 'returnPath' => [ |
|
| 57 | - $email->getReturnPath() |
|
| 58 | - ], |
|
| 59 | - 'messageId' => [ |
|
| 60 | - $email->getMessageId() |
|
| 61 | - ], |
|
| 62 | - 'domain' => [ |
|
| 63 | - $email->getDomain() |
|
| 64 | - ], |
|
| 65 | - 'headers' => [ |
|
| 66 | - $email->getHeaders() |
|
| 67 | - ], |
|
| 68 | - 'headerCharset' => [ |
|
| 69 | - $email->getHeaderCharset() |
|
| 70 | - ], |
|
| 71 | - 'theme' => [ |
|
| 72 | - $email->viewBuilder()->getTheme() |
|
| 73 | - ], |
|
| 74 | - 'profile' => [ |
|
| 75 | - $email->getProfile() |
|
| 76 | - ], |
|
| 77 | - 'emailFormat' => [ |
|
| 78 | - $email->getEmailFormat() |
|
| 79 | - ], |
|
| 80 | - 'subject' => method_exists($email, 'getOriginalSubject') ? [ |
|
| 81 | - $email->getOriginalSubject() |
|
| 82 | - ] : [ |
|
| 83 | - $email->getSubject() |
|
| 84 | - ], |
|
| 85 | - 'transport' => [ |
|
| 86 | - $this->_config['transport'] |
|
| 87 | - ], |
|
| 88 | - 'attachments' => [ |
|
| 89 | - $email->getAttachments() |
|
| 90 | - ], |
|
| 91 | - 'template' => [ |
|
| 92 | - $email->viewBuilder()->getTemplate() |
|
| 93 | - ], |
|
| 94 | - 'layout' => [ |
|
| 95 | - $email->viewBuilder()->getLayout() |
|
| 96 | - ], |
|
| 97 | - 'viewVars' => [ |
|
| 98 | - $email->getViewVars() |
|
| 99 | - ] |
|
| 100 | - ]; |
|
| 34 | + $settings = [ |
|
| 35 | + 'from' => [ |
|
| 36 | + $email->getFrom() |
|
| 37 | + ], |
|
| 38 | + 'to' => [ |
|
| 39 | + $email->getTo() |
|
| 40 | + ], |
|
| 41 | + 'cc' => [ |
|
| 42 | + $email->getCc() |
|
| 43 | + ], |
|
| 44 | + 'bcc' => [ |
|
| 45 | + $email->getBcc() |
|
| 46 | + ], |
|
| 47 | + 'charset' => [ |
|
| 48 | + $email->getCharset() |
|
| 49 | + ], |
|
| 50 | + 'replyTo' => [ |
|
| 51 | + $email->getReplyTo() |
|
| 52 | + ], |
|
| 53 | + 'readReceipt' => [ |
|
| 54 | + $email->getReadReceipt() |
|
| 55 | + ], |
|
| 56 | + 'returnPath' => [ |
|
| 57 | + $email->getReturnPath() |
|
| 58 | + ], |
|
| 59 | + 'messageId' => [ |
|
| 60 | + $email->getMessageId() |
|
| 61 | + ], |
|
| 62 | + 'domain' => [ |
|
| 63 | + $email->getDomain() |
|
| 64 | + ], |
|
| 65 | + 'headers' => [ |
|
| 66 | + $email->getHeaders() |
|
| 67 | + ], |
|
| 68 | + 'headerCharset' => [ |
|
| 69 | + $email->getHeaderCharset() |
|
| 70 | + ], |
|
| 71 | + 'theme' => [ |
|
| 72 | + $email->viewBuilder()->getTheme() |
|
| 73 | + ], |
|
| 74 | + 'profile' => [ |
|
| 75 | + $email->getProfile() |
|
| 76 | + ], |
|
| 77 | + 'emailFormat' => [ |
|
| 78 | + $email->getEmailFormat() |
|
| 79 | + ], |
|
| 80 | + 'subject' => method_exists($email, 'getOriginalSubject') ? [ |
|
| 81 | + $email->getOriginalSubject() |
|
| 82 | + ] : [ |
|
| 83 | + $email->getSubject() |
|
| 84 | + ], |
|
| 85 | + 'transport' => [ |
|
| 86 | + $this->_config['transport'] |
|
| 87 | + ], |
|
| 88 | + 'attachments' => [ |
|
| 89 | + $email->getAttachments() |
|
| 90 | + ], |
|
| 91 | + 'template' => [ |
|
| 92 | + $email->viewBuilder()->getTemplate() |
|
| 93 | + ], |
|
| 94 | + 'layout' => [ |
|
| 95 | + $email->viewBuilder()->getLayout() |
|
| 96 | + ], |
|
| 97 | + 'viewVars' => [ |
|
| 98 | + $email->getViewVars() |
|
| 99 | + ] |
|
| 100 | + ]; |
|
| 101 | 101 | |
| 102 | - foreach ($settings as $setting => $value) { |
|
| 103 | - if (array_key_exists(0, $value) && ($value[0] === null || $value[0] === [])) { |
|
| 104 | - unset($settings[$setting]); |
|
| 105 | - } |
|
| 106 | - } |
|
| 102 | + foreach ($settings as $setting => $value) { |
|
| 103 | + if (array_key_exists(0, $value) && ($value[0] === null || $value[0] === [])) { |
|
| 104 | + unset($settings[$setting]); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - $QueuedJobs = $this->getQueuedJobsModel(); |
|
| 109 | - $result = $QueuedJobs->createJob('Email', [ |
|
| 110 | - 'settings' => $settings |
|
| 111 | - ]); |
|
| 112 | - $result['headers'] = ''; |
|
| 113 | - $result['message'] = ''; |
|
| 108 | + $QueuedJobs = $this->getQueuedJobsModel(); |
|
| 109 | + $result = $QueuedJobs->createJob('Email', [ |
|
| 110 | + 'settings' => $settings |
|
| 111 | + ]); |
|
| 112 | + $result['headers'] = ''; |
|
| 113 | + $result['message'] = ''; |
|
| 114 | 114 | |
| 115 | - return $result->toArray(); |
|
| 116 | - } |
|
| 115 | + return $result->toArray(); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * |
|
| 120 | - * @return \Queue\Model\Table\QueuedTasksTable |
|
| 121 | - */ |
|
| 122 | - protected function getQueuedJobsModel() |
|
| 123 | - { |
|
| 124 | - /** @var \Queue\Model\Table\QueuedTasksTable $table */ |
|
| 125 | - $table = TableRegistry::getTableLocator()->get('Queue.QueuedTasks'); |
|
| 118 | + /** |
|
| 119 | + * |
|
| 120 | + * @return \Queue\Model\Table\QueuedTasksTable |
|
| 121 | + */ |
|
| 122 | + protected function getQueuedJobsModel() |
|
| 123 | + { |
|
| 124 | + /** @var \Queue\Model\Table\QueuedTasksTable $table */ |
|
| 125 | + $table = TableRegistry::getTableLocator()->get('Queue.QueuedTasks'); |
|
| 126 | 126 | |
| 127 | - return $table; |
|
| 128 | - } |
|
| 127 | + return $table; |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -15,34 +15,34 @@ |
||
| 15 | 15 | class QueueTransport extends AbstractTransport |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Send mail |
|
| 20 | - * |
|
| 21 | - * @param \Cake\Mailer\Email $email Email |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function send(Email $email) |
|
| 25 | - { |
|
| 26 | - if (!empty($this->_config['queue'])) { |
|
| 27 | - $this->_config = $this->_config['queue'] + $this->_config; |
|
| 28 | - $email->setConfig((array)$this->_config['queue'] + [ |
|
| 29 | - 'queue' => [] |
|
| 30 | - ]); |
|
| 31 | - unset($this->_config['queue']); |
|
| 32 | - } |
|
| 18 | + /** |
|
| 19 | + * Send mail |
|
| 20 | + * |
|
| 21 | + * @param \Cake\Mailer\Email $email Email |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function send(Email $email) |
|
| 25 | + { |
|
| 26 | + if (!empty($this->_config['queue'])) { |
|
| 27 | + $this->_config = $this->_config['queue'] + $this->_config; |
|
| 28 | + $email->setConfig((array)$this->_config['queue'] + [ |
|
| 29 | + 'queue' => [] |
|
| 30 | + ]); |
|
| 31 | + unset($this->_config['queue']); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - $transport = $this->_config['transport']; |
|
| 35 | - $email->setTransport($transport); |
|
| 34 | + $transport = $this->_config['transport']; |
|
| 35 | + $email->setTransport($transport); |
|
| 36 | 36 | |
| 37 | - /** @var \Queue\Model\Table\QueuedTasksTable $QueuedTasks */ |
|
| 38 | - $QueuedTasks = TableRegistry::getTableLocator()->get('Queue.QueuedTasks'); |
|
| 39 | - $result = $QueuedTasks->createJob('Email', [ |
|
| 40 | - 'transport' => $transport, |
|
| 41 | - 'settings' => $email |
|
| 42 | - ]); |
|
| 43 | - $result['headers'] = ''; |
|
| 44 | - $result['message'] = ''; |
|
| 37 | + /** @var \Queue\Model\Table\QueuedTasksTable $QueuedTasks */ |
|
| 38 | + $QueuedTasks = TableRegistry::getTableLocator()->get('Queue.QueuedTasks'); |
|
| 39 | + $result = $QueuedTasks->createJob('Email', [ |
|
| 40 | + 'transport' => $transport, |
|
| 41 | + 'settings' => $email |
|
| 42 | + ]); |
|
| 43 | + $result['headers'] = ''; |
|
| 44 | + $result['message'] = ''; |
|
| 45 | 45 | |
| 46 | - return $result->toArray(); |
|
| 47 | - } |
|
| 46 | + return $result->toArray(); |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -11,121 +11,121 @@ |
||
| 11 | 11 | class SimpleQueueTransportTest extends TestCase |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Fixtures to load |
|
| 16 | - * |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $fixtures = [ |
|
| 20 | - 'plugin.Queue.QueuedTasks' |
|
| 21 | - ]; |
|
| 14 | + /** |
|
| 15 | + * Fixtures to load |
|
| 16 | + * |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $fixtures = [ |
|
| 20 | + 'plugin.Queue.QueuedTasks' |
|
| 21 | + ]; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * |
|
| 25 | - * @var \Queue\Mailer\Transport\SimpleQueueTransport |
|
| 26 | - */ |
|
| 27 | - protected $QueueTransport; |
|
| 23 | + /** |
|
| 24 | + * |
|
| 25 | + * @var \Queue\Mailer\Transport\SimpleQueueTransport |
|
| 26 | + */ |
|
| 27 | + protected $QueueTransport; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Setup |
|
| 31 | - * |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function setUp() |
|
| 35 | - { |
|
| 36 | - parent::setUp(); |
|
| 37 | - $this->QueueTransport = new SimpleQueueTransport(); |
|
| 38 | - } |
|
| 29 | + /** |
|
| 30 | + * Setup |
|
| 31 | + * |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function setUp() |
|
| 35 | + { |
|
| 36 | + parent::setUp(); |
|
| 37 | + $this->QueueTransport = new SimpleQueueTransport(); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function testSendWithEmail() |
|
| 45 | - { |
|
| 46 | - $config = [ |
|
| 47 | - 'transport' => 'queue', |
|
| 48 | - 'charset' => 'utf-8', |
|
| 49 | - 'headerCharset' => 'utf-8' |
|
| 50 | - ]; |
|
| 40 | + /** |
|
| 41 | + * |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function testSendWithEmail() |
|
| 45 | + { |
|
| 46 | + $config = [ |
|
| 47 | + 'transport' => 'queue', |
|
| 48 | + 'charset' => 'utf-8', |
|
| 49 | + 'headerCharset' => 'utf-8' |
|
| 50 | + ]; |
|
| 51 | 51 | |
| 52 | - $this->QueueTransport->setConfig($config); |
|
| 53 | - $Email = new Email($config); |
|
| 52 | + $this->QueueTransport->setConfig($config); |
|
| 53 | + $Email = new Email($config); |
|
| 54 | 54 | |
| 55 | - $Email->setFrom('[email protected]', 'CakePHP Test'); |
|
| 56 | - $Email->setTo('[email protected]', 'CakePHP'); |
|
| 57 | - $Email->setCc([ |
|
| 58 | - '[email protected]' => 'Mark Story', |
|
| 59 | - '[email protected]' => 'Juan Basso' |
|
| 60 | - ]); |
|
| 61 | - $Email->setBcc('[email protected]'); |
|
| 62 | - $Email->setSubject('Testing Message'); |
|
| 63 | - $Email->setAttachments([ |
|
| 64 | - 'wow.txt' => [ |
|
| 65 | - 'data' => 'much wow!', |
|
| 66 | - 'mimetype' => 'text/plain', |
|
| 67 | - 'contentId' => 'important' |
|
| 68 | - ] |
|
| 69 | - ]); |
|
| 55 | + $Email->setFrom('[email protected]', 'CakePHP Test'); |
|
| 56 | + $Email->setTo('[email protected]', 'CakePHP'); |
|
| 57 | + $Email->setCc([ |
|
| 58 | + '[email protected]' => 'Mark Story', |
|
| 59 | + '[email protected]' => 'Juan Basso' |
|
| 60 | + ]); |
|
| 61 | + $Email->setBcc('[email protected]'); |
|
| 62 | + $Email->setSubject('Testing Message'); |
|
| 63 | + $Email->setAttachments([ |
|
| 64 | + 'wow.txt' => [ |
|
| 65 | + 'data' => 'much wow!', |
|
| 66 | + 'mimetype' => 'text/plain', |
|
| 67 | + 'contentId' => 'important' |
|
| 68 | + ] |
|
| 69 | + ]); |
|
| 70 | 70 | |
| 71 | - $Email->viewBuilder()->setLayout('test_layout'); |
|
| 72 | - $Email->viewBuilder()->setTemplate('test_template'); |
|
| 73 | - $Email->setSubject("L'utilisateur n'a pas pu être enregistré"); |
|
| 74 | - $Email->setReplyTo('[email protected]'); |
|
| 75 | - $Email->setReadReceipt('[email protected]'); |
|
| 76 | - $Email->setReturnPath('[email protected]'); |
|
| 77 | - $Email->setDomain('cakephp.org'); |
|
| 78 | - $Email->viewBuilder()->setTheme('EuroTheme'); |
|
| 79 | - $Email->setEmailFormat('both'); |
|
| 80 | - $Email->set('var1', 1); |
|
| 81 | - $Email->set('var2', 2); |
|
| 71 | + $Email->viewBuilder()->setLayout('test_layout'); |
|
| 72 | + $Email->viewBuilder()->setTemplate('test_template'); |
|
| 73 | + $Email->setSubject("L'utilisateur n'a pas pu être enregistré"); |
|
| 74 | + $Email->setReplyTo('[email protected]'); |
|
| 75 | + $Email->setReadReceipt('[email protected]'); |
|
| 76 | + $Email->setReturnPath('[email protected]'); |
|
| 77 | + $Email->setDomain('cakephp.org'); |
|
| 78 | + $Email->viewBuilder()->setTheme('EuroTheme'); |
|
| 79 | + $Email->setEmailFormat('both'); |
|
| 80 | + $Email->set('var1', 1); |
|
| 81 | + $Email->set('var2', 2); |
|
| 82 | 82 | |
| 83 | - $result = $this->QueueTransport->send($Email); |
|
| 84 | - $this->assertSame('Email', $result['task']); |
|
| 85 | - $this->assertTrue(strlen($result['data']) < 10000); |
|
| 83 | + $result = $this->QueueTransport->send($Email); |
|
| 84 | + $this->assertSame('Email', $result['task']); |
|
| 85 | + $this->assertTrue(strlen($result['data']) < 10000); |
|
| 86 | 86 | |
| 87 | - $output = unserialize($result['data']); |
|
| 88 | - $emailReconstructed = new Email($config); |
|
| 87 | + $output = unserialize($result['data']); |
|
| 88 | + $emailReconstructed = new Email($config); |
|
| 89 | 89 | |
| 90 | - foreach ($output['settings'] as $method => $setting) { |
|
| 91 | - $callable = $emailReconstructed; |
|
| 92 | - if (in_array($method, [ |
|
| 93 | - 'theme', |
|
| 94 | - 'template', |
|
| 95 | - 'layout' |
|
| 96 | - ])) { |
|
| 97 | - $callable = $callable->viewBuilder(); |
|
| 98 | - } |
|
| 99 | - $setter = 'set' . ucfirst($method); |
|
| 100 | - call_user_func_array([ |
|
| 101 | - $callable, |
|
| 102 | - $setter |
|
| 103 | - ], (array)$setting); |
|
| 104 | - } |
|
| 90 | + foreach ($output['settings'] as $method => $setting) { |
|
| 91 | + $callable = $emailReconstructed; |
|
| 92 | + if (in_array($method, [ |
|
| 93 | + 'theme', |
|
| 94 | + 'template', |
|
| 95 | + 'layout' |
|
| 96 | + ])) { |
|
| 97 | + $callable = $callable->viewBuilder(); |
|
| 98 | + } |
|
| 99 | + $setter = 'set' . ucfirst($method); |
|
| 100 | + call_user_func_array([ |
|
| 101 | + $callable, |
|
| 102 | + $setter |
|
| 103 | + ], (array)$setting); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - $this->assertEquals($emailReconstructed->getFrom(), $Email->getFrom()); |
|
| 107 | - $this->assertEquals($emailReconstructed->getTo(), $Email->getTo()); |
|
| 108 | - $this->assertEquals($emailReconstructed->getCc(), $Email->getCc()); |
|
| 109 | - $this->assertEquals($emailReconstructed->getBcc(), $Email->getBcc()); |
|
| 110 | - $this->assertEquals($emailReconstructed->getSubject(), $Email->getSubject()); |
|
| 111 | - $this->assertEquals($emailReconstructed->getCharset(), $Email->getCharset()); |
|
| 112 | - $this->assertEquals($emailReconstructed->getHeaderCharset(), $Email->getHeaderCharset()); |
|
| 113 | - $this->assertEquals($emailReconstructed->getEmailFormat(), $Email->getEmailFormat()); |
|
| 114 | - $this->assertEquals($emailReconstructed->getReplyTo(), $Email->getReplyTo()); |
|
| 115 | - $this->assertEquals($emailReconstructed->getReadReceipt(), $Email->getReadReceipt()); |
|
| 116 | - $this->assertEquals($emailReconstructed->getReturnPath(), $Email->getReturnPath()); |
|
| 117 | - // $this->assertEquals($emailReconstructed->getMessageId(), $Email->getMessageId()); |
|
| 118 | - $this->assertEquals($emailReconstructed->getDomain(), $Email->getDomain()); |
|
| 119 | - $this->assertEquals($emailReconstructed->viewBuilder() |
|
| 120 | - ->getTheme(), $Email->viewBuilder() |
|
| 121 | - ->getTheme()); |
|
| 122 | - $this->assertEquals($emailReconstructed->getProfile(), $Email->getProfile()); |
|
| 123 | - $this->assertEquals($emailReconstructed->getViewVars(), $Email->getViewVars()); |
|
| 124 | - $this->assertEquals($emailReconstructed->viewBuilder() |
|
| 125 | - ->getTemplate(), $Email->viewBuilder() |
|
| 126 | - ->getTemplate()); |
|
| 127 | - $this->assertEquals($emailReconstructed->viewBuilder() |
|
| 128 | - ->getLayout(), $Email->viewBuilder() |
|
| 129 | - ->getLayout()); |
|
| 130 | - } |
|
| 106 | + $this->assertEquals($emailReconstructed->getFrom(), $Email->getFrom()); |
|
| 107 | + $this->assertEquals($emailReconstructed->getTo(), $Email->getTo()); |
|
| 108 | + $this->assertEquals($emailReconstructed->getCc(), $Email->getCc()); |
|
| 109 | + $this->assertEquals($emailReconstructed->getBcc(), $Email->getBcc()); |
|
| 110 | + $this->assertEquals($emailReconstructed->getSubject(), $Email->getSubject()); |
|
| 111 | + $this->assertEquals($emailReconstructed->getCharset(), $Email->getCharset()); |
|
| 112 | + $this->assertEquals($emailReconstructed->getHeaderCharset(), $Email->getHeaderCharset()); |
|
| 113 | + $this->assertEquals($emailReconstructed->getEmailFormat(), $Email->getEmailFormat()); |
|
| 114 | + $this->assertEquals($emailReconstructed->getReplyTo(), $Email->getReplyTo()); |
|
| 115 | + $this->assertEquals($emailReconstructed->getReadReceipt(), $Email->getReadReceipt()); |
|
| 116 | + $this->assertEquals($emailReconstructed->getReturnPath(), $Email->getReturnPath()); |
|
| 117 | + // $this->assertEquals($emailReconstructed->getMessageId(), $Email->getMessageId()); |
|
| 118 | + $this->assertEquals($emailReconstructed->getDomain(), $Email->getDomain()); |
|
| 119 | + $this->assertEquals($emailReconstructed->viewBuilder() |
|
| 120 | + ->getTheme(), $Email->viewBuilder() |
|
| 121 | + ->getTheme()); |
|
| 122 | + $this->assertEquals($emailReconstructed->getProfile(), $Email->getProfile()); |
|
| 123 | + $this->assertEquals($emailReconstructed->getViewVars(), $Email->getViewVars()); |
|
| 124 | + $this->assertEquals($emailReconstructed->viewBuilder() |
|
| 125 | + ->getTemplate(), $Email->viewBuilder() |
|
| 126 | + ->getTemplate()); |
|
| 127 | + $this->assertEquals($emailReconstructed->viewBuilder() |
|
| 128 | + ->getLayout(), $Email->viewBuilder() |
|
| 129 | + ->getLayout()); |
|
| 130 | + } |
|
| 131 | 131 | } |
@@ -11,57 +11,57 @@ |
||
| 11 | 11 | class QueueTransportTest extends TestCase |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Fixtures to load |
|
| 16 | - * |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $fixtures = [ |
|
| 20 | - 'plugin.Queue.QueuedTasks' |
|
| 21 | - ]; |
|
| 14 | + /** |
|
| 15 | + * Fixtures to load |
|
| 16 | + * |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $fixtures = [ |
|
| 20 | + 'plugin.Queue.QueuedTasks' |
|
| 21 | + ]; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * |
|
| 25 | - * @var \Queue\Mailer\Transport\QueueTransport |
|
| 26 | - */ |
|
| 27 | - protected $QueueTransport; |
|
| 23 | + /** |
|
| 24 | + * |
|
| 25 | + * @var \Queue\Mailer\Transport\QueueTransport |
|
| 26 | + */ |
|
| 27 | + protected $QueueTransport; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Setup |
|
| 31 | - * |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function setUp() |
|
| 35 | - { |
|
| 36 | - parent::setUp(); |
|
| 37 | - $this->QueueTransport = new QueueTransport(); |
|
| 38 | - } |
|
| 29 | + /** |
|
| 30 | + * Setup |
|
| 31 | + * |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function setUp() |
|
| 35 | + { |
|
| 36 | + parent::setUp(); |
|
| 37 | + $this->QueueTransport = new QueueTransport(); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * TestSend method |
|
| 42 | - * |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function testSendWithEmail() |
|
| 46 | - { |
|
| 47 | - $Email = new Email(); |
|
| 48 | - $Email->setFrom('[email protected]', 'CakePHP Test'); |
|
| 49 | - $Email->setTo('[email protected]', 'CakePHP'); |
|
| 50 | - $Email->setCc([ |
|
| 51 | - '[email protected]' => 'Mark Story', |
|
| 52 | - '[email protected]' => 'Juan Basso' |
|
| 53 | - ]); |
|
| 54 | - $Email->setBcc('[email protected]'); |
|
| 55 | - $Email->setSubject('Testing Message'); |
|
| 56 | - $Email->setTransport('queue'); |
|
| 57 | - $config = $Email->getConfig('default'); |
|
| 58 | - $this->QueueTransport->setConfig($config); |
|
| 40 | + /** |
|
| 41 | + * TestSend method |
|
| 42 | + * |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function testSendWithEmail() |
|
| 46 | + { |
|
| 47 | + $Email = new Email(); |
|
| 48 | + $Email->setFrom('[email protected]', 'CakePHP Test'); |
|
| 49 | + $Email->setTo('[email protected]', 'CakePHP'); |
|
| 50 | + $Email->setCc([ |
|
| 51 | + '[email protected]' => 'Mark Story', |
|
| 52 | + '[email protected]' => 'Juan Basso' |
|
| 53 | + ]); |
|
| 54 | + $Email->setBcc('[email protected]'); |
|
| 55 | + $Email->setSubject('Testing Message'); |
|
| 56 | + $Email->setTransport('queue'); |
|
| 57 | + $config = $Email->getConfig('default'); |
|
| 58 | + $this->QueueTransport->setConfig($config); |
|
| 59 | 59 | |
| 60 | - $result = $this->QueueTransport->send($Email); |
|
| 61 | - $this->assertSame('Email', $result['task']); |
|
| 62 | - $this->assertTrue(strlen($result['data']) < 10000); |
|
| 60 | + $result = $this->QueueTransport->send($Email); |
|
| 61 | + $this->assertSame('Email', $result['task']); |
|
| 62 | + $this->assertTrue(strlen($result['data']) < 10000); |
|
| 63 | 63 | |
| 64 | - // $output = unserialize($result['data']); |
|
| 65 | - // $this->assertEquals('Testing Message', $output['settings']['_subject']); |
|
| 66 | - } |
|
| 64 | + // $output = unserialize($result['data']); |
|
| 65 | + // $this->assertEquals('Testing Message', $output['settings']['_subject']); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -8,80 +8,80 @@ |
||
| 8 | 8 | class TaskFinder |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * |
|
| 13 | - * @var array|null |
|
| 14 | - */ |
|
| 15 | - protected $tasks; |
|
| 11 | + /** |
|
| 12 | + * |
|
| 13 | + * @var array|null |
|
| 14 | + */ |
|
| 15 | + protected $tasks; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Returns all possible Queue tasks. |
|
| 19 | - * |
|
| 20 | - * Makes sure that app tasks are prioritized over plugin ones. |
|
| 21 | - * |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function allAppAndPluginTasks() |
|
| 25 | - { |
|
| 26 | - if ($this->tasks !== null) { |
|
| 27 | - return $this->tasks; |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Returns all possible Queue tasks. |
|
| 19 | + * |
|
| 20 | + * Makes sure that app tasks are prioritized over plugin ones. |
|
| 21 | + * |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function allAppAndPluginTasks() |
|
| 25 | + { |
|
| 26 | + if ($this->tasks !== null) { |
|
| 27 | + return $this->tasks; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - $paths = App::path('Shell/Task'); |
|
| 31 | - $this->tasks = []; |
|
| 30 | + $paths = App::path('Shell/Task'); |
|
| 31 | + $this->tasks = []; |
|
| 32 | 32 | |
| 33 | - foreach ($paths as $path) { |
|
| 34 | - $folder = new Folder($path); |
|
| 35 | - $this->tasks = $this->getAppPaths($folder); |
|
| 36 | - } |
|
| 37 | - $plugins = Plugin::loaded(); |
|
| 38 | - foreach ($plugins as $plugin) { |
|
| 39 | - $pluginPaths = App::path('Shell/Task', $plugin); |
|
| 40 | - foreach ($pluginPaths as $pluginPath) { |
|
| 41 | - $folder = new Folder($pluginPath); |
|
| 42 | - $pluginTasks = $this->getPluginPaths($folder, $plugin); |
|
| 43 | - $this->tasks = array_merge($this->tasks, $pluginTasks); |
|
| 44 | - } |
|
| 45 | - } |
|
| 33 | + foreach ($paths as $path) { |
|
| 34 | + $folder = new Folder($path); |
|
| 35 | + $this->tasks = $this->getAppPaths($folder); |
|
| 36 | + } |
|
| 37 | + $plugins = Plugin::loaded(); |
|
| 38 | + foreach ($plugins as $plugin) { |
|
| 39 | + $pluginPaths = App::path('Shell/Task', $plugin); |
|
| 40 | + foreach ($pluginPaths as $pluginPath) { |
|
| 41 | + $folder = new Folder($pluginPath); |
|
| 42 | + $pluginTasks = $this->getPluginPaths($folder, $plugin); |
|
| 43 | + $this->tasks = array_merge($this->tasks, $pluginTasks); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - return $this->tasks; |
|
| 48 | - } |
|
| 47 | + return $this->tasks; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * |
|
| 52 | - * @param \Cake\Filesystem\Folder $folder The directory |
|
| 53 | - * |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - protected function getAppPaths(Folder $folder) |
|
| 57 | - { |
|
| 58 | - $res = array_merge($this->tasks, $folder->find('Queue.+\.php')); |
|
| 59 | - foreach ($res as &$r) { |
|
| 60 | - $r = basename($r, 'Task.php'); |
|
| 61 | - } |
|
| 50 | + /** |
|
| 51 | + * |
|
| 52 | + * @param \Cake\Filesystem\Folder $folder The directory |
|
| 53 | + * |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + protected function getAppPaths(Folder $folder) |
|
| 57 | + { |
|
| 58 | + $res = array_merge($this->tasks, $folder->find('Queue.+\.php')); |
|
| 59 | + foreach ($res as &$r) { |
|
| 60 | + $r = basename($r, 'Task.php'); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - return $res; |
|
| 64 | - } |
|
| 63 | + return $res; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * @param \Cake\Filesystem\Folder $folder The directory |
|
| 69 | - * @param string $plugin The plugin name |
|
| 70 | - * |
|
| 71 | - * @return array |
|
| 72 | - */ |
|
| 73 | - protected function getPluginPaths(Folder $folder, $plugin) |
|
| 74 | - { |
|
| 75 | - $res = $folder->find('Queue.+Task\.php'); |
|
| 76 | - foreach ($res as $key => $r) { |
|
| 77 | - $name = basename($r, 'Task.php'); |
|
| 78 | - if (in_array($name, $this->tasks)) { |
|
| 79 | - unset($res[$key]); |
|
| 80 | - continue; |
|
| 81 | - } |
|
| 82 | - $res[$key] = $plugin . '.' . $name; |
|
| 83 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * @param \Cake\Filesystem\Folder $folder The directory |
|
| 69 | + * @param string $plugin The plugin name |
|
| 70 | + * |
|
| 71 | + * @return array |
|
| 72 | + */ |
|
| 73 | + protected function getPluginPaths(Folder $folder, $plugin) |
|
| 74 | + { |
|
| 75 | + $res = $folder->find('Queue.+Task\.php'); |
|
| 76 | + foreach ($res as $key => $r) { |
|
| 77 | + $name = basename($r, 'Task.php'); |
|
| 78 | + if (in_array($name, $this->tasks)) { |
|
| 79 | + unset($res[$key]); |
|
| 80 | + continue; |
|
| 81 | + } |
|
| 82 | + $res[$key] = $plugin . '.' . $name; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - return $res; |
|
| 86 | - } |
|
| 85 | + return $res; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -30,521 +30,521 @@ |
||
| 30 | 30 | class QueueShell extends Shell |
| 31 | 31 | { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - public $modelClass = 'Queue.QueuedTasks'; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * |
|
| 41 | - * @var array|null |
|
| 42 | - */ |
|
| 43 | - protected $_taskConf; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * |
|
| 47 | - * @var int |
|
| 48 | - */ |
|
| 49 | - protected $_time = 0; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * @var bool |
|
| 54 | - */ |
|
| 55 | - protected $_exit = false; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Overwrite shell initialize to dynamically load all Queue Related Tasks. |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function initialize() |
|
| 63 | - { |
|
| 64 | - $taskFinder = new TaskFinder(); |
|
| 65 | - $this->tasks = $taskFinder->allAppAndPluginTasks(); |
|
| 66 | - |
|
| 67 | - parent::initialize(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public function startup() |
|
| 75 | - { |
|
| 76 | - if ($this->param('quiet')) { |
|
| 77 | - $this->interactive = false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - parent::startup(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * |
|
| 85 | - * @return string |
|
| 86 | - */ |
|
| 87 | - public function getDescription() |
|
| 88 | - { |
|
| 89 | - $tasks = []; |
|
| 90 | - foreach ($this->taskNames as $loadedTask) { |
|
| 91 | - $tasks[] = "\t" . '* ' . $this->_taskName($loadedTask); |
|
| 92 | - } |
|
| 93 | - $tasks = implode(PHP_EOL, $tasks); |
|
| 94 | - |
|
| 95 | - $text = <<<TEXT |
|
| 33 | + /** |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + public $modelClass = 'Queue.QueuedTasks'; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * |
|
| 41 | + * @var array|null |
|
| 42 | + */ |
|
| 43 | + protected $_taskConf; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * |
|
| 47 | + * @var int |
|
| 48 | + */ |
|
| 49 | + protected $_time = 0; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * @var bool |
|
| 54 | + */ |
|
| 55 | + protected $_exit = false; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Overwrite shell initialize to dynamically load all Queue Related Tasks. |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function initialize() |
|
| 63 | + { |
|
| 64 | + $taskFinder = new TaskFinder(); |
|
| 65 | + $this->tasks = $taskFinder->allAppAndPluginTasks(); |
|
| 66 | + |
|
| 67 | + parent::initialize(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public function startup() |
|
| 75 | + { |
|
| 76 | + if ($this->param('quiet')) { |
|
| 77 | + $this->interactive = false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + parent::startup(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * |
|
| 85 | + * @return string |
|
| 86 | + */ |
|
| 87 | + public function getDescription() |
|
| 88 | + { |
|
| 89 | + $tasks = []; |
|
| 90 | + foreach ($this->taskNames as $loadedTask) { |
|
| 91 | + $tasks[] = "\t" . '* ' . $this->_taskName($loadedTask); |
|
| 92 | + } |
|
| 93 | + $tasks = implode(PHP_EOL, $tasks); |
|
| 94 | + |
|
| 95 | + $text = <<<TEXT |
|
| 96 | 96 | Simple and minimalistic job queue (or deferred-task) system. |
| 97 | 97 | |
| 98 | 98 | Available Tasks: |
| 99 | 99 | $tasks |
| 100 | 100 | TEXT; |
| 101 | 101 | |
| 102 | - return $text; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Look for a Queue Task of hte passed name and try to call add() on it. |
|
| 107 | - * A QueueTask may provide an add function to enable the user to create new jobs via commandline. |
|
| 108 | - * |
|
| 109 | - * @return void |
|
| 110 | - */ |
|
| 111 | - public function add() |
|
| 112 | - { |
|
| 113 | - if (count($this->args) < 1) { |
|
| 114 | - $this->out('Please call like this:'); |
|
| 115 | - $this->out(' bin/cake queue add <taskname>'); |
|
| 116 | - $this->_displayAvailableTasks(); |
|
| 117 | - |
|
| 118 | - return; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $name = Inflector::camelize($this->args[0]); |
|
| 122 | - if (in_array('Queue' . $name, $this->taskNames, true)) { |
|
| 123 | - /** @var \Queue\Shell\Task\QueueTask|\Queue\Shell\Task\AddInterface $task */ |
|
| 124 | - $task = $this->{'Queue' . $name}; |
|
| 125 | - if (!($task instanceof AddInterface)) { |
|
| 126 | - $this->abort('This task does not support adding via CLI call'); |
|
| 127 | - } |
|
| 128 | - $task->add(); |
|
| 129 | - } else { |
|
| 130 | - $this->out('Error: Task not found: ' . $name); |
|
| 131 | - $this->_displayAvailableTasks(); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Output the task without Queue or Task |
|
| 137 | - * example: QueueImageTask becomes Image on display |
|
| 138 | - * |
|
| 139 | - * @param string $task Task name |
|
| 140 | - * @return string Cleaned task name |
|
| 141 | - */ |
|
| 142 | - protected function _taskName($task) |
|
| 143 | - { |
|
| 144 | - if (strpos($task, 'Queue') === 0) { |
|
| 145 | - return substr($task, 5); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return $task; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Run a QueueWorker loop. |
|
| 153 | - * Runs a Queue Worker process which will try to find unassigned jobs in the queue |
|
| 154 | - * which it may run and try to fetch and execute them. |
|
| 155 | - * |
|
| 156 | - * @return void |
|
| 157 | - */ |
|
| 158 | - public function runworker() |
|
| 159 | - { |
|
| 160 | - // Enable Garbage Collector (PHP >= 5.3) |
|
| 161 | - if (function_exists('gc_enable')) { |
|
| 162 | - gc_enable(); |
|
| 163 | - } |
|
| 164 | - if (function_exists('pcntl_signal')) { |
|
| 165 | - pcntl_signal(SIGTERM, [ |
|
| 166 | - &$this, |
|
| 167 | - '_exit' |
|
| 168 | - ]); |
|
| 169 | - pcntl_signal(SIGINT, [ |
|
| 170 | - &$this, |
|
| 171 | - '_exit' |
|
| 172 | - ]); |
|
| 173 | - pcntl_signal(SIGTSTP, [ |
|
| 174 | - &$this, |
|
| 175 | - '_exit' |
|
| 176 | - ]); |
|
| 177 | - pcntl_signal(SIGQUIT, [ |
|
| 178 | - &$this, |
|
| 179 | - '_exit' |
|
| 180 | - ]); |
|
| 181 | - } |
|
| 182 | - $this->_exit = false; |
|
| 183 | - |
|
| 184 | - $startTime = time(); |
|
| 185 | - $types = $this->_stringToArray($this->param('type')); |
|
| 186 | - |
|
| 187 | - while (!$this->_exit) { |
|
| 188 | - $this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE); |
|
| 189 | - |
|
| 190 | - $queuedTask = $this->QueuedTasks->requestJob($this->_getTaskConf(), $types); |
|
| 191 | - |
|
| 192 | - if ($queuedTask) { |
|
| 193 | - $this->runJob($queuedTask); |
|
| 194 | - } elseif (Configure::read('Queue.exitWhenNothingToDo')) { |
|
| 195 | - $this->out(__d('queue', 'nothing to do, exiting.')); |
|
| 196 | - $this->_exit = true; |
|
| 197 | - } else { |
|
| 198 | - $this->out(__d('queue', 'nothing to do, sleeping.')); |
|
| 199 | - sleep(Config::sleepTime()); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // check if we are over the maximum runtime and end processing if so. |
|
| 203 | - if (Configure::readOrFail('Queue.workerMaxRuntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workerMaxRuntime')) { |
|
| 204 | - $this->_exit = true; |
|
| 205 | - $this->out(__d('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.')); |
|
| 206 | - } |
|
| 207 | - if ($this->_exit || mt_rand(0, 100) > (100 - (int)Config::gcprob())) { |
|
| 208 | - $this->out(__d('queue', 'Performing old job cleanup.')); |
|
| 209 | - $this->QueuedTasks->cleanOldJobs(); |
|
| 210 | - } |
|
| 211 | - $this->hr(); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - if ($this->param('verbose')) { |
|
| 215 | - $this->_log('endworker'); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * |
|
| 221 | - * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
| 222 | - * @return void |
|
| 223 | - */ |
|
| 224 | - protected function runJob(QueuedTask $queuedTask) |
|
| 225 | - { |
|
| 226 | - $this->out('Running Job of type "' . $queuedTask->task . '"'); |
|
| 227 | - $this->_log('job ' . $queuedTask->task . ', id ' . $queuedTask->id, null, false); |
|
| 228 | - $taskName = 'Queue' . $queuedTask->task; |
|
| 229 | - |
|
| 230 | - try { |
|
| 231 | - $this->_time = time(); |
|
| 232 | - |
|
| 233 | - $data = unserialize($queuedTask->data); |
|
| 234 | - /** @var \Queue\Shell\Task\QueueTask $task */ |
|
| 235 | - $task = $this->{$taskName}; |
|
| 236 | - if (!$task instanceof QueueTaskInterface) { |
|
| 237 | - throw new RuntimeException('Task must implement ' . QueueTaskInterface::class); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $return = $task->run((array)$data, $queuedTask->id); |
|
| 241 | - if ($return !== null) { |
|
| 242 | - trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED); |
|
| 243 | - } |
|
| 244 | - $failureMessage = $taskName . ' failed'; |
|
| 245 | - } catch (Throwable $e) { |
|
| 246 | - $return = false; |
|
| 247 | - |
|
| 248 | - $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
| 249 | - if (!($e instanceof QueueException)) { |
|
| 250 | - $failureMessage .= "\n" . $e->getTraceAsString(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $this->_logError($taskName . ' (job ' . $queuedTask->id . ')' . "\n" . $failureMessage); |
|
| 254 | - } catch (Exception $e) { |
|
| 255 | - $return = false; |
|
| 256 | - |
|
| 257 | - $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
| 258 | - $this->_logError($taskName . "\n" . $failureMessage); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - if ($return === false) { |
|
| 262 | - $this->QueuedTasks->markJobFailed($queuedTask, $failureMessage); |
|
| 263 | - $failedStatus = $this->QueuedTasks->getFailedStatus($queuedTask, $this->_getTaskConf()); |
|
| 264 | - $this->_log('job ' . $queuedTask->task . ', id ' . $queuedTask->id . ' failed and ' . $failedStatus); |
|
| 265 | - $this->out('Job did not finish, ' . $failedStatus . ' after try ' . $queuedTask->failed . '.'); |
|
| 266 | - |
|
| 267 | - return; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $this->QueuedTasks->markJobDone($queuedTask); |
|
| 271 | - $this->out('Job Finished.'); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Manually trigger a Finished job cleanup. |
|
| 276 | - * |
|
| 277 | - * @return void |
|
| 278 | - */ |
|
| 279 | - public function clean() |
|
| 280 | - { |
|
| 281 | - if (!Configure::read('Queue.cleanupTimeout')) { |
|
| 282 | - $this->abort('You disabled cleanuptimout in config. Aborting.'); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - $this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout'))); |
|
| 286 | - $this->QueuedTasks->cleanOldJobs(); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Display current settings |
|
| 291 | - * |
|
| 292 | - * @return void |
|
| 293 | - */ |
|
| 294 | - public function settings() |
|
| 295 | - { |
|
| 296 | - $this->out('Current Settings:'); |
|
| 297 | - $conf = (array)Configure::read('Queue'); |
|
| 298 | - foreach ($conf as $key => $val) { |
|
| 299 | - if ($val === false) { |
|
| 300 | - $val = 'no'; |
|
| 301 | - } |
|
| 302 | - if ($val === true) { |
|
| 303 | - $val = 'yes'; |
|
| 304 | - } |
|
| 305 | - $this->out('* ' . $key . ': ' . print_r($val, true)); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $this->out(); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Display some statistics about Finished Jobs. |
|
| 313 | - * |
|
| 314 | - * @return void |
|
| 315 | - */ |
|
| 316 | - public function stats() |
|
| 317 | - { |
|
| 318 | - $this->out('Jobs currently in the queue:'); |
|
| 319 | - |
|
| 320 | - $types = $this->QueuedTasks->getTypes()->toArray(); |
|
| 321 | - foreach ($types as $type) { |
|
| 322 | - $this->out(' ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $this->QueuedTasks->getLength($type)); |
|
| 323 | - } |
|
| 324 | - $this->hr(); |
|
| 325 | - $this->out('Total unfinished jobs: ' . $this->QueuedTasks->getLength()); |
|
| 326 | - $this->hr(); |
|
| 327 | - $this->out('Finished job statistics:'); |
|
| 328 | - $data = $this->QueuedTasks->getStats(); |
|
| 329 | - foreach ($data as $item) { |
|
| 330 | - $this->out(' ' . $item['task'] . ': '); |
|
| 331 | - $this->out(' Finished Jobs in Database: ' . $item['num']); |
|
| 332 | - $this->out(' Average Job existence : ' . str_pad(Number::precision($item['alltime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
| 333 | - $this->out(' Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
| 334 | - $this->out(' Average Execution time : ' . str_pad(Number::precision($item['runtime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Get option parser method to parse commandline options |
|
| 340 | - * |
|
| 341 | - * @return \Cake\Console\ConsoleOptionParser |
|
| 342 | - */ |
|
| 343 | - public function getOptionParser() |
|
| 344 | - { |
|
| 345 | - $subcommandParser = [ |
|
| 346 | - 'options' => [ |
|
| 347 | - /* |
|
| 102 | + return $text; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Look for a Queue Task of hte passed name and try to call add() on it. |
|
| 107 | + * A QueueTask may provide an add function to enable the user to create new jobs via commandline. |
|
| 108 | + * |
|
| 109 | + * @return void |
|
| 110 | + */ |
|
| 111 | + public function add() |
|
| 112 | + { |
|
| 113 | + if (count($this->args) < 1) { |
|
| 114 | + $this->out('Please call like this:'); |
|
| 115 | + $this->out(' bin/cake queue add <taskname>'); |
|
| 116 | + $this->_displayAvailableTasks(); |
|
| 117 | + |
|
| 118 | + return; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $name = Inflector::camelize($this->args[0]); |
|
| 122 | + if (in_array('Queue' . $name, $this->taskNames, true)) { |
|
| 123 | + /** @var \Queue\Shell\Task\QueueTask|\Queue\Shell\Task\AddInterface $task */ |
|
| 124 | + $task = $this->{'Queue' . $name}; |
|
| 125 | + if (!($task instanceof AddInterface)) { |
|
| 126 | + $this->abort('This task does not support adding via CLI call'); |
|
| 127 | + } |
|
| 128 | + $task->add(); |
|
| 129 | + } else { |
|
| 130 | + $this->out('Error: Task not found: ' . $name); |
|
| 131 | + $this->_displayAvailableTasks(); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Output the task without Queue or Task |
|
| 137 | + * example: QueueImageTask becomes Image on display |
|
| 138 | + * |
|
| 139 | + * @param string $task Task name |
|
| 140 | + * @return string Cleaned task name |
|
| 141 | + */ |
|
| 142 | + protected function _taskName($task) |
|
| 143 | + { |
|
| 144 | + if (strpos($task, 'Queue') === 0) { |
|
| 145 | + return substr($task, 5); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return $task; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Run a QueueWorker loop. |
|
| 153 | + * Runs a Queue Worker process which will try to find unassigned jobs in the queue |
|
| 154 | + * which it may run and try to fetch and execute them. |
|
| 155 | + * |
|
| 156 | + * @return void |
|
| 157 | + */ |
|
| 158 | + public function runworker() |
|
| 159 | + { |
|
| 160 | + // Enable Garbage Collector (PHP >= 5.3) |
|
| 161 | + if (function_exists('gc_enable')) { |
|
| 162 | + gc_enable(); |
|
| 163 | + } |
|
| 164 | + if (function_exists('pcntl_signal')) { |
|
| 165 | + pcntl_signal(SIGTERM, [ |
|
| 166 | + &$this, |
|
| 167 | + '_exit' |
|
| 168 | + ]); |
|
| 169 | + pcntl_signal(SIGINT, [ |
|
| 170 | + &$this, |
|
| 171 | + '_exit' |
|
| 172 | + ]); |
|
| 173 | + pcntl_signal(SIGTSTP, [ |
|
| 174 | + &$this, |
|
| 175 | + '_exit' |
|
| 176 | + ]); |
|
| 177 | + pcntl_signal(SIGQUIT, [ |
|
| 178 | + &$this, |
|
| 179 | + '_exit' |
|
| 180 | + ]); |
|
| 181 | + } |
|
| 182 | + $this->_exit = false; |
|
| 183 | + |
|
| 184 | + $startTime = time(); |
|
| 185 | + $types = $this->_stringToArray($this->param('type')); |
|
| 186 | + |
|
| 187 | + while (!$this->_exit) { |
|
| 188 | + $this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE); |
|
| 189 | + |
|
| 190 | + $queuedTask = $this->QueuedTasks->requestJob($this->_getTaskConf(), $types); |
|
| 191 | + |
|
| 192 | + if ($queuedTask) { |
|
| 193 | + $this->runJob($queuedTask); |
|
| 194 | + } elseif (Configure::read('Queue.exitWhenNothingToDo')) { |
|
| 195 | + $this->out(__d('queue', 'nothing to do, exiting.')); |
|
| 196 | + $this->_exit = true; |
|
| 197 | + } else { |
|
| 198 | + $this->out(__d('queue', 'nothing to do, sleeping.')); |
|
| 199 | + sleep(Config::sleepTime()); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // check if we are over the maximum runtime and end processing if so. |
|
| 203 | + if (Configure::readOrFail('Queue.workerMaxRuntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workerMaxRuntime')) { |
|
| 204 | + $this->_exit = true; |
|
| 205 | + $this->out(__d('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.')); |
|
| 206 | + } |
|
| 207 | + if ($this->_exit || mt_rand(0, 100) > (100 - (int)Config::gcprob())) { |
|
| 208 | + $this->out(__d('queue', 'Performing old job cleanup.')); |
|
| 209 | + $this->QueuedTasks->cleanOldJobs(); |
|
| 210 | + } |
|
| 211 | + $this->hr(); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + if ($this->param('verbose')) { |
|
| 215 | + $this->_log('endworker'); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * |
|
| 221 | + * @param \Queue\Model\Entity\QueuedTask $queuedTask Queued task |
|
| 222 | + * @return void |
|
| 223 | + */ |
|
| 224 | + protected function runJob(QueuedTask $queuedTask) |
|
| 225 | + { |
|
| 226 | + $this->out('Running Job of type "' . $queuedTask->task . '"'); |
|
| 227 | + $this->_log('job ' . $queuedTask->task . ', id ' . $queuedTask->id, null, false); |
|
| 228 | + $taskName = 'Queue' . $queuedTask->task; |
|
| 229 | + |
|
| 230 | + try { |
|
| 231 | + $this->_time = time(); |
|
| 232 | + |
|
| 233 | + $data = unserialize($queuedTask->data); |
|
| 234 | + /** @var \Queue\Shell\Task\QueueTask $task */ |
|
| 235 | + $task = $this->{$taskName}; |
|
| 236 | + if (!$task instanceof QueueTaskInterface) { |
|
| 237 | + throw new RuntimeException('Task must implement ' . QueueTaskInterface::class); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $return = $task->run((array)$data, $queuedTask->id); |
|
| 241 | + if ($return !== null) { |
|
| 242 | + trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED); |
|
| 243 | + } |
|
| 244 | + $failureMessage = $taskName . ' failed'; |
|
| 245 | + } catch (Throwable $e) { |
|
| 246 | + $return = false; |
|
| 247 | + |
|
| 248 | + $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
| 249 | + if (!($e instanceof QueueException)) { |
|
| 250 | + $failureMessage .= "\n" . $e->getTraceAsString(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $this->_logError($taskName . ' (job ' . $queuedTask->id . ')' . "\n" . $failureMessage); |
|
| 254 | + } catch (Exception $e) { |
|
| 255 | + $return = false; |
|
| 256 | + |
|
| 257 | + $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
| 258 | + $this->_logError($taskName . "\n" . $failureMessage); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + if ($return === false) { |
|
| 262 | + $this->QueuedTasks->markJobFailed($queuedTask, $failureMessage); |
|
| 263 | + $failedStatus = $this->QueuedTasks->getFailedStatus($queuedTask, $this->_getTaskConf()); |
|
| 264 | + $this->_log('job ' . $queuedTask->task . ', id ' . $queuedTask->id . ' failed and ' . $failedStatus); |
|
| 265 | + $this->out('Job did not finish, ' . $failedStatus . ' after try ' . $queuedTask->failed . '.'); |
|
| 266 | + |
|
| 267 | + return; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $this->QueuedTasks->markJobDone($queuedTask); |
|
| 271 | + $this->out('Job Finished.'); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Manually trigger a Finished job cleanup. |
|
| 276 | + * |
|
| 277 | + * @return void |
|
| 278 | + */ |
|
| 279 | + public function clean() |
|
| 280 | + { |
|
| 281 | + if (!Configure::read('Queue.cleanupTimeout')) { |
|
| 282 | + $this->abort('You disabled cleanuptimout in config. Aborting.'); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + $this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout'))); |
|
| 286 | + $this->QueuedTasks->cleanOldJobs(); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Display current settings |
|
| 291 | + * |
|
| 292 | + * @return void |
|
| 293 | + */ |
|
| 294 | + public function settings() |
|
| 295 | + { |
|
| 296 | + $this->out('Current Settings:'); |
|
| 297 | + $conf = (array)Configure::read('Queue'); |
|
| 298 | + foreach ($conf as $key => $val) { |
|
| 299 | + if ($val === false) { |
|
| 300 | + $val = 'no'; |
|
| 301 | + } |
|
| 302 | + if ($val === true) { |
|
| 303 | + $val = 'yes'; |
|
| 304 | + } |
|
| 305 | + $this->out('* ' . $key . ': ' . print_r($val, true)); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $this->out(); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Display some statistics about Finished Jobs. |
|
| 313 | + * |
|
| 314 | + * @return void |
|
| 315 | + */ |
|
| 316 | + public function stats() |
|
| 317 | + { |
|
| 318 | + $this->out('Jobs currently in the queue:'); |
|
| 319 | + |
|
| 320 | + $types = $this->QueuedTasks->getTypes()->toArray(); |
|
| 321 | + foreach ($types as $type) { |
|
| 322 | + $this->out(' ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $this->QueuedTasks->getLength($type)); |
|
| 323 | + } |
|
| 324 | + $this->hr(); |
|
| 325 | + $this->out('Total unfinished jobs: ' . $this->QueuedTasks->getLength()); |
|
| 326 | + $this->hr(); |
|
| 327 | + $this->out('Finished job statistics:'); |
|
| 328 | + $data = $this->QueuedTasks->getStats(); |
|
| 329 | + foreach ($data as $item) { |
|
| 330 | + $this->out(' ' . $item['task'] . ': '); |
|
| 331 | + $this->out(' Finished Jobs in Database: ' . $item['num']); |
|
| 332 | + $this->out(' Average Job existence : ' . str_pad(Number::precision($item['alltime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
| 333 | + $this->out(' Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
| 334 | + $this->out(' Average Execution time : ' . str_pad(Number::precision($item['runtime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Get option parser method to parse commandline options |
|
| 340 | + * |
|
| 341 | + * @return \Cake\Console\ConsoleOptionParser |
|
| 342 | + */ |
|
| 343 | + public function getOptionParser() |
|
| 344 | + { |
|
| 345 | + $subcommandParser = [ |
|
| 346 | + 'options' => [ |
|
| 347 | + /* |
|
| 348 | 348 | * 'dry-run'=> array( |
| 349 | 349 | * 'short' => 'd', |
| 350 | 350 | * 'help' => 'Dry run the update, no jobs will actually be added.', |
| 351 | 351 | * 'boolean' => true |
| 352 | 352 | * ), |
| 353 | 353 | */ |
| 354 | - ] |
|
| 355 | - ]; |
|
| 356 | - $subcommandParserFull = $subcommandParser; |
|
| 357 | - $subcommandParserFull['options']['type'] = [ |
|
| 358 | - 'short' => 't', |
|
| 359 | - 'help' => 'Type (comma separated list possible)', |
|
| 360 | - 'default' => null |
|
| 361 | - ]; |
|
| 362 | - |
|
| 363 | - return parent::getOptionParser()->setDescription($this->getDescription()) |
|
| 364 | - ->addSubcommand('clean', [ |
|
| 365 | - 'help' => 'Remove old jobs (cleanup)', |
|
| 366 | - 'parser' => $subcommandParser |
|
| 367 | - ]) |
|
| 368 | - ->addSubcommand('add', [ |
|
| 369 | - 'help' => 'Add Job', |
|
| 370 | - 'parser' => $subcommandParser |
|
| 371 | - ]) |
|
| 372 | - ->addSubcommand('stats', [ |
|
| 373 | - 'help' => 'Stats', |
|
| 374 | - 'parser' => $subcommandParserFull |
|
| 375 | - ]) |
|
| 376 | - ->addSubcommand('settings', [ |
|
| 377 | - 'help' => 'Settings', |
|
| 378 | - 'parser' => $subcommandParserFull |
|
| 379 | - ]) |
|
| 380 | - ->addSubcommand('runworker', [ |
|
| 381 | - 'help' => 'Run Worker', |
|
| 382 | - 'parser' => $subcommandParserFull |
|
| 383 | - ]); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Timestamped log. |
|
| 388 | - * |
|
| 389 | - * @param string $message Log type |
|
| 390 | - * @param string|null $pid PID of the process |
|
| 391 | - * @param bool $addDetails Details |
|
| 392 | - * @return void |
|
| 393 | - */ |
|
| 394 | - protected function _log($message, $pid = null, $addDetails = true) |
|
| 395 | - { |
|
| 396 | - if (!Configure::read('Queue.log')) { |
|
| 397 | - return; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - if ($addDetails) { |
|
| 401 | - $timeNeeded = $this->_timeNeeded(); |
|
| 402 | - $memoryUsage = $this->_memoryUsage(); |
|
| 403 | - $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - if ($pid) { |
|
| 407 | - $message .= ' (pid ' . $pid . ')'; |
|
| 408 | - } |
|
| 409 | - Log::write('info', $message, [ |
|
| 410 | - 'scope' => 'queue' |
|
| 411 | - ]); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * |
|
| 416 | - * @param string $message Message |
|
| 417 | - * @param string|null $pid PID of the process |
|
| 418 | - * @return void |
|
| 419 | - */ |
|
| 420 | - protected function _logError($message, $pid = null) |
|
| 421 | - { |
|
| 422 | - $timeNeeded = $this->_timeNeeded(); |
|
| 423 | - $memoryUsage = $this->_memoryUsage(); |
|
| 424 | - $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
| 425 | - |
|
| 426 | - if ($pid) { |
|
| 427 | - $message .= ' (pid ' . $pid . ')'; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - Log::write('error', $message); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Returns a List of available QueueTasks and their individual configurations. |
|
| 435 | - * |
|
| 436 | - * @return array |
|
| 437 | - */ |
|
| 438 | - protected function _getTaskConf() |
|
| 439 | - { |
|
| 440 | - if (!is_array($this->_taskConf)) { |
|
| 441 | - $this->_taskConf = []; |
|
| 442 | - foreach ($this->tasks as $task) { |
|
| 443 | - list ($pluginName, $taskName) = pluginSplit($task); |
|
| 444 | - |
|
| 445 | - $this->_taskConf[$taskName]['name'] = substr($taskName, 5); |
|
| 446 | - $this->_taskConf[$taskName]['plugin'] = $pluginName; |
|
| 447 | - if (property_exists($this->{$taskName}, 'timeout')) { |
|
| 448 | - $this->_taskConf[$taskName]['timeout'] = $this->{$taskName}->timeout; |
|
| 449 | - } else { |
|
| 450 | - $this->_taskConf[$taskName]['timeout'] = Config::defaultWorkerTimeout(); |
|
| 451 | - } |
|
| 452 | - if (property_exists($this->{$taskName}, 'retries')) { |
|
| 453 | - $this->_taskConf[$taskName]['retries'] = $this->{$taskName}->retries; |
|
| 454 | - } else { |
|
| 455 | - $this->_taskConf[$taskName]['retries'] = Config::defaultWorkerRetries(); |
|
| 456 | - } |
|
| 457 | - if (property_exists($this->{$taskName}, 'cleanupTimeout')) { |
|
| 458 | - $this->_taskConf[$taskName]['cleanupTimeout'] = $this->{$taskName}->cleanupTimeout; |
|
| 459 | - } else { |
|
| 460 | - $this->_taskConf[$taskName]['cleanupTimeout'] = Config::cleanupTimeout(); |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - return $this->_taskConf; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Signal handling to queue worker for clean shutdown |
|
| 470 | - * |
|
| 471 | - * @param int $signal The signal |
|
| 472 | - * @return void |
|
| 473 | - */ |
|
| 474 | - protected function _exit($signal) |
|
| 475 | - { |
|
| 476 | - $this->out(__d('queue', 'Caught signal {0}, exiting.', $signal)); |
|
| 477 | - $this->_exit = true; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * |
|
| 482 | - * @return void |
|
| 483 | - */ |
|
| 484 | - protected function _displayAvailableTasks() |
|
| 485 | - { |
|
| 486 | - $this->out('Available Tasks:'); |
|
| 487 | - foreach ($this->taskNames as $loadedTask) { |
|
| 488 | - $this->out("\t" . '* ' . $this->_taskName($loadedTask)); |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * |
|
| 494 | - * @return string Memory usage in MB. |
|
| 495 | - */ |
|
| 496 | - protected function _memoryUsage() |
|
| 497 | - { |
|
| 498 | - $limit = ini_get('memory_limit'); |
|
| 499 | - |
|
| 500 | - $used = number_format(memory_get_peak_usage(true) / (1024 * 1024), 0) . 'MB'; |
|
| 501 | - if ($limit !== '-1') { |
|
| 502 | - $used .= '/' . $limit; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - return $used; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * |
|
| 510 | - * @return string |
|
| 511 | - */ |
|
| 512 | - protected function _timeNeeded() |
|
| 513 | - { |
|
| 514 | - $diff = $this->_time() - $this->_time($this->_time); |
|
| 515 | - $seconds = max($diff, 1); |
|
| 516 | - |
|
| 517 | - return $seconds . 's'; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * |
|
| 522 | - * @param int|null $providedTime Provided time |
|
| 523 | - * |
|
| 524 | - * @return int |
|
| 525 | - */ |
|
| 526 | - protected function _time($providedTime = null) |
|
| 527 | - { |
|
| 528 | - if ($providedTime !== null) { |
|
| 529 | - return $providedTime; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - return time(); |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * |
|
| 537 | - * @param string|null $param String to convert |
|
| 538 | - * @return array |
|
| 539 | - */ |
|
| 540 | - protected function _stringToArray($param) |
|
| 541 | - { |
|
| 542 | - if (!$param) { |
|
| 543 | - return []; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - $array = Text::tokenize($param); |
|
| 547 | - |
|
| 548 | - return array_filter($array); |
|
| 549 | - } |
|
| 354 | + ] |
|
| 355 | + ]; |
|
| 356 | + $subcommandParserFull = $subcommandParser; |
|
| 357 | + $subcommandParserFull['options']['type'] = [ |
|
| 358 | + 'short' => 't', |
|
| 359 | + 'help' => 'Type (comma separated list possible)', |
|
| 360 | + 'default' => null |
|
| 361 | + ]; |
|
| 362 | + |
|
| 363 | + return parent::getOptionParser()->setDescription($this->getDescription()) |
|
| 364 | + ->addSubcommand('clean', [ |
|
| 365 | + 'help' => 'Remove old jobs (cleanup)', |
|
| 366 | + 'parser' => $subcommandParser |
|
| 367 | + ]) |
|
| 368 | + ->addSubcommand('add', [ |
|
| 369 | + 'help' => 'Add Job', |
|
| 370 | + 'parser' => $subcommandParser |
|
| 371 | + ]) |
|
| 372 | + ->addSubcommand('stats', [ |
|
| 373 | + 'help' => 'Stats', |
|
| 374 | + 'parser' => $subcommandParserFull |
|
| 375 | + ]) |
|
| 376 | + ->addSubcommand('settings', [ |
|
| 377 | + 'help' => 'Settings', |
|
| 378 | + 'parser' => $subcommandParserFull |
|
| 379 | + ]) |
|
| 380 | + ->addSubcommand('runworker', [ |
|
| 381 | + 'help' => 'Run Worker', |
|
| 382 | + 'parser' => $subcommandParserFull |
|
| 383 | + ]); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Timestamped log. |
|
| 388 | + * |
|
| 389 | + * @param string $message Log type |
|
| 390 | + * @param string|null $pid PID of the process |
|
| 391 | + * @param bool $addDetails Details |
|
| 392 | + * @return void |
|
| 393 | + */ |
|
| 394 | + protected function _log($message, $pid = null, $addDetails = true) |
|
| 395 | + { |
|
| 396 | + if (!Configure::read('Queue.log')) { |
|
| 397 | + return; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + if ($addDetails) { |
|
| 401 | + $timeNeeded = $this->_timeNeeded(); |
|
| 402 | + $memoryUsage = $this->_memoryUsage(); |
|
| 403 | + $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + if ($pid) { |
|
| 407 | + $message .= ' (pid ' . $pid . ')'; |
|
| 408 | + } |
|
| 409 | + Log::write('info', $message, [ |
|
| 410 | + 'scope' => 'queue' |
|
| 411 | + ]); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * |
|
| 416 | + * @param string $message Message |
|
| 417 | + * @param string|null $pid PID of the process |
|
| 418 | + * @return void |
|
| 419 | + */ |
|
| 420 | + protected function _logError($message, $pid = null) |
|
| 421 | + { |
|
| 422 | + $timeNeeded = $this->_timeNeeded(); |
|
| 423 | + $memoryUsage = $this->_memoryUsage(); |
|
| 424 | + $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
| 425 | + |
|
| 426 | + if ($pid) { |
|
| 427 | + $message .= ' (pid ' . $pid . ')'; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + Log::write('error', $message); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Returns a List of available QueueTasks and their individual configurations. |
|
| 435 | + * |
|
| 436 | + * @return array |
|
| 437 | + */ |
|
| 438 | + protected function _getTaskConf() |
|
| 439 | + { |
|
| 440 | + if (!is_array($this->_taskConf)) { |
|
| 441 | + $this->_taskConf = []; |
|
| 442 | + foreach ($this->tasks as $task) { |
|
| 443 | + list ($pluginName, $taskName) = pluginSplit($task); |
|
| 444 | + |
|
| 445 | + $this->_taskConf[$taskName]['name'] = substr($taskName, 5); |
|
| 446 | + $this->_taskConf[$taskName]['plugin'] = $pluginName; |
|
| 447 | + if (property_exists($this->{$taskName}, 'timeout')) { |
|
| 448 | + $this->_taskConf[$taskName]['timeout'] = $this->{$taskName}->timeout; |
|
| 449 | + } else { |
|
| 450 | + $this->_taskConf[$taskName]['timeout'] = Config::defaultWorkerTimeout(); |
|
| 451 | + } |
|
| 452 | + if (property_exists($this->{$taskName}, 'retries')) { |
|
| 453 | + $this->_taskConf[$taskName]['retries'] = $this->{$taskName}->retries; |
|
| 454 | + } else { |
|
| 455 | + $this->_taskConf[$taskName]['retries'] = Config::defaultWorkerRetries(); |
|
| 456 | + } |
|
| 457 | + if (property_exists($this->{$taskName}, 'cleanupTimeout')) { |
|
| 458 | + $this->_taskConf[$taskName]['cleanupTimeout'] = $this->{$taskName}->cleanupTimeout; |
|
| 459 | + } else { |
|
| 460 | + $this->_taskConf[$taskName]['cleanupTimeout'] = Config::cleanupTimeout(); |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + return $this->_taskConf; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Signal handling to queue worker for clean shutdown |
|
| 470 | + * |
|
| 471 | + * @param int $signal The signal |
|
| 472 | + * @return void |
|
| 473 | + */ |
|
| 474 | + protected function _exit($signal) |
|
| 475 | + { |
|
| 476 | + $this->out(__d('queue', 'Caught signal {0}, exiting.', $signal)); |
|
| 477 | + $this->_exit = true; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * |
|
| 482 | + * @return void |
|
| 483 | + */ |
|
| 484 | + protected function _displayAvailableTasks() |
|
| 485 | + { |
|
| 486 | + $this->out('Available Tasks:'); |
|
| 487 | + foreach ($this->taskNames as $loadedTask) { |
|
| 488 | + $this->out("\t" . '* ' . $this->_taskName($loadedTask)); |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * |
|
| 494 | + * @return string Memory usage in MB. |
|
| 495 | + */ |
|
| 496 | + protected function _memoryUsage() |
|
| 497 | + { |
|
| 498 | + $limit = ini_get('memory_limit'); |
|
| 499 | + |
|
| 500 | + $used = number_format(memory_get_peak_usage(true) / (1024 * 1024), 0) . 'MB'; |
|
| 501 | + if ($limit !== '-1') { |
|
| 502 | + $used .= '/' . $limit; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + return $used; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * |
|
| 510 | + * @return string |
|
| 511 | + */ |
|
| 512 | + protected function _timeNeeded() |
|
| 513 | + { |
|
| 514 | + $diff = $this->_time() - $this->_time($this->_time); |
|
| 515 | + $seconds = max($diff, 1); |
|
| 516 | + |
|
| 517 | + return $seconds . 's'; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * |
|
| 522 | + * @param int|null $providedTime Provided time |
|
| 523 | + * |
|
| 524 | + * @return int |
|
| 525 | + */ |
|
| 526 | + protected function _time($providedTime = null) |
|
| 527 | + { |
|
| 528 | + if ($providedTime !== null) { |
|
| 529 | + return $providedTime; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + return time(); |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * |
|
| 537 | + * @param string|null $param String to convert |
|
| 538 | + * @return array |
|
| 539 | + */ |
|
| 540 | + protected function _stringToArray($param) |
|
| 541 | + { |
|
| 542 | + if (!$param) { |
|
| 543 | + return []; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + $array = Text::tokenize($param); |
|
| 547 | + |
|
| 548 | + return array_filter($array); |
|
| 549 | + } |
|
| 550 | 550 | } |