Skip to content

Commit 9d1eeae

Browse files
Adds Job::setMaxAttempts and dispatch helper function (#5950)
Co-authored-by: 李铭昕 <[email protected]>
1 parent 1f1b583 commit 9d1eeae

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"hyperf/process": "Auto register the consumer process for server."
3333
},
3434
"autoload": {
35+
"files": [
36+
"src/Functions.php"
37+
],
3538
"psr-4": {
3639
"Hyperf\\AsyncQueue\\": "src/"
3740
}

src/Functions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\AsyncQueue;
13+
14+
use Hyperf\AsyncQueue\Driver\DriverFactory;
15+
use Hyperf\Context\ApplicationContext;
16+
17+
function dispatch(JobInterface $job, ?int $delay = null, ?int $maxAttempts = null, ?string $pool = null): bool
18+
{
19+
if (is_int($maxAttempts)) {
20+
$job->setMaxAttempts($maxAttempts);
21+
}
22+
23+
return ApplicationContext::getContainer()
24+
->get(DriverFactory::class)
25+
->get($pool ?? 'default')
26+
->push($job, $delay ?? 0);
27+
}

src/Job.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ abstract class Job implements JobInterface, CompressInterface, UnCompressInterfa
1818
{
1919
protected int $maxAttempts = 0;
2020

21+
public function setMaxAttempts(int $maxAttempts): static
22+
{
23+
$this->maxAttempts = $maxAttempts;
24+
25+
return $this;
26+
}
27+
2128
public function getMaxAttempts(): int
2229
{
2330
return $this->maxAttempts;

src/JobInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ interface JobInterface
1818
*/
1919
public function handle();
2020

21+
public function setMaxAttempts(int $maxAttempts): static;
22+
2123
public function getMaxAttempts(): int;
2224
}

0 commit comments

Comments
 (0)