Skip to content

Commit 55f1cf0

Browse files
committed
feat: add dead letter queue samples
1 parent 5264a4a commit 55f1cf0

8 files changed

+505
-3
lines changed

pubsub/api/composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
{
22
"require": {
33
"php": ">=5.4",
4-
"google/cloud-pubsub": "^1.27",
4+
"google/cloud-pubsub": "^1.29",
55
"symfony/console": " ^3.0"
66
},
77
"autoload": {
88
"files": [
99
"src/create_subscription.php",
1010
"src/create_topic.php",
11+
"src/dead_letter_create_subscription.php",
12+
"src/dead_letter_delivery_attempt.php",
13+
"src/dead_letter_remove.php",
14+
"src/dead_letter_update_subscription.php",
1115
"src/create_push_subscription.php",
1216
"src/delete_subscription.php",
1317
"src/delete_topic.php",

pubsub/api/pubsub.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,108 @@
138138
}
139139
});
140140

141+
$application->add(new Command('dead-letter'))
142+
->setDescription('Manage Dead Letter Policies for Pub\Sub')
143+
->setHelp(<<
144+
The %command.name% command manages Pub\Sub dead letter policies.
145+
146+
php %command.full_name% create --project my-project
147+
--topic my-topic
148+
--subscription my-subscription
149+
--dead-letter-topic my-dead-letter-topic
150+
151+
php %command.full_name% update --project my-project
152+
--topic my-topic
153+
--subscription my-subscription
154+
--dead-letter-topic my-dead-letter-topic
155+
156+
php %command.full_name% remove --project my-project
157+
--topic my-topic
158+
159+
php %command.full_name% pull --project my-project
160+
--topic my-topic --subscription my-subscription
161+
162+
EOF
163+
)
164+
->addArgument('action', InputArgument::REQUIRED, 'The action to take')
165+
->addOption('project', null, InputOption::VALUE_REQUIRED, 'Your Google Cloud project ID.')
166+
->addOption('topic', null, InputOption::VALUE_REQUIRED, 'The topic name.')
167+
->addOption('subscription', null, InputOption::VALUE_OPTIONAL, 'The subscription name.')
168+
->addOption('dead-letter-topic', null, InputOption::VALUE_OPTIONAL, 'The dead letter topic name.')
169+
->addOption('message', null, InputOption::VALUE_OPTIONAL, 'The value of a pubsub message.')
170+
->setCode(function ($input, $output) {
171+
$action = $input->getArgument('action');
172+
$projectId = $input->getOption('project');
173+
$topicName = $input->getOption('topic');
174+
$subscriptionName = $input->getOption('subscription');
175+
$deadLetterTopic = $input->getOption('dead-letter-topic');
176+
$message = $input->getOption('message');
177+
178+
switch ($action) {
179+
case 'create':
180+
if (!$subscriptionName || !$deadLetterTopic) {
181+
throw new \RuntimeException(
182+
'Subscription Name and Dead Letter Topic are required to create a subscription.'
183+
);
184+
}
185+
186+
dead_letter_create_subscription(
187+
$projectId,
188+
$topicName,
189+
$subscriptionName,
190+
$deadLetterTopic
191+
);
192+
break;
193+
194+
case 'update':
195+
if (!$subscriptionName || !$deadLetterTopic) {
196+
throw new \RuntimeException(
197+
'Subscription Name and Dead Letter Topic are required to update a subscription.'
198+
);
199+
}
200+
201+
dead_letter_update_subscription(
202+
$projectId,
203+
$topicName,
204+
$subscriptionName,
205+
$deadLetterTopic
206+
);
207+
break;
208+
209+
case 'remove':
210+
if (!$subscriptionName) {
211+
throw new \RuntimeException(
212+
'Subscription Name is required to remove a dead letter policy.'
213+
);
214+
}
215+
216+
dead_letter_remove(
217+
$projectId,
218+
$topicName,
219+
$subscriptionName
220+
);
221+
break;
222+
223+
case 'pull':
224+
if (!$subscriptionName || !$message) {
225+
throw new \RuntimeException(
226+
'Subscription Name and message is required to pull messages.'
227+
);
228+
}
229+
230+
dead_letter_delivery_attempt(
231+
$projectId,
232+
$topicName,
233+
$subscriptionName,
234+
$message
235+
);
236+
break;
237+
238+
default:
239+
throw new \RuntimeException(sprintf('%s is not a valid action', $action));
240+
}
241+
});
242+
141243
if (getenv('PHPUNIT_TESTS') === '1') {
142244
return $application;
143245
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_dead_letter_create_subscription]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/**
30+
* Creates a Pub/Sub subscription with dead letter policy enabled.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $topicName The Pub/Sub topic name.
34+
* @param string $subscriptionName The Pub/Sub subscription name.
35+
* @param string $deadLetterTopicName The Pub/Sub topic to use for dead letter policy.
36+
*/
37+
function dead_letter_create_subscription($projectId, $topicName, $subscriptionName, $deadLetterTopicName)
38+
{
39+
$pubsub = new PubSubClient([
40+
'projectId' => $projectId,
41+
]);
42+
43+
$topic = $pubsub->topic($topicName);
44+
$deadLetterTopic = $pubsub->topic($deadLetterTopicName);
45+
46+
$subscription = $topic->subscribe($subscriptionName, [
47+
'deadLetterPolicy' => [
48+
'deadLetterTopic' => $deadLetterTopic
49+
]
50+
]);
51+
52+
printf(
53+
'Subscription %s created with dead letter topic %s' . PHP_EOL,
54+
$subscription->name(),
55+
$deadLetterTopic->name()
56+
);
57+
}
58+
# [END pubsub_dead_letter_create_subscription]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_dead_letter_delivery_attempt]
27+
use Google\Cloud\PubSub\Message;
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Get the delivery attempt from a pulled message.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $subscriptionName The Pub/Sub subscription name.
36+
* @param string $message The contents of a pubsub message data field.
37+
*/
38+
function dead_letter_delivery_attempt($projectId, $topicName, $subscriptionName, $message)
39+
{
40+
$pubsub = new PubSubClient([
41+
'projectId' => $projectId,
42+
]);
43+
44+
$topic = $pubsub->topic($topicName);
45+
46+
// publish test message
47+
$topic->publish(new Message([
48+
'data' => $message
49+
]));
50+
51+
$subscription = $topic->subscription($subscriptionName);
52+
$messages = $subscription->pull();
53+
54+
foreach ($messages as $message) {
55+
printf('Received message %s' . PHP_EOL, $message->data());
56+
printf('Delivery attempt %d' . PHP_EOL, $message->deliveryAttempt());
57+
}
58+
print('Done' . PHP_EOL);
59+
}
60+
# [END pubsub_dead_letter_delivery_attempt]

pubsub/api/src/dead_letter_remove.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_dead_letter_remove]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/**
30+
* Remove dead letter policy from an existing subscription.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $topicName The Pub/Sub topic name.
34+
* @param string $subscriptionName The Pub/Sub subscription name.
35+
*/
36+
function dead_letter_remove($projectId, $topicName, $subscriptionName)
37+
{
38+
$pubsub = new PubSubClient([
39+
'projectId' => $projectId,
40+
]);
41+
42+
$topic = $pubsub->topic($topicName);
43+
44+
$subscription = $topic->subscription($subscriptionName);
45+
46+
// Provide deadLetterPolicy in the update mask, but omit from update fields to unset.
47+
$subscription->update([], [
48+
'updateMask' => [
49+
'deadLetterPolicy'
50+
]
51+
]);
52+
53+
printf(
54+
'Removed dead letter topic from subscription %s' . PHP_EOL,
55+
$subscription->name()
56+
);
57+
}
58+
# [END pubsub_dead_letter_remove]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_dead_letter_remove]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/**
30+
* Set the dead letter policy on an existing subscription.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $topicName The Pub/Sub topic name.
34+
* @param string $deadLetterTopicName The Pub/Sub topic to use for dead letter policy.
35+
*/
36+
function dead_letter_update_subscription($projectId, $topicName, $subscriptionName, $deadLetterTopicName)
37+
{
38+
$pubsub = new PubSubClient([
39+
'projectId' => $projectId,
40+
]);
41+
42+
$topic = $pubsub->topic($topicName);
43+
$deadLetterTopic = $pubsub->topic($deadLetterTopicName);
44+
45+
$subscription = $topic->subscription($subscriptionName);
46+
$subscription->update([
47+
'deadLetterPolicy' => [
48+
'deadLetterTopic' => $deadLetterTopic
49+
]
50+
]);
51+
52+
printf(
53+
'Subscription %s updated with dead letter topic %s' . PHP_EOL,
54+
$subscription->name(),
55+
$deadLetterTopic->name()
56+
);
57+
}
58+
# [END pubsub_dead_letter_remove]

0 commit comments

Comments
 (0)