From a349d419b6603efec4dcfb65fd00bebe19b1a41a Mon Sep 17 00:00:00 2001 From: vagrant Date: Tue, 26 Jan 2016 16:07:39 +0000 Subject: [PATCH] Adding priority field to queued taskes, using cakeDC migrations to add new column --- ...0_adding_priority_field_to_queue_tasks.php | 60 +++++++++++++++++++ Model/QueuedTask.php | 13 +++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 Config/Migration/1453822680_adding_priority_field_to_queue_tasks.php diff --git a/Config/Migration/1453822680_adding_priority_field_to_queue_tasks.php b/Config/Migration/1453822680_adding_priority_field_to_queue_tasks.php new file mode 100644 index 00000000..0226a984 --- /dev/null +++ b/Config/Migration/1453822680_adding_priority_field_to_queue_tasks.php @@ -0,0 +1,60 @@ + array( + 'create_field' => [ + 'queued_tasks' => [ + 'priority' => [ + 'type' => 'integer', + 'null' => false, + 'default' => 5, + 'length' => 4 + ], + 'indexes' => array( + 'priority' => array('column' => 'priority'), + ), + ], + ] + ), + 'down' => array( + 'drop_field' => [ + 'queued_tasks' => [ + 'priority' + ], + ] + ), + ); + +/** + * Before migration callback + * + * @param string $direction Direction of migration process (up or down) + * @return bool Should process continue + */ + public function before($direction) { + return true; + } + +/** + * After migration callback + * + * @param string $direction Direction of migration process (up or down) + * @return bool Should process continue + */ + public function after($direction) { + return true; + } +} diff --git a/Model/QueuedTask.php b/Model/QueuedTask.php index b1c44daa..ae8a9150 100644 --- a/Model/QueuedTask.php +++ b/Model/QueuedTask.php @@ -11,6 +11,8 @@ */ class QueuedTask extends QueueAppModel { + public $_next_priority = 5; + public $rateHistory = []; public $exit = false; @@ -61,6 +63,12 @@ public function initConfig() { Configure::write('Queue', $conf); } + public function nextPriority($priority) + { + $this->_next_priority = $priority; + return $this; + } + /** * Add a new Job to the Queue. * @@ -76,8 +84,10 @@ public function createJob($jobName, $data = null, $notBefore = null, $group = nu 'jobtype' => $jobName, 'data' => serialize($data), 'group' => $group, - 'reference' => $reference + 'reference' => $reference, + 'priority' => $this->_next_priority ]; + $this->_next_priority = 5; // reset to default; if ($notBefore !== null) { $data['notbefore'] = date('Y-m-d H:i:s', strtotime($notBefore)); } @@ -119,6 +129,7 @@ public function requestJob($capabilities, $group = null) { 'age', ], 'order' => [ + 'priority ASC', 'age ASC', 'id ASC' ],