Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit 1986efb

Browse files
committed
First commit
1 parent 030086e commit 1986efb

File tree

8 files changed

+557
-0
lines changed

8 files changed

+557
-0
lines changed

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
.DS_Store
3+
._*
4+

‎Master/ImageExampleWorker.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* Class JsonExampleWorker
5+
* @author it-novum GmbH
6+
* @repository https://github.com/it-novum/phpNSTA-Custom-Data-Example.git
7+
* @license MIT <http://opensource.org/licenses/MIT>
8+
*/
9+
10+
$ExampleWorker = new JsonExampleWorker();
11+
12+
echo 'Enter endless loop, press STRG+C to exit' . PHP_EOL;
13+
$ExampleWorker->loop();
14+
15+
class JsonExampleWorker {
16+
17+
/**
18+
* @var GearmanWorker
19+
*/
20+
private $GearmanWorker;
21+
22+
public function __construct() {
23+
$this->GearmanWorker = new GearmanWorker();
24+
$this->GearmanWorker->addServer('127.0.0.1', 4730);
25+
26+
//Set the queue and a callback function for processing the data
27+
//The callback needs to be public!
28+
$this->GearmanWorker->addFunction('phpnsta_custom_data', [$this, 'saveImage']);
29+
}
30+
31+
public function loop() {
32+
while (true) {
33+
// work() will block until there are records in the queue
34+
$this->GearmanWorker->work();
35+
}
36+
}
37+
38+
/**
39+
* @param GearmanJob $job
40+
*/
41+
public function saveImage(GearmanJob $job) {
42+
$image = $job->workload();
43+
$filename = '/tmp/example_output.png';
44+
45+
$file = fopen($filename, 'w+');
46+
fwrite($file, $image);
47+
fclose($file);
48+
49+
printf('Image saved to %s%s', $filename, PHP_EOL);
50+
51+
}
52+
53+
}

‎Master/JsonDaemonExampleWorker.php‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/**
4+
* Class Daemon
5+
* @author it-novum GmbH
6+
* @repository https://github.com/it-novum/phpNSTA-Custom-Data-Example.git
7+
* @license MIT <http://opensource.org/licenses/MIT>
8+
*/
9+
10+
$Daemon = new Daemon(
11+
new GearmanWorkerTimeout(),
12+
new OutputModule()
13+
);
14+
15+
printf(
16+
'Enter endless loop, press STRG+C to exit or send SIGTERM or SIGINT to my pid(%s) to stop the Daemon%s',
17+
getmypid(),
18+
PHP_EOL
19+
);
20+
$Daemon->run();
21+
22+
class Daemon {
23+
24+
/**
25+
* @var GearmanWorkerTimeout
26+
*/
27+
private $GearmanWorkerTimeout;
28+
29+
/**
30+
* @var OutputModule
31+
*/
32+
private $OutputModule;
33+
34+
public function __construct(GearmanWorkerTimeout $GearmanWorkerTimeout, OutputModule $OutputModule) {
35+
$this->GearmanWorkerTimeout = $GearmanWorkerTimeout;
36+
$this->OutputModule = $OutputModule;
37+
38+
//Signal handler
39+
pcntl_signal(SIGTERM, [$this, 'sigHandler']);
40+
pcntl_signal(SIGINT, [$this, 'sigHandler']);
41+
}
42+
43+
/**
44+
* @param int $signo
45+
*/
46+
public function sigHandler($signo) {
47+
switch ($signo) {
48+
case SIGTERM:
49+
case SIGINT:
50+
exit(0);
51+
break;
52+
}
53+
}
54+
55+
//Run the Daemon
56+
public function run() {
57+
while (true) {
58+
//Check for pending signals
59+
pcntl_signal_dispatch();
60+
61+
//Get data from gearman (will timeout after 1 second)
62+
$data = $this->GearmanWorkerTimeout->getData();
63+
64+
//Check if we have data, null on timeout
65+
if ($data !== null) {
66+
$this->OutputModule->out($data);
67+
}
68+
}
69+
}
70+
}
71+
72+
73+
class GearmanWorkerTimeout {
74+
75+
/**
76+
* @var GearmanWorker
77+
*/
78+
private $GearmanWorker;
79+
80+
/**
81+
* @var null|mixed
82+
*/
83+
private $data = null;
84+
85+
86+
public function __construct() {
87+
$this->GearmanWorker = new GearmanWorker();
88+
$this->GearmanWorker->addServer('127.0.0.1', 4730);
89+
90+
//Wait 1 second for data
91+
$this->GearmanWorker->setTimeout(1000);
92+
93+
//Set the queue and a callback function for processing the data
94+
//The callback needs to be public!
95+
$this->GearmanWorker->addFunction('phpnsta_custom_data', [$this, 'processData']);
96+
}
97+
98+
/**
99+
* @return null|mixed
100+
* @throws Exception
101+
*/
102+
public function getData() {
103+
$this->data = null;
104+
105+
// work() will block until timeout is expired or there is a record in the queue
106+
$this->GearmanWorker->work();
107+
108+
if ($this->GearmanWorker->returnCode() === GEARMAN_TIMEOUT) {
109+
//No jobs in queue
110+
return null;
111+
}
112+
113+
if ($this->GearmanWorker->returnCode() !== GEARMAN_SUCCESS) {
114+
throw new Exception(sprintf('Garman return code %s !== success', $this->GearmanWorker->returnCode()));
115+
}
116+
117+
//Return data
118+
if ($this->data !== null) {
119+
return $this->data;
120+
}
121+
122+
return null;
123+
}
124+
125+
/**
126+
* @param GearmanJob $job
127+
* @throws Exception
128+
*/
129+
public function processData(GearmanJob $job) {
130+
$data = json_decode($job->workload());
131+
if (json_last_error() !== JSON_ERROR_NONE) {
132+
throw new Exception('Json error: %s', json_last_error_msg());
133+
}
134+
135+
$this->data = $data;
136+
}
137+
138+
}
139+
140+
141+
class OutputModule {
142+
143+
/**
144+
* @param mixed $data
145+
*/
146+
public function out($data) {
147+
if (php_sapi_name() === 'cli') {
148+
$this->cli($data);
149+
return;
150+
}
151+
$this->html($data);
152+
}
153+
154+
private function cli($data) {
155+
print_r($data);
156+
echo PHP_EOL;
157+
}
158+
159+
private function html($data) {
160+
printf('<pre>%s</pre>', print_r($data, true));
161+
}
162+
163+
}

‎Master/JsonExampleWorker.php‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* Class JsonExampleWorker
5+
* @author it-novum GmbH
6+
* @repository https://github.com/it-novum/phpNSTA-Custom-Data-Example.git
7+
* @license MIT <http://opensource.org/licenses/MIT>
8+
*/
9+
10+
$ExampleWorker = new JsonExampleWorker();
11+
12+
echo 'Enter endless loop, press STRG+C to exit' . PHP_EOL;
13+
$ExampleWorker->loop();
14+
15+
class JsonExampleWorker {
16+
17+
/**
18+
* @var GearmanWorker
19+
*/
20+
private $GearmanWorker;
21+
22+
public function __construct() {
23+
$this->GearmanWorker = new GearmanWorker();
24+
$this->GearmanWorker->addServer('127.0.0.1', 4730);
25+
26+
//Set the queue and a callback function for processing the data
27+
//The callback needs to be public!
28+
$this->GearmanWorker->addFunction('phpnsta_custom_data', [$this, 'processData']);
29+
}
30+
31+
public function loop() {
32+
while (true) {
33+
// work() will block until there are records in the queue
34+
$this->GearmanWorker->work();
35+
}
36+
}
37+
38+
/**
39+
* @param GearmanJob $job
40+
*/
41+
public function processData(GearmanJob $job) {
42+
$data = json_decode($job->workload());
43+
if (json_last_error() !== JSON_ERROR_NONE) {
44+
$OutputModule = new OutputModule(json_last_error_msg());
45+
$OutputModule->out();
46+
return;
47+
}
48+
49+
$OutputModule = new OutputModule($data);
50+
$OutputModule->out();
51+
}
52+
53+
}
54+
55+
class OutputModule {
56+
57+
/**
58+
* @var mixed
59+
*/
60+
private $data;
61+
62+
/**
63+
* OutputModule constructor.
64+
* @param mixed $data
65+
*/
66+
public function __construct($data) {
67+
$this->data = $data;
68+
}
69+
70+
public function out() {
71+
if (php_sapi_name() === 'cli') {
72+
$this->cli();
73+
return;
74+
}
75+
$this->html();
76+
}
77+
78+
private function cli() {
79+
print_r($this->data);
80+
echo PHP_EOL;
81+
}
82+
83+
private function html() {
84+
printf('<pre>%s</pre>', print_r($this->data, true));
85+
}
86+
87+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /