Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 847079f

Browse files
Disable strict mode for transactions (#50)
* disable strict mode for transactions * update docs
1 parent f37f8e5 commit 847079f

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

‎docs/basic-usage.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,52 @@ You may be wondering what the `$this->data['message']` variable is all about. We
7979

8080
Throwing an exception is a way to let the queue worker know that the job has failed.
8181

82+
#### Using transactions
83+
84+
If you have to use transactions in your Job - this is a simple schema you can follow.
85+
86+
!!! note
87+
88+
Due to the nature of the queue worker, [Strict Mode](https://codeigniter.com/user_guide/database/transactions.html#strict-mode) is automatically disabled for the database connection assigned to the Database handler. That's because queue worker is a long-running process, and we don't want one failed transaction to affect others.
89+
90+
If you use the same connection group in your Job as defined in the Database handler, then in that case, you don't need to do anything.
91+
92+
On the other hand, if you are using a different group to connect to the database in your Job, then if you are using transactions, you should disable Strict Mode through the method: `$db->transStrict(false)` or by setting the `transStrict` option to `false` in your connection config group - the last option will disable Strict Mode globally.
93+
94+
```php
95+
// ...
96+
97+
class Email extends BaseJob implements JobInterface
98+
{
99+
/**
100+
* @throws Exception
101+
*/
102+
public function process(string $data):
103+
{
104+
try {
105+
$db = db_connect();
106+
// Disable Strict Mode
107+
$db->transStrict(false);
108+
$db->transBegin();
109+
110+
// Job logic goes here
111+
// Your code should throw an exception on error
112+
113+
if ($db->transStatus() === false) {
114+
$db->transRollback();
115+
} else {
116+
$db->transCommit();
117+
}
118+
} catch (Exception $e) {
119+
$db->transRollback();
120+
throw $e;
121+
}
122+
}
123+
}
124+
```
125+
126+
#### Other options
127+
82128
We can also configure some things on the job level. It's a number of tries, when the job is failing and time after the job will be retried again after failure. We can specify these options by using variables:
83129

84130
```php

‎docs/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ The configuration settings for `database` handler.
3939
* `getShared` - Weather to use shared instance. Default value: `true`.
4040
* `skipLocked` - Weather to use "skip locked" feature to maintain concurrency calls. Default to `true`.
4141

42+
!!! note
43+
44+
The [Strict Mode](https://codeigniter.com/user_guide/database/transactions.html#strict-mode) for the given `dbGroup` is automatically disabled - due to the nature of the queue worker.
45+
4246
### $redis
4347

4448
The configuration settings for `redis` handler. You need to have a [ext-redis](https://github.com/phpredis/phpredis) installed to use it.

‎src/Models/QueueJobFailedModel.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
namespace CodeIgniter\Queue\Models;
1515

16+
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\ConnectionInterface;
1618
use CodeIgniter\Model;
1719
use CodeIgniter\Queue\Entities\QueueJobFailed;
20+
use CodeIgniter\Validation\ValidationInterface;
21+
use Config\Database;
1822

1923
class QueueJobFailedModel extends Model
2024
{
@@ -37,4 +41,19 @@ class QueueJobFailedModel extends Model
3741

3842
// Callbacks
3943
protected $allowCallbacks = false;
44+
45+
public function __construct(?ConnectionInterface $db = null, ?ValidationInterface $validation = null)
46+
{
47+
$this->DBGroup = config('Queue')->database['dbGroup'];
48+
49+
/**
50+
* @var BaseConnection|null $db
51+
*/
52+
$db ??= Database::connect($this->DBGroup);
53+
54+
// Turn off the Strict Mode
55+
$db->transStrict(false);
56+
57+
parent::__construct($db, $validation);
58+
}
4059
}

‎src/Models/QueueJobModel.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
namespace CodeIgniter\Queue\Models;
1515

1616
use CodeIgniter\Database\BaseBuilder;
17+
use CodeIgniter\Database\BaseConnection;
18+
use CodeIgniter\Database\ConnectionInterface;
1719
use CodeIgniter\I18n\Time;
1820
use CodeIgniter\Model;
1921
use CodeIgniter\Queue\Entities\QueueJob;
2022
use CodeIgniter\Queue\Enums\Status;
23+
use CodeIgniter\Validation\ValidationInterface;
24+
use Config\Database;
2125
use ReflectionException;
2226

2327
class QueueJobModel extends Model
@@ -42,6 +46,21 @@ class QueueJobModel extends Model
4246
// Callbacks
4347
protected $allowCallbacks = false;
4448

49+
public function __construct(?ConnectionInterface $db = null, ?ValidationInterface $validation = null)
50+
{
51+
$this->DBGroup = config('Queue')->database['dbGroup'];
52+
53+
/**
54+
* @var BaseConnection|null $db
55+
*/
56+
$db ??= Database::connect($this->DBGroup);
57+
58+
// Turn off the Strict Mode
59+
$db->transStrict(false);
60+
61+
parent::__construct($db, $validation);
62+
}
63+
4564
/**
4665
* Get the oldest item from the queue.
4766
*

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /