From 032d995657bc148fc15922e4b6a7406c61b045b5 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Thu, 28 Mar 2019 09:38:36 -0700 Subject: [PATCH 1/5] Add HTTP task sample --- .../php72/tasks/apps/handler/composer.json | 1 - appengine/php72/tasks/snippets/README.md | 16 ++++- appengine/php72/tasks/snippets/composer.json | 2 +- .../tasks/snippets/src/create_http_task.php | 68 +++++++++++++++++++ .../php72/tasks/snippets/test/tasksTest.php | 23 ++++++- 5 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 appengine/php72/tasks/snippets/src/create_http_task.php diff --git a/appengine/php72/tasks/apps/handler/composer.json b/appengine/php72/tasks/apps/handler/composer.json index 36e17900ba..362531ebf8 100644 --- a/appengine/php72/tasks/apps/handler/composer.json +++ b/appengine/php72/tasks/apps/handler/composer.json @@ -1,6 +1,5 @@ { "require": { - "google/cloud-tasks": "^0.4.1", "google/cloud-logging": "^1.14" }, "require-dev": { diff --git a/appengine/php72/tasks/snippets/README.md b/appengine/php72/tasks/snippets/README.md index 05e5b309f9..4fcf93ca09 100644 --- a/appengine/php72/tasks/snippets/README.md +++ b/appengine/php72/tasks/snippets/README.md @@ -4,7 +4,8 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP. -`src/create_task.php` is a simple function to create app engine queue tasks. +`src/create_task.php` is a simple function to create tasks with App Engine routing. +`src/create_http_task.php` is a simple function to create tasks with an HTTP target. ## Setup: @@ -37,12 +38,22 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP "projects/my-project/locations/us-central1/queues/my-pull-queue", then the location is "us-central1"). -7. Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments are provided: +## Using App Engine Routing +1. Run `php src/create_task.php`. The usage will print for each if no arguments are provided: ``` $> php src/create_task.php Usage: php src/create_task.php PROJECT_ID LOCATION_ID QUEUE_ID [PAYLOAD] ``` + +## Using an HTTP Target +1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided: + + ``` + $> php src/create_http_task.php + Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD] + ``` + ## Contributing changes @@ -72,4 +83,3 @@ Then the queue ID, as specified at queue creation time. Queue IDs already created can be listed with `gcloud alpha tasks queues list`. export QUEUE_ID=my-appengine-queue - diff --git a/appengine/php72/tasks/snippets/composer.json b/appengine/php72/tasks/snippets/composer.json index 2ba7909fbd..edfe25e66d 100644 --- a/appengine/php72/tasks/snippets/composer.json +++ b/appengine/php72/tasks/snippets/composer.json @@ -1,6 +1,6 @@ { "require": { - "google/cloud-tasks": "^0.4.1" + "google/cloud-tasks": "^0.9.0" }, "require-dev": { "phpunit/phpunit": "^5", diff --git a/appengine/php72/tasks/snippets/src/create_http_task.php b/appengine/php72/tasks/snippets/src/create_http_task.php new file mode 100644 index 0000000000..820a3195e3 --- /dev/null +++ b/appengine/php72/tasks/snippets/src/create_http_task.php @@ -0,0 +1,68 @@ + 6) { + return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]\n", __FILE__); +} +list($_, $projectId, $locationId, $queueId, $url, $payload) = $argv; + +# [START cloud_tasks_create_http_task] +use Google\Cloud\Tasks\V2beta3\CloudTasksClient; +use Google\Cloud\Tasks\V2beta3\HttpMethod; +use Google\Cloud\Tasks\V2beta3\HttpRequest; +use Google\Cloud\Tasks\V2beta3\Task; + +/** Uncomment and populate these variables in your code */ +// $projectId = 'The Google project ID'; +// $locationId = 'The Location ID'; +// $queueId = 'The Cloud Tasks App Engine Queue ID'; +// $url = 'The full url path that the task request will be sent to.' +// $payload = 'The payload your task should carry to the task handler. Optional'; + +// Instantiate the client and queue name. +$client = new CloudTasksClient(); +$queueName = $client->queueName($projectId, $locationId, $queueId); + +// Create an Http Request Object. +$httpRequest = new HttpRequest(); +// The full url path that the task request will be sent to. +$httpRequest->setUrl($url); +// POST is the default HTTP method, but any HTTP method can be used. +$httpRequest->setHttpMethod(HttpMethod::POST); +// Setting a body value is only compatible with HTTP POST and PUT requests. +if (isset($payload)) { + $httpRequest->setBody($payload); +} + +// Create a Cloud Task object. +$task = new Task(); +$task->setHttpRequest($httpRequest); + +// Send request and print the task name. +$response = $client->createTask($queueName, $task); +printf('Created task %s' . PHP_EOL, $response->getName()); + +# [END cloud_tasks_create_http_task] diff --git a/appengine/php72/tasks/snippets/test/tasksTest.php b/appengine/php72/tasks/snippets/test/tasksTest.php index 89649f9f59..20c9adbafa 100644 --- a/appengine/php72/tasks/snippets/test/tasksTest.php +++ b/appengine/php72/tasks/snippets/test/tasksTest.php @@ -44,7 +44,28 @@ public function testCreateTask() ); $expectedOutput = sprintf('Created task %s', $taskNamePrefix); - $this->assertContains($expectedOutput, $output); + $this->assertStringContainsString($expectedOutput, $output); + } + + public function testCreateHttpTask() + { + $queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE'); + $location = $this->requireEnv('CLOUD_TASKS_LOCATION'); + + $output = $this->runSnippet('create_http_task', [ + $location, + $queue, + 'http://example.com/', + 'Task Details', + ]); + $taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/', + self::$projectId, + $location, + $queue + ); + + $expectedOutput = sprintf('Created task %s', $taskNamePrefix); + $this->assertStringContainsString($expectedOutput, $output); } private function runSnippet($sampleName, $params = []) From 4f83eb2ac665d3272a001a7bf391c0b68d2a8a71 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Wed, 10 Apr 2019 08:49:29 -0700 Subject: [PATCH 2/5] Revert test method --- appengine/php72/tasks/snippets/test/tasksTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appengine/php72/tasks/snippets/test/tasksTest.php b/appengine/php72/tasks/snippets/test/tasksTest.php index 20c9adbafa..8a4432cae2 100644 --- a/appengine/php72/tasks/snippets/test/tasksTest.php +++ b/appengine/php72/tasks/snippets/test/tasksTest.php @@ -44,7 +44,7 @@ public function testCreateTask() ); $expectedOutput = sprintf('Created task %s', $taskNamePrefix); - $this->assertStringContainsString($expectedOutput, $output); + $this->assertContains($expectedOutput, $output); } public function testCreateHttpTask() @@ -65,7 +65,7 @@ public function testCreateHttpTask() ); $expectedOutput = sprintf('Created task %s', $taskNamePrefix); - $this->assertStringContainsString($expectedOutput, $output); + $this->assertContains($expectedOutput, $output); } private function runSnippet($sampleName, $params = []) From ec11c753ca2db83de6a461737caee9f97efe9d26 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Tue, 16 Apr 2019 13:14:32 -0700 Subject: [PATCH 3/5] move http samples --- appengine/php72/tasks/snippets/README.md | 56 +++++---------- .../php72/tasks/snippets/test/tasksTest.php | 21 ------ tasks/README.md | 70 +++++++++++++++++++ tasks/composer.json | 9 +++ tasks/phpunit.xml.dist | 31 ++++++++ .../src/create_http_task.php | 0 tasks/test/tasksTest.php | 59 ++++++++++++++++ 7 files changed, 188 insertions(+), 58 deletions(-) create mode 100644 tasks/README.md create mode 100644 tasks/composer.json create mode 100644 tasks/phpunit.xml.dist rename {appengine/php72/tasks/snippets => tasks}/src/create_http_task.php (100%) create mode 100644 tasks/test/tasksTest.php diff --git a/appengine/php72/tasks/snippets/README.md b/appengine/php72/tasks/snippets/README.md index 4fcf93ca09..06ebe7410b 100644 --- a/appengine/php72/tasks/snippets/README.md +++ b/appengine/php72/tasks/snippets/README.md @@ -24,19 +24,31 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP 4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). Run `php composer.phar install` (if composer is installed locally) or `composer install` (if composer is installed globally). - 5. Create a Queue To create a queue using the Cloud SDK, use the following gcloud command: ```sh gcloud beta tasks queues create-app-engine-queue my-appengine-queue ``` -6. Identify the Location +6. Set environment variables: + + First, your project ID: + + export PROJECT_ID=my-project-id + + Then the queue ID, as specified at queue creation time. Queue IDs already + created can be listed with `gcloud alpha tasks queues list`. + + export QUEUE_ID=my-appengine-queue + + Then, identify the queue location - Determine the location ID, which can be discovered with - `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in - the "name" value (for instance, if the name is - "projects/my-project/locations/us-central1/queues/my-pull-queue", then the - location is "us-central1"). + Determine the location ID, which can be discovered with + `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in + the "name" value (for instance, if the name is + "projects/my-project/locations/us-central1/queues/my-pull-queue", then the + location is "us-central1"). + + export LOCATION_ID=us-central1 ## Using App Engine Routing 1. Run `php src/create_task.php`. The usage will print for each if no arguments are provided: @@ -45,15 +57,6 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP $> php src/create_task.php Usage: php src/create_task.php PROJECT_ID LOCATION_ID QUEUE_ID [PAYLOAD] ``` - -## Using an HTTP Target -1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided: - - ``` - $> php src/create_http_task.php - Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD] - ``` - ## Contributing changes @@ -62,24 +65,3 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP ## Licensing * See [LICENSE](../../LICENSE) - - - - -## Creating a queue - - -## Running the Samples - -Set the environment variables: - -Set environment variables: - -First, your project ID: - - export PROJECT_ID=my-project-id - -Then the queue ID, as specified at queue creation time. Queue IDs already -created can be listed with `gcloud alpha tasks queues list`. - - export QUEUE_ID=my-appengine-queue diff --git a/appengine/php72/tasks/snippets/test/tasksTest.php b/appengine/php72/tasks/snippets/test/tasksTest.php index 8a4432cae2..89649f9f59 100644 --- a/appengine/php72/tasks/snippets/test/tasksTest.php +++ b/appengine/php72/tasks/snippets/test/tasksTest.php @@ -47,27 +47,6 @@ public function testCreateTask() $this->assertContains($expectedOutput, $output); } - public function testCreateHttpTask() - { - $queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE'); - $location = $this->requireEnv('CLOUD_TASKS_LOCATION'); - - $output = $this->runSnippet('create_http_task', [ - $location, - $queue, - 'http://example.com/', - 'Task Details', - ]); - $taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/', - self::$projectId, - $location, - $queue - ); - - $expectedOutput = sprintf('Created task %s', $taskNamePrefix); - $this->assertContains($expectedOutput, $output); - } - private function runSnippet($sampleName, $params = []) { $argv = array_merge([0, self::$projectId], array_values($params)); diff --git a/tasks/README.md b/tasks/README.md new file mode 100644 index 0000000000..b266cb88cd --- /dev/null +++ b/tasks/README.md @@ -0,0 +1,70 @@ +# Google Cloud Tasks App Engine Queue Samples + +## Description + +Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP. + +`src/create_task.php` is a simple function to create tasks with App Engine routing. +`src/create_http_task.php` is a simple function to create tasks with an HTTP target. + +## Setup: + +1. **Enable APIs** - [Enable the Cloud Tasks API](https://console.cloud.google.com/flows/enableapi?apiid=cloudtasks) + and create a new project or select an existing project. +2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials" + and select "Service Account Key". Create a new service account, use the JSON key type, and + select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` + to the path of the JSON key that was downloaded. +3. **Clone the repo** and cd into this directory + + ```sh + $ git clone https://github.com/GoogleCloudPlatform/php-docs-samples + $ cd php-docs-samples/appengine/php72/tasks + ``` +4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). + Run `php composer.phar install` (if composer is installed locally) or `composer install` + (if composer is installed globally). +5. Create a Queue + To create a queue using the Cloud SDK, use the following gcloud command: + ```sh + gcloud beta tasks queues create-app-engine-queue my-appengine-queue + ``` +6. Set environment variables: + + First, your project ID: + + export PROJECT_ID=my-project-id + + Then the queue ID, as specified at queue creation time. Queue IDs already + created can be listed with `gcloud alpha tasks queues list`. + + export QUEUE_ID=my-appengine-queue + + Then, identify the queue location: + + Determine the location ID, which can be discovered with + `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in + the "name" value (for instance, if the name is + "projects/my-project/locations/us-central1/queues/my-pull-queue", then the + location is "us-central1"). + + export LOCATION_ID=us-central1 + + Lastly, set your target URL: + export URL= + +## Using an HTTP Target +1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided: + + ``` + $> php src/create_http_task.php + Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD] + ``` + +## Contributing changes + +* See [CONTRIBUTING.md](../../CONTRIBUTING.md) + +## Licensing + +* See [LICENSE](../../LICENSE) diff --git a/tasks/composer.json b/tasks/composer.json new file mode 100644 index 0000000000..edfe25e66d --- /dev/null +++ b/tasks/composer.json @@ -0,0 +1,9 @@ +{ + "require": { + "google/cloud-tasks": "^0.9.0" + }, + "require-dev": { + "phpunit/phpunit": "^5", + "google/cloud-tools": "^0.8.5" + } +} diff --git a/tasks/phpunit.xml.dist b/tasks/phpunit.xml.dist new file mode 100644 index 0000000000..734ea01608 --- /dev/null +++ b/tasks/phpunit.xml.dist @@ -0,0 +1,31 @@ + + + + + + test + + + + + + + + ./src + + + diff --git a/appengine/php72/tasks/snippets/src/create_http_task.php b/tasks/src/create_http_task.php similarity index 100% rename from appengine/php72/tasks/snippets/src/create_http_task.php rename to tasks/src/create_http_task.php diff --git a/tasks/test/tasksTest.php b/tasks/test/tasksTest.php new file mode 100644 index 0000000000..3030690fef --- /dev/null +++ b/tasks/test/tasksTest.php @@ -0,0 +1,59 @@ +requireEnv('CLOUD_TASKS_APPENGINE_QUEUE'); + $location = $this->requireEnv('CLOUD_TASKS_LOCATION'); + + $output = $this->runSnippet('create_http_task', [ + $location, + $queue, + 'http://example.com/', + 'Task Details', + ]); + $taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/', + self::$projectId, + $location, + $queue + ); + + $expectedOutput = sprintf('Created task %s', $taskNamePrefix); + $this->assertContains($expectedOutput, $output); + } + + private function runSnippet($sampleName, $params = []) + { + $argv = array_merge([0, self::$projectId], array_values($params)); + $argc = count($argv); + ob_start(); + require __DIR__ . "/../src/$sampleName.php"; + return ob_get_clean(); + } +} From 395773c56ccca0c2b1579d945e6e0b65b2e82611 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Tue, 16 Apr 2019 15:38:58 -0700 Subject: [PATCH 4/5] Update readmes --- appengine/php72/tasks/snippets/README.md | 1 - tasks/README.md | 1 - 2 files changed, 2 deletions(-) diff --git a/appengine/php72/tasks/snippets/README.md b/appengine/php72/tasks/snippets/README.md index 06ebe7410b..4264704bf1 100644 --- a/appengine/php72/tasks/snippets/README.md +++ b/appengine/php72/tasks/snippets/README.md @@ -5,7 +5,6 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP. `src/create_task.php` is a simple function to create tasks with App Engine routing. -`src/create_http_task.php` is a simple function to create tasks with an HTTP target. ## Setup: diff --git a/tasks/README.md b/tasks/README.md index b266cb88cd..dda71a7417 100644 --- a/tasks/README.md +++ b/tasks/README.md @@ -4,7 +4,6 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP. -`src/create_task.php` is a simple function to create tasks with App Engine routing. `src/create_http_task.php` is a simple function to create tasks with an HTTP target. ## Setup: From 50b53f5cbadcf2d6e94279170943b0c4cdf21e02 Mon Sep 17 00:00:00 2001 From: averikitsch Date: Thu, 18 Apr 2019 10:58:52 -0700 Subject: [PATCH 5/5] Update readme and license --- tasks/README.md | 39 ++++++++++++---------------------- tasks/phpunit.xml.dist | 2 +- tasks/src/create_http_task.php | 6 +++--- tasks/test/tasksTest.php | 2 +- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/tasks/README.md b/tasks/README.md index dda71a7417..29964372be 100644 --- a/tasks/README.md +++ b/tasks/README.md @@ -1,4 +1,4 @@ -# Google Cloud Tasks App Engine Queue Samples +# Google Cloud Tasks Samples ## Description @@ -18,7 +18,7 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP ```sh $ git clone https://github.com/GoogleCloudPlatform/php-docs-samples - $ cd php-docs-samples/appengine/php72/tasks + $ cd php-docs-samples/tasks ``` 4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). Run `php composer.phar install` (if composer is installed locally) or `composer install` @@ -28,29 +28,6 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP ```sh gcloud beta tasks queues create-app-engine-queue my-appengine-queue ``` -6. Set environment variables: - - First, your project ID: - - export PROJECT_ID=my-project-id - - Then the queue ID, as specified at queue creation time. Queue IDs already - created can be listed with `gcloud alpha tasks queues list`. - - export QUEUE_ID=my-appengine-queue - - Then, identify the queue location: - - Determine the location ID, which can be discovered with - `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in - the "name" value (for instance, if the name is - "projects/my-project/locations/us-central1/queues/my-pull-queue", then the - location is "us-central1"). - - export LOCATION_ID=us-central1 - - Lastly, set your target URL: - export URL= ## Using an HTTP Target 1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided: @@ -60,6 +37,18 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD] ``` + where: + * `PROJECT_ID` is your Google Cloud Project id. + * `QUEUE_ID` is your queue id (ie my-appengine-queue). + Queue IDs already created can be listed with `gcloud alpha tasks queues list`. + * `LOCATION_ID` is the location of your queue. + Determine the location ID, which can be discovered with + `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in + the "name" value (for instance, if the name is + "projects/my-project/locations/us-central1/queues/my-pull-queue", then the + location is "us-central1"). + * `URL` is the full URL to your target endpoint. + ## Contributing changes * See [CONTRIBUTING.md](../../CONTRIBUTING.md) diff --git a/tasks/phpunit.xml.dist b/tasks/phpunit.xml.dist index 734ea01608..2f8d312733 100644 --- a/tasks/phpunit.xml.dist +++ b/tasks/phpunit.xml.dist @@ -1,6 +1,6 @@