Skip to content

Commit f8eec00

Browse files
author
euromark
committed
Add test for requeue
1 parent e3d1836 commit f8eec00

File tree

4 files changed

+162
-11
lines changed

4 files changed

+162
-11
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ language: php
33
php:
44
- 5.3
55
- 5.4
6+
- 5.5
7+
- 5.6
68

79
env:
810
- CAKE_VERSION=master
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# CakePHP Queue Plugin
2+
[![Build Status](https://api.travis-ci.org/dereuromark/cakephp-queue.png)](https://travis-ci.org/dereuromark/cakephp-queue)
3+
[![License](https://poser.pugx.org/dereuromark/cakephp-queue/license.png)](https://packagist.org/packages/dereuromark/cakephp-queue)
4+
[![Total Downloads](https://poser.pugx.org/dereuromark/cakephp-queue/d/total.png)](https://packagist.org/packages/dereuromark/cakephp-queue)
25

3-
modified by Mark Scherer (dereuromark)
4-
- cake2.x support
5-
- some minor fixes
6-
- added crontasks (as a different approach on specific problems)
7-
- possible Tools Plugin dependencies for frontend access via /admin/queue
6+
Modified by Mark Scherer (dereuromark)
7+
- CakePHP2.x support
8+
- Some minor fixes
9+
- Added crontasks (as a different approach on specific problems)
10+
- Possible (optional) Tools Plugin dependencies for frontend access via /admin/queue
811

9-
new:
10-
- config key "queue" is now "Queue" ($config['Queue'][...])
12+
New:
13+
- Config key "queue" is now "Queue" ($config['Queue'][...])
1114

1215

1316
## Background:
@@ -35,13 +38,11 @@ The Queue Plugin provides a simple method to create and run such non-user-intera
3538
While you can run multiple workers, and can (to some extend) spread these workers to different machines via a shared database,
3639
you should seriously consider using a more advanced system for high volume/high number of workers systems.
3740

38-
### Status
39-
[![Build Status](https://api.travis-ci.org/dereuromark/cakephp-queue.png)](https://travis-ci.org/dereuromark/cakephp-queue)
40-
41-
4241
## Installation:
4342

4443
* Copy the files in this directory into APP/Plugin/Queue
44+
Ideally, using composer and `require dereuromark/cakephp-queue`
45+
4546
* Enable the plugin within your APP/Config/bootstrap.php (unless you loadAll)
4647

4748
CakePlugin::load('Queue');

Test/Case/Console/Command/QueueShellTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ public function testAdd() {
9494
$this->assertTrue(in_array('Running Job of type "Example"', $this->QueueShell->out));
9595
}
9696

97+
/**
98+
* QueueShellTest::testRetry()
99+
*
100+
* @return void
101+
*/
102+
public function testRetry() {
103+
$this->QueueShell->args[] = 'RetryExample';
104+
$result = $this->QueueShell->add();
105+
$this->assertEmpty($this->QueueShell->out);
106+
107+
$result = $this->QueueShell->runworker();
108+
//debug($this->QueueShell->out);
109+
$this->assertTrue(in_array('Job did not finish, requeued.', $this->QueueShell->out));
110+
}
111+
97112
}
98113

99114
class TestQueueShell extends QueueShell {

0 commit comments

Comments
 (0)