12167: Report request-id when receiving SIGINT on arv-put.
authorLucas Di Pentima <ldipentima@veritasgenetics.com>
Thu, 15 Mar 2018 19:01:10 +0000 (16:01 -0300)
committerLucas Di Pentima <ldipentima@veritasgenetics.com>
Thu, 15 Mar 2018 19:33:23 +0000 (16:33 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <ldipentima@veritasgenetics.com>

sdk/python/arvados/commands/put.py
sdk/python/tests/test_arv_put.py

index 3f4766d39865beb23b950c1342540320973fef2a..5dde8e53c933d05b2facbf8df284941635da3b42 100644 (file)
@@ -979,6 +979,7 @@ def progress_writer(progress_func, outfile=sys.stderr):
     return write_progress
 
 def exit_signal_handler(sigcode, frame):
+    logging.getLogger('arvados.arv_put').error("Caught signal {}, exiting.".format(sigcode))
     sys.exit(-sigcode)
 
 def desired_project_uuid(api_client, project_uuid, num_retries):
@@ -1011,6 +1012,11 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
     if api_client is None:
         api_client = arvados.api('v1', request_id=request_id)
 
+    # Install our signal handler for each code in CAUGHT_SIGNALS, and save
+    # the originals.
+    orig_signal_handlers = {sigcode: signal.signal(sigcode, exit_signal_handler)
+                            for sigcode in CAUGHT_SIGNALS}
+
     # Determine the name to use
     if args.name:
         if args.stream or args.raw:
@@ -1127,11 +1133,6 @@ def main(arguments=None, stdout=sys.stdout, stderr=sys.stderr):
             "arv-put: %s" % str(error)]))
         sys.exit(1)
 
-    # Install our signal handler for each code in CAUGHT_SIGNALS, and save
-    # the originals.
-    orig_signal_handlers = {sigcode: signal.signal(sigcode, exit_signal_handler)
-                            for sigcode in CAUGHT_SIGNALS}
-
     if not args.dry_run and not args.update_collection and args.resume and writer.bytes_written > 0:
         logger.warning("\n".join([
             "arv-put: Resuming previous upload from last checkpoint.",
index e11c30959a05cb2f473eb93239d6b8a6d0b8ca43..0ad632322ef514d58c301e2a58431ca8d5bec894 100644 (file)
@@ -19,6 +19,7 @@ import pwd
 import random
 import re
 import shutil
+import signal
 import subprocess
 import sys
 import tempfile
@@ -857,6 +858,28 @@ class ArvPutIntegrationTest(run_test_server.TestCaseWithServers,
         self.assertIn('4a9c8b735dce4b5fa3acf221a0b13628+11',
                       pipe.stdout.read().decode())
 
+    def test_sigint_logs_request_id(self):
+        # Connect 'yes' command output to arv-put, wait for a second and
+        # send SIGINT to arv-put's process, then check if its output includes
+        # the X-Request-Id.
+        input_stream = subprocess.Popen(
+            'yes', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        pipe = subprocess.Popen(
+            [sys.executable, arv_put.__file__, '--stream'],
+            stdin=input_stream.stdout, stdout=subprocess.PIPE,
+            stderr=subprocess.STDOUT, env=self.ENVIRON)
+        time.sleep(1)
+        pipe.send_signal(signal.SIGINT)
+        deadline = time.time() + 5
+        while (pipe.poll() is None) and (time.time() < deadline):
+            time.sleep(.1)
+        returncode = pipe.poll()
+        input_stream.terminate()
+        if returncode is None:
+            pipe.terminate()
+            self.fail("arv-put did not exited within 5 seconds")
+        self.assertRegex(pipe.stdout.read().decode(), r'\(X-Request-Id: req-[a-z0-9]{20}\)')
+
     def test_ArvPutSignedManifest(self):
         # ArvPutSignedManifest runs "arv-put foo" and then attempts to get
         # the newly created manifest from the API server, testing to confirm