[ARVADOS] updated: 6ca562b6cf88f66bb4a36f357a55caacc0bc8be1

git at public.curoverse.com git at public.curoverse.com
Wed Aug 26 17:06:41 EDT 2015


Summary of changes:
 sdk/python/arvados/__init__.py | 95 +++++++++++++++++++++++++++---------------
 sdk/python/tests/test_init.py  | 52 ++++++++++++++---------
 2 files changed, 94 insertions(+), 53 deletions(-)

       via  6ca562b6cf88f66bb4a36f357a55caacc0bc8be1 (commit)
      from  c6a0f9cb0b21d4c960c3fb172f5e7982997b1072 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit 6ca562b6cf88f66bb4a36f357a55caacc0bc8be1
Author: Bryan Cosca <bcosc at curoverse.com>
Date:   Wed Aug 26 17:06:33 2015 -0400

    6600: Added retry to current_job and current_task

diff --git a/sdk/python/arvados/__init__.py b/sdk/python/arvados/__init__.py
index 5f1864a..02900dd 100644
--- a/sdk/python/arvados/__init__.py
+++ b/sdk/python/arvados/__init__.py
@@ -48,7 +48,6 @@ def task_set_output(self,s,api_client=None,num_retries=None):
     if not num_retries:
         num_retries = 0
 
-    print api_client
     output_retry_loop = RetryLoop(num_retries=num_retries, backoff_start=0)
     for tries_left in output_retry_loop:
         try:
@@ -68,10 +67,10 @@ def task_set_output(self,s,api_client=None,num_retries=None):
                 else:
                     if tries_left > 0:
                         print "Retrying with {} tries left".format(tries_left)
-            else:
-                print "Output success"
-                output_retry_loop.save_result(result)
-                output_retry_loop.success = True
+            #else:
+            #    print "Output success"
+            #    output_retry_loop.save_result(result) we can't have a result here, so what is actually going on when this happens
+            #    output_retry_loop.success = True, for example, a ApiError with status_code 200? what does that mean?
         except errors.HttpError as error:
             print "HttpError raised"
             if not retry.check_http_response_success(error.status_code):
@@ -83,21 +82,21 @@ def task_set_output(self,s,api_client=None,num_retries=None):
                     if tries_left > 0:
                         print "Retrying with {} tries left".format(tries_left+1)
                     #logger.debug("HttpError returned {}, retrying with {} tries left".format(error.status_code,tries_left+1))
-            else:
-                print "Output success"
-                output_retry_loop.save_result(result)
+            #else:
+            #    print "Output success" This is also the HttpError with status_code 200 problem... Does that actually happen?
+            #    output_retry_loop.save_result(result)
         else:
-            print "Output is good"
+            print "task_set_output saved"
             output_retry_loop.save_result(result)
     if output_retry_loop.success():
-        print "Output Retry loop is successful"
+        print "task_set_output is successful"
         return output_retry_loop.last_result()
     else:
-        print "Output Retry loop is not successful, task_set_output_failed."
+        print "task_set_output failed."
         raise
 
 _current_task = None
-def current_task(api_client=None):
+def current_task(api_client=None, num_retries=None):
     global _current_task
 
     if _current_task:
@@ -106,39 +105,69 @@ def current_task(api_client=None):
     if not api_client:
         api_client = api('v1')
 
-    current_task_retry_loop = RetryLoop(num_retries=2, backoff_start=1)
+    if not num_retries:
+        num_retries = 0
+
+    current_task_retry_loop = RetryLoop(num_retries=num_retries, backoff_start=0)
     for tries_left in current_task_retry_loop:
-	try:
-	    t = api_client.job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
-	except TemporaryError as error:
-	    logger.debug("Error in current_task api call: {} ({} tries left)".format(error,tries_left))
-	else:
-	    current_task_retry_loop.save_result(result)
+    	try:
+    	    result = api_client.job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
+    	except errors.ApiError as error:
+            if not retry.check_http_response_success(error.resp.status):
+                if retry.check_http_response_success(error.resp.status) == False:
+                    print "Non-retryable ApiError raised, logging error: {}".format(error)
+                    raise
+                    output_retry_loop.save_result(error)
+                else:
+                    if tries_left > 0:
+                        print "Retrying with {} tries left".format(tries_left)
+    	else:
+    	    current_task_retry_loop.save_result(result)
     if current_task_retry_loop.success():
-        t = UserDict.UserDict(t)
-        t.set_output = types.MethodType(task_set_output, t)
-        t.tmpdir = os.environ['TASK_WORK']
-        _current_task = t
-        return t
+        result = UserDict.UserDict(result)
+        result.set_output = types.MethodType(task_set_output, result)
+        result.tmpdir = os.environ['TASK_WORK']
+        _current_task = result
+        return result
+    else:
+        print "current_task failed."
+        raise
 
 _current_job = None
-def current_job():
+def current_job(api_client=None, num_retries=None):
     global _current_job
     if _current_job:
         return _current_job
-    current_job_retry_loop = RetryLoop(num_retries=5, backoff_start=1)
+
+    if not api_client:
+        api_client = api('v1')
+
+    if not num_retries:
+        num_retries = 0
+
+    current_job_retry_loop = RetryLoop(num_retries=num_retries, backoff_start=0)
     for tries_left in current_job_retry_loop:
 	try:
-	    t = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
-	except TemporaryError as error:
-	    logger.debug("Error in current_job api call: {} ({} tries left)".format(error,tries_left))
+	    result = api_client.jobs().get(uuid=os.environ['JOB_UUID']).execute()
+    except errors.ApiError as error:
+            if not retry.check_http_response_success(error.resp.status):
+                if retry.check_http_response_success(error.resp.status) == False:
+                    print "Non-retryable ApiError raised, logging error: {}".format(error)
+                    output_retry_loop.save_result(error)
+                    raise
+                else:
+                    if tries_left > 0:
+                        print "Retrying with {} tries left".format(tries_left)
 	else:
 	    current_job_retry_loop.save_result(result)
     if current_job_retry_loop.success():
-        t = UserDict.UserDict(t)
-        t.tmpdir = os.environ['JOB_WORK']
-        _current_job = t
-        return t
+        result = UserDict.UserDict(result)
+        result.tmpdir = os.environ['JOB_WORK']
+        _current_job = result
+        return result
+    else:
+        print "current_job failed."
+        raise
 
 def getjobparam(*args):
     return current_job()['script_parameters'].get(*args)
diff --git a/sdk/python/tests/test_init.py b/sdk/python/tests/test_init.py
index a70d315..5d23915 100644
--- a/sdk/python/tests/test_init.py
+++ b/sdk/python/tests/test_init.py
@@ -14,12 +14,12 @@ from arvados_testutil import queue_with
 from arvados_testutil import mock_responses
 
 @tutil.skip_sleep
-class SDKTestCase(unittest.TestCase, tutil.ApiClientMock):
+class TaskSetOutputTestCase(unittest.TestCase, tutil.ApiClientMock):
     def mock_call_update(self, api_mock, code, body):
         self._mock_api_call(api_mock, code, body)
 
     def api_client_mock(self, status=200):
-        api_client = super(SDKTestCase, self).api_client_mock()
+        api_client = super(TaskSetOutputTestCase, self).api_client_mock()
         self.mock_call_update(api_client.job_tasks().update, status, 'foo_file')
         return api_client
 
@@ -45,46 +45,58 @@ class SDKTestCase(unittest.TestCase, tutil.ApiClientMock):
 
     def test_mock_retry_success(self):
         api_client = mock.MagicMock()
-        # api_client = self.api_client_mock(code)
-        # with self.create_side_effect(api_client,500,200):
-        #api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"), 
-        #                                                       arvados.errors.ApiError(tutil.fake_httplib2_response(200), "{}")]
-        with tutil.mock_keep_responses(self,500,200):
-            arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=1)
-            #api_client.job_tasks().update().execute.assert_called_with(num_retries=1)
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"), 
+                                                               tutil.fake_httplib2_response(200)]
+        arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=1)
 
     def test_mock_retry_fail(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,500,400):
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(404), "{}")]
+        with self.assertRaises(arvados.errors.ApiError) as err_check:
             arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=1)
 
     def test_mock_retry_retry(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,500,500):
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}")]
+        with self.assertRaises(arvados.errors.ApiError) as err_check:
             arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=1)
 
     def test_mock_retry_success_retry(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,500,200,500):
-            arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               tutil.fake_httplib2_response(200),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}")]
+        arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
 
     def test_mock_retry_fail_retry(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,500,400,500):
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(400), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}")]
+        with self.assertRaises(arvados.errors.ApiError) as err_check:
             arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
 
-    def test_mock_sucess_retry_retry(self):
+    def test_mock_success_retry_retry(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,200,500,500):
-            arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
+        api_client.job_tasks().update().execute.side_effect = [tutil.fake_httplib2_response(200),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}")]
+        arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
 
     def test_mock_retry_retry_success(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,500,500,200):
-            arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               tutil.fake_httplib2_response(200)]
+        arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
 
     def test_mock_retry_retry_fail(self):
         api_client = mock.MagicMock()
-        with tutil.mock_keep_responses(self,500,500,400):
+        api_client.job_tasks().update().execute.side_effect = [arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(500), "{}"),
+                                                               arvados.errors.ApiError(tutil.fake_httplib2_response(400), "{}")]
+        with self.assertRaises(arvados.errors.ApiError) as err_check:
             arvados.task_set_output({'uuid':'zzzzz-zzzzz-zzzzzzzzzzzzzzz'},s='d41d8cd98f00b204e9800998ecf8427e+0',api_client=api_client,num_retries=2)
 

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list