Repository: rucio/rucio

Issue:
Hermes throwing exception on python3
Motivation
----------

cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons Exception in thread Thread-7:
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons Traceback (most recent call last):
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     self.run()
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/threading.py", line 864, in run
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     self._target(*self._args, **self._kwargs)
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/local/lib/python3.6/site-packages/rucio/daemons/hermes/hermes.py", line 123, in deliver_emails
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     smtp.sendmail(msg['From'], message['payload']['to'], msg.as_string())
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/email/message.py", line 158, in as_string
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     g.flatten(self, unixfrom=unixfrom)
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/email/generator.py", line 116, in flatten
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     self._write(msg)
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/email/generator.py", line 195, in _write
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     self._write_headers(msg)
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/email/generator.py", line 222, in _write_headers
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     self.write(self.policy.fold(h, v))
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/email/_policybase.py", line 326, in fold
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     return self._fold(name, value, sanitize=True)
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons   File "/usr/lib64/python3.6/email/_policybase.py", line 369, in _fold
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons     parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons AttributeError: 'bytes' object has no attribute 'encode'
cms-ruciod-prod-hermes-6df59dfdd5-mgxkw rucio-daemons

Modification
------------

Having the subject encoded as utf-8 sets off this error.



Discussion:

Resolving patch:
diff --git a/lib/rucio/daemons/hermes/hermes.py b/lib/rucio/daemons/hermes/hermes.py
--- a/lib/rucio/daemons/hermes/hermes.py
+++ b/lib/rucio/daemons/hermes/hermes.py
@@ -21,7 +21,7 @@
 # - Martin Barisits <martin.barisits@cern.ch>, 2016-2019
 # - Robert Illingworth <illingwo@fnal.gov>, 2018
 # - Hannes Hansen <hannes.jakob.hansen@cern.ch>, 2019
-# - Eric Vaandering <ewv@fnal.gov>, 2019
+# - Eric Vaandering <ewv@fnal.gov>, 2019-2021
 # - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2020
 
 '''
@@ -112,7 +112,10 @@ def deliver_emails(once=False, send_email=True, thread=0, bulk=1000, delay=10):
 
                 msg['From'] = email_from
                 msg['To'] = ', '.join(message['payload']['to'])
-                msg['Subject'] = message['payload']['subject'].encode('utf-8')
+                if PY2:
+                    msg['Subject'] = message['payload']['subject'].encode('utf-8')
+                else:
+                    msg['Subject'] = message['payload']['subject']
 
                 if send_email:
                     smtp = smtplib.SMTP()
diff --git a/lib/rucio/daemons/hermes/hermes2.py b/lib/rucio/daemons/hermes/hermes2.py
--- a/lib/rucio/daemons/hermes/hermes2.py
+++ b/lib/rucio/daemons/hermes/hermes2.py
@@ -17,6 +17,7 @@
 # - Cedric Serfon <cedric.serfon@cern.ch>, 2020
 # - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2020
 # - Mario Lassnig <mario.lassnig@cern.ch>, 2020
+# - Eric Vaandering <ewv@fnal.gov>, 2021
 
 '''
    Hermes2 is a daemon that get the messages and sends them to external services (influxDB, ES, ActiveMQ).
@@ -279,7 +280,10 @@ def deliver_emails(messages, prepend_str):
 
             msg['From'] = email_from
             msg['To'] = ', '.join(message['payload']['to'])
-            msg['Subject'] = message['payload']['subject'].encode('utf-8')
+            if PY2:
+                msg['Subject'] = message['payload']['subject'].encode('utf-8')
+            else:
+                msg['Subject'] = message['payload']['subject']
 
             try:
                 smtp = smtplib.SMTP()


Test patch: