|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author MGriesbach@gmail.com |
| 4 | + * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
| 5 | + * @link http://github.com/MSeven/cakephp_queue |
| 6 | + */ |
| 7 | +App::uses('AppShell', 'Console/Command'); |
| 8 | + |
| 9 | +/** |
| 10 | + * A Simple QueueTask example. |
| 11 | + * |
| 12 | + */ |
| 13 | +class QueueRetryExampleTask extends AppShell { |
| 14 | + |
| 15 | +/** |
| 16 | + * Adding the QueueTask Model |
| 17 | + * |
| 18 | + * @var array |
| 19 | + */ |
| 20 | + public $uses = array( |
| 21 | + 'Queue.QueuedTask' |
| 22 | + ); |
| 23 | + |
| 24 | +/** |
| 25 | + * ZendStudio Codecomplete Hint |
| 26 | + * |
| 27 | + * @var QueuedTask |
| 28 | + */ |
| 29 | + public $QueuedTask; |
| 30 | + |
| 31 | +/** |
| 32 | + * Timeout for run, after which the Task is reassigned to a new worker. |
| 33 | + * |
| 34 | + * @var int |
| 35 | + */ |
| 36 | + public $timeout = 10; |
| 37 | + |
| 38 | +/** |
| 39 | + * Number of times a failed instance of this task should be restarted before giving up. |
| 40 | + * |
| 41 | + * @var int |
| 42 | + */ |
| 43 | + public $retries = 5; |
| 44 | + |
| 45 | +/** |
| 46 | + * Stores any failure messages triggered during run() |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + public $failureMessage = ''; |
| 51 | + |
| 52 | +/** |
| 53 | + * @var bool |
| 54 | + */ |
| 55 | + public $autoUnserialize = true; |
| 56 | + |
| 57 | +/** |
| 58 | + * Constructs this Shell instance. |
| 59 | + * |
| 60 | + * @param ConsoleOutput $stdout A ConsoleOutput object for stdout. |
| 61 | + * @param ConsoleOutput $stderr A ConsoleOutput object for stderr. |
| 62 | + * @param ConsoleInput $stdin A ConsoleInput object for stdin. |
| 63 | + */ |
| 64 | + public function __construct($stdout = null, $stderr = null, $stdin = null) { |
| 65 | + parent::__construct($stdout, $stderr, $stdin); |
| 66 | + |
| 67 | + $this->file = TMP . 'task_retry.txt'; |
| 68 | + } |
| 69 | + |
| 70 | +/** |
| 71 | + * Example add functionality. |
| 72 | + * Will create one example job in the queue, which later will be executed using run(); |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function add() { |
| 77 | + $this->out('CakePHP Queue Retry Example task.'); |
| 78 | + $this->hr(); |
| 79 | + $this->out('This is a very simple example of a QueueTask and how retries work.'); |
| 80 | + $this->out('I will now add an example Job into the Queue.'); |
| 81 | + $this->out('This job will only produce some console output on the worker that it runs on.'); |
| 82 | + $this->out(' '); |
| 83 | + $this->out('To run a Worker use:'); |
| 84 | + $this->out(' cake Queue.Queue runworker'); |
| 85 | + $this->out(' '); |
| 86 | + $this->out('You can find the sourcecode of this task in: '); |
| 87 | + $this->out(__FILE__); |
| 88 | + $this->out(' '); |
| 89 | + |
| 90 | + file_put_contents($this->file, '0'); |
| 91 | + |
| 92 | + /* |
| 93 | + * Adding a task of type 'example' with no additionally passed data |
| 94 | + */ |
| 95 | + if ($this->QueuedTask->createJob('RetryExample', null)) { |
| 96 | + $this->out('OK, job created, now run the worker'); |
| 97 | + } else { |
| 98 | + $this->err('Could not create Job'); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +/** |
| 103 | + * Example run function. |
| 104 | + * This function is executed, when a worker is executing a task. |
| 105 | + * The return parameter will determine, if the task will be marked completed, or be requeued. |
| 106 | + * |
| 107 | + * @param array $data The array passed to QueuedTask->createJob() |
| 108 | + * @return bool Success |
| 109 | + */ |
| 110 | + public function run($data) { |
| 111 | + $count = (int)file_get_contents($this->file); |
| 112 | + |
| 113 | + $this->hr(); |
| 114 | + $this->out('CakePHP Queue Example task.'); |
| 115 | + $this->hr(); |
| 116 | + |
| 117 | + if ($count < 3) { |
| 118 | + $count++; |
| 119 | + file_put_contents($this->file, (string)$count); |
| 120 | + $this->out(' ->Sry, the Retry Example Job failed. Try again.<-'); |
| 121 | + $this->out(' '); |
| 122 | + $this->out(' '); |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + $this->out(' ->Success, the Retry Example Job was run.<-'); |
| 127 | + $this->out(' '); |
| 128 | + $this->out(' '); |
| 129 | + |
| 130 | + unlink($this->file); |
| 131 | + return true; |
| 132 | + } |
| 133 | +} |
0 commit comments