Skip to content

Commit

Permalink
feat(batch/simple): add a simple cloud batch example (#321)
Browse files Browse the repository at this point in the history
* feat(batch/simple): add a simple cloud batch example

* try adding batch/simple to ci

* add custom dockerfile and cloudbuild

* remove docker files

* revert unrelated formating changes

* address comments

* Update batch/simple/simple.cc

Co-authored-by: Carlos O'Ryan <[email protected]>

* remove the json dependency and add error handling for resource exhausted

* add a comment about printing

* format fixg

---------

Co-authored-by: Carlos O'Ryan <[email protected]>
  • Loading branch information
alevenberg and coryan authored Apr 1, 2024
1 parent 09796ba commit 25a15d3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include(ExternalProject)

set(samples
# cmake-format: sort
batch/simple
bigquery/write
cloud-run-hello-world
gcs-fast-transfers
Expand Down
27 changes: 27 additions & 0 deletions batch/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ~~~
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ~~~

cmake_minimum_required(VERSION 3.20)

# Define the project name and where to report bugs.
set(PACKAGE_BUGREPORT
"https://github.com/GoogleCloudPlatform/cpp-samples/issues")
project(cpp-samples-batch CXX)

find_package(google_cloud_cpp_batch REQUIRED)

add_executable(simple simple.cc)
target_link_libraries("simple" PRIVATE google-cloud-cpp::batch)
42 changes: 42 additions & 0 deletions batch/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Using Cloud Batch

This example shows how to take a job.json and run a Cloud Batch job using C++.

If you are not familiar with the Batch API, we recommend you first read the
[API overview] before starting this guide.

## Compiling the Example

This project uses `vcpkg` to install its dependencies. Clone `vcpkg` in your
`$HOME`:

```shell
git clone -C $HOME https://github.com/microsoft/vcpkg.git
```

Install the typical development tools, on Ubuntu you would use:

```shell
apt update && apt install -y build-essential cmake git ninja-build pkg-config g++ curl tar zip unzip
```

In this directory compile the dependencies and the code, this can take as long
as an hour, depending on the performance of your workstation:

```shell
cd cpp-samples/batch/simple
cmake -S . -B .build -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build .build
```

## Run the sample

Run the example, replace the `[PROJECT ID]` placeholder with the id of your
project:

```shell
.build/simple [PROJECT ID] us-central1 test-container-run hello-world-container.json
```

[api overview]: https://cloud.google.com/batch/docs
42 changes: 42 additions & 0 deletions batch/simple/hello-world-container.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
"imageUri": "gcr.io/google-containers/busybox",
"entrypoint": "/bin/sh",
"commands": [
"-c",
"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
]
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
},
"maxRetryCount": 2,
"maxRunDuration": "3600s"
},
"taskCount": 4,
"parallelism": 2
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
},
"labels": {
"department": "finance",
"env": "testing"
},
"logsPolicy": {
"destination": "CLOUD_LOGGING"
}
}
73 changes: 73 additions & 0 deletions batch/simple/simple.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <google/cloud/batch/v1/batch_client.h>
#include <google/cloud/location.h>
#include <fstream>
#include <iostream>

int main(int argc, char* argv[]) try {
if (argc != 5) {
std::cerr << "Usage: " << argv[0]
<< " <project-id> <region-id> <job-id> <job-json-file>\n";
return 1;
}

namespace batch = ::google::cloud::batch_v1;

std::string const project_id = argv[1];
auto const location = google::cloud::Location(argv[1], argv[2]);
std::string const job_id = argv[3];
std::string const job_file = argv[4];

// Parse the json and convert into protobuf format.
std::ifstream file(job_file, std::ios::in);
if (!file.is_open()) {
std::cout << "Failed to open JSON file: " << job_file << '\n';
return 0;
}
auto contents = std::string{std::istreambuf_iterator<char>(file), {}};
google::cloud::batch::v1::Job job;
google::protobuf::util::JsonParseOptions options;
google::protobuf::util::Status status =
google::protobuf::util::JsonStringToMessage(contents, &job, options);
if (!status.ok()) throw status;

// Create the cloud batch client.
auto client = batch::BatchServiceClient(batch::MakeBatchServiceConnection());

// Create a job.
auto response = client.CreateJob(location.FullName(), job, job_id);

if (!response) {
if (response.status().code() ==
google::cloud::StatusCode::kResourceExhausted) {
std::cout << "There already exists a job for the parent `"
<< location.FullName() << "` and job_id: `" << job_id
<< "`. Please try again with a new job id.\n";
return 0;
}
throw std::move(response).status();
}

// On success, print the job.
std::cout << "Job : " << response->DebugString() << "\n";
return 0;
} catch (google::cloud::Status const& status) {
std::cerr << "google::cloud::Status thrown: " << status << "\n";
return 1;
} catch (google::protobuf::util::Status const& status) {
std::cerr << "google::protobuf::util::Status thrown: " << status << "\n";
return 1;
}
13 changes: 13 additions & 0 deletions batch/simple/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "gcp-cpp-samples-batch",
"version-string": "unversioned",
"homepage": "https://github.com/GoogleCloudPlatform/cpp-samples/",
"description": "An example using the Cloud Batch API",
"dependencies": [
{
"name": "google-cloud-cpp",
"default-features": false,
"features": ["batch"]
}
]
}

0 comments on commit 25a15d3

Please sign in to comment.