|
9 | 9 | require(__DIR__ . '/../vendor/autoload.php'); |
10 | 10 |
|
11 | 11 | use Clue\React\Redis\Factory as RedisFactory; |
12 | | -use React\EventLoop\Factory as LoopFactory; |
| 12 | +use React\EventLoop\Loop; |
13 | 13 | use React\Promise\PromiseInterface; |
14 | 14 | use RTCKit\React\Redlock\Custodian; |
15 | 15 | use RTCKit\React\Redlock\Lock; |
|
22 | 22 | } |
23 | 23 |
|
24 | 24 | /* Instantiate prerequisites */ |
25 | | -$loop = LoopFactory::create(); |
26 | | -$factory = new RedisFactory($loop); |
| 25 | +$factory = new RedisFactory(Loop::get()); |
27 | 26 | $client = $factory->createLazyClient($host); |
28 | 27 |
|
29 | 28 | /* Instantiate our lock custodian */ |
30 | | -$custodian = new Custodian($loop, $client); |
| 29 | +$custodian = new Custodian($client); |
31 | 30 |
|
32 | 31 | /* First routine to acquire the lock */ |
33 | | -$loop->addTimer(0.001, function () use ($custodian): void { |
| 32 | +Loop::addTimer(0.001, function () use ($custodian): void { |
34 | 33 | /* Lock '02-spin' for 5 seconds */ |
35 | 34 | $custodian->acquire('02-spin', 5) |
36 | 35 | ->then(function (?Lock $lock): void { |
|
46 | 45 | }); |
47 | 46 |
|
48 | 47 | /* Hopeful routine, attempting to eventually acquire the lock */ |
49 | | -$loop->addTimer(1, function ($timer) use ($custodian, $loop): void { |
| 48 | +Loop::addTimer(1, function ($timer) use ($custodian): void { |
50 | 49 | /* Attempt to lock '02-spin', trying up to 7 times with a one second pause between retries. |
51 | 50 | Once secured, hold the lock for 10 seconds. */ |
52 | 51 | $custodian->spin(7, 1, '02-spin', 10) |
|
60 | 59 | ->otherwise(function (Throwable $t): void { |
61 | 60 | echo "[hopeful] Something bad happened:" . PHP_EOL . " > " . $t->getMessage() . PHP_EOL; |
62 | 61 | }) |
63 | | - ->always(function() use ($loop): void { |
64 | | - $loop->stop(); |
| 62 | + ->always(function(): void { |
| 63 | + Loop::stop(); |
65 | 64 |
|
66 | 65 | echo "Bye!" . PHP_EOL; |
67 | 66 | }); |
68 | 67 | }); |
69 | | - |
70 | | -$loop->run(); |
0 commit comments