2727
2828from .errors import ResponseError , EntryCreatedError , OperationCompletionError
2929
30+ POST_LOGBATCH_RETRY_COUNT = 10
3031logger = logging .getLogger (__name__ )
3132logger .addHandler (logging .NullHandler ())
3233
@@ -165,6 +166,7 @@ def __init__(self,
165166 endpoint ,
166167 project ,
167168 token ,
169+ log_batch_size = 20 ,
168170 is_skipped_an_issue = True ,
169171 verify_ssl = True ,
170172 retries = None ,
@@ -175,12 +177,15 @@ def __init__(self,
175177 endpoint: endpoint of report portal service.
176178 project: project name to use for launch names.
177179 token: authorization token.
180+ log_batch_size: option to set the maximum number of logs
181+ that can be processed in one batch
178182 is_skipped_an_issue: option to mark skipped tests as not
179183 'To Investigate' items on Server side.
180184 verify_ssl: option to not verify ssl certificates
181185 """
182- super ( ReportPortalService , self ). __init__ ()
186+ self . _batch_logs = []
183187 self .endpoint = endpoint
188+ self .log_batch_size = log_batch_size
184189 self .project = project
185190 self .token = token
186191 self .is_skipped_an_issue = is_skipped_an_issue
@@ -231,6 +236,9 @@ def finish_launch(self, end_time, status=None, **kwargs):
231236 Status can be one of the followings:
232237 (PASSED, FAILED, STOPPED, SKIPPED, RESETED, CANCELLED)
233238 """
239+ # process log batches firstly:
240+ if self ._batch_logs :
241+ self .log_batch ([], force = True )
234242 data = {
235243 "endTime" : end_time ,
236244 "status" : status
@@ -395,7 +403,7 @@ def log(self, time, message, level=None, attachment=None, item_id=None):
395403 logger .debug ("log - ID: %s" , item_id )
396404 return _get_id (r )
397405
398- def log_batch (self , log_data , item_id = None ):
406+ def log_batch (self , log_data , item_id = None , force = False ):
399407 """
400408 Log batch of messages with attachment.
401409
@@ -407,11 +415,17 @@ def log_batch(self, log_data, item_id=None):
407415 name: name of attachment
408416 data: fileobj or content
409417 mime: content type for attachment
418+ item_id: UUID of the test item that owns log_data
419+ force: Flag that forces client to process all the logs
420+ stored in self._batch_logs immediately
410421 """
422+ self ._batch_logs += log_data
423+ if len (self ._batch_logs ) < self .log_batch_size and not force :
424+ return
411425 url = uri_join (self .base_url_v2 , "log" )
412426
413427 attachments = []
414- for log_item in log_data :
428+ for log_item in self . _batch_logs :
415429 if item_id :
416430 log_item ["itemUuid" ] = item_id
417431 log_item ["launchUuid" ] = self .launch_id
@@ -435,30 +449,27 @@ def log_batch(self, log_data, item_id=None):
435449 files = [(
436450 "json_request_part" , (
437451 None ,
438- json .dumps (log_data ),
452+ json .dumps (self . _batch_logs ),
439453 "application/json"
440454 )
441455 )]
442456 files .extend (attachments )
443- from reportportal_client import POST_LOGBATCH_RETRY_COUNT
444457 for i in range (POST_LOGBATCH_RETRY_COUNT ):
445458 try :
446459 r = self .session .post (
447460 url = url ,
448461 files = files ,
449462 verify = self .verify_ssl
450463 )
464+ logger .debug ("log_batch - ID: %s" , item_id )
465+ logger .debug ("log_batch response: %s" , r .text )
466+ self ._batch_logs = []
467+ return _get_data (r )
451468 except KeyError :
452469 if i < POST_LOGBATCH_RETRY_COUNT - 1 :
453470 continue
454471 else :
455472 raise
456- break
457-
458- logger .debug ("log_batch - ID: %s" , item_id )
459- logger .debug ("log_batch response: %s" , r .text )
460-
461- return _get_data (r )
462473
463474 @staticmethod
464475 def get_system_information (agent_name = 'agent_name' ):
0 commit comments