Skip to content

Commit

Permalink
config: autoAckBeforeJobStart set true value by default
Browse files Browse the repository at this point in the history
  • Loading branch information
kcloze committed Aug 18, 2019
1 parent 82530aa commit 2c38bcb
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions config.php
Expand Up @@ -35,8 +35,8 @@
'minTime'=> 0.0001, //单个job最少执行时间
],
'topics' => [
//'autoAckBeforeJobStart' => true, // true 开启; false 关闭;job没跑之前是否直接ack,这样业务代码里面有exit、die等致命错误会丢弃消息,防止消息积压
['name'=>'MyJob', 'workerMinNum'=>1, 'workerMaxNum'=>3, 'queueMaxNum'=>10000, 'queueMaxNumForProcess' => 100, 'autoAckBeforeJobStart'=>false],
//'autoAckBeforeJobStart' => true, // true 开启; false 关闭;默认为true,job没跑之前是否直接ack,这样业务代码里面有exit、die等致命错误会丢弃消息,防止消息积压
['name'=>'MyJob', 'workerMinNum'=>1, 'workerMaxNum'=>3, 'queueMaxNum'=>10000, 'queueMaxNumForProcess' => 100, 'autoAckBeforeJobStart'=>true],
['name'=> 'MyJob2', 'workerMinNum'=>1, 'workerMaxNum'=>3, 'autoAckBeforeJobStart'=>true],
['name'=> 'MyJob3', 'workerMinNum'=>1, 'workerMaxNum'=>1],
['name'=> 'DefaultClassMethod.test1', 'workerMinNum'=>1, 'workerMaxNum'=>2, 'defaultJobClass'=>'DefaultClassMethod', 'defaultJobMethod'=>'test1'],
Expand Down
5 changes: 2 additions & 3 deletions src/Config.php
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of PHP CS Fixer.
* This file is part of Swoole-jobs
* (c) kcloze <pei.greet@qq.com>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down Expand Up @@ -36,11 +36,10 @@ public static function getConfig()
* @param mixed $topic
* @param mixed $name
* */

public static function getTopicConfig($config, $topic, $name)
{
$key=array_search($topic, array_column($config, 'name'), true);

return $config[$key][$name] ?? false;
return $config[$key][$name] ?? true;
}
}
2 changes: 1 addition & 1 deletion src/Jobs.php
Expand Up @@ -75,7 +75,7 @@ public function run($topic='')
}

$this->logger->log('pop data: ' . json_encode($data), 'info');
$autoAckBeforeJobStart=Config::getTopicConfig($this->config['job']['topics'], $topic, 'autoAckBeforeJobStart');
$autoAckBeforeJobStart=Config::getTopicConfig($this->config['job']['topics'], $topic, 'autoAckBeforeJobStart') ?? true;
if (true === $autoAckBeforeJobStart) {
$this->queue->ack();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/MyJob.php
Expand Up @@ -17,7 +17,7 @@ public static function test1($a, $b)
// $client = new \GuzzleHttp\Client();
// $res = $client->request('GET', 'https://www.oschina.net/', ['timeout' => 3]);
// echo $res->getStatusCode() . ' test1| title: ' . $a . ' time: ' . $b . PHP_EOL;
die('oh,my gad!');
die('oh,my gad!').PHP_EOL;//模拟job异常退出,如果设置autoAckBeforeJobStart为false,则消息出队列之后会回到队列,消息不丢失
echo ' test1| title: ' . $a . ' time: ' . $b . PHP_EOL;
}

Expand Down

0 comments on commit 2c38bcb

Please sign in to comment.