Skip to content

Commit 6da80c4

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-1311: Add "disable_locking" parameter (#97)
1 parent 47d8697 commit 6da80c4

File tree

3 files changed

+134
-8
lines changed

3 files changed

+134
-8
lines changed

src/Config/Environment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Environment
3232
const DEFAULT_ADMIN_FIRSTNAME = 'Admin';
3333
const DEFAULT_ADMIN_LASTNAME = 'Username';
3434

35+
const VAR_REDIS_SESSION_DISABLE_LOCKING = 'REDIS_SESSION_DISABLE_LOCKING';
3536
/**
3637
* Let's keep variable names same for both phases.
3738
*/

src/Process/Deploy/InstallUpdate/ConfigUpdate/Redis.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ public function execute()
8080
? $cacheConfig
8181
: array_replace_recursive($config['cache'], $cacheConfig);
8282

83+
$redisSessionConfig = [
84+
'host' => $redisConfig[0]['host'],
85+
'port' => $redisConfig[0]['port'],
86+
'database' => 0,
87+
'disable_locking' => (int)$this->isLockingDisabled()
88+
];
8389
$config['session'] = [
8490
'save' => 'redis',
85-
'redis' => [
86-
'host' => $redisConfig[0]['host'],
87-
'port' => $redisConfig[0]['port'],
88-
'database' => 0,
89-
],
91+
'redis' => array_replace_recursive(
92+
$config['session']['redis'] ?? [],
93+
$redisSessionConfig
94+
)
9095
];
9196
} else {
9297
$config = $this->removeRedisConfiguration($config);
@@ -122,4 +127,18 @@ private function removeRedisConfiguration($config)
122127

123128
return $config;
124129
}
130+
131+
/**
132+
* Checks if disable_locking options is enabled.
133+
* By default this method returns true and disable_locking options will be set to 1.
134+
* For turning this option off environment variable 'REDIS_SESSION_DISABLE_LOCKING' should have value 'disabled'.
135+
*
136+
* @return bool
137+
*/
138+
private function isLockingDisabled(): bool
139+
{
140+
$envValue = $this->environment->getVariable(Environment::VAR_REDIS_SESSION_DISABLE_LOCKING);
141+
142+
return $envValue !== Environment::VAL_DISABLED;
143+
}
125144
}

src/Test/Unit/Process/Deploy/InstallUpdate/ConfigUpdate/RedisTest.php

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class RedisTest extends TestCase
4949
protected function setUp()
5050
{
5151
$this->environmentMock = $this->getMockBuilder(Environment::class)
52-
->setMethods(['getRelationships', 'getAdminUrl'])
52+
->setMethods(['getRelationships', 'getAdminUrl', 'getVariable'])
5353
->disableOriginalConstructor()
5454
->getMock();
5555
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
@@ -65,7 +65,12 @@ protected function setUp()
6565
);
6666
}
6767

68-
public function testExecute()
68+
/**
69+
* @param $envSessionLocking
70+
* @param int $expectedDisableLocking
71+
* @dataProvider executeDataProvider
72+
*/
73+
public function testExecute($envSessionLocking, int $expectedDisableLocking)
6974
{
7075
$this->loggerMock->expects($this->once())
7176
->method('info')
@@ -83,7 +88,10 @@ public function testExecute()
8388
$this->environmentMock->expects($this->any())
8489
->method('getAdminUrl')
8590
->willReturn('admin');
86-
91+
$this->environmentMock->expects($this->once())
92+
->method('getVariable')
93+
->with(Environment::VAR_REDIS_SESSION_DISABLE_LOCKING)
94+
->willReturn($envSessionLocking);
8795
$this->configReaderMock->expects($this->once())
8896
->method('read')
8997
->willReturn([]);
@@ -117,13 +125,32 @@ public function testExecute()
117125
'host' => '127.0.0.1',
118126
'port' => '6379',
119127
'database' => 0,
128+
'disable_locking' => $expectedDisableLocking
120129
],
121130
],
122131
]);
123132

124133
$this->process->execute();
125134
}
126135

136+
public function executeDataProvider()
137+
{
138+
return [
139+
[
140+
'',
141+
1
142+
],
143+
[
144+
Environment::VAL_DISABLED,
145+
0
146+
],
147+
[
148+
Environment::VAL_ENABLED,
149+
1
150+
]
151+
];
152+
}
153+
127154
public function testExecuteRemovingRedis()
128155
{
129156
$this->loggerMock->expects($this->once())
@@ -182,4 +209,83 @@ public function testExecuteRemovingRedis()
182209

183210
$this->process->execute();
184211
}
212+
213+
214+
public function testExecuteWithDifferentRedisOptions()
215+
{
216+
$this->loggerMock->expects($this->once())
217+
->method('info')
218+
->with('Updating env.php Redis cache configuration.');
219+
$this->environmentMock->expects($this->any())
220+
->method('getRelationships')
221+
->willReturn([
222+
'redis' => [
223+
0 => [
224+
'host' => '127.0.0.1',
225+
'port' => '6379'
226+
]
227+
],
228+
]);
229+
$this->environmentMock->expects($this->any())
230+
->method('getAdminUrl')
231+
->willReturn('admin');
232+
$this->environmentMock->expects($this->once())
233+
->method('getVariable')
234+
->with(Environment::VAR_REDIS_SESSION_DISABLE_LOCKING)
235+
->willReturn('');
236+
$this->configReaderMock->expects($this->once())
237+
->method('read')
238+
->willReturn([
239+
'session' => [
240+
'redis' => [
241+
'max_concurrency' => 10,
242+
'bot_first_lifetime' => 100,
243+
'bot_lifetime' => 10000,
244+
'min_lifetime' => 100,
245+
'max_lifetime' => 10000
246+
]
247+
]
248+
]);
249+
250+
$this->configWriterMock->expects($this->once())
251+
->method('write')
252+
->with([
253+
'cache' => [
254+
'frontend' => [
255+
'default' => [
256+
'backend' => 'Cm_Cache_Backend_Redis',
257+
'backend_options' => [
258+
'server' => '127.0.0.1',
259+
'port' => '6379',
260+
'database' => 1,
261+
],
262+
],
263+
'page_cache' => [
264+
'backend' => 'Cm_Cache_Backend_Redis',
265+
'backend_options' => [
266+
'server' => '127.0.0.1',
267+
'port' => '6379',
268+
'database' => 1,
269+
],
270+
],
271+
],
272+
],
273+
'session' => [
274+
'save' => 'redis',
275+
'redis' => [
276+
'host' => '127.0.0.1',
277+
'port' => '6379',
278+
'database' => 0,
279+
'disable_locking' => 1,
280+
'max_concurrency' => 10,
281+
'bot_first_lifetime' => 100,
282+
'bot_lifetime' => 10000,
283+
'min_lifetime' => 100,
284+
'max_lifetime' => 10000
285+
],
286+
],
287+
]);
288+
289+
$this->process->execute();
290+
}
185291
}

0 commit comments

Comments
 (0)