Skip to content

Commit

Permalink
fix logging error
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Jul 4, 2023
1 parent 8200866 commit 90949ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pioreactor/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def emit(self, record) -> None:
if (record.levelno == logging.ERROR) and config.getboolean(
"data_sharing_with_pioreactor", "send_errors_to_Pioreactor", fallback=False
):
publish_to_pioreactor_cloud("reported_errors", payload)
publish_to_pioreactor_cloud("reported_errors", data_str=payload)

# if Python exits too quickly, the last msg might never make it to the broker.
mqtt_msg.wait_for_publish(timeout=1)
Expand Down
20 changes: 14 additions & 6 deletions pioreactor/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,30 +382,38 @@ def __exit__(self, *args):
self.client.disconnect()


def publish_to_pioreactor_cloud(endpoint: str, json=None) -> None:
def publish_to_pioreactor_cloud(
endpoint: str, data_dict: Optional[dict] = None, data_str: Optional[str] = None
) -> None:
"""
Parameters
------------
endpoint: the function to send to the data to
json: (optional) json data to send in the body.
json: (optional) data to send in the body.
"""
from pioreactor.mureq import post
from pioreactor.whoami import get_hashed_serial_number, is_testing_env
from pioreactor.utils.timing import current_utc_timestamp
from json import dumps

assert data_dict is not None and data_str is not None

if is_testing_env():
return

if json is not None:
json["hashed_serial_number"] = get_hashed_serial_number()
json["timestamp"] = current_utc_timestamp()
if data_dict is not None:
data_dict["hashed_serial_number"] = get_hashed_serial_number()
data_dict["timestamp"] = current_utc_timestamp()
body = dumps(data_dict).encode("utf-8")
elif data_str is not None:
body = data_str.encode("utf-8")

headers = {"Content-type": "application/json", "Accept": "text/plain"}
try:
post(
f"https://cloud.pioreactor.com/{endpoint}",
body=json.encode("utf-8"),
body=body,
headers=headers,
)
except Exception:
Expand Down

0 comments on commit 90949ea

Please sign in to comment.