1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Examples |
4
|
|
|
* @subpackage ExampleGibson |
5
|
|
|
* |
6
|
|
|
* @author Vasily Zorin <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace PHPDaemon\Examples; |
9
|
|
|
|
10
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ExampleGibson |
14
|
|
|
* @package PHPDaemon\Applications |
15
|
|
|
* For testing gideros functionality |
16
|
|
|
*/ |
17
|
|
|
class ExampleGibson extends \PHPDaemon\Core\AppInstance |
18
|
|
|
{ |
19
|
|
|
public $gibson; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Called when the worker is ready to go. |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function onReady() |
26
|
|
|
{ |
27
|
|
|
$this->gibson = \PHPDaemon\Clients\Gibson\Pool::getInstance(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates Request. |
32
|
|
|
* @param object Request. |
33
|
|
|
* @param object Upstream application instance. |
34
|
|
|
* @return ExampleGibsonRequest Request. |
35
|
|
|
*/ |
36
|
|
|
public function beginRequest($req, $upstream) |
37
|
|
|
{ |
38
|
|
|
return new ExampleGibsonRequest($this, $upstream, $req); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
class ExampleGibsonRequest extends Generic |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
public $job; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function init() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$job = $this->job = new \PHPDaemon\Core\ComplexJob(function ($job) { // called when job is done |
53
|
|
|
$this->wakeup(); // wake up the request immediately |
54
|
|
|
$job->keep(); // prevent from cleaning |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
if (isset($_GET['fill'])) { |
58
|
|
|
for ($i = 0; $i < 100; ++$i) { |
59
|
|
|
$this->appInstance->gibson->set(3600, 'key' . $i, 'val' . $i); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$job('testquery', function ($jobname, $job) { // registering job named 'testquery' |
64
|
|
|
$this->appInstance->gibson->mget('key99', function ($conn) use ($job, $jobname) { |
|
|
|
|
65
|
|
|
if ($conn->isFinal()) { |
66
|
|
|
$job->setResult($jobname, $conn->result); |
67
|
|
|
} |
68
|
|
|
}); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
$job(); // let the fun begin |
72
|
|
|
|
73
|
|
|
$this->sleep(1, true); // setting timeout*/ |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Called when request iterated. |
78
|
|
|
* @return integer Status. |
|
|
|
|
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function run() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
$this->header('Content-Type: text/html'); |
84
|
|
|
} catch (\Exception $e) { |
|
|
|
|
85
|
|
|
} |
86
|
|
|
?> |
87
|
|
|
<!DOCTYPE html> |
88
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
89
|
|
|
<head> |
90
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
91
|
|
|
<title>Example with Gibson</title> |
92
|
|
|
</head> |
93
|
|
|
<body> |
94
|
|
|
<?php |
95
|
|
|
if ($r = $this->job->getResult('testquery')) { |
96
|
|
|
echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>'; |
97
|
|
|
var_dump($r); |
|
|
|
|
98
|
|
|
echo '</pre>'; |
99
|
|
|
} else { |
100
|
|
|
echo '<h1>Something went wrong! We have no result...</h1>'; |
101
|
|
|
} |
102
|
|
|
echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6); |
103
|
|
|
?> |
104
|
|
|
</body> |
105
|
|
|
</html> |
106
|
|
|
<?php |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.