-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciSubscriber.php
49 lines (39 loc) · 1.12 KB
/
FibonacciSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
require_once 'Base.php';
class FibonacciSubscriber extends Base
{
private $timeout;
private $redis;
public function __construct($timeout = 1000)
{
parent::__construct();
$this->timeout = $timeout;
$this->redis = $this->getRedis();
$this->subscriber();
}
public function subscriber()
{
echo "Fibonacci numbers Subscriber started...\n";
if ($this->redis) {
$i = 0;
echo "Reading from channel \"Fibonacci\"\n";
while (true) {
$curNumber = $this->redis->lpop('fib');
if ($curNumber || $curNumber === '0') {
var_dump($curNumber);
++$i;
$this->updateCount('count_fib', $i);
$this->updateSum($curNumber);
}
usleep($this->timeout * 1000);
}
echo "\n";
}
}
}
// "t" stands for Timeout
$options = getopt('t:');
if (!isset($options['t'])) {
exit("Usage: php <filename> -t [int]\n");
}
$generator = new FibonacciSubscriber((int)$options['t']);