From 644f4fac6a06f269e342bac43966f3d400715ef6 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 13 Oct 2020 15:40:10 +0200 Subject: Factor out and generalize worker result uploading --- bbot/types.hxx | 1 + bbot/worker/worker.cxx | 88 +++++++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/bbot/types.hxx b/bbot/types.hxx index 35f8a69..7e485f7 100644 --- a/bbot/types.hxx +++ b/bbot/types.hxx @@ -61,6 +61,7 @@ namespace bbot using std::invalid_argument; using std::runtime_error; using std::system_error; + using std::generic_category; using io_error = std::ios_base::failure; // diff --git a/bbot/worker/worker.cxx b/bbot/worker/worker.cxx index a12a266..2ad8947 100644 --- a/bbot/worker/worker.cxx +++ b/bbot/worker/worker.cxx @@ -344,6 +344,53 @@ run_b (tracer& t, verbosity, buildspec, forward (a)...); } +// Upload manifest to the specified TFTP URL with curl. Issue diagnostics and +// throw failed on invalid manifest or process management errors and throw +// io_error for input/output errors or non-zero curl exit. +// +template +static void +upload_manifest (tracer& trace, + const string& url, + const T& m, + const char* what) +{ + try + { + tftp_curl c (trace, + path ("-"), + nullfd, + curl::put, + url, + "--tftp-blksize", tftp_blksize, + "--max-time", tftp_put_timeout); + + manifest_serializer s (c.out, url); + m.serialize (s); + + c.out.close (); + if (!c.wait ()) + throw_generic_ios_failure (EIO, "non-zero curl exit code"); + } + catch (const manifest_serialization& e) + { + fail << "invalid " << what << " manifest: " << e.description; + } + catch (const process_error& e) + { + fail << "unable to execute curl: " << e; + } + catch (const system_error& e) + { + const auto& c (e.code ()); + + if (c.category () == generic_category ()) + throw_generic_ios_failure (c.value (), e.what ()); + else + throw_system_ios_failure (c.value (), e.what ()); + } +} + static int bbot:: build (size_t argc, const char* argv[]) { @@ -1613,35 +1660,14 @@ build (size_t argc, const char* argv[]) try { - tftp_curl c (trace, - path ("-"), - nullfd, - curl::put, - url, - "--tftp-blksize", tftp_blksize, - "--max-time", tftp_put_timeout); - - manifest_serializer s (c.out, url); - rm.serialize (s); - - c.out.close (); - if (!c.wait ()) - throw_generic_error (EIO); + upload_manifest (trace, url, rm, "result"); // We use exit code 2 to signal abnormal termination but where we managed // to upload the result manifest. See startup() for details. // return rm.status != result_status::abnormal ? 0 : 2; } - catch (const manifest_serialization& e) - { - fail << "invalid result manifest: " << e.description; - } - catch (const io_error& e) // In case not derived from system_error. - { - error << "unable to upload result manifest to " << url << ": " << e; - } - catch (const system_error& e) + catch (const io_error& e) { error << "unable to upload result manifest to " << url << ": " << e; } @@ -1815,21 +1841,9 @@ startup () try { - tftp_curl c (trace, - path ("-"), - nullfd, - curl::put, - url, - "--tftp-blksize", tftp_blksize, - "--max-time", tftp_put_timeout); - - serialize_manifest (rm, c.out, url, "result"); - c.out.close (); - - if (!c.wait ()) - throw_generic_error (EIO); + upload_manifest (trace, url, rm, "result"); } - catch (const system_error& e) + catch (const io_error& e) { fail << "unable to upload result manifest to " << url << ": " << e; } -- cgit v1.1