Skip to content

Commit f66cc44

Browse files
author
Takashi Matsuo
authored
Adding sample applications for supervisord config (GoogleCloudPlatform#545)
* Adding sample applications for supervisord config * Removed composer.lock files * CS fix
1 parent d06276a commit f66cc44

File tree

15 files changed

+289
-0
lines changed

15 files changed

+289
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[program:quote-updater]
2+
command = php %(ENV_APP_DIR)s/worker.php
3+
stdout_logfile = /dev/stdout
4+
stdout_logfile_maxbytes=0
5+
stderr_logfile = /dev/stderr
6+
stderr_logfile_maxbytes=0
7+
user = www-data
8+
autostart = true
9+
autorestart = true
10+
priority = 5
11+
stopwaitsecs = 20
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
env: flex
2+
runtime: php
3+
4+
runtime_config:
5+
document_root: web
6+
7+
manual_scaling:
8+
instances: 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require-dev": {
3+
"google/cloud-tools": "^0.6.8"
4+
}
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="test/bootstrap.php" colors="true">
18+
<testsuites>
19+
<testsuite>
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
phpunit>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Google\Cloud\Samples\AppEngine\Supervisord\Addition;
18+
19+
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class DeployTest extends TestCase
23+
{
24+
use AppEngineDeploymentTrait;
25+
26+
public function testIndex()
27+
{
28+
// Wait for the background process to fetch the data.
29+
sleep(10);
30+
// Access the modules app top page.
31+
$resp = $this->client->get('/');
32+
$this->assertEquals('200', $resp->getStatusCode(),
33+
'top page status code');
34+
$this->assertNotEmpty((string) $resp->getBody(), 'top page content');
35+
}
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
use Google\Cloud\TestUtils\PHPUnit4To6Shim;
20+
21+
PHPUnit4To6Shim::install();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
echo file_get_contents('/tmp/currentQuote');
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// This is a dumb worker process for just updating the predefined file with a
19+
// random quote.
20+
21+
$nextFile = '/tmp/nextQuote';
22+
$currentFile = '/tmp/currentQuote';
23+
24+
set_time_limit(0);
25+
26+
while (1) {
27+
$quote = file_get_contents(
28+
'https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json'
29+
);
30+
if ($quote) {
31+
$jsonQuote = json_decode($quote);
32+
if (file_put_contents($nextFile, $jsonQuote->quoteText)) {
33+
// Atomic update
34+
rename($nextFile, $currentFile);
35+
}
36+
}
37+
// Update the quote every hour.
38+
sleep(3600);
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
env: flex
2+
runtime: php
3+
4+
runtime_config:
5+
document_root: .
6+
7+
manual_scaling:
8+
instances: 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"require": {
3+
"react/http": "^0.8"
4+
},
5+
"require-dev": {
6+
"google/cloud-tools": "^0.6.8"
7+
}
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
require_once __DIR__ . '/vendor/autoload.php';
4+
5+
use Psr\Http\Message\ServerRequestInterface;
6+
use React\EventLoop\Factory;
7+
use React\Http\Response;
8+
use React\Http\Server;
9+
10+
$port = getenv('PORT') ? intval(getenv('PORT')) : 8080;
11+
12+
$loop = Factory::create();
13+
$server = new Server(function (ServerRequestInterface $request) {
14+
return new Response(
15+
200,
16+
array(
17+
'Content-Type' => 'text/plain'
18+
),
19+
"Hello World!\n"
20+
);
21+
});
22+
$socket = new \React\Socket\Server('0.0.0.0:' . $port, $loop);
23+
$server->listen($socket);
24+
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;
25+
$loop->run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="test/bootstrap.php" colors="true">
18+
<testsuites>
19+
<testsuite>
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
phpunit>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[supervisord]
2+
nodaemon = true
3+
logfile = /dev/null
4+
logfile_maxbytes = 0
5+
pidfile = /var/run/supervisord.pid
6+
7+
[program:react-server]
8+
command = php %(ENV_APP_DIR)s/index.php
9+
stdout_logfile = /dev/stdout
10+
stdout_logfile_maxbytes=0
11+
stderr_logfile = /dev/stderr
12+
stderr_logfile_maxbytes=0
13+
user = www-data
14+
autostart = true
15+
autorestart = true
16+
priority = 5
17+
stopwaitsecs = 20
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Google\Cloud\Samples\AppEngine\Supervisord\Replacement;
18+
19+
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class DeployTest extends TestCase
23+
{
24+
use AppEngineDeploymentTrait;
25+
26+
public function testIndex()
27+
{
28+
// Access the modules app top page.
29+
$resp = $this->client->get('/');
30+
$this->assertEquals('200', $resp->getStatusCode(),
31+
'top page status code');
32+
$this->assertEquals(
33+
"Hello World!\n",
34+
(string) $resp->getBody(),
35+
'top page content'
36+
);
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
use Google\Cloud\TestUtils\PHPUnit4To6Shim;
20+
21+
PHPUnit4To6Shim::install();

0 commit comments

Comments
 (0)