Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pubsub/src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,44 @@
return new Response('', 400);
});

$app->post('/create_topic_and_subscription', function () use ($app) {
/** @var Google_Client $client */
$client = $app['google_client'];
$projectName = sprintf('projects/%s', $app['project_id']);
$pubsub = new Google_Service_Pubsub($client);
$topic = new Google_Service_Pubsub_Topic();
$topic->setName(sprintf('%s/topics/%s', $projectName, $app['topic']));
try {
$pubsub->projects_topics->create($topic->getName(), $topic);
} catch (Google_Service_Exception $e) {
// 409 is ok. The topic already exists.
if ($e->getCode() != 409) {
throw $e;
}
}
$subscription = new Google_Service_Pubsub_Subscription();
$subscription->setName(sprintf(
'%s/subscriptions/%s',
$projectName,
$app['topic']
));
$subscription->setTopic($topic->getName());
$config = new Google_Service_Pubsub_PushConfig();
$project_id = $app['project_id'];
$config->setPushEndpoint("https://$project_id.appspot.com/receive_message");
$subscription->setPushConfig($config);
try {
$pubsub->projects_subscriptions->create(
$subscription->getName(),
$subscription
);
} catch (Google_Service_Exception $e) {
// 409 is ok. The subscription already exists.
if ($e->getCode() != 409) {
throw $e;
}
}
return 'OK';
});

return $app;
16 changes: 16 additions & 0 deletions pubsub/web/js/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pubsub.PubsubController = function($http, $log, $timeout) {
this.interval = 1;
this.isAutoUpdating = true;
this.failCount = 0;
this.hasAttemptedToCreateTopicAndSubscription = false;
this.fetchMessages();
};

Expand Down Expand Up @@ -54,6 +55,21 @@ pubsub.PubsubController.prototype.sendMessage = function(message) {
self.message = null;
}).error(function(data, status) {
self.logger.error('Failed to send the message. Status: ' + status + '.');
if (!self.hasAttemptedToCreateTopicAndSubscription) {
// Try to create the topic and subscription once.
self.hasAttemptedToCreateTopicAndSubscription = true;
self.logger.info('Trying to create the topic and subscription...');
self.http({
method: 'POST',
url: '/create_topic_and_subscription'
}).success(function(data, status) {
// Try one more time to send the message.
self.sendMessage(message);
}).error(function(data, status) {
self.logger.error('Failed to create the topic and subscription. ' +
'Status: ' + status + '.');
});
}
});
};

Expand Down