Skip to content

Commit 1777518

Browse files
committed
Create commands with simple examples of locks
1 parent 96e8018 commit 1777518

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

cli.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,40 @@
1212
use Monolog\Logger;
1313
use Monolog\Handler\StreamHandler;
1414
use LockExamples\Cli\Application as LockApplication;
15+
use LockExamples\Resource\UnsafeSharedResource;
16+
use Simple\SHM\Block;
1517

1618
$app = new LockApplication();
1719

1820
$app->addStore('flock', new FlockStore(sys_get_temp_dir()));
1921

20-
$app->command('simple:nolock [--test]', function (OutputInterface $output, Factory $factory, $input) {
21-
$output->writeln('Hello world');
22+
$app->command('simple:nolock', function (OutputInterface $output, Factory $factory) {
23+
$resource = new UnsafeSharedResource('simple:lock');
24+
$resource->reset();
25+
26+
do {
27+
$counter = $resource->read();
28+
$output->writeln($counter);
29+
$resource->write(++$counter);
30+
} while(true);
31+
});
32+
33+
$app->command('simple:lock', function (OutputInterface $output, Factory $factory) {
34+
$resource = new UnsafeSharedResource('simple:lock');
35+
$resource->reset();
36+
37+
$lock = $factory->createLock('simple:lock');
38+
39+
do {
40+
$lock->acquire(true);
41+
try {
42+
$counter = $resource->read();
43+
$output->writeln($counter);
44+
$resource->write(++$counter);
45+
} finally {
46+
$lock->release();
47+
}
48+
} while(true);
2249
});
2350

2451

pm2/simple-lock.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apps:
2+
- name : simple:lock
3+
script : cli.php
4+
args :
5+
- simple:lock
6+
- flock
7+
instances : 4
8+
exec_mode : fork
9+
cwd : /app
10+
interpreter : /usr/bin/php

pm2/simple-nolock.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apps:
2+
- name : simple:nolock
3+
script : cli.php
4+
args :
5+
- simple:nolock
6+
- flock
7+
instances : 4
8+
exec_mode : fork
9+
cwd : /app
10+
interpreter : /usr/bin/php

0 commit comments

Comments
 (0)