Skip to content

Commit 736dac2

Browse files
authored
refactor pubsub sample for flex (GoogleCloudPlatform#292)
1 parent 865c79b commit 736dac2

File tree

6 files changed

+36
-58
lines changed

6 files changed

+36
-58
lines changed

appengine/flexible/pubsub/README.md

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,5 @@
33
## Description
44

55
This sample demonstrates how to invoke PubSub from Google App Engine Flexible
6-
Environment.
7-
8-
The sample code lives in [a parent pubsub directory](../../../pubsub/app).
9-
Only one configuration file differs: `app.yaml`.
10-
11-
## Register your application
12-
13-
## Configuration
14-
15-
- Edit `app.yaml`. Replace `YOUR_PROJECT_ID` with your google project id.
16-
17-
- Copy `app.yaml` into [../../../pubsub/app](../../../pubsub/app). Ex:
18-
```sh
19-
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cp -f app.yaml ../../../pubsub/app
20-
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cd ../../../pubsub/app
21-
~/gitrepos/php-docs-samples/pubsub$
22-
```
23-
24-
- Follow the [Prerequisite Instructions](../../../pubsub/app/README.md#prerequisites)
25-
26-
## Deploy the application to App Engine
27-
28-
```
29-
$ gcloud app deploy --set-default --project YOUR_PROJECT_ID
30-
```
31-
32-
Then access the following URL:
33-
https://{YOUR_PROJECT_NAME}.appspot.com/
34-
35-
## Run the application locally
36-
37-
```
38-
/usr/bin/php -S localhost:8910 -t web
39-
```
40-
41-
## Contributing changes
42-
43-
* See [CONTRIBUTING.md](../../../CONTRIBUTING.md)
44-
45-
## Licensing
46-
47-
* See [LICENSE](../../../LICENSE)
48-
6+
Environment. See the section **Deploy to App Engine Flexible** in [Google PubSub PHP Sample Application](../../../pubsub/app).
497

pubsub/app/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ Then access the following URL:
6666

6767
## Deploy to App Engine Flexible
6868

69-
See the instructions [here](../appengine/flexible/pubsub/README.md).
69+
- Change `YOUR_PROJECT_ID` in `app.yaml.flexible` to your project ID.
70+
71+
Run the following gcloud command to deploy your app:
72+
73+
```
74+
$ gcloud app deploy app.yaml.flexible
75+
```
76+
77+
Then access the following URL:
78+
https://{YOUR_PROJECT_NAME}.appspot.com/
7079

7180
## Run using Dev Appserver
7281

pubsub/app/app.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
$app->get('/fetch_messages', function () use ($app) {
3838
// get PUSH pubsub messages
39+
$projectId = $app['project_id'];
40+
$subscriptionName = $app['subscription'];
3941
$datastore = $app['datastore'];
4042
$query = $datastore->query()->kind('PubSubPushMessage');
4143
$messages = [];
@@ -48,10 +50,12 @@
4850
if ($pushKeys) {
4951
$datastore->deleteBatch($pushKeys);
5052
}
51-
53+
# [START pull]
5254
// get PULL pubsub messages
53-
$pubsub = $app['pubsub'];
54-
$subscription = $pubsub->subscription($app['subscription']);
55+
$pubsub = new PubSubClient([
56+
'projectId' => $projectId,
57+
]);
58+
$subscription = $pubsub->subscription($subscriptionName);
5559
$pullMessages = [];
5660
foreach ($subscription->pull(['returnImmediately' => true]) as $pullMessage) {
5761
$pullMessages[] = $pullMessage;
@@ -61,11 +65,12 @@
6165
if ($pullMessages) {
6266
$subscription->acknowledgeBatch($pullMessages);
6367
}
64-
68+
# [END pull]
6569
return new JsonResponse($messages);
6670
});
6771

6872
$app->post('/receive_message', function () use ($app) {
73+
# [START receive]
6974
// pull the message from the post body
7075
$json = $app['request']->getContent();
7176
$request = json_decode($json, true);
@@ -75,23 +80,30 @@
7580
) {
7681
return new Response('', 400);
7782
}
83+
# [END receive]
7884
// store the push message in datastore
7985
$datastore = $app['datastore'];
8086
$message = $datastore->entity('PubSubPushMessage', [
8187
'message' => $message
8288
]);
8389
$datastore->insert($message);
84-
8590
return new Response();
8691
});
8792

8893
$app->post('/send_message', function () use ($app) {
94+
$projectId = $app['project_id'];
95+
$topicName = $app['topic'];
96+
# [START send]
8997
if ($message = $app['request']->get('message')) {
90-
$pubsub = $app['pubsub'];
91-
$topic = $pubsub->topic($app['topic']);
98+
// Publish the pubsub message to the topic
99+
$pubsub = new PubSubClient([
100+
'projectId' => $projectId,
101+
]);
102+
$topic = $pubsub->topic($topicName);
92103
$response = $topic->publish(['data' => $message]);
93104
return new Response('', 204);
94105
}
106+
# [END send]
95107
return new Response('', 400);
96108
});
97109

@@ -101,10 +113,4 @@
101113
]);
102114
};
103115

104-
$app['pubsub'] = function () use ($app) {
105-
return new PubSubClient([
106-
'projectId' => $app['project_id'],
107-
]);
108-
};
109-
110116
return $app;

pubsub/app/sample_message.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"message": {
3+
"data": "SGVsbG8sIFdvcmxkIQ=="
4+
}
5+
}

pubsub/app/test/DeployAppEngineFlexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DeployAppEngineFlexTest extends \PHPUnit_Framework_TestCase
2626
public function beforeDeploy()
2727
{
2828
$tmpDir = FileUtil::cloneDirectoryIntoTmp(__DIR__ . '/..');
29-
FileUtil::copyDir(__DIR__ . '/../../../appengine/flexible/pubsub', $tmpDir);
29+
copy(__DIR__ . '/../app.yaml.flexible', $tmpDir . '/app.yaml');
3030
self::$gcloudWrapper->setDir($tmpDir);
3131
chdir($tmpDir);
3232
}

0 commit comments

Comments
 (0)