Skip to content

Commit 297d528

Browse files
authored
Merge pull request #121 from sheldonreiff/make-max-attempts-optional-in-bake-command
Make max attempts optional in bake job command
2 parents 8a0b4c7 + 4dd3951 commit 297d528

File tree

7 files changed

+57
-16
lines changed

7 files changed

+57
-16
lines changed

docs/en/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ A simple job that logs received messages would look like::
118118
use LogTrait;
119119

120120
/**
121-
* The maximum number of times the job may be attempted.
121+
* The maximum number of times the job may be attempted. (optional property)
122122
*
123123
* @var int|null
124124
*/

src/Command/JobCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ public function templateData(Arguments $arguments): array
5555
{
5656
$parentData = parent::templateData($arguments);
5757

58+
$maxAttempts = $arguments->getOption('max-attempts');
59+
5860
$data = [
5961
'isUnique' => $arguments->getOption('unique'),
62+
'maxAttempts' => $maxAttempts ? (int)$maxAttempts : null,
6063
];
6164

6265
return array_merge($parentData, $data);
@@ -77,6 +80,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
7780
->addArgument('name', [
7881
'help' => 'The name of the queue job class to create.',
7982
])
83+
->addOption('max-attempts', [
84+
'help' => 'The maximum number of times the job may be attempted.',
85+
'default' => null,
86+
])
8087
->addOption('unique', [
8188
'help' => 'Whether there should be only one instance of a job on the queue at a time.',
8289
'boolean' => true,

templates/bake/job.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ use Interop\Queue\Processor;
2727
*/
2828
class {{ name }}Job implements JobInterface
2929
{
30+
{% if maxAttempts %}
3031
/**
3132
* The maximum number of times the job may be attempted.
3233
*
3334
* @var int|null
3435
*/
35-
public static $maxAttempts = 3;
36+
public static $maxAttempts = {{ maxAttempts }};
3637
38+
{% endif %}
3739
{% if isUnique %}
3840
/**
3941
* Whether there should be only one instance of a job on the queue at a time. (optional property)

tests/TestCase/Task/JobTaskTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,18 @@ public function testMainWithUnique()
8686
file_get_contents($this->generatedFile)
8787
);
8888
}
89+
90+
public function testMainWithMaxAttempts()
91+
{
92+
$this->generatedFile = APP . 'Job' . DS . 'UploadJob.php';
93+
94+
$this->exec('bake job upload --max-attempts 3');
95+
$this->assertExitCode(Command::CODE_SUCCESS);
96+
$this->assertFileExists($this->generatedFile);
97+
$this->assertOutputContains('Creating file ' . $this->generatedFile);
98+
$this->assertSameAsFile(
99+
$this->comparisonDir . 'JobTaskWithMaxAttempts.php',
100+
file_get_contents($this->generatedFile)
101+
);
102+
}
89103
}

tests/comparisons/JobTask.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
*/
1313
class UploadJob implements JobInterface
1414
{
15-
/**
16-
* The maximum number of times the job may be attempted.
17-
*
18-
* @var int|null
19-
*/
20-
public static $maxAttempts = 3;
21-
2215
/**
2316
* Executes logic for UploadJob
2417
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TestApp\Job;
5+
6+
use Cake\Queue\Job\JobInterface;
7+
use Cake\Queue\Job\Message;
8+
use Interop\Queue\Processor;
9+
10+
/**
11+
* Upload job
12+
*/
13+
class UploadJob implements JobInterface
14+
{
15+
/**
16+
* The maximum number of times the job may be attempted.
17+
*
18+
* @var int|null
19+
*/
20+
public static $maxAttempts = 3;
21+
22+
/**
23+
* Executes logic for UploadJob
24+
*
25+
* @param \Cake\Queue\Job\Message $message job message
26+
* @return string|null
27+
*/
28+
public function execute(Message $message): ?string
29+
{
30+
return Processor::ACK;
31+
}
32+
}

tests/comparisons/JobTaskWithUnique.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
*/
1313
class UploadJob implements JobInterface
1414
{
15-
/**
16-
* The maximum number of times the job may be attempted.
17-
*
18-
* @var int|null
19-
*/
20-
public static $maxAttempts = 3;
21-
2215
/**
2316
* Whether there should be only one instance of a job on the queue at a time. (optional property)
2417
*

0 commit comments

Comments
 (0)