MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cpp-samples / main

Function main

batch/simple/simple.cc:20–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18#include <iostream>
19
20int main(int argc, char* argv[]) try {
21 if (argc != 5) {
22 std::cerr << "Usage: " << argv[0]
23 << " <project-id> <region-id> <job-id> <job-json-file>\n";
24 return 1;
25 }
26
27 namespace batch = ::google::cloud::batch_v1;
28
29 std::string const project_id = argv[1];
30 auto const location = google::cloud::Location(argv[1], argv[2]);
31 std::string const job_id = argv[3];
32 std::string const job_file = argv[4];
33
34 // Parse the json and convert into protobuf format.
35 std::ifstream file(job_file, std::ios::in);
36 if (!file.is_open()) {
37 std::cout << "Failed to open JSON file: " << job_file << '\n';
38 return 0;
39 }
40 auto contents = std::string{std::istreambuf_iterator<char>(file), {}};
41 google::cloud::batch::v1::Job job;
42 google::protobuf::util::JsonParseOptions options;
43 google::protobuf::util::Status status =
44 google::protobuf::util::JsonStringToMessage(contents, &job, options);
45 if (!status.ok()) throw status;
46
47 // Create the cloud batch client.
48 auto client = batch::BatchServiceClient(batch::MakeBatchServiceConnection());
49
50 // Create a job.
51 auto response = client.CreateJob(location.FullName(), job, job_id);
52
53 if (!response) {
54 if (response.status().code() ==
55 google::cloud::StatusCode::kResourceExhausted) {
56 std::cout << "There already exists a job for the parent `"
57 << location.FullName() << "` and job_id: `" << job_id
58 << "`. Please try again with a new job id.\n";
59 return 0;
60 }
61 throw std::move(response).status();
62 }
63
64 // On success, print the job.
65 std::cout << "Job : " << response->DebugString() << "\n";
66 return 0;
67} catch (google::cloud::Status const& status) {
68 std::cerr << "google::cloud::Status thrown: " << status << "\n";
69 return 1;
70} catch (google::protobuf::util::Status const& status) {
71 std::cerr << "google::protobuf::util::Status thrown: " << status << "\n";
72 return 1;
73}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected