|
1 | | -"""This module contains management functionality for processing logs.""" |
2 | | - |
3 | 1 | # Copyright (c) 2022 EPAM Systems |
4 | 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 3 | # you may not use this file except in compliance with the License. |
|
13 | 11 | # See the License for the specific language governing permissions and |
14 | 12 | # limitations under the License |
15 | 13 |
|
16 | | -import logging |
17 | | -from threading import Lock |
18 | | - |
19 | | -from six.moves import queue |
20 | | - |
21 | | -from reportportal_client import helpers |
22 | | -from reportportal_client.core.rp_requests import ( |
23 | | - HttpRequest, |
24 | | - RPFile, |
25 | | - RPLogBatch, |
26 | | - RPRequestLog |
27 | | -) |
28 | | -from reportportal_client.core.worker import APIWorker |
29 | | -from reportportal_client.static.defines import NOT_FOUND |
30 | | - |
31 | | -logger = logging.getLogger(__name__) |
32 | | - |
33 | | -MAX_LOG_BATCH_PAYLOAD_SIZE = 65000000 |
34 | | - |
35 | | - |
36 | | -class LogManager(object): |
37 | | - """Manager of the log items.""" |
| 14 | +"""Deprecated module stub to avoid execution crashes. |
38 | 15 |
|
39 | | - def __init__(self, rp_url, session, api_version, launch_id, project_name, |
40 | | - max_entry_number=20, verify_ssl=True, |
41 | | - max_payload_size=MAX_LOG_BATCH_PAYLOAD_SIZE): |
42 | | - """Initialize instance attributes. |
| 16 | +.. deprecated:: 5.2.4 |
| 17 | + Use `logs.log_manager` instead. |
| 18 | +""" |
43 | 19 |
|
44 | | - :param rp_url: Report portal URL |
45 | | - :param session: HTTP Session object |
46 | | - :param api_version: RP API version |
47 | | - :param launch_id: Parent launch UUID |
48 | | - :param project_name: RP project name |
49 | | - :param max_entry_number: The amount of log objects that need to be |
50 | | - gathered before processing |
51 | | - :param verify_ssl: Indicates that it is necessary to verify SSL |
52 | | - certificates within HTTP request |
53 | | - :param max_payload_size: maximum size in bytes of logs that can be |
54 | | - processed in one batch |
55 | | - """ |
56 | | - self._lock = Lock() |
57 | | - self._batch = [] |
58 | | - self._payload_size = helpers.TYPICAL_MULTIPART_FOOTER_LENGTH |
59 | | - self._worker = None |
60 | | - self.api_version = api_version |
61 | | - self.queue = queue.PriorityQueue() |
62 | | - self.launch_id = launch_id |
63 | | - self.max_entry_number = max_entry_number |
64 | | - self.max_payload_size = max_payload_size |
65 | | - self.project_name = project_name |
66 | | - self.rp_url = rp_url |
67 | | - self.session = session |
68 | | - self.verify_ssl = verify_ssl |
| 20 | +import warnings |
69 | 21 |
|
70 | | - self._log_endpoint = ( |
71 | | - '{rp_url}/api/{version}/{project_name}/log' |
72 | | - .format(rp_url=rp_url, version=self.api_version, |
73 | | - project_name=self.project_name)) |
| 22 | +from reportportal_client.logs.log_manager import LogManager, \ |
| 23 | + MAX_LOG_BATCH_PAYLOAD_SIZE |
74 | 24 |
|
75 | | - def _send_batch(self): |
76 | | - """Send existing batch logs to the worker.""" |
77 | | - batch = RPLogBatch(self._batch) |
78 | | - http_request = HttpRequest( |
79 | | - self.session.post, self._log_endpoint, files=batch.payload, |
80 | | - verify_ssl=self.verify_ssl) |
81 | | - batch.http_request = http_request |
82 | | - self._worker.send(batch) |
83 | | - self._batch = [] |
84 | | - self._payload_size = helpers.TYPICAL_MULTIPART_FOOTER_LENGTH |
85 | | - |
86 | | - def _log_process(self, log_req): |
87 | | - """Process the given log request. |
88 | | -
|
89 | | - :param log_req: RPRequestLog object |
90 | | - """ |
91 | | - with self._lock: |
92 | | - rq_size = log_req.multipart_size |
93 | | - if self._payload_size + rq_size >= self.max_payload_size: |
94 | | - if len(self._batch) > 0: |
95 | | - self._send_batch() |
96 | | - self._batch.append(log_req) |
97 | | - self._payload_size += rq_size |
98 | | - if len(self._batch) >= self.max_entry_number: |
99 | | - self._send_batch() |
100 | | - |
101 | | - def log(self, time, message=None, level=None, attachment=None, |
102 | | - item_id=None): |
103 | | - """Log message. Can be added to test item in any state. |
104 | | -
|
105 | | - :param time: Log time |
106 | | - :param message: Log message |
107 | | - :param level: Log level |
108 | | - :param attachment: Attachments(images,files,etc.) |
109 | | - :param item_id: parent item UUID |
110 | | - """ |
111 | | - if item_id is NOT_FOUND: |
112 | | - logger.warning("Attempt to log to non-existent item") |
113 | | - return |
114 | | - rp_file = RPFile(**attachment) if attachment else None |
115 | | - rp_log = RPRequestLog(self.launch_id, time, rp_file, item_id, |
116 | | - level, message) |
117 | | - self._log_process(rp_log) |
118 | | - |
119 | | - def start(self): |
120 | | - """Create a new instance of the Worker class and start it.""" |
121 | | - if not self._worker: |
122 | | - # the worker might be already created in case of deserialization |
123 | | - self._worker = APIWorker(self.queue) |
124 | | - self._worker.start() |
125 | | - |
126 | | - def stop(self): |
127 | | - """Send last batches to the worker followed by the stop command.""" |
128 | | - if self._worker: |
129 | | - with self._lock: |
130 | | - if self._batch: |
131 | | - self._send_batch() |
132 | | - logger.debug('Waiting for worker {0} to complete' |
133 | | - 'processing batches.'.format(self._worker)) |
134 | | - self._worker.stop() |
| 25 | +warnings.warn( |
| 26 | + message="`core.log_manager` is deprecated since 5.2.4 and will be subject " |
| 27 | + "for removing in the next major version. Use logs.log_manager` " |
| 28 | + "instead", |
| 29 | + category=DeprecationWarning, |
| 30 | + stacklevel=2 |
| 31 | +) |
135 | 32 |
|
136 | | - def stop_force(self): |
137 | | - """Send stop immediate command to the worker.""" |
138 | | - if self._worker: |
139 | | - self._worker.stop_immediate() |
| 33 | +__all__ = [ |
| 34 | + 'LogManager', |
| 35 | + 'MAX_LOG_BATCH_PAYLOAD_SIZE' |
| 36 | +] |
0 commit comments