File tree Expand file tree Collapse file tree 7 files changed +57
-16
lines changed Expand file tree Collapse file tree 7 files changed +57
-16
lines changed Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change @@ -27,13 +27,15 @@ use Interop\Queue\Processor;
2727 */
2828class {{ 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)
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1212 */
1313class 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 *
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1212 */
1313class 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 *
You can’t perform that action at this time.
0 commit comments