| 24 | #include <thread> |
| 25 | |
| 26 | int main(int argc, char* argv[]) try { |
| 27 | if (argc != 6) { |
| 28 | std::cerr << "Usage: " << argv[0] |
| 29 | << " <project-id> <region-id> <job-id> <job-json-file> " |
| 30 | "<repository-name>\n"; |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | namespace batch = ::google::cloud::batch_v1; |
| 35 | |
| 36 | auto const project_id = std::string(argv[1]); |
| 37 | auto const location = google::cloud::Location(argv[1], argv[2]); |
| 38 | auto const job_id = std::string(argv[3]); |
| 39 | auto const job_file = std::string(argv[4]); |
| 40 | auto const repository_name = std::string(argv[5]); |
| 41 | |
| 42 | // Parse the json and convert into protobuf format. |
| 43 | std::ifstream file(job_file, std::ios::in); |
| 44 | if (!file.is_open()) { |
| 45 | std::cout << "Failed to open JSON file: " << job_file << '\n'; |
| 46 | return 0; |
| 47 | } |
| 48 | auto contents = std::string{std::istreambuf_iterator<char>(file), {}}; |
| 49 | google::cloud::batch::v1::Job job; |
| 50 | google::protobuf::util::JsonParseOptions options; |
| 51 | google::protobuf::util::Status status = |
| 52 | google::protobuf::util::JsonStringToMessage(contents, &job, options); |
| 53 | if (!status.ok()) throw status; |
| 54 | |
| 55 | // Modify the job for the containerized application |
| 56 | auto container = job.mutable_task_groups() |
| 57 | ->at(0) |
| 58 | .mutable_task_spec() |
| 59 | ->mutable_runnables() |
| 60 | ->at(0) |
| 61 | .mutable_container(); |
| 62 | |
| 63 | std::string image_uri = |
| 64 | std::format("{}-docker.pkg.dev/{}/{}/application-image:latest", |
| 65 | location.location_id(), project_id, repository_name); |
| 66 | container->set_image_uri(image_uri); |
| 67 | |
| 68 | // Create the cloud batch client. |
| 69 | auto client = batch::BatchServiceClient(batch::MakeBatchServiceConnection()); |
| 70 | |
| 71 | // Create a job. |
| 72 | auto response = client.CreateJob(location.FullName(), job, job_id); |
| 73 | |
| 74 | if (response.status().code() != google::cloud::StatusCode::kOk) { |
| 75 | if (response.status().code() == |
| 76 | google::cloud::StatusCode::kResourceExhausted) { |
| 77 | std::cout << "There already exists a job for the parent `" |
| 78 | << location.FullName() << "` and job_id: `" << job_id |
| 79 | << "`. Please try again with a new job id.\n"; |
| 80 | return 0; |
| 81 | } |
| 82 | throw std::move(response).status(); |
| 83 | } |
nothing calls this directly
no outgoing calls
no test coverage detected