Skip to content

Commit fdd9acb

Browse files
averikitschbshaffer
authored andcommitted
Add HTTP Cloud Tasks sample (GoogleCloudPlatform#866)
1 parent fa90d48 commit fdd9acb

File tree

7 files changed

+248
-32
lines changed

7 files changed

+248
-32
lines changed

appengine/php72/tasks/snippets/README.md

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP.
66

7-
`src/create_task.php` is a simple function to create app engine queue tasks.
7+
`src/create_task.php` is a simple function to create tasks with App Engine routing.
88

99
## Setup:
1010

@@ -23,21 +23,34 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP
2323
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
2424
Run `php composer.phar install` (if composer is installed locally) or `composer install`
2525
(if composer is installed globally).
26-
2726
5. Create a Queue
2827
To create a queue using the Cloud SDK, use the following gcloud command:
2928
```sh
3029
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
3130
```
32-
6. Identify the Location
31+
6. Set environment variables:
32+
33+
First, your project ID:
34+
35+
export PROJECT_ID=my-project-id
36+
37+
Then the queue ID, as specified at queue creation time. Queue IDs already
38+
created can be listed with `gcloud alpha tasks queues list`.
39+
40+
export QUEUE_ID=my-appengine-queue
3341

34-
Determine the location ID, which can be discovered with
35-
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
36-
the "name" value (for instance, if the name is
37-
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
38-
location is "us-central1").
42+
Then, identify the queue location
3943

40-
7. Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments are provided:
44+
Determine the location ID, which can be discovered with
45+
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
46+
the "name" value (for instance, if the name is
47+
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
48+
location is "us-central1").
49+
50+
export LOCATION_ID=us-central1
51+
52+
## Using App Engine Routing
53+
1. Run `php src/create_task.php`. The usage will print for each if no arguments are provided:
4154

4255
```
4356
$> php src/create_task.php
@@ -51,25 +64,3 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP
5164
## Licensing
5265

5366
* See [LICENSE](../../LICENSE)
54-
55-
56-
57-
58-
## Creating a queue
59-
60-
61-
## Running the Samples
62-
63-
Set the environment variables:
64-
65-
Set environment variables:
66-
67-
First, your project ID:
68-
69-
export PROJECT_ID=my-project-id
70-
71-
Then the queue ID, as specified at queue creation time. Queue IDs already
72-
created can be listed with `gcloud alpha tasks queues list`.
73-
74-
export QUEUE_ID=my-appengine-queue
75-

appengine/php72/tasks/snippets/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-tasks": "^0.8.0"
3+
"google/cloud-tasks": "^0.9.0"
44
},
55
"require-dev": {
66
"phpunit/phpunit": "^5",

tasks/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Google Cloud Tasks Samples
2+
3+
## Description
4+
5+
Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP.
6+
7+
`src/create_http_task.php` is a simple function to create tasks with an HTTP target.
8+
9+
## Setup:
10+
11+
1. **Enable APIs** - [Enable the Cloud Tasks API](https://console.cloud.google.com/flows/enableapi?apiid=cloudtasks)
12+
and create a new project or select an existing project.
13+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
14+
and select "Service Account Key". Create a new service account, use the JSON key type, and
15+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
16+
to the path of the JSON key that was downloaded.
17+
3. **Clone the repo** and cd into this directory
18+
19+
```sh
20+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
21+
$ cd php-docs-samples/tasks
22+
```
23+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
24+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
25+
(if composer is installed globally).
26+
5. Create a Queue
27+
To create a queue using the Cloud SDK, use the following gcloud command:
28+
```sh
29+
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
30+
```
31+
32+
## Using an HTTP Target
33+
1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided:
34+
35+
```
36+
$> php src/create_http_task.php
37+
Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]
38+
```
39+
40+
where:
41+
* `PROJECT_ID` is your Google Cloud Project id.
42+
* `QUEUE_ID` is your queue id (ie my-appengine-queue).
43+
Queue IDs already created can be listed with `gcloud alpha tasks queues list`.
44+
* `LOCATION_ID` is the location of your queue.
45+
Determine the location ID, which can be discovered with
46+
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
47+
the "name" value (for instance, if the name is
48+
"projects/my-project/locations/us-central1/queues/my-pull-queue", then the
49+
location is "us-central1").
50+
* `URL` is the full URL to your target endpoint.
51+
52+
## Contributing changes
53+
54+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
55+
56+
## Licensing
57+
58+
* See [LICENSE](../../LICENSE)

tasks/composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"require": {
3+
"google/cloud-tasks": "^0.9.0"
4+
},
5+
"require-dev": {
6+
"phpunit/phpunit": "^5",
7+
"google/cloud-tools": "^0.8.5"
8+
}
9+
}

tasks/phpunit.xml.dist

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="./vendor/autoload.php">
18+
<testsuites>
19+
<testsuite name="Cloud Tasks test">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./srcdirectory>
29+
whitelist>
30+
filter>
31+
phpunit>

tasks/src/create_http_task.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
/**
3+
* Copyright 2019 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/tree/master/tasks/README.md
22+
*/
23+
24+
// Include Google Cloud dependendencies using Composer
25+
require_once __DIR__ . '/../vendor/autoload.php';
26+
27+
if ($argc < 5 || $argc > 6) {
28+
return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]\n", __FILE__);
29+
}
30+
list($_, $projectId, $locationId, $queueId, $url, $payload) = $argv;
31+
32+
# [START cloud_tasks_create_http_task]
33+
use Google\Cloud\Tasks\V2beta3\CloudTasksClient;
34+
use Google\Cloud\Tasks\V2beta3\HttpMethod;
35+
use Google\Cloud\Tasks\V2beta3\HttpRequest;
36+
use Google\Cloud\Tasks\V2beta3\Task;
37+
38+
/** Uncomment and populate these variables in your code */
39+
// $projectId = 'The Google project ID';
40+
// $locationId = 'The Location ID';
41+
// $queueId = 'The Cloud Tasks Queue ID';
42+
// $url = 'The full url path that the task request will be sent to.'
43+
// $payload = 'The payload your task should carry to the task handler. Optional';
44+
45+
// Instantiate the client and queue name.
46+
$client = new CloudTasksClient();
47+
$queueName = $client->queueName($projectId, $locationId, $queueId);
48+
49+
// Create an Http Request Object.
50+
$httpRequest = new HttpRequest();
51+
// The full url path that the task request will be sent to.
52+
$httpRequest->setUrl($url);
53+
// POST is the default HTTP method, but any HTTP method can be used.
54+
$httpRequest->setHttpMethod(HttpMethod::POST);
55+
// Setting a body value is only compatible with HTTP POST and PUT requests.
56+
if (isset($payload)) {
57+
$httpRequest->setBody($payload);
58+
}
59+
60+
// Create a Cloud Task object.
61+
$task = new Task();
62+
$task->setHttpRequest($httpRequest);
63+
64+
// Send request and print the task name.
65+
$response = $client->createTask($queueName, $task);
66+
printf('Created task %s' . PHP_EOL, $response->getName());
67+
68+
# [END cloud_tasks_create_http_task]

tasks/test/tasksTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2019 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+
namespace Google\Cloud\Samples\Tasks\Tests;
19+
20+
use Google\Cloud\TestUtils\TestTrait;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Unit Tests for tasks commands.
25+
*/
26+
class TasksTest extends TestCase
27+
{
28+
use TestTrait;
29+
30+
public function testCreateHttpTask()
31+
{
32+
$queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE');
33+
$location = $this->requireEnv('CLOUD_TASKS_LOCATION');
34+
35+
$output = $this->runSnippet('create_http_task', [
36+
$location,
37+
$queue,
38+
'http://example.com',
39+
'Task Details',
40+
]);
41+
$taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/',
42+
self::$projectId,
43+
$location,
44+
$queue
45+
);
46+
47+
$expectedOutput = sprintf('Created task %s', $taskNamePrefix);
48+
$this->assertContains($expectedOutput, $output);
49+
}
50+
51+
private function runSnippet($sampleName, $params = [])
52+
{
53+
$argv = array_merge([0, self::$projectId], array_values($params));
54+
$argc = count($argv);
55+
ob_start();
56+
require __DIR__ . "/../src/$sampleName.php";
57+
return ob_get_clean();
58+
}
59+
}

0 commit comments

Comments
 (0)