Skip to content

Commit 74871e6

Browse files
Merge pull request GoogleCloudPlatform#118 from GoogleCloudPlatform/pubsubAutoCreate
Automatically create the topic and subscription.
2 parents a7c0f9a + 51d7319 commit 74871e6

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

pubsub/src/app.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,44 @@
106106
return new Response('', 400);
107107
});
108108

109+
$app->post('/create_topic_and_subscription', function () use ($app) {
110+
/** @var Google_Client $client */
111+
$client = $app['google_client'];
112+
$projectName = sprintf('projects/%s', $app['project_id']);
113+
$pubsub = new Google_Service_Pubsub($client);
114+
$topic = new Google_Service_Pubsub_Topic();
115+
$topic->setName(sprintf('%s/topics/%s', $projectName, $app['topic']));
116+
try {
117+
$pubsub->projects_topics->create($topic->getName(), $topic);
118+
} catch (Google_Service_Exception $e) {
119+
// 409 is ok. The topic already exists.
120+
if ($e->getCode() != 409) {
121+
throw $e;
122+
}
123+
}
124+
$subscription = new Google_Service_Pubsub_Subscription();
125+
$subscription->setName(sprintf(
126+
'%s/subscriptions/%s',
127+
$projectName,
128+
$app['topic']
129+
));
130+
$subscription->setTopic($topic->getName());
131+
$config = new Google_Service_Pubsub_PushConfig();
132+
$project_id = $app['project_id'];
133+
$config->setPushEndpoint("https://$project_id.appspot.com/receive_message");
134+
$subscription->setPushConfig($config);
135+
try {
136+
$pubsub->projects_subscriptions->create(
137+
$subscription->getName(),
138+
$subscription
139+
);
140+
} catch (Google_Service_Exception $e) {
141+
// 409 is ok. The subscription already exists.
142+
if ($e->getCode() != 409) {
143+
throw $e;
144+
}
145+
}
146+
return 'OK';
147+
});
148+
109149
return $app;

pubsub/web/js/pubsub.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pubsub.PubsubController = function($http, $log, $timeout) {
1616
this.interval = 1;
1717
this.isAutoUpdating = true;
1818
this.failCount = 0;
19+
this.hasAttemptedToCreateTopicAndSubscription = false;
1920
this.fetchMessages();
2021
};
2122

@@ -54,6 +55,21 @@ pubsub.PubsubController.prototype.sendMessage = function(message) {
5455
self.message = null;
5556
}).error(function(data, status) {
5657
self.logger.error('Failed to send the message. Status: ' + status + '.');
58+
if (!self.hasAttemptedToCreateTopicAndSubscription) {
59+
// Try to create the topic and subscription once.
60+
self.hasAttemptedToCreateTopicAndSubscription = true;
61+
self.logger.info('Trying to create the topic and subscription...');
62+
self.http({
63+
method: 'POST',
64+
url: '/create_topic_and_subscription'
65+
}).success(function(data, status) {
66+
// Try one more time to send the message.
67+
self.sendMessage(message);
68+
}).error(function(data, status) {
69+
self.logger.error('Failed to create the topic and subscription. ' +
70+
'Status: ' + status + '.');
71+
});
72+
}
5773
});
5874
};
5975

0 commit comments

Comments
 (0)